完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#include #define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode #define FOSC 11059200L //System frequenc #define BAUD 9600 //UART baudrate #define I2CERR_NO_ERROR 0 #define DELAY_TIME 15 //H: 25uS-45uS //第二步:调这个参数和下面的参数,使得每次读写时的SCl时序如参考图所示:每个bite读写的时间是30us左右; #define DELAY_ST 20 //Hb:18mS-25mS //第一个下降沿和第二个下降沿之间的间隔是20ms左右 //#ifndef UINT8 typedef unsigned char UINT8; //#endif //#ifndef UINT16 typedef unsigned int UINT16; //#endif ***it SDA = P0^0; ***it SETBUSY =P0^2; ***it SCL = P0^1; ***it LED = P1^6; idata bit busy_1; unsigned int count_0=0; xdata unsigned char ReceiveBuf[130] = {0}; //接收数据(指令)存放数组 xdata unsigned char ReceiveBuf1[230] = {0}; //接收数据(指令)存放数组 xdata unsigned char ReceiveBuf2[232] = {0}; //接收数据(指令)存放数组 idata unsigned char ReceiveCount = 0; //接收数据(指令)字节数 idata unsigned char ReceiveFlag = 0; //接收数据(指令)标志位 xdata unsigned char check=0; unsigned char data2[10]={0x30,0x00,0x01,0x1c,0xE3,0x00,0xFF,0x20,0x88,0xD7}; void Uart_Isr(); void Serial_Init(void); void time0_init(); void tm0_isr(); void SendData(unsigned char dat); void SendString(unsigned char *s); void Delay10ms(unsigned int c); UINT8 readI2C2(char* readtempbuf); void writeI2C(char* data2, UINT8 count); void Set_SDA_IO(void); void SetSDAInput(void); void SetSDAOutput(void); void SetSDALow(void); void SetSDAHigh(void); UINT8 GetDINStatus(void); void DelayTime( UINT16 lTime ); void SendACKSign(void); UINT16 I2CStart(void); UINT16 I2CWriteData( char bData ); void remote_poweron_init(void); void Set_SCL_IO(void); void SetSCLOutput(void); void SetSCLInput(void); void SetSCLLow(void); void SetSCLHigh(void); void Set_BUSY_IO(void); void SetBUSYInput(void); void SetBUSYOutput(void); void SetBUSYHigh(void); void SetBUSYLow(void); UINT8 GetBUSYStatus(void); UINT8 GetACKSign(void); UINT16 I2CClose(void); UINT16 I2COpen(void); UINT16 I2CStop(void); void Learn_start2(void); void main() { //初始化变量 unsigned char i; //初始化函数 time0_init(); Serial_Init(); remote_poweron_init(); while(1) { if(ReceiveFlag==1) { ReceiveFlag=0; if((ReceiveBuf[0]==0x30)&&(ReceiveBuf[1]==0x20))//第三步1,如果收到的是30 20H开头的数据,进入方法2,学习,处理数据,发回上层或本主控处理发送 { //学习 Learn_start2(); Delay10ms(10); while(BUSY==0) Delay10ms(1); if(BUSY==1) { readI2C2(ReceiveBuf1); //进入学习后,busy由低变高,读数据 Delay10ms(10 ); for(i=0;i<230;i++) { SendData(ReceiveBuf1[i]); } while(1) for(i=0;i<=2;i++) { LED=0; Delay10ms(10); LED=1; Delay10ms(10); } Delay10ms(100); ReceiveBuf2[0]=0x30; ReceiveBuf2[1]=0x03; for(i=1;i<230;i++) { ReceiveBuf2[i+1]=ReceiveBuf1[i]; check=ReceiveBuf1[i]+check; } check=check+0x03+0x30; ReceiveBuf2[231]=(unsigned char)check; //本主控处理,加校验 for(i=0;i<232;i++) { SendData(ReceiveBuf2[i]); } } // while(1) // for(i=0;i<=2;i++) // { // LED=0; // Delay10ms(10); // LED=1; // Delay10ms(10); // } Delay10ms(500); //hxd019芯片规定,连续读写的间隔至少0.8S,这里可以修改 while(1) { writeI2C(ReceiveBuf2, 232); Delay10ms(200); } for(i=0; i<230; i++) { ReceiveBuf1[i] = 0; } for(i=0; i<232; i++) { ReceiveBuf2[i] = 0; } } else { //转发 for(i=0; i SendData(ReceiveBuf[i]); } Delay10ms(100); writeI2C(ReceiveBuf, ReceiveCount); //第三步2,下发数据给hxd019,上层给什么数据就发什么数据,这里可以发测试数据data2: for(i=0; i ReceiveBuf[i] = 0; } ReceiveCount = 0; Delay10ms(100); } } } } void tm0_isr() interrupt 1 //1ms定时器中断函数 { TL0 = T1MS; //reload timer0 low byte TH0 = T1MS >> 8; //reload timer0 high byte) if(ReceiveFlag == 2) { count_0++; if (count_0 >= 10) //1ms * 1000 -> 1s { count_0 = 0; //reset counter ReceiveFlag = 1; } } } void time0_init() { TMOD = 0x01; //set timer0 as mode1 (16-bit) TL0 = T1MS; //initial timer0 low byte TH0 = T1MS >> 8; //initial timer0 high byte TR0 = 1; //timer0 start running ET0 = 1; //enable timer0 interrupt EA = 1; //open global interrupt switch count_0 = 0; //initial counter } void Serial_Init(void) { SCON0 = 0x50; //8-bit variable UART TMOD |= 0x20; //Set Timer1 as 8-bit auto reload mode TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule TR1 = 1; //Timer1 start run ES0 = 1; //Enable UART interrupt REN0 = 1; EA = 1; //Open master interrupt switch } void Uart_Isr() interrupt 4 using 1 { if (RI0) { RI0 = 0; //Clear receive interrupt flag ReceiveFlag = 2; ReceiveBuf[ReceiveCount++] = SBUF0; count_0 = 0; } if (TI0) { TI0 = 0; //Clear transmit interrupt flag busy_1 = 0; //Clear transmit busy flag } } void SendData(unsigned char dat) { while (busy_1 ); //Wait for the completion of the previous data is sent busy_1 = 1; SBUF0 = dat; //Send data to UART buffer } void Delay10ms(unsigned int c) //误差 0us { unsigned char a,b; for(;c>0;c--) for(b=168;b>0;b--) for(a=163;a>0;a--); } //配置三个IO=================================================================================================== void PORT_Init (void) { XBR0 = 0x00; XBR2 = 0x40; P0MDOUT = 0x00; P1MDOUT |= 0x40; P0=0XFF; } //=====================================================上电初始化或待机相关IO设置函数 void remote_poweron_init(void) /// { Set_SDA_IO(); SetSDAHigh(); SetSDAOutput(); Set_SCL_IO(); SetSCLHigh(); SetSCLOutput(); Set_BUSY_IO(); SetBUSYInput(); } void DelayTime( UINT16 lTime ) { UINT16 i; for(i=0;i ; } } void RemoteDelayms(UINT16 data1) { UINT16 i; while(data1--) { for(i=0;i<552;i++){}// 1ms每次循环5000 次 } } //=========== UINT8 GetACKSign(void) { UINT8 ACKSign; UINT16 dataRead=0; SetSDAInput(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); ACKSign = GetDINStatus(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); return ACKSign; } void SendACKSign(void) { SetSDAOutput(); DelayTime(DELAY_TIME); SetSDALow(); ///hxd;后加低SDA_Low,没有验证过 DelayTime(DELAY_TIME); SetSCLHigh(); //第9次的CLK的高 DelayTime(DELAY_TIME); //;1mS SetSCLLow(); //1mS } UINT16 I2COpen(void) { Set_SDA_IO(); Set_SCL_IO(); SetSDAOutput(); SetSCLOutput(); SetSCLHigh(); SetSDAHigh(); return I2CERR_NO_ERROR; } UINT16 I2CClose(void) { SetSDAOutput(); SetSCLOutput(); SetSCLHigh(); SetSDAHigh(); return I2CERR_NO_ERROR; } UINT16 I2CStart(void) { // int i; // UINT8 Data_Bit,ACKSign,TmpDat; // Data_Bit = 8; SetSDAOutput(); SetSCLOutput(); SetSCLHigh(); SetSDAHigh(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSDALow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); return 1; } UINT16 I2CStop(void) { SetSDAOutput(); SetSCLOutput(); SetSCLLow(); SetSDALow(); DelayTime(DELAY_TIME); SetSCLHigh(); DelayTime(DELAY_TIME); SetSDAHigh(); DelayTime(DELAY_TIME); return I2CERR_NO_ERROR; } UINT16 I2CWriteData( char bData ) { UINT8 Data_Bit,ACKSign/*,TmpDat*/; char i; //kal_int8 i; SetSDAOutput(); SetSCLLow(); DelayTime(DELAY_TIME); for(i=7;i>=0;i--) { DelayTime(DELAY_TIME); Data_Bit=(bData>>i)& 0x01; if(Data_Bit) SetSDAHigh(); else SetSDALow(); DelayTime(DELAY_TIME); SetSCLHigh(); DelayTime(DELAY_TIME); SetSCLLow(); } ACKSign=GetACKSign(); return ACKSign; } ///urc_send void writeI2C(char *data2, UINT8 count) //hxd;通用写 { UINT8 i; UINT8 j = 0; char iBuffer; I2COpen(); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); RemoteDelayms(DELAY_ST); //20ms //14 I2CStart(); DelayTime(DELAY_TIME); for(i = 0; i < count; i++) //count=7,初值 { iBuffer = data2[i]; I2CWriteData(iBuffer); DelayTime(DELAY_TIME); } DelayTime(DELAY_TIME); I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); } ///================================================hxd; //hxd;============================================================== void Learn_start2(void) { I2COpen(); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); RemoteDelayms(DELAY_ST); //20ms //14 I2CStart(); DelayTime(DELAY_TIME); I2CWriteData(0x30); DelayTime(DELAY_TIME); I2CWriteData(0x20); DelayTime(DELAY_TIME); I2CWriteData(0x50); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); } //================================== UINT16 I2CReadData(UINT8* pbData) { // UINT8 Data_Bit; UINT8 ACKSign; UINT8 readdata = 0; UINT8 i=8; SetSDAInput(); while (i--) { readdata<<=1; SetSCLHigh(); DelayTime(DELAY_TIME); readdata |= GetDINStatus(); //读不到时可以这样试:readdata |= GetDINStatus()?0x01:0x00; SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); //hxd;加delay } SetSCLLow(); DelayTime(DELAY_TIME); *pbData = readdata; SendACKSign(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); //hxd;1G以上的系统要加,test return I2CERR_NO_ERROR; } //============================= //=================== UINT8 readI2C2(char* readtempbuf) //UINT8 { UINT8 bValue; UINT8 i=0; UINT8 checksum; I2COpen(); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); RemoteDelayms(DELAY_ST); //20ms I2CStart(); DelayTime(DELAY_TIME); //---------------------------------------- //write I2CWriteData(0x30); DelayTime(DELAY_TIME); //address point I2CWriteData(0x62); DelayTime(DELAY_TIME); //--------------------------------------- //read I2CStart(); DelayTime(DELAY_TIME); I2CWriteData(0x31); DelayTime(DELAY_TIME); I2CReadData(&bValue); //wjs;read:FCS(1B) DelayTime(DELAY_TIME); //wjs;1G以上的系统要加 if(bValue != 0x00) { I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); // kal_prompt_trace(MOD_TST, "remote_study_type_error"); return 0; } i = 0; readtempbuf[i] = bValue; // kal_prompt_trace(MOD_TST, "remote_I2C_data[%d] = %d",i,readtempbuf[i]); checksum = 0xc3; for(i = 1; i < 230; i++) //wjs;read:learndata(109B)+120=230 { I2CReadData(&bValue); DelayTime(DELAY_TIME); readtempbuf[i] = bValue; // kal_prompt_trace(MOD_TST, "remote_I2C_data[%d] = %d",i,readtempbuf[i]); checksum += bValue; } I2CReadData(&bValue); //wjs;read:CK(1B) ????? DelayTime(DELAY_TIME); I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); } #include #include #define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode #define FOSC 11059200L //System frequenc #define BAUD 9600 //UART baudrate #define I2CERR_NO_ERROR 0 #define DELAY_TIME 15 //H: 25uS-45uS //第二步:调这个参数和下面的参数,使得每次读写时的SCl时序如参考图所示:每个bite读写的时间是30us左右; #define DELAY_ST 20 //Hb:18mS-25mS //第一个下降沿和第二个下降沿之间的间隔是20ms左右 //#ifndef UINT8 typedef unsigned char UINT8; //#endif //#ifndef UINT16 typedef unsigned int UINT16; //#endif ***it SDA = P0^0; ***it SETBUSY =P0^2; ***it SCL = P0^1; ***it LED = P1^6; idata bit busy_1; unsigned int count_0=0; xdata unsigned char ReceiveBuf[130] = {0}; //接收数据(指令)存放数组 xdata unsigned char ReceiveBuf1[230] = {0}; //接收数据(指令)存放数组 xdata unsigned char ReceiveBuf2[232] = {0}; //接收数据(指令)存放数组 idata unsigned char ReceiveCount = 0; //接收数据(指令)字节数 idata unsigned char ReceiveFlag = 0; //接收数据(指令)标志位 xdata unsigned char check=0; unsigned char data2[10]={0x30,0x00,0x01,0x1c,0xE3,0x00,0xFF,0x20,0x88,0xD7}; void Uart_Isr(); void Serial_Init(void); void time0_init(); void tm0_isr(); void SendData(unsigned char dat); void SendString(unsigned char *s); void Delay10ms(unsigned int c); UINT8 readI2C2(char* readtempbuf); void writeI2C(char* data2, UINT8 count); void Set_SDA_IO(void); void SetSDAInput(void); void SetSDAOutput(void); void SetSDALow(void); void SetSDAHigh(void); UINT8 GetDINStatus(void); void DelayTime( UINT16 lTime ); void SendACKSign(void); UINT16 I2CStart(void); UINT16 I2CWriteData( char bData ); void remote_poweron_init(void); void Set_SCL_IO(void); void SetSCLOutput(void); void SetSCLInput(void); void SetSCLLow(void); void SetSCLHigh(void); void Set_BUSY_IO(void); void SetBUSYInput(void); void SetBUSYOutput(void); void SetBUSYHigh(void); void SetBUSYLow(void); UINT8 GetBUSYStatus(void); UINT8 GetACKSign(void); UINT16 I2CClose(void); UINT16 I2COpen(void); UINT16 I2CStop(void); void Learn_start2(void); void main() { //初始化变量 unsigned char i; //初始化函数 time0_init(); Serial_Init(); remote_poweron_init(); while(1) { if(ReceiveFlag==1) { ReceiveFlag=0; if((ReceiveBuf[0]==0x30)&&(ReceiveBuf[1]==0x20))//第三步1,如果收到的是30 20H开头的数据,进入方法2,学习,处理数据,发回上层或本主控处理发送 { //学习 Learn_start2(); Delay10ms(10); while(BUSY==0) Delay10ms(1); if(BUSY==1) { readI2C2(ReceiveBuf1); //进入学习后,busy由低变高,读数据 Delay10ms(10 ); for(i=0;i<230;i++) { SendData(ReceiveBuf1[i]); } while(1) for(i=0;i<=2;i++) { LED=0; Delay10ms(10); LED=1; Delay10ms(10); } Delay10ms(100); ReceiveBuf2[0]=0x30; ReceiveBuf2[1]=0x03; for(i=1;i<230;i++) { ReceiveBuf2[i+1]=ReceiveBuf1[i]; check=ReceiveBuf1[i]+check; } check=check+0x03+0x30; ReceiveBuf2[231]=(unsigned char)check; //本主控处理,加校验 for(i=0;i<232;i++) { SendData(ReceiveBuf2[i]); } } // while(1) // for(i=0;i<=2;i++) // { // LED=0; // Delay10ms(10); // LED=1; // Delay10ms(10); // } Delay10ms(500); //hxd019芯片规定,连续读写的间隔至少0.8S,这里可以修改 while(1) { writeI2C(ReceiveBuf2, 232); Delay10ms(200); } for(i=0; i<230; i++) { ReceiveBuf1[i] = 0; } for(i=0; i<232; i++) { ReceiveBuf2[i] = 0; } } else { //转发 for(i=0; i SendData(ReceiveBuf[i]); } Delay10ms(100); writeI2C(ReceiveBuf, ReceiveCount); //第三步2,下发数据给hxd019,上层给什么数据就发什么数据,这里可以发测试数据data2: for(i=0; i ReceiveBuf[i] = 0; } ReceiveCount = 0; Delay10ms(100); } } } } void tm0_isr() interrupt 1 //1ms定时器中断函数 { TL0 = T1MS; //reload timer0 low byte TH0 = T1MS >> 8; //reload timer0 high byte) if(ReceiveFlag == 2) { count_0++; if (count_0 >= 10) //1ms * 1000 -> 1s { count_0 = 0; //reset counter ReceiveFlag = 1; } } } void time0_init() { TMOD = 0x01; //set timer0 as mode1 (16-bit) TL0 = T1MS; //initial timer0 low byte TH0 = T1MS >> 8; //initial timer0 high byte TR0 = 1; //timer0 start running ET0 = 1; //enable timer0 interrupt EA = 1; //open global interrupt switch count_0 = 0; //initial counter } void Serial_Init(void) { SCON0 = 0x50; //8-bit variable UART TMOD |= 0x20; //Set Timer1 as 8-bit auto reload mode TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule TR1 = 1; //Timer1 start run ES0 = 1; //Enable UART interrupt REN0 = 1; EA = 1; //Open master interrupt switch } void Uart_Isr() interrupt 4 using 1 { if (RI0) { RI0 = 0; //Clear receive interrupt flag ReceiveFlag = 2; ReceiveBuf[ReceiveCount++] = SBUF0; count_0 = 0; } if (TI0) { TI0 = 0; //Clear transmit interrupt flag busy_1 = 0; //Clear transmit busy flag } } void SendData(unsigned char dat) { while (busy_1 ); //Wait for the completion of the previous data is sent busy_1 = 1; SBUF0 = dat; //Send data to UART buffer } void Delay10ms(unsigned int c) //误差 0us { unsigned char a,b; for(;c>0;c--) for(b=168;b>0;b--) for(a=163;a>0;a--); } //配置三个IO=================================================================================================== void PORT_Init (void) { XBR0 = 0x00; XBR2 = 0x40; P0MDOUT = 0x00; P1MDOUT |= 0x40; P0=0XFF; } //=====================================================上电初始化或待机相关IO设置函数 void remote_poweron_init(void) /// { Set_SDA_IO(); SetSDAHigh(); SetSDAOutput(); Set_SCL_IO(); SetSCLHigh(); SetSCLOutput(); Set_BUSY_IO(); SetBUSYInput(); } void DelayTime( UINT16 lTime ) { UINT16 i; for(i=0;i ; } } void RemoteDelayms(UINT16 data1) { UINT16 i; while(data1--) { for(i=0;i<552;i++){}// 1ms每次循环5000 次 } } //=========== UINT8 GetACKSign(void) { UINT8 ACKSign; UINT16 dataRead=0; SetSDAInput(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); ACKSign = GetDINStatus(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); return ACKSign; } void SendACKSign(void) { SetSDAOutput(); DelayTime(DELAY_TIME); SetSDALow(); ///hxd;后加低SDA_Low,没有验证过 DelayTime(DELAY_TIME); SetSCLHigh(); //第9次的CLK的高 DelayTime(DELAY_TIME); //;1mS SetSCLLow(); //1mS } UINT16 I2COpen(void) { Set_SDA_IO(); Set_SCL_IO(); SetSDAOutput(); SetSCLOutput(); SetSCLHigh(); SetSDAHigh(); return I2CERR_NO_ERROR; } UINT16 I2CClose(void) { SetSDAOutput(); SetSCLOutput(); SetSCLHigh(); SetSDAHigh(); return I2CERR_NO_ERROR; } UINT16 I2CStart(void) { // int i; // UINT8 Data_Bit,ACKSign,TmpDat; // Data_Bit = 8; SetSDAOutput(); SetSCLOutput(); SetSCLHigh(); SetSDAHigh(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSDALow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); return 1; } UINT16 I2CStop(void) { SetSDAOutput(); SetSCLOutput(); SetSCLLow(); SetSDALow(); DelayTime(DELAY_TIME); SetSCLHigh(); DelayTime(DELAY_TIME); SetSDAHigh(); DelayTime(DELAY_TIME); return I2CERR_NO_ERROR; } UINT16 I2CWriteData( char bData ) { UINT8 Data_Bit,ACKSign/*,TmpDat*/; char i; //kal_int8 i; SetSDAOutput(); SetSCLLow(); DelayTime(DELAY_TIME); for(i=7;i>=0;i--) { DelayTime(DELAY_TIME); Data_Bit=(bData>>i)& 0x01; if(Data_Bit) SetSDAHigh(); else SetSDALow(); DelayTime(DELAY_TIME); SetSCLHigh(); DelayTime(DELAY_TIME); SetSCLLow(); } ACKSign=GetACKSign(); return ACKSign; } ///urc_send void writeI2C(char *data2, UINT8 count) //hxd;通用写 { UINT8 i; UINT8 j = 0; char iBuffer; I2COpen(); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); RemoteDelayms(DELAY_ST); //20ms //14 I2CStart(); DelayTime(DELAY_TIME); for(i = 0; i < count; i++) //count=7,初值 { iBuffer = data2[i]; I2CWriteData(iBuffer); DelayTime(DELAY_TIME); } DelayTime(DELAY_TIME); I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); } ///================================================hxd; //hxd;============================================================== void Learn_start2(void) { I2COpen(); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); RemoteDelayms(DELAY_ST); //20ms //14 I2CStart(); DelayTime(DELAY_TIME); I2CWriteData(0x30); DelayTime(DELAY_TIME); I2CWriteData(0x20); DelayTime(DELAY_TIME); I2CWriteData(0x50); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); } //================================== UINT16 I2CReadData(UINT8* pbData) { // UINT8 Data_Bit; UINT8 ACKSign; UINT8 readdata = 0; UINT8 i=8; SetSDAInput(); while (i--) { readdata<<=1; SetSCLHigh(); DelayTime(DELAY_TIME); readdata |= GetDINStatus(); //读不到时可以这样试:readdata |= GetDINStatus()?0x01:0x00; SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); //hxd;加delay } SetSCLLow(); DelayTime(DELAY_TIME); *pbData = readdata; SendACKSign(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); //hxd;1G以上的系统要加,test return I2CERR_NO_ERROR; } //============================= //=================== UINT8 readI2C2(char* readtempbuf) //UINT8 { UINT8 bValue; UINT8 i=0; UINT8 checksum; I2COpen(); DelayTime(DELAY_TIME); SetSCLLow(); DelayTime(DELAY_TIME); DelayTime(DELAY_TIME); SetSCLHigh(); RemoteDelayms(DELAY_ST); //20ms I2CStart(); DelayTime(DELAY_TIME); //---------------------------------------- //write I2CWriteData(0x30); DelayTime(DELAY_TIME); //address point I2CWriteData(0x62); DelayTime(DELAY_TIME); //--------------------------------------- //read I2CStart(); DelayTime(DELAY_TIME); I2CWriteData(0x31); DelayTime(DELAY_TIME); I2CReadData(&bValue); //wjs;read:FCS(1B) DelayTime(DELAY_TIME); //wjs;1G以上的系统要加 if(bValue != 0x00) { I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); // kal_prompt_trace(MOD_TST, "remote_study_type_error"); return 0; } i = 0; readtempbuf[i] = bValue; // kal_prompt_trace(MOD_TST, "remote_I2C_data[%d] = %d",i,readtempbuf[i]); checksum = 0xc3; for(i = 1; i < 230; i++) //wjs;read:learndata(109B)+120=230 { I2CReadData(&bValue); DelayTime(DELAY_TIME); readtempbuf[i] = bValue; // kal_prompt_trace(MOD_TST, "remote_I2C_data[%d] = %d",i,readtempbuf[i]); checksum += bValue; } I2CReadData(&bValue); //wjs;read:CK(1B) ????? DelayTime(DELAY_TIME); I2CStop(); DelayTime(DELAY_TIME); I2CClose(); DelayTime(DELAY_TIME); } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
3032个成员聚集在这个小组
加入小组2928 浏览 1 评论
MSP430FR5994 使用库函数 定时器触发AD问题请教
3600 浏览 2 评论
请问怎么把下面51单片机的代码改成msp430 g2 pocket的代码,还有改下时间变成30秒
2341 浏览 1 评论
4804 浏览 1 评论
2573 浏览 1 评论
1328浏览 3评论
MSP430FR5994 使用库函数 定时器触发AD问题请教
3600浏览 2评论
2928浏览 1评论
1510浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-29 05:45 , Processed in 1.412083 second(s), Total 76, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号