[其他ST产品] stm32c8t6矩阵控制led亮灯 显示时间在4线oled屏幕

[复制链接]
 楼主| 9dome猫 发表于 2022-8-22 23:17 | 显示全部楼层 |阅读模式

直接上代码文章末尾讲解 注文章篇幅较长可能需要点阅读时间

首先是key.h文件


  1. #define KEY_HANG1 GPIO_Pin_0
  2. #define KEY_HANG2 GPIO_Pin_1
  3. #define KEY_HANG3 GPIO_Pin_2
  4. #define KEY_HANG4 GPIO_Pin_3


  5. #define KEY1  GPIO_Pin_4
  6. #define KEY2  GPIO_Pin_5
  7. #define KEY3  GPIO_Pin_6
  8. #define KEY4  GPIO_Pin_7


  9. #define KEY1_Input  GPIO_ReadInputDataBit(KEY_PROT,KEY1)
  10. #define KEY2_Input  GPIO_ReadInputDataBit(KEY_PROT,KEY2)
  11. #define KEY3_Input  GPIO_ReadInputDataBit(KEY_PROT,KEY3)
  12. #define KEY4_Input  GPIO_ReadInputDataBit(KEY_PROT,KEY4)


  13. #define KEY_PROT GPIOA


  14. extern  u8  flag;  
  15. u16 *abc_read();
  16. int sun1_read();

  17. void KEY_Init(void);//IO初始化
  18. void KEY_config(u8 *key);
  19. u8 Read_KeyValue(void);
  20. void Key_Input( u8 *key1);




评论

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/shihuntz/article/details/123933145 ———————————————— 版权声明:本文为CSDN博主「shihuntz」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/shihuntz/article/details/1  发表于 2022-8-22 23:20
 楼主| 9dome猫 发表于 2022-8-22 23:23 | 显示全部楼层
oled.h文件
  1. #ifndef __OLED_H
  2. #define __OLED_H

  3. #include "sys.h"
  4. #include "stdlib.h"       

  5. //-----------------OLED端口定义----------------



  6. #define OLED_SCL_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_1)//SCL   A 0
  7. #define OLED_SCL_Set() GPIO_SetBits(GPIOB,GPIO_Pin_1)

  8. #define OLED_SDA_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_10)//DIN   A 1
  9. #define OLED_SDA_Set() GPIO_SetBits(GPIOB,GPIO_Pin_10)

  10. #define OLED_RES_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_7)//RES   A 2
  11. #define OLED_RES_Set() GPIO_SetBits(GPIOB,GPIO_Pin_7)


  12. #define OLED_CMD  0        //写命令
  13. #define OLED_DATA 1        //写数据


  14. extern  u8  flag;  

  15. void KEY_Init(void);//IO初始化
  16. void KEY_config(u8 *key);
  17. u8 Read_KeyValue(void);
  18. void Key_Input( u8 *key1);




  19. void OLED_ClearPoint(u8 x,u8 y);
  20. void OLED_ColorTurn(u8 i);
  21. void OLED_DisplayTurn(u8 i);
  22. void I2C_Start(void);
  23. void I2C_Stop(void);
  24. void I2C_WaitAck(void);
  25. void Send_Byte(u8 dat);
  26. void OLED_WR_Byte(u8 dat,u8 mode);
  27. void OLED_DisPlay_On(void);
  28. void OLED_DisPlay_Off(void);
  29. void OLED_Refresh(void);
  30. void OLED_Clear(void);
  31. void OLED_DrawPoint(u8 x,u8 y,u8 t);
  32. void OLED_DrawLine(u8 x1,u8 y1,u8 x2,u8 y2,u8 mode);
  33. void OLED_DrawCircle(u8 x,u8 y,u8 r);
  34. void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1,u8 mode);
  35. void OLED_ShowChar6x8(u8 x,u8 y,u8 chr,u8 mode);
  36. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 size1,u8 mode);
  37. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1,u8 mode);
  38. void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1,u8 mode);
  39. void OLED_ScrollDisplay(u8 num,u8 space,u8 mode);
  40. void OLED_ShowPicture(u8 x,u8 y,u8 sizex,u8 sizey,u8 BMP[],u8 mode);
  41. void OLED_Init(void);

  42. #endif


 楼主| 9dome猫 发表于 2022-8-22 23:30 | 显示全部楼层
