[AT32F423] 【AT-START-F423测评】OLED显示采集温度

[复制链接]
 楼主| yinwuqing110 发表于 2023-12-3 16:04 | 显示全部楼层 |阅读模式
     回顾上期帖子:https://bbs.21ic.com/icview-3343774-1-1.html,咱们使用DS18B20作为外设,通过单线式通讯方式,采集周围环境的温度,并通过串口打印当前的温度值。今天周末,南方的室内温度还不算太低,不知道此时此刻的北方气温怎么样。
      本节在上期的基础上,加入了驱动OLED显示部分的功能,硬件连接:GPIOB6--->SCL,GPIOB7--->SDA。借助“PCtoLCD2002”工具可以实现中文字符的显示,bmp单色位图图片的显示。参考的部分代码展示如下:
  1. #include "oled.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "at32f423.h"         
  5. #include "at32f423_board.h"
  6. #include "string.h"
  7. #include "oledfont.h"

  8. #include "i2c_application.h"
  9. #include "at32f423_conf.h"
  10. #define I2Cx_ADDRESS                     0x78

  11. #define I2Cx_PORT                        I2C1
  12. #define I2Cx_CLK                         CRM_I2C1_PERIPH_CLOCK
  13. #define I2Cx_DMA                         DMA1
  14. #define I2Cx_DMA_CLK                     CRM_DMA1_PERIPH_CLOCK

  15. #define I2Cx_SCL_GPIO_CLK                CRM_GPIOB_PERIPH_CLOCK
  16. #define I2Cx_SCL_GPIO_PIN                GPIO_PINS_6
  17. #define I2Cx_SCL_GPIO_PinsSource         GPIO_PINS_SOURCE6
  18. #define I2Cx_SCL_GPIO_PORT               GPIOB
  19. #define I2Cx_SCL_GPIO_MUX                GPIO_MUX_4

  20. #define I2Cx_SDA_GPIO_CLK                CRM_GPIOB_PERIPH_CLOCK
  21. #define I2Cx_SDA_GPIO_PIN                GPIO_PINS_7
  22. #define I2Cx_SDA_GPIO_PinsSource         GPIO_PINS_SOURCE7
  23. #define I2Cx_SDA_GPIO_PORT               GPIOB
  24. #define I2Cx_SDA_GPIO_MUX                GPIO_MUX_4
  25. #define I2Cx_CLKCTRL                            0x10F03C6A   

  26. i2c_handle_type hi2cx;

  27. static unsigned char OLED_buffer[1024] = {0};
  28. void OLED_WR_Byte(unsigned char dat,unsigned char cmd) {
  29.         if(cmd) {
  30.                  Write_IIC_Data(dat);
  31.         } else {
  32.                 Write_IIC_Command(dat);
  33.         }
  34. }

  35. void OLED_Set_Pos(unsigned char x, unsigned char y) {
  36.         OLED_WR_Byte(YLevel+y/PAGE_SIZE,OLED_CMD);
  37.         OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
  38.         OLED_WR_Byte((x&0x0f),OLED_CMD);
  39. }  

  40. void OLED_Display_On(void) {
  41.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  42.         OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
  43.         OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
  44. }

  45. void OLED_Display_Off(void) {
  46.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  47.         OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
  48.         OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
  49. }

  50. void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color) {
  51.         if(color) {
  52.                 OLED_buffer[(y/PAGE_SIZE)*WIDTH+x]|= (1<<(y%PAGE_SIZE))&0xff;
  53.         } else {
  54.                 OLED_buffer[(y/PAGE_SIZE)*WIDTH+x]&= ~((1<<(y%PAGE_SIZE))&0xff);
  55.         }
  56. }       

  57. void OLED_Display(void) {
  58.         uint8_t i,n;
  59.         for(i=0;i<(PAGE_SIZE);i++)  {
  60.                 OLED_WR_Byte (YLevel+i,OLED_CMD);    //设置页地址(0~3)
  61.                 OLED_WR_Byte (XLevelL,OLED_CMD);      //设置显示位置—列低地址
  62.                 OLED_WR_Byte (XLevelH,OLED_CMD);      //设置显示位置—列高地址   
  63.                 for(n=0;n<WIDTH;n++) {
  64.                         OLED_WR_Byte(OLED_buffer[i*WIDTH+n],OLED_DATA);
  65.                 }
  66.         }   //更新显示
  67. }

  68. void OLED_Clear(void) {
  69.         memset(OLED_buffer,0,sizeof(OLED_buffer));
  70.         OLED_Display();
  71. }

  72. void OLED_Init(void) {
  73.         II2CGpioInit();
  74.         delay_ms(200);

  75. /**************初始化SSD1306*****************/       
  76. //        OLED_Display_Off(); //power off
  77.         OLED_WR_Byte(0xAE,OLED_CMD);//--display off       
  78.         OLED_WR_Byte(0x20,OLED_CMD);//---set low column address
  79.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  80.         OLED_WR_Byte(0xb0,OLED_CMD);
  81.         OLED_WR_Byte(0xC8,OLED_CMD);//-not offset
  82.         OLED_WR_Byte(0x00,OLED_CMD);// contract control
  83.         OLED_WR_Byte(0x10,OLED_CMD);
  84.         OLED_WR_Byte(0x40,OLED_CMD);//--128
  85.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  86.         OLED_WR_Byte(0xFF,OLED_CMD);
  87.         OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
  88.         OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse       
  89.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  90.         OLED_WR_Byte(0x3F,OLED_CMD);       
  91.         OLED_WR_Byte(0xa4,OLED_CMD);//-set display offset
  92.         OLED_WR_Byte(0xd3,OLED_CMD);
  93.         OLED_WR_Byte(0x00,OLED_CMD);
  94.         OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
  95.         OLED_WR_Byte(0xF0,OLED_CMD);       
  96.         OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
  97.         OLED_WR_Byte(0x22,OLED_CMD);       
  98.         OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
  99.         OLED_WR_Byte(0x12,OLED_CMD);       
  100.         OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
  101.         OLED_WR_Byte(0x20,OLED_CMD);       
  102.         OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
  103.         OLED_WR_Byte(0x14,OLED_CMD);       
  104.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel

  105. }  

  106. void i2c_lowlevel_init(i2c_handle_type* hi2c)
  107. {
  108.   gpio_init_type gpio_init_structure;

  109.   if(hi2c->i2cx == I2Cx_PORT)
  110.   {
  111.     /* i2c periph clock enable */
  112.     crm_periph_clock_enable(I2Cx_CLK, TRUE);
  113.     crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
  114.     crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);

  115.     /* gpio configuration */
  116.     gpio_pin_mux_config(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_GPIO_PinsSource, I2Cx_SCL_GPIO_MUX);

  117.     gpio_pin_mux_config(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_GPIO_PinsSource, I2Cx_SDA_GPIO_MUX);

  118.     /* configure i2c pins: scl */
  119.     gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  120.     gpio_init_structure.gpio_mode           = GPIO_MODE_MUX;
  121.     gpio_init_structure.gpio_out_type       = GPIO_OUTPUT_OPEN_DRAIN;
  122.     gpio_init_structure.gpio_pull           = GPIO_PULL_UP;

  123.     gpio_init_structure.gpio_pins           = I2Cx_SCL_GPIO_PIN;
  124.     gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_init_structure);

  125.     /* configure i2c pins: sda */
  126.     gpio_init_structure.gpio_pins           = I2Cx_SDA_GPIO_PIN;
  127.     gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_init_structure);

  128.     /* config i2c */
  129.     i2c_init(hi2c->i2cx, 0x0F, I2Cx_CLKCTRL);

  130.     i2c_own_address1_set(hi2c->i2cx, I2C_ADDRESS_MODE_7BIT, I2Cx_ADDRESS);
  131.   }
  132. }

  133. void II2CGpioInit(void)
  134. {
  135.         #if 0
  136.         gpio_init_type gpio_init_struct;
  137.         /* enable gpioc periph clock */
  138.   crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  139.        
  140.        
  141.   gpio_default_para_init(&gpio_init_struct);

  142.   /* gpio output config */
  143.   gpio_bits_write(GPIOC, IO_SCL_PIN | IO_SDA_PIN, TRUE);
  144.        
  145.          gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  146.   gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
  147.   gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  148.   gpio_init_struct.gpio_pins = IO_SCL_PIN | IO_SDA_PIN;
  149.   gpio_init_struct.gpio_pull = GPIO_PULL_UP;
  150.   gpio_init(GPIOC, &gpio_init_struct);
  151.         #else
  152.                 gpio_init_type gpio_init_structure;
  153.           crm_periph_clock_enable(I2Cx_CLK, TRUE);
  154.     crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
  155.     crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);

  156.     /* gpio configuration */
  157.     gpio_pin_mux_config(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_GPIO_PinsSource, I2Cx_SCL_GPIO_MUX);

  158.     gpio_pin_mux_config(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_GPIO_PinsSource, I2Cx_SDA_GPIO_MUX);

  159.     /* configure i2c pins: scl */
  160.     gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  161.     gpio_init_structure.gpio_mode           = GPIO_MODE_MUX;
  162.     gpio_init_structure.gpio_out_type       = GPIO_OUTPUT_OPEN_DRAIN;
  163.     gpio_init_structure.gpio_pull           = GPIO_PULL_UP;

  164.     gpio_init_structure.gpio_pins           = I2Cx_SCL_GPIO_PIN;
  165.     gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_init_structure);

  166.     /* configure i2c pins: sda */
  167.     gpio_init_structure.gpio_pins           = I2Cx_SDA_GPIO_PIN;
  168.     gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_init_structure);
  169.                
  170.                 hi2cx.i2cx = I2Cx_PORT;

  171.   /* i2c config */
  172.   i2c_config(&hi2cx);
  173.         #endif
  174. }
  175. void IIC_Start(void) {
  176.         OLED_SCL_SET();
  177.         OLED_SDA_SET();
  178.         OLED_SDA_CLR();
  179.         OLED_SCL_CLR();
  180. }

  181. void IIC_Stop(void) {
  182.         OLED_SCL_SET();
  183.         OLED_SDA_CLR();
  184.         OLED_SDA_SET();
  185. }

  186. void IIC_Wait_Ack(void) {
  187.         OLED_SCL_SET();
  188.         OLED_SCL_CLR();
  189. }

  190. void Write_IIC_Byte(uint8_t IIC_Byte) {
  191.         i2c_status_type i2c_status;
  192.         #if 0
  193.         uint8_t i;
  194.         uint8_t m,da;
  195.         da=IIC_Byte;
  196.         OLED_SCL_CLR();
  197.         for(i=0;i<8;i++) {
  198.                 m=da;
  199.                 m=m&0x80;
  200.                 if(m==0x80) {
  201.                         OLED_SDA_SET();
  202.                 } else {
  203.                         OLED_SDA_CLR();
  204.                 }
  205.                 da=da<<1;
  206.                 OLED_SCL_SET();
  207.                 OLED_SCL_CLR();
  208.         }
  209.         #else
  210.         uint8_t buff[2] = {0};
  211.         buff[0] = IIC_Byte;
  212.         i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS, buff, 1, 1000);
  213.         if(i2c_status != I2C_OK)
  214.         {
  215.                 printf("erro send %d",i2c_status);
  216.         }
  217.         #endif
  218. }

  219. void Write_IIC_Command(uint8_t IIC_Command) {
  220.         i2c_status_type i2c_status;
  221.         #if 0
  222.         IIC_Start();
  223.         Write_IIC_Byte(IIC_SLAVE_ADDR);            //Slave address,SA0=0
  224.         IIC_Wait_Ack();       
  225.         Write_IIC_Byte(0x00);                        //write command
  226.         IIC_Wait_Ack();       
  227.         Write_IIC_Byte(IIC_Command);
  228.         IIC_Wait_Ack();       
  229.         IIC_Stop();
  230.         #else
  231.         uint8_t buff[2] = {0};
  232.         buff[0] = 0x00;
  233.         buff[1] = IIC_Command;
  234.         i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS, buff, 2, 1000);
  235.         if(i2c_status != I2C_OK)
  236.         {
  237.                 printf("erro send %d",i2c_status);
  238.         }
  239.         #endif
  240. }

  241. void Write_IIC_Data(uint8_t IIC_Data) {
  242.         i2c_status_type i2c_status;
  243.         #if 0
  244.         IIC_Start();
  245.         Write_IIC_Byte(IIC_SLAVE_ADDR);                        //D/C#=0; R/W#=0
  246.         IIC_Wait_Ack();       
  247.         Write_IIC_Byte(0x40);                        //write data
  248.         IIC_Wait_Ack();       
  249.         Write_IIC_Byte(IIC_Data);
  250.         IIC_Wait_Ack();       
  251.         IIC_Stop();
  252.         #else
  253.         uint8_t buff[2] = {0};
  254.         buff[0] = 0x40;
  255.         buff[1] = IIC_Data;
  256.         i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS, buff, 2, 1000);
  257.         if(i2c_status != I2C_OK)
  258.         {
  259.                 printf("erro send %d",i2c_status);
  260.         }
  261.         #endif
  262. }

  263. void GUI_DrawPoint(uint8_t x,uint8_t y,uint8_t color) {
  264.         OLED_Set_Pixel(x,y,color);
  265.         OLED_Display();
  266. }

  267. void GUI_Fill(uint8_t sx,uint8_t sy,uint8_t ex,uint8_t ey,uint8_t color) {
  268.         uint8_t i,j;
  269.         uint8_t width=ex-sx+1;                 //得到填充的宽度
  270.         uint8_t height=ey-sy+1;                //高度
  271.         for(i=0;i<height;i++)         {
  272.                 for(j=0;j<width;j++) {
  273.                         OLED_Set_Pixel(sx+j, sy+i,color);
  274.                 }               
  275.         }
  276.         OLED_Display();
  277. }

  278. void GUI_DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color) {
  279.         uint16_t t;
  280.         int xerr=0,yerr=0,delta_x,delta_y,distance;
  281.         int incx,incy,uRow,uCol;

  282.         delta_x=x2-x1; //计算坐标增量
  283.         delta_y=y2-y1;
  284.         uRow=x1;
  285.         uCol=y1;
  286.         if(delta_x>0) {
  287.                 incx=1; //设置单步方向
  288.         } else if(delta_x==0) {
  289.                 incx=0;//垂直线
  290.         }else {
  291.                 incx=-1;
  292.                 delta_x=-delta_x;
  293.         }

  294.         if(delta_y>0) {
  295.                 incy=1;
  296.         } else if(delta_y==0) {
  297.                 incy=0;//水平线
  298.         } else{
  299.                 incy=-1;delta_y=-delta_y;
  300.         }

  301.         if (delta_x>delta_y) {
  302.                 distance=delta_x; //选取基本增量坐标轴
  303.         } else {
  304.                 distance=delta_y;
  305.         }

  306.         //画线输出
  307.         for(t=0;t<=distance+1;t++ ) {
  308.                 OLED_Set_Pixel(uRow,uCol,color);
  309.                 xerr+=delta_x ;
  310.                 yerr+=delta_y ;
  311.                 if(xerr>distance) {
  312.                         xerr-=distance;
  313.                         uRow+=incx;
  314.                 }

  315.                 if(yerr>distance) {
  316.                         yerr-=distance;
  317.                         uCol+=incy;
  318.                 }
  319.         }  
  320.         OLED_Display();
  321. }

  322. void GUI_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color) {
  323.         GUI_DrawLine(x1,y1,x2,y1,color);
  324.         GUI_DrawLine(x1,y1,x1,y2,color);
  325.         GUI_DrawLine(x1,y2,x2,y2,color);
  326.         GUI_DrawLine(x2,y1,x2,y2,color);
  327. }

  328. void GUI_FillRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color) {
  329.         GUI_Fill(x1,y1,x2,y2,color);
  330. }

  331. void GUI_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size,uint8_t mode) {
  332.     unsigned char c=0,i=0,tmp,j=0;
  333.     c=chr-' ';//得到偏移后的值
  334.     if(x>WIDTH-1){x=0;y=y+2;}
  335.     if(Char_Size ==16) {
  336.         for(i=0;i<16;i++) {
  337.           if(mode) {
  338.                 tmp = F8X16[c*16+i];
  339.             } else {
  340.                 tmp = ~(F8X16[c*16+i]);
  341.             }
  342.             for(j=0;j<8;j++) {
  343.                 if(tmp&(0x80>>j)) {
  344.                     OLED_Set_Pixel(x+j, y+i,1);
  345.                 } else {
  346.                     OLED_Set_Pixel(x+j, y+i,0);
  347.                 }
  348.             }
  349.         }
  350.     } else if(Char_Size==8) {
  351.         for(i=0;i<8;i++) {
  352.             if(mode) {
  353.                 tmp = F6x8[c][i];
  354.             } else {
  355.                 tmp = ~(F6x8[c][i]);
  356.             }
  357.             for(j=0;j<8;j++) {
  358.                 if(tmp&(0x80>>j)) {
  359.                     OLED_Set_Pixel(x+j, y+i,1);
  360.                 } else {
  361.                     OLED_Set_Pixel(x+j, y+i,0);
  362.                 }
  363.             }
  364.         }
  365.         } else {
  366.                 return;
  367.         }
  368. }

  369. void GUI_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size,uint8_t mode) {
  370.         unsigned char j=0,csize;
  371.         if(Char_Size == 16) {
  372.         csize = Char_Size/2;
  373.         } else if(Char_Size == 8) {
  374.         csize = Char_Size/2+2;
  375.         } else {
  376.                 return;
  377.         }

  378.         while (chr[j]!='\0') {
  379.                 GUI_ShowChar(x,y,chr[j],Char_Size,mode);
  380.                 x+=csize;
  381.                 if(x>120) {
  382.                         x=0;
  383.                         y+=Char_Size;
  384.                 }
  385.                 j++;
  386.         }
  387.         OLED_Display();
  388. }

  389. static uint32_t mypow(uint8_t m,uint8_t n) {
  390.         uint32_t result=1;
  391.         while(n--)result*=m;   
  392.         return result;
  393. }

  394. void GUI_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t Size,uint8_t mode) {
  395.         uint8_t t,temp;
  396.         uint8_t enshow=0,csize;
  397.     if(Size == 16) {
  398.         csize = Size/2;
  399.         } else if(Size == 8) {
  400.         csize = Size/2+2;
  401.         } else {
  402.                 return;
  403.         }

  404.         for(t=0;t<len;t++) {
  405.                 temp=(num/mypow(10,len-t-1))%10;
  406.                 if(enshow==0&&t<(len-1)) {
  407.                         if(temp==0) {
  408.                                 GUI_ShowChar(x+csize*t,y,' ',Size,mode);
  409.                                 continue;
  410.                         } else {
  411.                 enshow=1;
  412.             }
  413.                 }
  414.                  GUI_ShowChar(x+csize*t,y,temp+'0',Size,mode);
  415.         }
  416.         OLED_Display();
  417. }  

  418. void OLED_ShowFNum(u8 x,u8 y,float Fnum,u8 size1,u8 mode)
  419. {
  420.         unsigned int i,flen;
  421.         unsigned char Data[sizeof(Fnum)];                                //数据位数由sizeof(Fnum) 来判断,灵活创建数组大小
  422.     sprintf((char *)Data,"%.3f",Fnum);                               //保留小数点后3位小数,打印到Data数组中
  423.         flen = sizeof(Data)+1;                                                                                                                         //判断浮点数长度,方便后期打印输出

  424.         for(i=0;i<flen;i++){                                                                                                                                 //根据转后字符长度打印输出
  425.                   x+=8;                                                                                                                                                                                //每个字符占8位,向后占位                                                                        
  426.                   GUI_ShowChar(x,y,Data[i],size1,mode);                             //调用oled字符显示函数,在OLED屏上逐个显示         
  427.                 }
  428. }

  429. void GUI_ShowCHinese(uint8_t x,uint8_t y,uint8_t hsize,uint8_t *str,uint8_t mode) {
  430.         while(*str!='\0') {
  431.                 if(hsize == 16) {
  432.                         GUI_ShowFont16(x,y,str,mode);

  433.                 } else if(hsize == 24) {
  434.                         //GUI_ShowFont24(x,y,str,mode);
  435.                 } else if(hsize == 32) {
  436.                         //GUI_ShowFont32(x,y,str,mode);

  437.         } else if(hsize == 12) {
  438.                         GUI_ShowFont12(x,y,str,mode);

  439.                 } else {
  440.                         return;
  441.                 }
  442.         if (hsize==12) {
  443.             x+=16;
  444.         } else {
  445.             x+=hsize;
  446.         }

  447.                 if(x>WIDTH-hsize) {
  448.                         x=0;
  449.                         y+=hsize;
  450.                 }
  451.                 str+=2;
  452.         }
  453.         OLED_Display();
  454. }

  455. void GUI_DrawBMP(uint8_t x,uint8_t y,uint8_t width, uint8_t height, uint8_t BMP[], uint8_t mode) {
  456.     uint8_t i,j,k;
  457.     uint8_t tmp;
  458.     for(i=0;i<height;i++) {
  459.                 for(j=0;j<(width+7)/8;j++) {
  460.                     if(mode) {
  461.                                 tmp = BMP[i*((width+7)/8)+j];
  462.                         } else {
  463.                                 tmp = ~BMP[i*((width+7)/8)+j];
  464.                         }

  465.                         for(k=0;k<8;k++) {
  466.                                 if(tmp&(0x80>>k)) {
  467.                                         OLED_Set_Pixel(x+j*8+k, y+i,1);
  468.                                 } else {
  469.                                         OLED_Set_Pixel(x+j*8+k, y+i,0);
  470.                                 }
  471.                         }
  472.         }
  473.     }
  474.     OLED_Display();
  475. }
  1. #ifndef __OLED_H
  2. #define __OLED_H                                   

  3. #include "at32f423.h"

  4. #define PAGE_SIZE    8
  5. #define XLevelL                   0x00
  6. #define XLevelH                   0x10
  7. #define YLevel       0xB0
  8. #define        Brightness         0xFF
  9. #define WIDTH              128
  10. #define HEIGHT              64       

  11. #define OLED_CMD     0        //写命令
  12. #define OLED_DATA    1        //写数据
  13. #define IIC_SLAVE_ADDR 0x78
  14. //IIC操作函数
  15. void II2CGpioInit(void);
  16. void IIC_Start(void);
  17. void IIC_Stop(void);
  18. void IIC_Wait_Ack(void);
  19. void Write_IIC_Byte(uint8_t IIC_Byte);
  20. void Write_IIC_Command(uint8_t IIC_Command);
  21. void Write_IIC_Data(uint8_t IIC_Data);

  22. //OLED控制函数
  23. void OLED_WR_Byte(unsigned char dat,unsigned char cmd);
  24. void OLED_Display_On(void);
  25. void OLED_Display_Off(void);
  26. void OLED_Set_Pos(unsigned char x, unsigned char y);
  27. void OLED_Init(void);
  28. void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color);
  29. void OLED_Display(void);
  30. void OLED_DisplayBMP(unsigned char *datoid);
  31. void OLED_Clear(void);
  32. //void OLED_Clear(unsigned char dat);

  33. //GUI画图函数
  34. void GUI_DrawPoint(unsigned char x, unsigned char y, unsigned char color);
  35. void GUI_Fill(unsigned char sx,unsigned char sy,unsigned char ex,unsigned char ey,unsigned char color);
  36. void GUI_DrawLine(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2,unsigned char color);
  37. void GUI_DrawRectangle(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2,unsigned char color);
  38. void GUI_FillRectangle(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2,unsigned char color);
  39. void GUI_DrawCircle(unsigned char xc, unsigned char yc, unsigned char color, unsigned char r);
  40. void GUI_FillCircle(unsigned char xc, unsigned char yc, unsigned char color, unsigned char r);
  41. void GUI_DrawTriangel(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char color);
  42. void GUI_FillTriangel(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char color);
  43. void GUI_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size,unsigned char mode);
  44. void GUI_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t Size,uint8_t mode);
  45. void OLED_ShowFNum(u8 x,u8 y,float Fnum,u8 size1,u8 mode);
  46. void GUI_ShowString(unsigned char x,unsigned char y,unsigned char *chr,unsigned char Char_Size,unsigned char mode);
  47. void GUI_ShowFont16(unsigned char x,unsigned char y,unsigned char *s,unsigned char mode);
  48. void GUI_ShowFont24(unsigned char x,unsigned char y,unsigned char *s,unsigned char mode);
  49. void GUI_ShowFont32(unsigned char x,unsigned char y,unsigned char *s,unsigned char mode);
  50. void GUI_ShowCHinese(unsigned char x,unsigned char y,unsigned char hsize,unsigned char *str,unsigned char mode);
  51. void GUI_DrawBMP(unsigned char x,unsigned char y,unsigned char width, unsigned char height, unsigned char BMP[], unsigned char mode);
  52. #endif
  1. #include "at32f423_board.h"
  2. #include "at32f423_clock.h"
  3. #include "ds18b20.h"
  4. #include "oled.h"
  5. #include "bmp.h"

  6. int main(void)
  7. {
  8.         uint8_t i, DS18B20ID[8];
  9.         char str[50];
  10.        __IO char ch,count=0;
  11. float temperature;
  12.   system_clock_config();
  13.   at32_board_init();
  14.         uart_print_init(115200);
  15.         OLED_Init();                       
  16.         OLED_Clear();
  17.         while (DS18B20_Init())
  18.         {
  19.                         printf("DS18B20 Init Error\r\n");
  20.                         delay_ms(1000);
  21.         }
  22.         DS18B20_ReadId(DS18B20ID);
  23.   printf("DS18B20的序列号是: 0x");  
  24.         for ( i = 0; i < 8; i ++ )            
  25.           printf ( "%.2X", DS18B20ID[i]);
  26.   printf("\n");  
  27.   sprintf(str,"DS18B20的序列号是:0x%02X%02X%02X%02X%02X%02X%02X%02X",DS18B20ID[0],DS18B20ID[1],DS18B20ID[2],DS18B20ID[3],DS18B20ID[4],DS18B20ID[5],DS18B20ID[6],DS18B20ID[7]);
  28. OLED_Clear();
  29.         GUI_ShowCHinese(26,10,16,(unsigned char *)"欢迎使用",0);
  30.         GUI_ShowCHinese(10,28,16,(unsigned char *)"雅特力开发板",0);
  31.         GUI_ShowString(10,46,(uint8_t *)"AT32F423VCT7",16,0);
  32.         delay_ms(1800);
  33. while(1)
  34.   {
  35.     temperature=DS18B20_GetTemp_MatchRom(DS18B20ID);
  36.     /* 打印通过 DS18B20 序列号获取的温度值 */
  37.     printf("获取该序列号器件的温度:%.1f\n",temperature);
  38.                 sprintf(str,"当前温度值为:%0.3f℃",temperature);
  39.                 OLED_Clear();
  40.                 GUI_ShowCHinese(10,10,16,(unsigned char *)"当前室内温度",0);
  41.                 GUI_ShowString(106,10,(uint8_t *)":",16,0);
  42.                 OLED_ShowFNum(32,28,temperature,16,0);
  43.                 GUI_ShowCHinese(80,28,16,(unsigned char *)"℃",0);
  44.                 GUI_ShowString(48,46,(uint8_t *)"2023-12-03",16,0);
  45.                 delay_ms(800);
  46.                 OLED_Clear();
  47. GUI_DrawBMP(0,0,128,64,BMP2,0);
  48.                 delay_ms(800);       
  49.   }
  50. }
   制作的单色位图如下:
**.bmp
     使用“PCtoLCD2002”的参数设置如下:
参数设置.png
      工程编译完成后,下载到开发板,OLED屏显示想关字符与图片,在此期间,将手指握住DS18B20模块,则采集的温度数值会迅速上升;然后将手指挪开,则采集的温度数值会缓慢回降。
https://v.youku.com/v_show/id_XNjIwOTE5Nzk2OA==.html
https://v.youku.com/v_show/id_XNjIwOTE5Nzk2OA==.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则

106

主题

1098

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部