兄弟,参加电子设计大赛吧??b题?这个最重要的是看芯片资料,芯片资料内对通信方式,协议,时序,都有的,可以利用单片机自带的通信接口,即端口第二功能。也可以自己制定时序。这是24c02的程序。用的ATmega16;最后祝你有个好成绩#include<iom16v.h>#include<macros.h>#include"delay.h"#include"lcd.h"#defineucharunsignedchar#defineuintunsignedint/***************************************************************************函数名称:ERROR();*功能:蜂鸣器报错*参数:无*描述:PA0口置低可以鸣响蜂鸣器**************************************************************************/voidERROR(){uinti;DDRA|=(1<<PA0);PORTA&=~(1<<PA0);for(i=0;i<10000;i++);PORTA|=(1<<PA0);}//********************************************************************8//*函数向24c02发送一个字节//*名称wtwib();//*参数地址,数据//*********************************************************************/voidwtwib(ucharad,ucharda){TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送startwhile(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x08)ERROR();TWDR=0xA0;//adressofe2promTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x18)ERROR();TWDR=ad;//adressofthedataTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x28)ERROR();TWDR=da;//sendthedatatothebusTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x28)ERROR();TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//sendthestopsignal}//=================================================================//=======readabytefrom24c02//========rtwib()//==========needaddress//================================================================ucharrtwib(ucharad){ucharda;TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//sendthesignalofstartwhile(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x08)ERROR();TWDR=0xA1;//adressofe2promTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x18)ERROR();TWDR=ad;//adressofthedataTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x28)ERROR();TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//sendthesignalofstartwhile(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x08)ERROR();TWDR=0xA1;//adressofe2promTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x18)ERROR();TWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x58)ERROR();da=TWDR;TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//sendthestopsignalreturnda;}//========================================================//======sendapagetothe24c02//======wtwis();//=======needaddressanddataandnumber;//=========================================================voidwtwip(ucharad,ucharn,uchar*da){uchari;TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送startwhile(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x08)ERROR();TWDR=0xA0;//adressofe2promTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x18)ERROR();TWDR=ad;//adressofthedataTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x28)ERROR();for(i=0;i<n;i++){TWDR=da[i];//sendthedatatothebusTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x28)ERROR();}TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//sendthestopsignal}//====================================================//========readastringfromthe24C02//========rtwis();//========needaddress,number//=====================================================voidrtwis(ucharad,uchar*p,ucharn){uchari;TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//sendthesignalofstartwhile(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x08)ERROR();TWDR=0xA1;//adressofe2promTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x18)ERROR();TWDR=ad;//adressofthedataTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x28)ERROR();TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//sendthesignalofstartwhile(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x08)ERROR();TWDR=0xA1;//adressofe2promTWCR=(1<<TWINT)|(1<<TWEN);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x18)ERROR();for(i=0;i<n;i++){TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);while(!(TWCR&(1<<TWINT)));if((TWSR&0xF8)!=0x50)ERROR();p[i]=TWDR;}TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//sendthestopsignal}voidmain(){uchara[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},i=0;init_lcd();TWBR=0x1F;TWSR=0x02;for(i=0;i<16;i++)wtwib(0x00+i,i);for(i=0;i<16;i++)rtwib(0x00+i);for(i=0;i<16;i++)a[i]=0;for(i=0;i<16;i++)send_d(a[i]+48);}
2023-08-05 08:14:334