oled.c
  1. #include "oled.h"
  2. #include "stdlib.h"
  3. #include "oledfont.h"
  4. #include "delay.h"
  5. #include "stm32f10x.h"
  6. #include "usart.h"

  7. u8 OLED_GRAM[144][8];



  8. //反显函数
  9. void OLED_ColorTurn(u8 i)
  10. {
  11.         if(i==0)
  12.                 {
  13.                         OLED_WR_Byte(0xA6,OLED_CMD);//正常显示
  14.                 }
  15.         if(i==1)
  16.                 {
  17.                         OLED_WR_Byte(0xA7,OLED_CMD);//反色显示
  18.                 }
  19. }

  20. //屏幕旋转180度
  21. void OLED_DisplayTurn(u8 i)
  22. {
  23.         if(i==0)
  24.                 {
  25.                         OLED_WR_Byte(0xC8,OLED_CMD);//正常显示
  26.                         OLED_WR_Byte(0xA1,OLED_CMD);
  27.                 }
  28.         if(i==1)
  29.                 {
  30.                         OLED_WR_Byte(0xC0,OLED_CMD);//反转显示
  31.                         OLED_WR_Byte(0xA0,OLED_CMD);
  32.                 }
  33. }

  34. //延时
  35. void IIC_delay(void)
  36. {
  37.         u8 t=3;
  38.         while(t--);
  39. }

  40. //起始信号
  41. void I2C_Start(void)
  42. {
  43.         OLED_SDA_Set();
  44.         OLED_SCL_Set();
  45.         IIC_delay();
  46.         OLED_SDA_Clr();
  47.         IIC_delay();
  48.         OLED_SCL_Clr();
  49.         IIC_delay();
  50. }

  51. //结束信号
  52. void I2C_Stop(void)
  53. {
  54.         OLED_SDA_Clr();
  55.         OLED_SCL_Set();
  56.         IIC_delay();
  57.         OLED_SDA_Set();
  58. }

  59. //等待信号响应
  60. void I2C_WaitAck(void) //测数据信号的电平
  61. {
  62.         OLED_SDA_Set();
  63.         IIC_delay();
  64.         OLED_SCL_Set();
  65.         IIC_delay();
  66.         OLED_SCL_Clr();
  67.         IIC_delay();
  68. }

  69. //写入一个字节
  70. void Send_Byte(u8 dat)
  71. {
  72.         u8 i;
  73.         for(i=0;i<8;i++)
  74.         {
  75.                 if(dat&0x80)//将dat的8位从最高位依次写入
  76.                 {
  77.                         OLED_SDA_Set();
  78.     }
  79.                 else
  80.                 {
  81.                         OLED_SDA_Clr();
  82.     }
  83.                 IIC_delay();
  84.                 OLED_SCL_Set();
  85.                 IIC_delay();
  86.                 OLED_SCL_Clr();//将时钟信号设置为低电平
  87.                 dat<<=1;
  88.   }
  89. }

  90. //发送一个字节
  91. //mode:数据/命令标志 0,表示命令;1,表示数据;
  92. void OLED_WR_Byte(u8 dat,u8 mode)
  93. {
  94.         I2C_Start();
  95.         Send_Byte(0x78);
  96.         I2C_WaitAck();
  97.         if(mode){Send_Byte(0x40);}
  98.   else{Send_Byte(0x00);}
  99.         I2C_WaitAck();
  100.         Send_Byte(dat);
  101.         I2C_WaitAck();
  102.         I2C_Stop();
  103. }

  104. //开启OLED显示
  105. void OLED_DisPlay_On(void)
  106. {
  107.         OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
  108.         OLED_WR_Byte(0x14,OLED_CMD);//开启电荷泵
  109.         OLED_WR_Byte(0xAF,OLED_CMD);//点亮屏幕
  110. }

  111. //关闭OLED显示
  112. void OLED_DisPlay_Off(void)
  113. {
  114.         OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
  115.         OLED_WR_Byte(0x10,OLED_CMD);//关闭电荷泵
  116.         OLED_WR_Byte(0xAE,OLED_CMD);//关闭屏幕
  117. }

  118. //更新显存到OLED       
  119. void OLED_Refresh(void)
  120. {
  121.         u8 i,n;
  122.         for(i=0;i<8;i++)
  123.         {
  124.                 OLED_WR_Byte(0xb0+i,OLED_CMD); //设置行起始地址
  125.                 OLED_WR_Byte(0x00,OLED_CMD);   //设置低列起始地址
  126.                 OLED_WR_Byte(0x10,OLED_CMD);   //设置高列起始地址
  127.                 I2C_Start();
  128.                 Send_Byte(0x78);
  129.                 I2C_WaitAck();
  130.                 Send_Byte(0x40);
  131.                 I2C_WaitAck();
  132.                 for(n=0;n<128;n++)
  133.                 {
  134.                         Send_Byte(OLED_GRAM[n][i]);
  135.                         I2C_WaitAck();
  136.                 }
  137.                 I2C_Stop();
  138.   }
  139. }

  140. //清屏函数
  141. void OLED_Clear(void)
  142. {
  143.         u8 i,n;
  144.         for(i=0;i<8;i++)
  145.         {
  146.            for(n=0;n<128;n++)
  147.                         {
  148.                          OLED_GRAM[n][i]=0;//清除所有数据
  149.                         }
  150.   }
  151.         //OLED_Refresh();//更新显示
  152. }

  153. //画点
  154. //x:0~127
  155. //y:0~63
  156. //t:1 填充 0,清空       
  157. void OLED_DrawPoint(u8 x,u8 y,u8 t)
  158. {
  159.         u8 i,m,n;
  160.         i=y/8;
  161.         m=y%8;
  162.         n=1<<m;
  163.         if(t){OLED_GRAM[x][i]|=n;}
  164.         else
  165.         {
  166.                 OLED_GRAM[x][i]=~OLED_GRAM[x][i];
  167.                 OLED_GRAM[x][i]|=n;
  168.                 OLED_GRAM[x][i]=~OLED_GRAM[x][i];
  169.         }
  170. }

  171. //画线
  172. //x1,y1:起点坐标
  173. //x2,y2:结束坐标
  174. void OLED_DrawLine(u8 x1,u8 y1,u8 x2,u8 y2,u8 mode)
  175. {
  176.         u16 t;
  177.         int xerr=0,yerr=0,delta_x,delta_y,distance;
  178.         int incx,incy,uRow,uCol;
  179.         delta_x=x2-x1; //计算坐标增量
  180.         delta_y=y2-y1;
  181.         uRow=x1;//画线起点坐标
  182.         uCol=y1;
  183.         if(delta_x>0)incx=1; //设置单步方向
  184.         else if (delta_x==0)incx=0;//垂直线
  185.         else {incx=-1;delta_x=-delta_x;}
  186.         if(delta_y>0)incy=1;
  187.         else if (delta_y==0)incy=0;//水平线
  188.         else {incy=-1;delta_y=-delta_x;}
  189.         if(delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴
  190.         else distance=delta_y;
  191.         for(t=0;t<distance+1;t++)
  192.         {
  193.                 OLED_DrawPoint(uRow,uCol,mode);//画点
  194.                 xerr+=delta_x;
  195.                 yerr+=delta_y;
  196.                 if(xerr>distance)
  197.                 {
  198.                         xerr-=distance;
  199.                         uRow+=incx;
  200.                 }
  201.                 if(yerr>distance)
  202.                 {
  203.                         yerr-=distance;
  204.                         uCol+=incy;
  205.                 }
  206.         }
  207. }
  208. //x,y:圆心坐标
  209. //r:圆的半径
  210. void OLED_DrawCircle(u8 x,u8 y,u8 r)
  211. {
  212.         int a, b,num;
  213.     a = 0;
  214.     b = r;
  215.     while(2 * b * b >= r * r)      
  216.     {
  217.         OLED_DrawPoint(x + a, y - b,1);
  218.         OLED_DrawPoint(x - a, y - b,1);
  219.         OLED_DrawPoint(x - a, y + b,1);
  220.         OLED_DrawPoint(x + a, y + b,1);

  221.         OLED_DrawPoint(x + b, y + a,1);
  222.         OLED_DrawPoint(x + b, y - a,1);
  223.         OLED_DrawPoint(x - b, y - a,1);
  224.         OLED_DrawPoint(x - b, y + a,1);
  225.         
  226.         a++;
  227.         num = (a * a + b * b) - r*r;//计算画的点离圆心的距离
  228.         if(num > 0)
  229.         {
  230.             b--;
  231.             a--;
  232.         }
  233.     }
  234. }



  235. //在指定位置显示一个字符,包括部分字符
  236. //x:0~127
  237. //y:0~63
  238. //size1:选择字体 6x8/6x12/8x16/12x24
  239. //mode:0,反色显示;1,正常显示
  240. void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1,u8 mode)
  241. {
  242.         u8 i,m,temp,size2,chr1;
  243.         u8 x0=x,y0=y;
  244.         if(size1==8)size2=6;
  245.         else size2=(size1/8+((size1%8)?1:0))*(size1/2);  //得到字体一个字符对应点阵集所占的字节数
  246.         chr1=chr-' ';  //计算偏移后的值
  247.         for(i=0;i<size2;i++)
  248.         {
  249.                 if(size1==8)
  250.                           {temp=asc2_0806[chr1][i];} //调用0806字体
  251.                 else if(size1==12)
  252.         {temp=asc2_1206[chr1][i];} //调用1206字体
  253.                 else if(size1==16)
  254.         {temp=asc2_1608[chr1][i];} //调用1608字体
  255.                 else if(size1==24)
  256.         {temp=asc2_2412[chr1][i];} //调用2412字体
  257.                 else return;
  258.                 for(m=0;m<8;m++)
  259.                 {
  260.                         if(temp&0x01)OLED_DrawPoint(x,y,mode);
  261.                         else OLED_DrawPoint(x,y,!mode);
  262.                         temp>>=1;
  263.                         y++;
  264.                 }
  265.                 x++;
  266.                 if((size1!=8)&&((x-x0)==size1/2))
  267.                 {x=x0;y0=y0+8;}
  268.                 y=y0;
  269.   }
  270. }


  271. //显示字符串
  272. //x,y:起点坐标  
  273. //size1:字体大小
  274. //*chr:字符串起始地址
  275. //mode:0,反色显示;1,正常显示
  276. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 size1,u8 mode)
  277. {
  278.         while((*chr>=' ')&&(*chr<='~'))//判断是不是非法字符!
  279.         {
  280.                 OLED_ShowChar(x,y,*chr,size1,mode);
  281.                 if(size1==8)x+=6;
  282.                 else x+=size1/2;
  283.                 chr++;
  284.   }
  285. }

  286. //m^n
  287. u32 OLED_Pow(u8 m,u8 n)
  288. {
  289.         u32 result=1;
  290.         while(n--)
  291.         {
  292.           result*=m;
  293.         }
  294.         return result;
  295. }


  296. //显示数字
  297. //x,y :起点坐标
  298. //num :要显示的数字
  299. //len :数字的位数
  300. //size:字体大小
  301. //mode:0,反色显示;1,正常显示
  302. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1,u8 mode)
  303. {
  304.         u8 t,temp,m=0;
  305.         if(size1==8)m=2;
  306.         for(t=0;t<len;t++)
  307.         {
  308.                 temp=(num/OLED_Pow(10,len-t-1))%10;
  309.                         if(temp==0)
  310.                         {
  311.                                 OLED_ShowChar(x+(size1/2+m)*t,y,'0',size1,mode);
  312.       }
  313.                         else
  314.                         {
  315.                           OLED_ShowChar(x+(size1/2+m)*t,y,temp+'0',size1,mode);
  316.                         }
  317.   }
  318. }


  319. //显示汉字
  320. //x,y:起点坐标
  321. //num:汉字对应的序号
  322. //mode:0,反色显示;1,正常显示
  323. void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1,u8 mode)
  324. {
  325.         u8 m,temp;
  326.         u8 x0=x,y0=y;
  327.         u16 i,size3=(size1/8+((size1%8)?1:0))*size1;  //得到字体一个字符对应点阵集所占的字节数
  328.         for(i=0;i<size3;i++)
  329.         {
  330.                 if(size1==16)
  331.                                 {temp=Hzk1[num][i];}//调用16*16字体
  332.                 else if(size1==24)
  333.                                 {temp=Hzk2[num][i];}//调用24*24字体
  334.                 else if(size1==32)      
  335.                                 {temp=Hzk3[num][i];}//调用32*32字体
  336.                 else if(size1==64)
  337.                                 {temp=Hzk4[num][i];}//调用64*64字体
  338.                 else return;
  339.                 for(m=0;m<8;m++)
  340.                 {
  341.                         if(temp&0x01)OLED_DrawPoint(x,y,mode);
  342.                         else OLED_DrawPoint(x,y,!mode);
  343.                         temp>>=1;
  344.                         y++;
  345.                 }
  346.                 x++;
  347.                 if((x-x0)==size1)
  348.                 {x=x0;y0=y0+8;}
  349.                 y=y0;
  350.         }
  351. }

  352. //num 显示汉字的个数
  353. //space 每一遍显示的间隔
  354. //mode:0,反色显示;1,正常显示
  355. void OLED_ScrollDisplay(u8 num,u8 space,u8 mode)
  356. {
  357.         u8 i,n,t=0,m=0,r;
  358.         while(1)
  359.         {
  360.                 if(m==0)
  361.                 {
  362.             OLED_ShowChinese(128,24,t,16,mode); //写入一个汉字保存在OLED_GRAM[][]数组中
  363.                         t++;
  364.                 }
  365.                 if(t==num)
  366.                         {
  367.                                 for(r=0;r<16*space;r++)      //显示间隔
  368.                                  {
  369.                                         for(i=1;i<144;i++)
  370.                                                 {
  371.                                                         for(n=0;n<8;n++)
  372.                                                         {
  373.                                                                 OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
  374.                                                         }
  375.                                                 }
  376.            OLED_Refresh();
  377.                                  }
  378.         t=0;
  379.       }
  380.                 m++;
  381.                 if(m==16){m=0;}
  382.                 for(i=1;i<144;i++)   //实现左移
  383.                 {
  384.                         for(n=0;n<8;n++)
  385.                         {
  386.                                 OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
  387.                         }
  388.                 }
  389.                 OLED_Refresh();
  390.         }
  391. }

  392. //x,y:起点坐标
  393. //sizex,sizey,图片长宽
  394. //BMP[]:要写入的图片数组
  395. //mode:0,反色显示;1,正常显示
  396. void OLED_ShowPicture(u8 x,u8 y,u8 sizex,u8 sizey,u8 BMP[],u8 mode)
  397. {
  398.         u16 j=0;
  399.         u8 i,n,temp,m;
  400.         u8 x0=x,y0=y;
  401.         sizey=sizey/8+((sizey%8)?1:0);
  402.         for(n=0;n<sizey;n++)
  403.         {
  404.                  for(i=0;i<sizex;i++)
  405.                  {
  406.                                 temp=BMP[j];
  407.                                 j++;
  408.                                 for(m=0;m<8;m++)
  409.                                 {
  410.                                         if(temp&0x01)OLED_DrawPoint(x,y,mode);
  411.                                         else OLED_DrawPoint(x,y,!mode);
  412.                                         temp>>=1;
  413.                                         y++;
  414.                                 }
  415.                                 x++;
  416.                                 if((x-x0)==sizex)
  417.                                 {
  418.                                         x=x0;
  419.                                         y0=y0+8;
  420.                                 }
  421.                                 y=y0;
  422.      }
  423.          }
  424. }


  425. //OLED的初始化
  426. void OLED_Init(void)
  427. {
  428.         GPIO_InitTypeDef  GPIO_InitStructure;
  429.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);         //使能A端口时钟
  430.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_1;         
  431.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;                 //开漏输出
  432.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  433.         GPIO_Init(GPIOB, &GPIO_InitStructure);          //初始化PA0,1
  434.         GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_1);

  435.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;         
  436.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出              
  437.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz                     
  438.         GPIO_Init(GPIOB, &GPIO_InitStructure);          //初始化PA2                  
  439.         GPIO_SetBits(GPIOB,GPIO_Pin_7);
  440.        
  441.         OLED_RES_Clr();
  442.         delay_ms(200);
  443.         OLED_RES_Set();
  444.        
  445.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  446.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  447.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  448.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  449.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  450.         OLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current Brightness
  451.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  452.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  453.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  454.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  455.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  456.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  457.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  458.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  459.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  460.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  461.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  462.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  463.         OLED_WR_Byte(0x12,OLED_CMD);
  464.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  465.         OLED_WR_Byte(0x30,OLED_CMD);//Set VCOM Deselect Level
  466.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  467.         OLED_WR_Byte(0x02,OLED_CMD);//
  468.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  469.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  470.         OLED_Clear();
  471.         OLED_WR_Byte(0xAF,OLED_CMD);
  472. }


 楼主| 9dome猫 发表于 2022-8-22 23:40 | 显示全部楼层
