⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 24c1.c

📁 使用c51单片机制作的气象五要素采集程序 包括DS1302温度以及TLC2543、24c04
💻 C
字号:
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long

sbit WDOG=P1^7;        //看门狗
sbit I2C_SCK=P1^6;     //24cxx的时钟线
sbit I2C_SDA=P1^5;     //24CXX的数据线

//uchar rbuf[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},wbuf[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //24c 操作

void FeedDog(void)
{
     WDOG=!WDOG;
}

void  Delay_10_uS(void)
{
        char i=10;
        while(i--);
}

void Delay_N_mS( uint n_milisecond)  /* n mS delay */
{
        uchar idata i;
        while(n_milisecond--)
        {
                i=37;
                while(i--);
        }
}

void I2C_Init(void)
{
        I2C_SCK=0;
        I2C_SDA=0;
}

bit I2C_Start(void)
{
        Delay_10_uS();
        I2C_SDA =1;
        Delay_10_uS();
        I2C_SCK =1;
        Delay_10_uS();
        if ( I2C_SDA == 0) return 0;
        if ( I2C_SCK == 0) return 0;
        I2C_SDA = 0;
        Delay_10_uS();
        I2C_SCK = 0;
        Delay_10_uS();
        return 1;
}

void  I2C_Stop(void)
{
        Delay_10_uS();
        I2C_SDA = 0;
        Delay_10_uS();
        I2C_SCK = 1;
        Delay_10_uS();
        I2C_SDA = 1;
        Delay_10_uS();
}

void I2C_Ack(void)
{
        Delay_10_uS();
        I2C_SDA=0;
        Delay_10_uS();
        I2C_SCK=1;
        Delay_10_uS();
        I2C_SCK=0;
        Delay_10_uS();
}

void I2C_Nack(void)
{
        Delay_10_uS();
        I2C_SDA=1;
        Delay_10_uS();
        I2C_SCK=1;
        Delay_10_uS();
        I2C_SCK=0;
        Delay_10_uS();
}

bit I2C_Send_Byte( uchar d)
{
        uchar idata i = 8;
        bit bit_ack;
        while( i-- )
        {
                Delay_10_uS();
                if ( d &0x80 )   I2C_SDA =1;
                else             I2C_SDA =0;
                Delay_10_uS();
                I2C_SCK = 1;
                Delay_10_uS();
                I2C_SCK = 0;
                d = d << 1;
        }
        Delay_10_uS();
        I2C_SDA = 1;
        Delay_10_uS();
        I2C_SCK = 1;
        Delay_10_uS();
        bit_ack = I2C_SDA;
        I2C_SCK =0;
        Delay_10_uS();
        return bit_ack;
}

uchar I2C_Receive_Byte(void)
{
        uchar idata i = 8, d;
        Delay_10_uS();
        I2C_SDA = 1;
        while ( i--)
        {
                d = d << 1;
                Delay_10_uS();
                I2C_SCK =1;
                if ( I2C_SDA ) d++;
                Delay_10_uS();
                I2C_SCK =0;
        }
        return d;
}

void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)
{
        FeedDog();
        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_N_mS(10);       /* waiting for write cycle to be completed */
                ((uchar*)mcu_address)++;
                AT24C64_address++;
        }
}

void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)
{
       FeedDog();
       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++;
       }
}
 /*
main()
{
         uchar idata wbuf[9]={7,7,0,0,12,1,1,1,45},rbuf[9]={0,0,0,0,0,0,0,0,0},i=5;
        while(1)
        {       //P23=0;
               i++;
                //AT24C64_W(0,10,5);
                //wbuf[0]=34;
                //wbuf[1]=7;
                //wbuf[2]=8;
                AT24C64_R(&rbuf,512,9);
                AT24C64_W(&wbuf,512,9);
                AT24C64_R(&rbuf,0,9);
                AT24C64_R(&rbuf,256,9);
                i++;
         }
}
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -