TM1638八位数码管八按键驱动程序(stm32f103驱动,使用了stm32 3.5库)

TM1638八位数码管八按键驱动程序(stm32f103驱动,使用了stm32 3.5库)http://blog.csdn.net/junzia/article/details/38843861

[cpp]  view plain  copy
  1. /********************************************************************************************** 
  2. **Program Assignment: Driver for TM1638 digital tube 
  3. **Author        : Wuwang 
  4. **Date              : 2014.8.26 9:00 
  5. **Description       : This is a driver for the board which is controled by thechip of tm1638.  
  6.               The board has eight digital tubes which have eight segments and eight keys. 
  7. ***********************************************************************************************/  
  8. #include "main.h"                              //#include "stm32f10x.h"   
  9.   
  10. /*********************define and global variables*********************************************/  
  11. #define STB GPIO_Pin_0                          //chip-select line  
  12. #define CLK GPIO_Pin_1                                      //clock line  
  13. #define DIO GPIO_Pin_2                                                                      //data line  
  14. #define Set(x) GPIO_SetBits(GPIOA,(x))              //Sets the selected data port bits  
  15. #define Reset(x) GPIO_ResetBits(GPIOA,(x))          //Resets the selected data port bits  
  16. #define Get(x) GPIO_ReadInputDataBit(GPIOA,(x))==SET        //Read the specified input port pin  
  17.   
  18.   
  19. uint16_t const tm_dat[2][14]={{'0','1','2','3','4','5',     //the char and its segment code   
  20.             '6','7','8','9','.','-','_',' '},  
  21.             {0x3F,0x06,0x5B,0x4F,0x66,0x6D,  
  22.             0x7D,0x07,0x7F,0x6F,0x80,0x40,  
  23.             0x08,0x00}};  
  24.   
  25. /*********************************************************************************************** 
  26. *Function Name: RCC_Config       
  27. *Purpose      : Configration Clock 
  28. ***********************************************************************************************/  
  29. void RCC_Config(){  
  30.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  
  31. }  
  32.   
  33. /*********************************************************************************************** 
  34. *Function Name: GPIO_Config      
  35. *Purpose      : Configration GPIO 
  36. ***********************************************************************************************/  
  37. void GPIO_Config(){  
  38.     GPIO_InitTypeDef GPIO_InitStructure;  
  39.     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;  
  40.     GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;  
  41.     GPIO_InitStructure.GPIO_Pin=STB|CLK|DIO;  
  42.     GPIO_Init(GPIOA,&GPIO_InitStructure);  
  43. }  
  44. /*********************************************************************************************** 
  45. *Function Name: Write_Byte       
  46. *Purpose      : Write one byte to the data port 
  47. *params       : byte  -------8-bits byte   
  48. *return       : none 
  49. ***********************************************************************************************/  
  50. void Write_Byte(uint8_t byte){  
  51.     uint8_t i=0;  
  52.     for(i=0;i<8;i++){  
  53.         Reset(CLK);  
  54.         if(byte&0x01){  
  55.             Set(DIO);  
  56.         }else{  
  57.             Reset(DIO);  
  58.         }  
  59.         Set(CLK);  
  60.         byte>>=1;  
  61.     }  
  62. }  
  63.   
  64. /*********************************************************************************************** 
  65. *Function Name: Read_Byte        
  66. *Purpose      : Read one byte from data port 
  67. *params       : none 
  68. *return       : the 8-bits byte which is read from data port 
  69. ***********************************************************************************************/  
  70. int8_t Read_Byte(){  
  71.     uint8_t i=0;  
  72.     uint8_t temp=0x00;  
  73.     for(i=0;i<8;i++){  
  74.         Set(CLK);  
  75.         temp>>=1;  
  76.         if(Get(DIO)){  
  77.             temp|=0x80;  
  78.         }  
  79.         Reset(CLK);  
  80.     }  
  81.     return temp;  
  82. }  
  83.   
  84. /*********************************************************************************************** 
  85. *Function Name: Write_Cmd        
  86. *Purpose      : Write a conmand to the data port 
  87. *params       : cmd  -------8-bits byte,the conmand,check the data sheet to find the conmand  
  88. *return       : none 
  89. ***********************************************************************************************/  
  90. void Write_Cmd(uint8_t cmd){  
  91.     Set(STB);  
  92.     Reset(STB);  
  93.     Write_Byte(cmd);  
  94. }  
  95.   
  96. /*********************************************************************************************** 
  97. *Function Name: Read_Key         
  98. *Purpose      : Read the key number which has been pressed 
  99. *params       : none 
  100. *return       : the number of the key. 0-8.  "return 0" represents no key has been pressed. 
  101. ***********************************************************************************************/  
  102. int8_t Read_Key(){  
  103.     uint8_t i=0;  
  104.     uint8_t key1=0x00;  
  105.     uint16_t key2=0x00;  
  106.     Write_Cmd(0x42);  
  107.     Set(DIO);                       //this is obligatory, check the data sheet,GPIO  
  108.     for(i=0;i<4;i++){  
  109.         key1=Read_Byte();  
  110.         key2|=(key1<<i);  
  111.     }  
  112.     key2>>=1;  
  113.     for(i=0;i<8;i++){  
  114.         if(0x01<<i==key2)return i+1;  
  115.     }  
  116.     return 0;  
  117. }  
  118.   
  119. /*********************************************************************************************** 
  120. *Function Name: Write_Dat        
  121. *Purpose      : Write data to the location specified 
  122. *params       : addr  ------the address,0x00 to 0x0f 
  123.         dat   ------the data,segment code 
  124. *return       : none 
  125. ***********************************************************************************************/  
  126. void Write_Dat(uint8_t addr,uint8_t dat){  
  127.     Write_Cmd(0x44);  
  128.     Write_Cmd(0xc0|addr);  
  129.     Write_Byte(dat);  
  130. }  
  131.   
  132. /*********************************************************************************************** 
  133. *Function Name: TM1638_SendData      
  134. *Purpose      : Write data to the location specified 
  135. *params       : i     ------the bit code of digtal tube,0 to 7 
  136.         str   ------the string,the char which was not in tm_data will be replace with "''". 
  137. *return       : none 
  138. ***********************************************************************************************/  
  139. void TM1638_SendData(uint8_t i,char * str){  
  140.     int j=0,k=0;  
  141.     unsigned char chr;  
  142.     for(;i<8;i++){  
  143.         k=0;  
  144.         for(j=0;j<14;j++){  
  145.             if(*str==tm_dat[0][j]){  
  146.                 chr=tm_dat[1][j];  
  147.                 k=1;  
  148.                 break;  
  149.             }  
  150.         }  
  151.           
  152.         if(k==0){  
  153.             chr=0x00;  
  154.         }  
  155.           
  156.         if(*(str+1)=='.'){  
  157.             chr|=0x80;  
  158.             Write_Dat(i*2,chr);  
  159.             str++;  
  160.         }else{  
  161.             Write_Dat(i*2,chr);  
  162.         }  
  163.         str++;  
  164.         if(*str=='\0')break;  
  165.     }  
  166. }  
  167.   
  168. /*********************************************************************************************** 
  169. *Function Name: TM1638_Init      
  170. *Purpose      : the initialization of tm1638 
  171. *params       : none 
  172. *return       : none 
  173. ***********************************************************************************************/  
  174. void TM1638_Init(){  
  175.     int i=0;  
  176.     RCC_Config();  
  177.     GPIO_Config();  
  178.     Write_Cmd(0x8a);  
  179.     Write_Cmd(0x40);  
  180.     for(i=0;i<16;i++){  
  181.         Write_Byte(0x00);  
  182.     }  
  183. }  