key.c文件
  1. #include "stm32f10x.h"
  2. #include "key.h"
  3. #include "sys.h"
  4. #include "delay.h"
  5. #include "usart.h"


  6. //按键初始化函数
  7. void KEY_Init(void) //IO初始化
  8. {
  9.         GPIO_InitTypeDef GPIO_InitStructure;

  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);

  11.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_5;  
  12.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //推挽输出
  14.         GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
  15.         GPIO_SetBits(GPIOB,GPIO_Pin_5);  
  16.        
  17.         GPIO_InitStructure.GPIO_Pin  = KEY1|KEY2|KEY3|KEY4;   //行  a  4 5 6 7
  18.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA0设置成输入,默认上拉  
  19.         GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
  20.        
  21.        
  22.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;  //lie  
  23.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                                                                                                         //a 4 5 6 7
  24.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
  25.         GPIO_Init(GPIOA, &GPIO_InitStructure);  
  26.         GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);       //a 0 1 2 3
  27. }



  28. u8 Key = 0;
  29. u8 flag=0;
  30. u16 abc[9] = {0,0,0,0,0,0,0,0,0};

  31. u8 i = 0;
  32. u8 Flag = 0;
  33. int sum1 = 0;

  34. int sun1_read()
  35. {
  36.    return sum1;
  37. }

  38. u16 * abd_read();


  39. u16 *abc_read()
  40. {
  41.                 return &abc[0];
  42. }


  43. int Wei_Shu(u8 a)
  44. {
  45.         int sum = 1, i = 0;
  46.         for(;i<a;i++)
  47.         {
  48.         sum =        sum*10;
  49.         }
  50.         return sum;
  51. }


  52. u16 time(u16 abc[9],u8 j)
  53. {       
  54.         int time = 0 ,a = j ,shijian = 0;
  55.                 for(time = 0; time < j; time++ )
  56.         {
  57.                 shijian +=  abc[time]*Wei_Shu(a--);  
  58.         }
  59.         return shijian;               
  60. }

  61. char ok = 0;
  62. char led_liang = 0;
  63. void Key_Input( u8 *key1)
  64. {       
  65.         if(ok==1){
  66.        
  67.        
  68.                 if(*key1 == 16 && Flag == 0)
  69.         {
  70.                 Flag = 1;
  71.         }
  72.         else if(Flag == 1)
  73.         {               
  74.                
  75.          if(*key1 == 16 && Flag == 1)
  76.         {
  77.                 Flag = 0;
  78.                
  79.                         sum1 = (short)(time(abc,i)/10);
  80.                         printf(" shijian =%d\r\n",sum1);
  81.                
  82.                          if(0<sum1)
  83.                         {
  84.                                 sum1 = sum1*1000;
  85.                                 led_liang = 1;
  86.                         }
  87.                         //
  88.                 for(i=0;i<9;i++)
  89.                         abc[i] = 0;       
  90.                 i = 0;       
  91.          }       
  92.                         else if(*key1<10)
  93.                                 {
  94.                                         abc[i] = *key1;
  95.                                         i++;
  96.                                                 if(i>9)
  97.                                                 {
  98.                                                          i = 0;
  99.                                           }
  100.                 }
  101.         }
  102.        
  103.         else if(*key1 == 13){
  104.                 sum1 = 0;
  105.                                         //
  106.                 for(i=0;i<9;i++)
  107.                         abc[i] = 0;       
  108.                 i = 0;       
  109.         }
  110.        
  111.        
  112.         ok = 0;
  113. }
  114.        
  115.                 if(led_liang==1 && sum1>0){
  116.                                 GPIO_SetBits(GPIOB,GPIO_Pin_5);       
  117.                                 //delay_ms(1);               
  118.                                 sum1--;                       
  119.                         }
  120.                
  121.                         if(sum1==0){
  122.                                 led_liang = 0;
  123.                                 GPIO_ResetBits(GPIOB,GPIO_Pin_5);       
  124.                         }
  125.                


  126. }

  127. void KEY_config(u8 *Key)
  128. {
  129.         char i=1,j=1;
  130.         *Key = 0;
  131.         for(;i<5;i++){
  132.                          GPIO_ResetBits(GPIOA,GPIO_Pin_0<<(i-1));     //先拉低
  133.                         if((KEY1_Input == RESET) || (KEY2_Input == RESET) || (KEY3_Input == RESET) || (KEY4_Input == RESET))
  134.                         {
  135.                                         delay_ms(10);
  136.                                         for(j=1;j<5;j++){
  137.                                                 if(GPIO_ReadInputDataBit(KEY_PROT,GPIO_Pin_4<<(j-1)) == RESET){
  138.                                                         flag = 1;
  139.                                                         *Key = (i-1)*4+j;
  140.                                                         while(!GPIO_ReadInputDataBit(KEY_PROT,GPIO_Pin_4<<(j-1))){};
  141.                                                         //Key_Input(Key);
  142.                                                                 ok = 1;
  143.                                                         break;                                               
  144.                                                 }
  145.                                                 else
  146.                                                         flag = 0;
  147.                                         }
  148.                   }
  149.                         GPIO_SetBits(GPIOA,GPIO_Pin_0<<(i-1)); //再次拉高
  150.          }
  151. }


 楼主| 9dome猫 发表于 2022-8-22 23:43 | 显示全部楼层
