程序代码在附件中,取模软件来自正点原子免费学习资料,本人接触这行不久代码有不足之处可以自行探索修改。因为我是照着正点原子学习的,所以工程文件也是照着正点原子弄的,制作比较简单适合新手学习。
运行结果如下图
取模设置如图:
图片取模软件是Image2Lcd 2.9,请百度自行获取
STM32单片机源程序如下:- #include "oled.h"
- #include "stdlib.h"
- #include "delay.h"
- #include "1.h"
- u8 OLED_GRAM[128][8];
- /*引脚初始化*/
- void OLED_I2C_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- OLED_W_SCL(1);
- OLED_W_SDA(1);
- }
- /**
- * @brief I2C开始
- * @param 无
- * @retval 无
- */
- void OLED_I2C_Start(void)
- {
- OLED_W_SDA(1);
- OLED_W_SCL(1);
- OLED_W_SDA(0);
- OLED_W_SCL(0);
- }
- /**
- * @brief I2C停止
- * @param 无
- * @retval 无
- */
- void OLED_I2C_Stop(void)
- {
- OLED_W_SDA(0);
- OLED_W_SCL(1);
- OLED_W_SDA(1);
- }
- /**
- * @brief I2C发送一个字节
- * @param Byte 要发送的一个字节
- * @retval 无
- */
- void OLED_I2C_SendByte(uint8_t Byte)
- {
- uint8_t i;
- for (i = 0; i < 8; i++)
- {
- OLED_W_SDA(Byte & (0x80 >> i));
- OLED_W_SCL(1);
- OLED_W_SCL(0);
- }
- OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
- OLED_W_SCL(0);
- }
- /**
- * @brief OLED写命令
- * @param Command 要写入的命令
- * @retval 无
- */
- void OLED_WriteCommand(uint8_t Command)
- {
- OLED_I2C_Start();
- OLED_I2C_SendByte(0x78); //从机地址
- OLED_I2C_SendByte(0x00); //写命令
- OLED_I2C_SendByte(Command);
- OLED_I2C_Stop();
- }
- /**
- * @brief OLED写数据
- * @param Data 要写入的数据
- * @retval 无
- */
- void OLED_WriteData(uint8_t Data)
- {
- OLED_I2C_Start();
- OLED_I2C_SendByte(0x78); //从机地址
- OLED_I2C_SendByte(0x40); //写数据
- OLED_I2C_SendByte(Data);
- OLED_I2C_Stop();
- }
- //更新显存到OLED
- void OLED_Refresh_Gram(void)
- {
- u8 i,n;
- for(i=0;i<8;i++)
- {
- OLED_WriteCommand(0xB0+i); //设置Y位置
- OLED_WriteCommand(0x10); //设置X位置高4位
- OLED_WriteCommand(0x00); //设置X位置低4位
- for(n=0;n<128;n++)
- {
- OLED_WriteData(OLED_GRAM[n][i]);
- }
- }
- }
- //画点
- //x:0~127
- //y:0~63
- //t:1 填充 0,清空
- void OLED_DrawPoint(u8 x,u8 y,u8 t)
- {
- u8 pos,bx,temp=0;
- if(x>127||y>63)
- {
- return;//超出范围了.
- }
- pos=7-y/8;
- bx=y%8;
- temp=1<<(7-bx);
- if(t)
- {
- OLED_GRAM[x][pos]|=temp;
- }
- else OLED_GRAM[x][pos]&=~temp;
- }
- //m^n函数
- u32 mypow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
- void OLED_Clear(void)
- {
- u8 i,n;
- for(i=0;i<8;i++)
- {
- for(n=0;n<128;n++)
- {
- OLED_GRAM[n][i]=0X00;
- }
- }
- OLED_Refresh_Gram();//更新显示
- }
- /**
- * @brief OLED设置光标位置
- * @param Y 以左上角为原点,向下方向的坐标,范围:0~7
- * @param X 以左上角为原点,向右方向的坐标,范围:0~127
- * @retval 无
- */
- void OLED_SetCursor(uint8_t Y, uint8_t X)
- {
- OLED_WriteCommand(0xB0 | Y); //设置Y位置
- OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置低4位
- OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置高4位
- }
- /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
- void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char PI[])
- {
- unsigned int j=0;
- unsigned char x,y;
-
- // if(y1%8==0) y=y1/8;
- // else y=y1/8+1;
- for(y=y0;y<y1;y++)
- {
- OLED_SetCursor(y,x0); //第一个设置y,第二个设置x//设置光标位置左上角(0,0) 往下0-7 , 往右0-127
- for(x=x0;x<x1;x++)
- {
- OLED_WriteData(PI[j++]); //写数据
- }
- }
- }
- void OLED_BMP(void)
- {
- OLED_Clear();
- OLED_DrawBMP(0,0,127,0,(unsigned char *)gImage_1);
- OLED_DrawBMP(0,0,127,1,(unsigned char *)gImage_1);
- OLED_DrawBMP(0,0,127,2,(unsigned char *)gImage_1);
- OLED_DrawBMP(0,0,127,3,(unsigned char *)gImage_1);
- OLED_DrawBMP(0,0,127,4,(unsigned char *)gImage_1);
- OLED_DrawBMP(0,0,127,5,(unsigned char *)gImage_1);
- OLED_DrawBMP(0,0,127,6,(unsigned char *)gImage_1);
- OLED_DrawBMP(0,0,127,7,(unsigned char *)gImage_1);
- }
- /**
- * @brief OLED初始化
- * @param 无
- * @retval 无
- */
- void OLED_Init(void)
- {
- uint32_t i, j;
-
- for (i = 0; i < 1000; i++) //上电延时
- {
- for (j = 0; j < 1000; j++);
- }
-
- OLED_I2C_Init(); //端口初始化
-
- OLED_WriteCommand(0xAE); //关闭显示
-
- OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
- OLED_WriteCommand(0x80);
-
- OLED_WriteCommand(0xA8); //设置多路复用率
- OLED_WriteCommand(0x3F);
-
- OLED_WriteCommand(0xD3); //设置显示偏移
- OLED_WriteCommand(0x00);
-
- OLED_WriteCommand(0x40); //设置显示开始行
-
- OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置
-
- OLED_WriteCommand(0xC0); //设置上下方向,0xC8正常 0xC0上下反置
- OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
- OLED_WriteCommand(0x12);
-
- OLED_WriteCommand(0x81); //设置对比度控制
- OLED_WriteCommand(0xCF);
- OLED_WriteCommand(0xD9); //设置预充电周期
- OLED_WriteCommand(0xF1);
- OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
- OLED_WriteCommand(0x30);
- OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
- OLED_WriteCommand(0xA6); //设置正常/倒转显示
- OLED_WriteCommand(0x8D); //设置充电泵
- OLED_WriteCommand(0x14);
- OLED_WriteCommand(0xAF); //开启显示
-
- OLED_Clear(); //OLED清屏
- }
复制代码
Keil代码下载:
Keil代码和图片.7z
(205.79 KB, 下载次数: 45)
|