#include //1638he165合并程序 2018/5/26 #include #include #define uchar unsigned char #define uint unsigned int sbit SU0=P2^0; //计数脉冲识别 sbit SU1=P2^1; //计数脉冲识别 sbit SU2=P2^2; sbit DJ1=P1^0; sbit DJ2=P1^1; sbit QH=P3^2; //输出端 sbit CK=P3^3; //时钟 上升沿有效 sbit PL=P3^4; //移位控制 低电平有效 uchar temp; uchar temp1; uchar tempH; uchar tempL; bit weia; bit weib; bit ding; unsigned char num[8]; //各个数码管显示的值 unsigned int dingshiqi=0; unsigned int num1,num2; unsigned int su1_a,su1_b; unsigned int su2_a,su2_b; unsigned int su2,su4,su1; unsigned char wei,wei1; unsigned int k; void delay1ms(uint i) //1ms延时程序 { uchar j; while(i--) { for(j=0;j0; j--); } uint read_int165(void) { uchar i=0; uint read_data=0; PL=0; //置数,读入并行输入数据 _nop_(); PL=1; //移位,并口被锁存,串行转换开始 _nop_(); for(i=0;i<16;i++) //设定16位输入 { read_data<<=1; if(QH) { read_data|=QH; } CK=0; //下降沿 _nop_(); CK=1; _nop_(); //上升沿 } return read_data; } void init_t0() { TMOD = 0x02; //8位自动载定时器 TH0 = 0x06; TL0 = 0x06; TR0 = 1; //启动定时器 ET0=1; //允许定时器0中断 EA=1; //开总中断 } void main(void) { unsigned char i; init_t0(); init_TM1638(); for(i=0;i<8;i++) Write_DATA(i<>8); //获取高八位,存在tempH tempL=(uchar)temp; //获取低八位存在tempL P2=tempH; //接收的字节高八位显示在P2 P1=tempL; //接收的低八位显示在P1 } { i=Read_key(); switch(i) { case 0: //1--1 { while(Read_key()==i); //等待按键释放 su1_a = 0; su2_a = 0; wei=0; weia=1; ding=0; }break; case 1: { while(Read_key()==i); //等待按键释放 weia=0; wei++; if(wei>=3)wei = 0; }break; case 2: { while(Read_key()==i); //等待按键释放 if(wei==1) su1_b++; if(su1_b>5500) su1_b=0; if(wei==2) su2_b++; if(su2_b>5500) su2_b=0; }break; case 3: { while(Read_key()==i); //等待按键释放 if(wei==1) { if(su1_b>0)su1_b--; } if(wei==2) { if(su2_b>0)su2_b--; } }break; case 4: { while(Read_key()==i); //等待按键释放 ding=~ding; }break; case 5:{ while(Read_key()==i); }break; case 6:{ while(Read_key()==i); } break; case 7:{ while(Read_key()==i); }break; } if(wei==0) //脉冲输入计数 { if((ding==0)&&(weia==1)) { if(SU0 ==0) { delay(1); if(SU0==0) { while(!SU0); su1_a++; } } if(SU1 ==0) { delay(1); if(SU1==0) { while(!SU1); su2_a++; } } if(su1_a==su1_b) su1_a = 0; if(su2_a==su2_b) { ding = 1; } } Write_DATA(3*2,tab[su1_a]); Write_DATA(2*2,tab[su1_a0/10]); Write_DATA(1*2,tab[su1_a00/100]); Write_DATA(0*2,tab[su1_a000/1000]); Write_DATA(7*2,tab[su2_a]); Write_DATA(6*2,tab[su2_a0/10]); Write_DATA(5*2,tab[su2_a00/100]); Write_DATA(4*2,tab[su2_a000/1000]); } if(wei==1) //左边数码管设置 { k++; if(k>40)k = 0; if(k>10) { Write_DATA(3*2,tab[su1_b]); Write_DATA(2*2,tab[su1_b0/10]); Write_DATA(1*2,tab[su1_b00/100]); Write_DATA(0*2,tab[su1_b000/1000]); } else { Write_DATA(0*2,tab[20]); Write_DATA(1*2,tab[20]); Write_DATA(2*2,tab[20]); Write_DATA(3*2,tab[20]); } } if(wei==2) //右边数码管设置 { Write_DATA(3*2,tab[su1_a]); Write_DATA(2*2,tab[su1_a0/10]); Write_DATA(1*2,tab[su1_a00/100]); Write_DATA(0*2,tab[su1_a000/1000]); k++; if(k>40)k = 0; if(k>10) { Write_DATA(7*2,tab[su2_b]); Write_DATA(6*2,tab[su2_b0/10]); Write_DATA(5*2,tab[su2_b00/100]); Write_DATA(4*2,tab[su2_b000/1000]); } else { Write_DATA(4*2,tab[20]); Write_DATA(5*2,tab[20]); Write_DATA(6*2,tab[20]); Write_DATA(7*2,tab[20]); } } } } } void time0() interrupt 1 //使用的是定时器T0 { dingshiqi++; if(dingshiqi>=3686) { dingshiqi = 0; } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值