main.c函数
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "usart.h"
  6. #include "exti.h"
  7. #include "beep.h"
  8. #include "timer.h"
  9. #include "stm32f10x_tim.h"
  10. #include "oled.h"


  11. u8 Key000=0;

  12. //定时器3中断服务程序
  13. void TIM3_IRQHandler(void)   //TIM3中断
  14. {
  15.         if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
  16.                 {
  17.                 TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx更新中断标志
  18.                         KEY_config(&Key000);
  19.                         Key_Input(&Key000);
  20.                 }
  21. }

  22. char a[9]={0,0,0,0,0,0,0,0,0};
  23. char *abd_xiugai(u16 abc[9]){
  24.         char i = 0;
  25.   while(abc[i]!=0){
  26.                 a[i] = abc[i]  +  48;
  27.                 i++;
  28.                 if(i==7)
  29.                         break;
  30.                 }
  31.         return a;
  32. }
  33.          
  34. char b[5];
  35. char *sun1_read_xiu_acill(int a){
  36.         //1234000s
  37.        
  38.          a = a/1000;
  39.        
  40.          b[3] = (char)(a%10+48);
  41.          b[2] = (char)((a/10)%10)+48;
  42.          b[1] = (char)((a/100)%10)+48;
  43.          b[0] = (char)(a/1000)+48;
  44.          b[4] = 0;
  45.          return b;
  46. }

  47. int main(void)
  48. {               
  49.         u8 is_anxia = 0;
  50.         delay_init();                     //延时函数初始化         
  51.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  52.         uart_init(4800);         //串口初始化4800
  53.          KEY_Init();
  54.         //LED_Init();                                  //初始化与LED连接的硬件接口
  55.         //LED0=0;                                        //先点亮红灯
  56.         TIM3_Int_Init(89999,2);
  57.         OLED_Init();
  58.         OLED_ColorTurn(0);//0正常显示,1 反色显示
  59.   OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示

  60.         while(1)
  61.         {                        

  62.                 OLED_Clear();
  63.                
  64.                 if(*(abc_read()+0) == 0 && sun1_read()==0)
  65.                         OLED_ShowString(10,0,"P 16",16,1);
  66.                 else if(sun1_read()==0)
  67.                         OLED_ShowString(10,0,abd_xiugai(abc_read()),16,1);
  68.                 else if(sun1_read()!=0)
  69.                         OLED_ShowString(10,0,sun1_read_xiu_acill(sun1_read()),16,1);

  70.                
  71.                 OLED_Refresh();
  72.         }
  73.         }

  74. /*
  75. void TIM3_IRQHandler(void)   //TIM3中断
  76. {
  77.         if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
  78.                 {
  79.                 TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx更新中断标志
  80.        
  81.                                 Key_Input(&key);
  82.                 }
  83. }*/

 楼主| 9dome猫 发表于 2022-8-22 23:46 | 显示全部楼层
