C8051F310 SMBus 调试OKWord文件下载.docx
- 文档编号:20794715
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:12
- 大小:19.34KB
C8051F310 SMBus 调试OKWord文件下载.docx
《C8051F310 SMBus 调试OKWord文件下载.docx》由会员分享,可在线阅读,更多相关《C8051F310 SMBus 调试OKWord文件下载.docx(12页珍藏版)》请在冰豆网上搜索。
CKCON|=0x04;
//定时器0使用系统时钟
TH0=0xEC;
//400k/s400kHZ
EIE1=0x01;
//EnableSMBusinterrupt允许SMB0的中断请求
SMB0CF|=0xD0;
//EnableSMBusand允许SDA建立和保持时间扩展禁止从方式
SMB0CN|=0x80;
//SMBus工作在主方式
TR0=1;
//Enabletimer1
}
voidTimer3_Init(void)
TMR3RLL=0x9F;
//Timer3willoverflowat25ms
TMR3RLH=0x38;
EIE1|=0x80;
//Enabletimer3interrupt允许TF3L或TF3H标志的中断请求
TMR3CN=0x04;
//使能定时器3
//------------------------------------------------------------------------------------
//Timer3InterruptServiceRoutine(ISR)
//
//ATimer3interruptindicatesanSMBusSCLlowtimeout.
//TheSMBusisdisabledandre-enabledhere
voidTimer3_ISR(void)interrupt14
SMB0CF&
=~0x80;
//禁止SMBus
SMB0CF|=0x80;
//重新启动SMBus
TMR3CN&
//中断标志清零
//SMBusInterruptServiceRoutine(ISR)SMB0DAT存放地址的
//SMBusISRstatemachine
//-Masteronlyimplementation-noslaveorarbitrationstatesdefined
//-Allincomingdataiswrittenstartingattheglobalpointer<
pSMB_DATA_IN>
//-Alloutgoingdataisreadfromtheglobalpointer<
pSMB_DATA_OUT>
voidSMBus_ISR(void)interrupt7
bitFAIL=0;
//UsedbytheISRtoflagfailed
//transfers
//numberofdatabytessentor
//received
staticbitSEND_START=0;
//Sendastart
switch(SMB0CN&
0xF0)//Statusvector高四位&
上不变低四位都为零
{
//MasterTransmitter/Receiver:
STARTconditiontransmitted.
caseSMB_MTSTA:
//【送器件地址】//开始传送case是0xE0
SMB0DAT=(TARGET|SMB_RW);
//Loadaddressofthetargetslave
//0XA1
//SMB0DAT|=SMB_RW//LoadR/Wbit
STA=0;
//ManuallyclearSTARTbitSTA:
SMBus起始标志
i=0;
//resetdatabytecounter
break;
/*为什么一开始第一步EEPROM_ByteRead()就要返回值?
结果到最后temp里有值却不返回*/
/*按照每个地址存一个一不应该下下移啊找的原因了*/
//MasterTransmitter:
Databyte(orSlaveAddress)transmitted
caseSMB_MTDB:
//case是0xC0//(MT)数据字节传送SMB0CN:
SMBus控制寄存器
if(ACK)/*时序上是低电平这个也可能是原因*///SlaveAddressorDataByte接收到“确认”
{//Acknowledged?
if(SEND_START)//
{/*按照时序应答之后开始根据程序的话会先进入判断之后再开始*/
STA=1;
//SMBus开始
SEND_START=0;
}
if(SMB_SENDWORDADDR)//Arewesendingthewordaddress?
高八位
SMB_SENDWORDADDR=0;
//Clearflag高八标志位
SMB0DAT=WORD_ADDR;
/*因为是AT24C08这只送一位地址*/
//【送高位地址】endword'
shigh8bitsaddress
if(SMB_RANDOMREAD)
SEND_START=1;
//sendaSTARTafterthenextACKcycle
SMB_RW=READ;
//elseif(SMB_SENDWORDADDR1)//Arewesendingthewordaddress?
低八位
//{
//SMB_SENDWORDADDR1=0;
//Clearflag
//SMB0DAT=WORD_ADDR1;
//【送低位地址】sendword'
slow8bitsaddress
///*一下注销部分移到上面因为只传一位地址*/
////if(SMB_RANDOMREAD)
////{
////SEND_START=1;
////SMB_RW=READ;
////}
//break;
//}
if(SMB_RW==WRITE)//IsthistransferaWRITE?
if(i<
SMB_DATA_LEN)//Istheredatatosend?
SMB_DATA_LEN=1;
{//【写数据】
SMB0DAT=*pSMB_DATA_OUT;
//senddatabyte*pSMB_DATA_OUT是指针指的是地址
pSMB_DATA_OUT++;
//incrementdataoutpointer数据++是加地址指向例如从0x00>
>
0x01
i++;
else
STO=1;
//setSTOtotermintetransfer传送接收
SMBUS_WRITE_FLAG=1;
//加标志位
i=0;
//i清零
else{}//IfthistransferisaREAD,
//thentakenoaction.Slave
//addresswastransmitted.A
//separate'
case'
isdefined
//fordatabyterecieved.
else//IfslaveNACK,
if(SMB_ACKPOLL)
//Restarttransfer
else
FAIL=1;
//Indicatefailedtransfer
}//andhandleatendofISR
//MasterReceiver:
bytereceived
caseSMB_MRDB:
//0x80//(MR)数据字节接收P0:
端口0寄存器
if(ACK)
{
if(SMB_RW==READ)
{
i++;
temp=SMB0DAT;
if(i==SMB_DATA_LEN)//Thisisthelastbyte
ACK=0;
//SendNACKtoindicatelastbyte
SMBUS_RECV_FLAG=1;
//ofthistransfer
STO=1;
i=0;
//SendSTOPtoterminatetransfer
}
}
default:
//所以的case事件都不成立的执行的事件
//andhandleatendofISR
if(FAIL)//Ifthetransferfailed,
SMB0CN&
=~0x40;
//Resetcommunication
SMB0CN|=0x40;
//先清零再发送模式
SI=0;
//clearinterruptflag清中断
//Functions
//EEPROM_ByteWrite()
//Thisfunctionwritesthevaluein<
dat>
tolocation<
addr>
intheEEPROMthen
//pollstheEEPROMuntilthewriteiscomplete.
/*addr原为int型unsignedintaddr现改成unsignedcharaddr*/
voidEEPROM_ByteWrite(unsignedcharaddr,unsignedchardat)//地址数据
//SetSMBusISRparameters
TARGET=EEPROM_ADDR;
//Settargetslaveaddress
SMB_RW=WRITE;
//Marknexttransferasawrite
SMB_SENDWORDADDR=1;
//Sendhigh8bitsWordAddressafterSlaveAddress
//SMB_SENDWORDADDR1=1;
/*原来为256两位地址现注销掉*/Sendlow8bitsWordAddressafterSlaveAddress
SMB_RANDOMREAD=0;
//DonotsendaSTARTsignalafter
//thewordaddress
SMB_ACKPOLL=1;
//EnableAcknowledgePolling(TheISR
//willautomaticallyrestartthe
//transferiftheslavedoesnot
//acknoledgeitsaddress.
//SpecifytheOutgoingData
WORD_ADDR=addr;
/*AT24C08一位地址修改*/
//WORD_ADDR=addr/256;
/*原来为256两位地址现注销掉*/Setthetargetaddressinthe
//EEPROM'
sinternalmemoryspace
//WORD_ADDR1=addr%256;
SMB_SINGLEBYTE_OUT=dat;
//storedat(localvariable)inaglobal
//variablesotheISRcanreaditafter
//thisfunctionexits
pSMB_DATA_OUT=&
SMB_SINGLEBYTE_OUT;
//Theoutgoingdatapointerpointsto
SMB_DATA_LEN=1;
//SpecifytoISRthatthenexttransfer
//willcontainonedatabyte
//InitiateSMBusTransfer
//EEPROM_ByteRead()
//Thisfunctionreturnsasinglebytefromlocation<
//pollsthe<
SMB_BUSY>
flaguntilthereadiscomplete.
unsignedcharEEPROM_ByteRead(unsignedcharaddr)//读的时候只能读一个字节,其他的是没有存,存不住的
{//即使放到了temp_char[8]中,但是在上电时就会失去了
//Arandomreadstartsasawrite
//thenchangestoareadafter
//therepeatedstartissent.The
//ISRhandlesthisswitchoverif
//the<
SMB_RANDOMREAD>
bitisset.
/*原为256两位地址现注销掉*///Sendlow8bitsWordAddressafterSlaveAddress
SMB_RANDOMREAD=1;
//SMB_RW=READ;
//SendaSTARTafterthewordaddress
//EnableAcknowledgePolling
//SpecifytheIncomingData
WORD_ADDR=addr;
/*原来为256两位地址现注销掉*/Setthetargetaddressinthe
while(SMBUS_RECV_FLAG==0);
/*原两句注销到了,所以每次STA=1之后就开始返回*/
SMBUS_RECV_FLAG=0;
/*加上之后是等STO=1也就是结束之后再返回这个值*/
//有returnretval就相当于A=retval
returntemp;
//SMBus.h文件
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
externvoidI2C_Init(void);
externvoidTimer3_Init(void);
externunsignedchartemp_char[12];
externbitSMBUS_RECV_FLAG;
externbitSMBUS_WRITE_FLAG;
sfr16DP=0x82;
sfr16ADC0=0xbd;
sfr16ADC0GT=0xc3;
sfr16ADC0LT=0xc5;
sfr16RCAP2=0xeb;
sfr16TMR2=0xcc;
sfr16TMR3=0x94;
#ifdefINT_ISR_C
#defineWRITE0x00//SMBusWRITEcommand
#defineREAD0x01//SMBusREADcommand
//Deviceaddresses(7bits,lsbisadon'
tcare)
#defineWrite_EE_Addr0xA0//器件写地址+写命令
#defineRead_EE_Addr0xa1//器件写地址+读命令
#defineEEPROM_ADDR0xA0//铁电存储器的地址
//Note:
Thisaddressisspecified
//intheMicrochip24C256
//datasheet.
//SMBusBufferSize
#defineSMB_BUFF_SIZE0x08//接收和发送的长度可以改吗?
//Definesthemaximumnumberofbytes
//应该是没用上这个声明//thatcanbesentorreceivedina
//singletransfer
//Statusvector-top4bitsonly
#defineSMB_MTSTA0xE0//(MT)开始传送ACC:
累加器
#defineSMB_MTDB0xC0//(MT)数据字节传送SMB0CN:
SMBus控制寄
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C8051F310 SMBus 调试OK 调试 OK