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. }  

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值