先来个总述

接线方式  矩阵键盘的8个引脚对应GPIOA 0---7

                led灯对应 GPIOB 5     可以替换为一个LS298N模块控制电机 或者其他的很多东西

                 oled屏幕对应接线 scl - GPIO B1

                                               sda-GPIOB7

                                                VCC和GND随意了
 楼主| 9dome猫 发表于 2022-8-22 23:47 | 显示全部楼层
接下来是对此代码的详解

        首先是对于key.h 文件的解释我们对于矩阵键盘对应IO口的一个声明,之后我们可以将对应的IO口进行简化方便使用,也使我们的代码更有可读性。

对于oled.h也是同理,这就不在啰嗦了,应该都懂吧
 楼主| 9dome猫 发表于 2022-8-22 23:48 | 显示全部楼层
对于key.c 文件是本文章的核心部分

        首先是对于矩阵键盘八个对应引脚的初始化这里我们选择扫描的方式来进行按键检测,但是之后我遇到了一个大问题 那就是延时的问题 会发现在我们使用oled后,时间会变得不可控。
 楼主| 9dome猫 发表于 2022-8-22 23:50 | 显示全部楼层
在这里我们对于八个IO口选择 4个推挽输出 4个上拉输出的模式  在这里我将led灯对应的IO口初始化也放在这里了。对于io口的初始化就不在详细的说明 ,没什么好说的 只需要注意和.h文件的对应关系
 楼主| 9dome猫 发表于 2022-8-22 23:51 | 显示全部楼层
