读写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 char DATA) //写数据函数

{

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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值