这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 嵌入式开发» MCU» 51单片机读写24C64及TM1638显示按键模块

共14条 1/2 1 2 跳转至

51单片机读写24C64及TM1638显示按键模块

专家
2022-01-13 10:24:39 被打赏200分(兑奖) 打赏

一个测试读写24C64以及TM1638数码管显示、矩阵按键模块的工程,有源码。经过测试,没问题。


bit I2C_Start(void) {

delay_us(10);

I2C_SDA =1;

delay_us(10);

I2C_SCK =1;

delay_us(10);

if ( I2C_SDA == 0) return 0;

if ( I2C_SCK == 0) return 0;

I2C_SDA = 0;

delay_us(10);

I2C_SCK = 0;

delay_us(10);

return 1;

}


void I2C_Stop(void) {

delay_us(10);

I2C_SDA = 0;

delay_us(10);

I2C_SCK = 1;

delay_us(10);

I2C_SDA = 1;

delay_us(10);

}


void I2C_Ack(void) {

delay_us(10);

I2C_SDA=0;

delay_us(10);

I2C_SCK=1;

delay_us(10);

I2C_SCK=0;

delay_us(10);

}


void I2C_Nack(void) {

delay_us(10);

I2C_SDA=1;

delay_us(10);

I2C_SCK=1;

delay_us(10);

I2C_SCK=0;

delay_us(10);

}


bit I2C_Send_Byte( uchar d) {

uchar i = 8;

bit bit_ack;

while( i-- ) {

delay_us(10);

if (d & 0x80 ) I2C_SDA =1;

else I2C_SDA =0;

delay_us(10);

I2C_SCK = 1;

delay_us(10);

I2C_SCK = 0;

d = d << 1;

}

delay_us(10);

I2C_SDA = 1;

delay_us(10);

I2C_SCK = 1;

delay_us(10);

bit_ack = I2C_SDA;

I2C_SCK =0;

delay_us(10);

return bit_ack;

}


uchar I2C_Receive_Byte(void) {

uchar i = 8, d;

delay_us(10);

I2C_SDA = 1;

while ( i--) {

d = d << 1;

delay_us(10);

I2C_SCK =1;

if ( I2C_SDA ) d++;

delay_us(10);

I2C_SCK =0;

}

return d;

}


void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count) {

I2C_WP=0;

while(count--) {

I2C_Start();

/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */

I2C_Send_Byte( 0xa0 );

I2C_Send_Byte( AT24C64_address/256 );

I2C_Send_Byte( AT24C64_address %256 );

I2C_Send_Byte( *(uchar*)mcu_address );

I2C_Stop();

delay_ms(10); /* waiTIng for write cycle to be completed */

((uchar*)mcu_address)++;

AT24C64_address++;

}

I2C_WP=1;

}


//

void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count) {

while(count--) {

I2C_Start();

/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/ /* 24C16 USE */

I2C_Send_Byte( 0xa0 );

I2C_Send_Byte( AT24C64_address/256 );

I2C_Send_Byte( AT24C64_address % 256 );

I2C_Start();

/*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/

I2C_Send_Byte( 0xa1 );

*(uchar*)mcu_address = I2C_Receive_Byte();

I2C_Nack();

I2C_Stop();

((uchar*)mcu_address)++;

AT24C64_address++;

}

}

void TM1638_Write(unsigned charDATA)//写数据函数

{

unsigned char i;

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

{

CLK=0;

if(DATA&0X01)

DIO=1;

else

DIO=0;

DATA>>=1;

CLK=1;

}

}

unsigned char TM1638_Read(void)//读数据函数

{

unsigned char i;

unsigned char temp=0;

DIO=1;//设置为输入

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

{

temp>>=1;

CLK=0;

if(DIO)

temp|=0x80;

CLK=1;

}

return temp;

}

void Write_COM(unsigned char cmd)//发送命令字

{

STB=0;

TM1638_Write(cmd);

STB=1;

}


unsigned char Read_key(void)

{

unsigned char c[4],i,key_value=0;

STB=0;

TM1638_Write(0x42);

delay_ms(100);

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

c[i]=TM1638_Read();

STB=1;//4个字节数据合成一个字节

if(c[0]==0x04) key_value=1;

if(c[0]==0x40) key_value=2;

if(c[1]==0x04) key_value=3;

if(c[1]==0x40) key_value=4;

if(c[2]==0x04) key_value=5;

if(c[2]==0x40) key_value=6;

if(c[3]==0x04) key_value=7;

if(c[3]==0x40) key_value=8;

if(c[0]==0x02) key_value=9;

if(c[0]==0x20) key_value=10;

if(c[1]==0x02) key_value=11;

if(c[1]==0x20) key_value=12;

if(c[2]==0x02) key_value=13;

if(c[2]==0x20) key_value=14;

if(c[3]==0x02) key_value=15;

if(c[3]==0x20) key_value=16;

delay_ms(100);

return (key_value);

}

TM1638_24C64.zip




关键词: 单片机 24C64 TM1638显示按键模块

工程师
2022-01-18 19:38:36 打赏
2楼

干货


工程师
2022-01-18 19:47:21 打赏
3楼

很实用 谢谢分享


工程师
2022-01-18 19:50:59 打赏
4楼

很实用的文章


工程师
2022-01-18 19:55:14 打赏
5楼

感谢楼主分享


工程师
2022-01-18 20:04:03 打赏
6楼

感谢楼主分享


工程师
2022-01-18 20:18:13 打赏
7楼

干货


工程师
2022-01-18 23:46:58 打赏
8楼

感谢分享


高工
2022-01-19 23:37:44 打赏
9楼

模块总结的挺好的


工程师
2022-01-20 11:08:33 打赏
10楼

好家伙!纯干货!


共14条 1/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]