之后我们来说说

void KEY_config(u8 *Key)
 楼主| 9dome猫 发表于 2022-8-22 23:52 | 显示全部楼层
对于此函数需要一丁点的位操作知识 用到了一点<<    >>的操作

  1. void KEY_config(u8 *Key)
  2. {
  3.         char i=1,j=1;
  4.         *Key = 0;
  5.         for(;i<5;i++){
  6.                          GPIO_ResetBits(GPIOA,GPIO_Pin_0<<(i-1));     //先拉低
  7.                         if((KEY1_Input == RESET) || (KEY2_Input == RESET) || (KEY3_Input == RESET) || (KEY4_Input == RESET))
  8.                         {
  9.                                         delay_ms(10);
  10.                                         for(j=1;j<5;j++){
  11.                                                 if(GPIO_ReadInputDataBit(KEY_PROT,GPIO_Pin_4<<(j-1)) == RESET){
  12.                                                         flag = 1;
  13.                                                         *Key = (i-1)*4+j;
  14.                                                         while(!GPIO_ReadInputDataBit(KEY_PROT,GPIO_Pin_4<<(j-1))){};
  15.                                                         //Key_Input(Key);
  16.                                                                 ok = 1;
  17.                                                         break;                                               
  18.                                                 }
  19.                                                 else
  20.                                                         flag = 0;
  21.                                         }
  22.                   }
  23.                         GPIO_SetBits(GPIOA,GPIO_Pin_0<<(i-1)); //再次拉高
  24.          }
  25. }

 楼主| 9dome猫 发表于 2022-8-22 23:53 | 显示全部楼层
 楼主| 9dome猫 发表于 2022-8-22 23:54 | 显示全部楼层
