24c02存储多个数据.docx
- 文档编号:10569353
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:7
- 大小:43.26KB
24c02存储多个数据.docx
《24c02存储多个数据.docx》由会员分享,可在线阅读,更多相关《24c02存储多个数据.docx(7页珍藏版)》请在冰点文库上搜索。
24c02存储多个数据
24c02存储多个数据
24c02存储多个数据
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitsda=P2^0;//IO口定义
sbitscl=P2^1;
//此为待写入24c02的数据。
为了便于验证结果,数组的内容为周期重复的。
charcodemusic[]={
0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,
0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,
/////////24C02读写驱动程序////////////////////
voiddelay1(unsignedintm)
{
unsignedintn;
for(n=0;n } voidinit()//24c02初始化子程序 { scl=1; nop(); sda=1; nop(); } voidstart()//启动I2C总线 { sda=1; nop(); scl=1; nop(); sda=0; nop(); scl=0; nop(); } voidstop()//停止I2C总线 { sda=0; nop(); scl=1; nop(); sda=1; nop(); } voidwritebyte(unsignedcharj)//写一个字节 { unsignedchari,temp; temp=j; for(i=0;i<8;i++) { temp=temp<<1; scl=0; nop(); sda=CY;//temp左移时,移出的值放入了CY中 nop(); scl=1;//待sda线上的数据稳定后,将scl拉高 nop(); } scl=0; nop(); sda=1; nop(); } unsignedcharreadbyte()//读一个字节 { unsignedchari,j,k=0; scl=0;nop();sda=1; for(i=0;i<8;i++) { nop();scl=1;nop(); if(sda==1) j=1; else j=0; k=(k<<1)|j; scl=0; } nop(); return(k); } voidclock()//I2C总线时钟 { unsignedchari=0; scl=1; nop(); while((sda==1)&&(i<255)) i++; scl=0; nop(); } ////////从24c02的地址address中读取一个字节数据///// unsignedcharread24c02(unsignedcharaddress) { unsignedchari; start(); writebyte(0xa0); clock(); writebyte(address); clock(); start(); writebyte(0xa1); clock(); i=readbyte(); stop(); delay1(100); return(i); } //////向24c02的address地址中写入一字节数据info///// voidwrite24c02(unsignedcharaddress,unsignedcharinfo) { start(); writebyte(0xa0); clock(); writebyte(address); clock(); writebyte(info); clock(); stop(); delay1(5000);//这个延时一定要足够长,否则会出错。 因为24c02在从sda上取得数据后,还需要一定时间的烧录过程。 } voidmain() { ucharadd,i,j,m; init();//初始化24C02 P1=0X55; while(add! =sizeof(music)) { write24c02(0x00+add,music[add]); add++; if(add%4==0) P1=~P1; } P1=0X00; //到此为止,向24C02中写入数据的过程均已结束。 下面的程序为附加的。 //将已写入的数据再读出,送到P1口,通过观察led的亮灭来检验写入的数据是否正确。 while(m! =add) { buffer[i]=read24c02(m); i++; m++; } while(j! =add) { P1=buffer[j]; j++; delay(1000); } while (1); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 24 c02 存储 数据
![提示](https://static.bingdoc.com/images/bang_tan.gif)