#include <AT89X52.H> sbit G=P1^4; //P1.4为显示颜色控制信号端口 sbit R=P1^5; //P1.5为显示颜色控制信号端口 unsigned char one_word=0x00; //接收计数 unsigned char data_buf[32]; //接收缓存 unsigned char i = 0x00; //缓存地址偏移变量 //--------------------------------------------- unsigned char abcd=0x00; //数据偏移量; /////////24C08读写驱动程序//////////////////// sbit scl=P3^4; //24c08 SCL sbit sda=P3^5; //24c08 SDA void delay1(unsigned char x) { unsigned int i; for(i=0;i<x;i++); } void flash() { ; ; } void x24c08_init() //24c08初始化子程序 { scl=1; flash(); sda=1; flash(); } void start() //启动I2C总线 { sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash(); } void stop() //停止I2C总线 { sda=0; flash(); scl=1; flash(); sda=1; flash(); } void writex(unsigned char j) //写一个字节 { unsigned char i,temp; temp=j; for (i=0;i<8;i++) { temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash(); } scl=0; flash(); sda=1; flash(); } unsigned char readx() //读一个字节 { unsigned char i,j,k=0; scl=0; flash(); sda=1; for (i=0;i<8;i++) { flash(); scl=1; flash(); if (sda==1) j=1; else j=0; k=(k<<1)|j; scl=0; } flash(); return(k); } void clock() //I2C总线时钟 { unsigned char i=0; scl=1; flash(); while ((sda==1)&&(i<255))i++; scl=0; flash(); } ////////从24c02的地址address中读取一个字节数据///// unsigned char x24c08_read(unsigned char address) { unsigned char i; start(); writex(0xa0); clock(); writex(address); clock(); start(); writex(0xa1); clock(); i=readx(); stop(); delay1(10); return(i); } //////向24c02的address地址中写入一字节数据info///// void x24c08_write(unsigned char address,unsigned char info) { EA=0; start(); writex(0xa0); clock(); writex(address); clock(); writex(info); clock(); stop(); EA=1; delay1(50); } /////////////24C08读写驱动程序完/////////////////////
//------------串口中断部分-------// void Init_Com(void) { TMOD = 0x20; PCON = 0x00; SCON = 0x50; //;串口为方式1 8 UART TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; //;波特率9600 REN = 1; ES = 1; EA = 1; } void ComInt() interrupt 4 { unsigned char dat; unsigned char abc=0x00; ES = 0; RI = 0; if(i>31) //缓存地址偏移变量rest { i = 0x00; } if(one_word>32)//PC DOWN 计数 偏移变量rest(好)字 { one_word=0x01; } one_word++; i++; dat = SBUF; data_buf = dat; //LED缓存 SBUF = dat; do{}while( TI == 0 ); TI = 0; ES = 1; } //-------串口end----------------- //-----------READ IIC STAR-------------------- void IIC_start(void) { unsigned char IIC_add=0x00; //IIC unsigned char DATA_add=0x00; //缓存 Init_Com(); //开串口中断 if(one_word>31) //判断是否接收完一个汉字的数据 { DATA_add=0x00; //初始化缓存地址偏移变量 for(DATA_add=0x00;DATA_add<31;) //字扫描;IIC地址初始化 { x24c08_write(IIC_add,data_buf[DATA_add]); //在24c08的地址IIC_add中写入数据 DATA_add++; //缓存地址加一 IIC_add++; //IIC地址加一 } } } //-------------IIC end ----------------- void main(void) { x24c08_init(); //初始化24C08 IIC_start(); //IIC存储 //sec=x24c08_read(abcd); //读出保存的数据赋于sec //x24c08_write(abcd,sec); //在24c08的地址2中写入数据sec }
|