程序代碼在附件中,取模軟件來自正點原子免費學習資料,本人接觸這行不久代碼有不足之處可以自行探索修改。因為我是照著正點原子學習的,所以工程文件也是照著正點原子弄的,制作比較簡單適合新手學習。
運行結果如下圖
51hei圖片_20220915184648.jpg (3.35 MB, 下載次數: 42)
下載附件
最終結果
2022-9-15 18:49 上傳
取模設置如圖:
捕獲.PNG (44.04 KB, 下載次數: 52)
下載附件
取模
2022-9-15 18:49 上傳
圖片取模軟件是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)
2022-9-22 04:19 上傳
點擊文件名下載附件
程序和圖片 下載積分: 黑幣 -5
|