1. 程式人生 > >基於STM32的常用數碼管晶片TM1637驅動

基於STM32的常用數碼管晶片TM1637驅動

unsigned char READ_KEY(void)
{
 unsigned char i,temp=0;
 TM1637_start();
 TM1637_Write(READ_COMMAND);
 DIO_IN;
 for(i=0;i<8;i++)
  {
   SCLK_L;
   Delay_us(1);
   temp<<=1;
   SCLK_H;
   if(HAL_GPIO_ReadPin(GPIOB, BIG_SDA_Pin))temp+=1;
   Delay_us(1);
  }
 TM1637_ack();
 TM1637_stop();
 return temp;
}
void display_fix(unsigned char add,unsigned char DispData)  //指定地址寫入資料
{
 TM1637_start();          //start signal sent to TM1637 from MCU
 TM1637_Write(ADD_FIX);//
 TM1637_stop();           //
 TM1637_start();          //
 TM1637_Write(ADDR_COMMAND|add);//
 TM1637_Write(DispData);//
 TM1637_stop();            //
}
void display_char(unsigned char add,unsigned char dis_char)
{
 unsigned char point_flag,char_code;
 point_flag=dis_char&0x80;
 dis_char&=0x7f;
 if(dis_char<16)
  char_code=tab[dis_char];
 else switch(dis_char)
  {
   case 'L':char_code=0x38;break;
   case 'H':char_code=0x76;break;
   case 'U':char_code=0x3e;break;
   case 'N':char_code=0x37;break;
   case 'P':char_code=0x73;break;
   case 'J':char_code=0x0e;break;
   case 'O':char_code=0x3f;break;
   case 'o':char_code=0x5c;break;
   case 'n':char_code=0x54;break;
   case 'Y':char_code=0x6e;break;
   case ' ':char_code=0;break;
   default:char_code=dis_char;break;
  }
 display_fix(add,char_code|point_flag);
}
void init_TM1637(void)
{
 unsigned char i;
 TM1637_start();
 TM1637_Write(ADD_AUTO);
 TM1637_stop(); 
 TM1637_start();
 TM1637_Write(ADDR_COMMAND);
 for(i=0;i<6;i++)
 TM1637_Write(0);
 TM1637_stop(); 
 TM1637_start();
 TM1637_Write(BRIGHT);
 TM1637_stop();
}