c8051f020 帮忙看一下,能不能实现学习,led灯亮
时间:10-02
整理:3721RD
点击:
#include<c8051f020.h>
#include <intrins.h>
#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
sbit SDA = P1^0; //第一步:配置主控和hxd019d芯片连接的三个pin脚,参见如下:配置三个IO
sbit SETBUSY =P1^2;
sbit SCL = P1^1;
sbit 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 Port_Init (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]);
}
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]);
}
}
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<ReceiveCount; i++)
{
SendData(ReceiveBuf[i]);
}
Delay10ms(100);
while(1)
for(i=2;i>=0;i--)
{
LED=0;
Delay10ms(10);
LED=1;
Delay10ms(10);
}
writeI2C(ReceiveBuf, ReceiveCount); //第三步2,下发数据给hxd019,上层给什么数据就发什么数据,这里可以发测试数据data2:
for(i=0; i<ReceiveCount; i++)
{
ReceiveBuf[i] = 0;
}
ReceiveCount = 0;
Delay10ms(100);
// while(1)
// for(i=3;i>=0;i--)
// {
// LED=0;
// Delay10ms(10);
// LED=1;
// Delay10ms(10);
// }
}
}
}
}
void PORT_Init (void)
{
P1MDOUT = 0x40; // Make the LED (P1.6) a push-pull
// output
XBR0 = 0x01; // Enable SMBus
XBR2 = 0x40; // Enable crossbar and weak pull-ups
}
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 Set_SDA_IO(void)
{
// GPIO_ModeSetup(REMOTE_DIN_PIN, 0x00); //设置与SDA通讯的管脚为IO模式,不是AD,中断等其它模式
}
void SetSDAInput(void)
{
// GPIO_InitIO(0,REMOTE_DIN_PIN); ///设置与SDA通讯的管脚为输入模式,关闭下拉
XBR0=0X01;
XBR2=0X40;
P1MDIN =0X00;
}
void SetSDAOutput(void)
{
// GPIO_InitIO(1,REMOTE_DIN_PIN); //设置与SDA通讯的管脚为输出模式
XBR0=0X01;
XBR2=0X40;
P1MDOUT =0X00;
}
void SetSDAHigh(void)
{
// GPIO_WriteIO(1,REMOTE_DIN_PIN); //设置与SDA通讯的管脚为输出1,高电平
SDA=1;
}
void SetSDALow(void)
{
// GPIO_WriteIO(0,REMOTE_DIN_PIN); //设置与SDA通讯的管脚为输出0,低电平
SDA=0;
}
UINT8 GetDINStatus(void)
{
return SDA; //读SDA的状态
}
/*******************************************
SCK_PIN 同上,参考SDA
*******************************************/
void Set_SCL_IO(void)
{
}
void SetSCLOutput(void)
{
XBR0 = 0x01;
XBR2 =0X40;
P1MDOUT =0X00;
}
void SetSCLHigh(void)
{
SCL=1;
}
void SetSCLLow(void)
{
SCL=0;
}
/*******************************************
BUSY_PIN =============同上,参考SDA
*******************************************/
void Set_BUSY_IO(void)
{
}
void SetBUSYInput(void)
{
// GPIO_InitIO(0,REMOTE_DIN_PIN); ///设置与通讯的管脚为输入模式,关闭下拉
XBR0=0X01;
XBR2=0X40;
P1MDIN =0X00;
}
//=====================================================上电初始化或待机相关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<lTime;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 <intrins.h>
#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
sbit SDA = P1^0; //第一步:配置主控和hxd019d芯片连接的三个pin脚,参见如下:配置三个IO
sbit SETBUSY =P1^2;
sbit SCL = P1^1;
sbit 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 Port_Init (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]);
}
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]);
}
}
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<ReceiveCount; i++)
{
SendData(ReceiveBuf[i]);
}
Delay10ms(100);
while(1)
for(i=2;i>=0;i--)
{
LED=0;
Delay10ms(10);
LED=1;
Delay10ms(10);
}
writeI2C(ReceiveBuf, ReceiveCount); //第三步2,下发数据给hxd019,上层给什么数据就发什么数据,这里可以发测试数据data2:
for(i=0; i<ReceiveCount; i++)
{
ReceiveBuf[i] = 0;
}
ReceiveCount = 0;
Delay10ms(100);
// while(1)
// for(i=3;i>=0;i--)
// {
// LED=0;
// Delay10ms(10);
// LED=1;
// Delay10ms(10);
// }
}
}
}
}
void PORT_Init (void)
{
P1MDOUT = 0x40; // Make the LED (P1.6) a push-pull
// output
XBR0 = 0x01; // Enable SMBus
XBR2 = 0x40; // Enable crossbar and weak pull-ups
}
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 Set_SDA_IO(void)
{
// GPIO_ModeSetup(REMOTE_DIN_PIN, 0x00); //设置与SDA通讯的管脚为IO模式,不是AD,中断等其它模式
}
void SetSDAInput(void)
{
// GPIO_InitIO(0,REMOTE_DIN_PIN); ///设置与SDA通讯的管脚为输入模式,关闭下拉
XBR0=0X01;
XBR2=0X40;
P1MDIN =0X00;
}
void SetSDAOutput(void)
{
// GPIO_InitIO(1,REMOTE_DIN_PIN); //设置与SDA通讯的管脚为输出模式
XBR0=0X01;
XBR2=0X40;
P1MDOUT =0X00;
}
void SetSDAHigh(void)
{
// GPIO_WriteIO(1,REMOTE_DIN_PIN); //设置与SDA通讯的管脚为输出1,高电平
SDA=1;
}
void SetSDALow(void)
{
// GPIO_WriteIO(0,REMOTE_DIN_PIN); //设置与SDA通讯的管脚为输出0,低电平
SDA=0;
}
UINT8 GetDINStatus(void)
{
return SDA; //读SDA的状态
}
/*******************************************
SCK_PIN 同上,参考SDA
*******************************************/
void Set_SCL_IO(void)
{
}
void SetSCLOutput(void)
{
XBR0 = 0x01;
XBR2 =0X40;
P1MDOUT =0X00;
}
void SetSCLHigh(void)
{
SCL=1;
}
void SetSCLLow(void)
{
SCL=0;
}
/*******************************************
BUSY_PIN =============同上,参考SDA
*******************************************/
void Set_BUSY_IO(void)
{
}
void SetBUSYInput(void)
{
// GPIO_InitIO(0,REMOTE_DIN_PIN); ///设置与通讯的管脚为输入模式,关闭下拉
XBR0=0X01;
XBR2=0X40;
P1MDIN =0X00;
}
//=====================================================上电初始化或待机相关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<lTime;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);
}