我们在初始化时将4路IO口设置为默认上拉  也就是一直输出高电平

所以我们只需要循环检测 电平的高低 如果有按键按下 那么图上边的一排1 必然有一路变为低电平

我们只需要在循环扫描一列4路哪一路为低电平  如果2路都为低电平则可以确认按键的位置 相当于在按键位置花了一个十字 确认位置。
 楼主| 9dome猫 发表于 2022-8-22 23:55 | 显示全部楼层
好了原理已经说清了,借来下进入我们的代码部分

我们循环拉低四行的电平 那么比如我们再按下第一个按键时可以知道第一列的电平也会被拉低

这就对应了我们的前五行代码

之间插入一个按键消抖操作
Uriah 发表于 2022-9-30 10:30 | 显示全部楼层

待向GPIO(通用I/O端口)的输入从0变为1时,程序可以一定的间隔来检查GPIO的状态
Bblythe 发表于 2022-9-30 13:29 | 显示全部楼层

CPLD解密,DSP解密都习惯称为单片机解密
saservice 发表于 2022-10-1 12:56 | 显示全部楼层
stm32驱动oled如何实现反色显示表示选中功能
kkzz 发表于 2022-10-1 13:26 | 显示全部楼层
stm32怎样才能同时驱动OLED和LCD两个屏
qiufengsd 发表于 2022-10-1 13:52 | 显示全部楼层
stm32硬件IIC不好用               
jkl21 发表于 2022-10-1 14:25 | 显示全部楼层
stm32 怎么向oled输出数据
您需要登录后才可以回帖 登录 | 注册

本版积分规则

139

主题

1575

帖子

2

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