TM1638 test program STM8S

Publisher:心若水仙Latest update time:2020-01-13 Source: eefocusKeywords:TM1638 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//Applicable to STM8S003F3

//main.c file

//Experimental phenomenon: When the power is turned on, all digital tubes are on and display 0; when a button is pressed, the corresponding digital tube number increases by 1, and the corresponding LED light turns on

#include "stm8s.h"

#include "tm1638.h"

unsigned char num[8]; //The value displayed by each digital tube

int main(void)

{

u8 i;

       init_TM1638(); //Initialize TM1638

       for(i=0;i<8;i++)

       Write_DATA(i<<1,tab[0]); //Initialize registers (all digital tubes display 0 when powered on)

       while(1)

       {

              i=Read_key(); //Read key value

              if(i<8)

              {

                     num[i]++;

                     while(Read_key()==i); //Wait for the key to be released

                     if(num[i]>15)

                     num[i]=0; //Display values ​​from 0 to F

                     Write_DATA(i*2,tab[num[i]]);

                     Write_allLED(1<              }

       }

}



#ifdef USE_FULL_ASSERT

void assert_failed(u8* file,u32 line)

{

while(1)

  {

  }

}

#endif


//tm1638.h file

//PD4 pin connected to DIO //Data output and input

//PD5 pin connected to CLK //clock

//PD6 pin connected to STB

#ifndef _TM1638_H

#define _TM1638_H

#include "stm8s.h"

#define DATA_COMMAND 0X40

#define DISP_COMMAND 0x80

#define ADDR_COMMAND 0XC0 

//Common cathode digital tube display code

unsigned char tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,

                        0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; //1~f

void TM1638_Write(unsigned char DATA) //Write data function

{

u8 i;

       for(i=0;i<8;i++)

       {

             GPIO_WriteLow(GPIOD,GPIO_PIN_5); //CLK=0

              if(DATA&0X01)

                     GPIO_WriteHigh(GPIOD,GPIO_PIN_4); //DIO=1

              else

                     GPIO_WriteLow(GPIOD,GPIO_PIN_4); //DIO=0

              DATA>>=1;

              GPIO_WriteHigh(GPIOD,GPIO_PIN_5); //CLK=1

       }

}


unsigned char TM1638_Read(void) // read data function

{

       u8 i;

       u8 temp=0; //Store the read value

       GPIO_WriteHigh(GPIOD,GPIO_PIN_4); //Set to input (DIO=1)

       for(i=0;i<8;i++)

       {

              temp>>=1;

              GPIO_WriteLow(GPIOD,GPIO_PIN_5); //CLK=0

              if(GPIO_ReadInputPin(GPIOD,GPIO_PIN_4))//DIO==1?

                     temp|=0x80;

              GPIO_WriteHigh(GPIOD,GPIO_PIN_5); //CLK=1

       }

       return temp;

}


void Write_COM(unsigned char cmd) //Send command word

{

/*

Initialize the serial interface on the rising or falling edge, and then wait for receiving instructions. The first byte after STB is low is the instruction.

When STB is high, CLK is ignored.

*/


       GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0

       TM1638_Write(cmd);

       GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //STB=1


}


unsigned char Read_key(void)


{

       u8 c[4],i,key_value=0;

       GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0

       TM1638_Write(0x42); //Read key scan data command

       for(i=0;i<4;i++)             

              c[i]=TM1638_Read(); // Need to read 4 bytes continuously, can't skip

       GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //4 bytes of data are combined into one byte

       for(i=0;i<4;i++)

              key_value|=c[i]<       for(i=0;i<8;i++)

              if((0x01<                     break;

       return i; //Return the key number read

}


void Write_DATA(unsigned char add,unsignedchar DATA) //Write data to the specified address

{

       Write_COM(0x44);

       GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0

       TM1638_Write(0xc0|add); //The first byte of STB is low as the instruction

       TM1638_Write(DATA); //The second byte and after are used as data

       GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //STB=1

}


/*

void Write_oneLED(unsigned char num,unsigned char flag) //Control a LED function individually, num is the LED number to be controlled, flag is off when 0, and is on when not 0

{

       if(flag)

              Write_DATA(2*num+1,1);

       else

              Write_DATA(2*num+1,0);

} */

void Write_allLED(unsigned char LED_flag) //Control all LED functions, LED_flag indicates the status of each LED

{

       u8 i;

       for(i=0;i<8;i++)

              {

                    if(LED_flag&(1<                            //Write_DATA(2*i+1,3);

                            Write_DATA(2*i+1,1);

                     else

                            Write_DATA(2*i+1,0);

              }

}

 

//TM1638 initialization function

void init_TM1638(void)

{

  u8 i;

GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_SLOW); //Initialize related GPIO ports

GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_SLOW);

GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_SLOW);

       Write_COM(0x8b); //Brightness (0x88-0x8f) 8 levels of brightness adjustable

       Write_COM(0x40); //Add address automatically plus 1

       GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0         

       TM1638_Write(0xc0); //Set the starting address

       for(i=0;i<16;i++) //Transmit 16 bytes of data

              TM1638_Write(0x00);

       GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //STB=1

}

#endif

Keywords:TM1638 Reference address:TM1638 test program STM8S

Previous article:STM8S003F3 usage summary - timer
Next article:STM8S003 external interrupt recognizes the forward and reverse rotation of the mechanical encoder

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号