|
程序代碼在附件中,取模軟件來(lái)自正點(diǎn)原子免費(fèi)學(xué)習(xí)資料,本人接觸這行不久代碼有不足之處可以自行探索修改。因?yàn)槲沂钦罩c(diǎn)原子學(xué)習(xí)的,所以工程文件也是照著正點(diǎn)原子弄的,制作比較簡(jiǎn)單適合新手學(xué)習(xí)。
運(yùn)行結(jié)果如下圖
最終結(jié)果
取模設(shè)置如圖:
取模
圖片取模軟件是Image2Lcd 2.9,請(qǐng)百度自行獲取
STM32單片機(jī)源程序如下:- #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 無(wú)
- * @retval 無(wú)
- */
- void OLED_I2C_Start(void)
- {
- OLED_W_SDA(1);
- OLED_W_SCL(1);
- OLED_W_SDA(0);
- OLED_W_SCL(0);
- }
- /**
- * @brief I2C停止
- * @param 無(wú)
- * @retval 無(wú)
- */
- void OLED_I2C_Stop(void)
- {
- OLED_W_SDA(0);
- OLED_W_SCL(1);
- OLED_W_SDA(1);
- }
- /**
- * @brief I2C發(fā)送一個(gè)字節(jié)
- * @param Byte 要發(fā)送的一個(gè)字節(jié)
- * @retval 無(wú)
- */
- 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); //額外的一個(gè)時(shí)鐘,不處理應(yīng)答信號(hào)
- OLED_W_SCL(0);
- }
- /**
- * @brief OLED寫命令
- * @param Command 要寫入的命令
- * @retval 無(wú)
- */
- void OLED_WriteCommand(uint8_t Command)
- {
- OLED_I2C_Start();
- OLED_I2C_SendByte(0x78); //從機(jī)地址
- OLED_I2C_SendByte(0x00); //寫命令
- OLED_I2C_SendByte(Command);
- OLED_I2C_Stop();
- }
- /**
- * @brief OLED寫數(shù)據(jù)
- * @param Data 要寫入的數(shù)據(jù)
- * @retval 無(wú)
- */
- void OLED_WriteData(uint8_t Data)
- {
- OLED_I2C_Start();
- OLED_I2C_SendByte(0x78); //從機(jī)地址
- OLED_I2C_SendByte(0x40); //寫數(shù)據(jù)
- 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); //設(shè)置Y位置
- OLED_WriteCommand(0x10); //設(shè)置X位置高4位
- OLED_WriteCommand(0x00); //設(shè)置X位置低4位
- for(n=0;n<128;n++)
- {
- OLED_WriteData(OLED_GRAM[n][i]);
- }
- }
- }
- //畫點(diǎn)
- //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函數(shù)
- u32 mypow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- //清屏函數(shù),清完屏,整個(gè)屏幕是黑色的!和沒(méi)點(diǎn)亮一樣!!!
- 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設(shè)置光標(biāo)位置
- * @param Y 以左上角為原點(diǎn),向下方向的坐標(biāo),范圍:0~7
- * @param X 以左上角為原點(diǎn),向右方向的坐標(biāo),范圍:0~127
- * @retval 無(wú)
- */
- void OLED_SetCursor(uint8_t Y, uint8_t X)
- {
- OLED_WriteCommand(0xB0 | Y); //設(shè)置Y位置
- OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //設(shè)置X位置低4位
- OLED_WriteCommand(0x00 | (X & 0x0F)); //設(shè)置X位置高4位
- }
- /***********功能描述:顯示顯示BMP圖片128×64起始點(diǎn)坐標(biāo)(x,y),x的范圍0~127,y為頁(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); //第一個(gè)設(shè)置y,第二個(gè)設(shè)置x//設(shè)置光標(biāo)位置左上角(0,0) 往下0-7 , 往右0-127
- for(x=x0;x<x1;x++)
- {
- OLED_WriteData(PI[j++]); //寫數(shù)據(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 無(wú)
- * @retval 無(wú)
- */
- void OLED_Init(void)
- {
- uint32_t i, j;
-
- for (i = 0; i < 1000; i++) //上電延時(shí)
- {
- for (j = 0; j < 1000; j++);
- }
-
- OLED_I2C_Init(); //端口初始化
-
- OLED_WriteCommand(0xAE); //關(guān)閉顯示
-
- OLED_WriteCommand(0xD5); //設(shè)置顯示時(shí)鐘分頻比/振蕩器頻率
- OLED_WriteCommand(0x80);
-
- OLED_WriteCommand(0xA8); //設(shè)置多路復(fù)用率
- OLED_WriteCommand(0x3F);
-
- OLED_WriteCommand(0xD3); //設(shè)置顯示偏移
- OLED_WriteCommand(0x00);
-
- OLED_WriteCommand(0x40); //設(shè)置顯示開始行
-
- OLED_WriteCommand(0xA1); //設(shè)置左右方向,0xA1正常 0xA0左右反置
-
- OLED_WriteCommand(0xC0); //設(shè)置上下方向,0xC8正常 0xC0上下反置
- OLED_WriteCommand(0xDA); //設(shè)置COM引腳硬件配置
- OLED_WriteCommand(0x12);
-
- OLED_WriteCommand(0x81); //設(shè)置對(duì)比度控制
- OLED_WriteCommand(0xCF);
- OLED_WriteCommand(0xD9); //設(shè)置預(yù)充電周期
- OLED_WriteCommand(0xF1);
- OLED_WriteCommand(0xDB); //設(shè)置VCOMH取消選擇級(jí)別
- OLED_WriteCommand(0x30);
- OLED_WriteCommand(0xA4); //設(shè)置整個(gè)顯示打開/關(guān)閉
- OLED_WriteCommand(0xA6); //設(shè)置正常/倒轉(zhuǎn)顯示
- OLED_WriteCommand(0x8D); //設(shè)置充電泵
- OLED_WriteCommand(0x14);
- OLED_WriteCommand(0xAF); //開啟顯示
-
- OLED_Clear(); //OLED清屏
- }
復(fù)制代碼
Keil代碼下載:
Keil代碼和圖片.7z
(205.79 KB, 下載次數(shù): 45)
2022-9-22 04:19 上傳
點(diǎn)擊文件名下載附件
程序和圖片
|
評(píng)分
-
查看全部評(píng)分
|