当前位置:网站首页>Driver and code of tm1640

Driver and code of tm1640

2020-12-08 12:33:57 osc_ 7wfxe2gv

TM1640 Driver and code

   drive TM 1640  It's not difficult , We just need to pay attention to 1640 Level change during communication , First of all, we can see 1640 The data book for ,( Complete code, click the sharing link at the end of the article , Self search )

 Insert picture description here

 Note the input and end conditions of the data , The input condition is CLK  For high voltage ,DIN From low to high , We use tradition 51 For example, SCM , Write TM1640 Input start function of ;

void TM1640_start ()
{

CLK = 0;			//CLK=0,DIN= 0 Increase code reliability 
DIN = 1;
CLK = 1;
delay_ms(5);       	//5 Millisecond delay 
DIN = 0;
delay_ms (5);
CLK = 0;
delay_ms (5);

}
The same end condition is CLK For the high time DIN The level changes from low to high , Write its ending function ;
void TM1640_stop ()
{


CLK = 0;             //CLK=0,DIN=0 To prevent the end of the transmission of data at the end of the instruction conflict 
DIN = 0;
CLK = 1;
delay_ms (5);
DIN = 1;
delay_ms (5);

}
We can see TM1640 In fact, it's not really bus communication , It's just through two IO Port level changes to achieve control ;TM1640 Is to split an eight bit data into a single data transmission for transmission 8 Time , Complete a data transmission , Pay attention to the level change during transmission ; Write its data transfer function ;
void send_byte (uint num)
{
uint q;



for (q= 8;q>0;q--)
{
	CLK = 0;
	if (num&0x01)
	{
			DIN = 1;
			delay_ms (5);
	}
	else 
	{
			DIN = 0;
			delay_ms(5);
	}
CLK = 1;
num = num>>1;
}
delay_um (5);

}
There are many ways to write transfer functions , I don't want to repeat it here .
The following code takes automatic address as an example ,TM1640 Default address add mode ,
 Insert picture description here


The default mode is auto add mode , So you can't choose a function mode , But for better understanding , Just below ;
You can see from the first picture that , want TM1640 Normal display nixie tube , Data transmission is sequential ,
1, Set up the data ( We usually only use Self adding mode 0x40, Fixed address mode 0x44)
Its function is ;


void model (uint k)
{

if (k)
{
	TM1640_start ();
	send_byte (0x40);// Add one automatically 
	TM640_stop ();
}
else 
{
	TM1640_start ();
	send_byte (0x44);// Fixed address 
	TM1640_stop ();

}

2, Set the address , The address is 16 position , They correspond to each other 16 Digital tube , You can see from the figure above that the address bits are from 0xc0~0xcf; Address transfer does not require a specific function , You can use send_byte ( ) Conduct ,
for example ;

TM1640_start();
send_byte (0xc0);

3, Set up transfer data , There are sixteen , Each data is continuous with each other , There is no need to use in the middle stop() Function breaks ; What you need to know is 16 The digital tube is circularly displayed , It's like the left shift of MCU data , It's like moving right

void wite_date () // Address automatic plus the first mock exam
{

TM1640_start ();
send_byte (0xc0);            // Enter the first address 
send_byte (num_display[0]);  // Input digital display data 1~16;
send_byte (num_display[1]);
send_byte (num_display[2]);
send_byte (num_display[3]);
send_byte (num_display[4]);
send_byte (num_display[5]);
send_byte (num_display[6]);
send_byte (num_display[7]);
send_byte (num_display[8]);
send_byte (num_display[9]);
send_byte (num_display[10]);
send_byte (num_display[11]);
send_byte (num_display[12]);
send_byte (num_display[13]);
send_byte (num_display[14]);
send_byte (num_display[15]);
TM1640_stop ();

}
This length is written to change the value of each nixie tube later , Readers can be concise ,

4, Display control , In fact, it is the brightness adjustment of the nixie tube , The smaller the pulse width, the higher the brightness
 Insert picture description here
void led_display (uint f)
{


start ();
send_byte (f);
stop ();

}

There is the first mock exam for the fixed address mode in the address automatic add mode , However, it should be noted that the fixed address mode should be used after both address and data occur stop () Function stop ,star () After the function starts, the next bit address and data transmission is carried out .

about TM1640 So far as I'm concerned , If you have any questions, please leave a message at the back .

link :https://pan.baidu.com/s/1ru7SMuWvpc9qvbdrxXw5zg
Extraction code :1640

版权声明
本文为[osc_ 7wfxe2gv]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012081233004903.html