|
请大佬帮我看一下 ,我的开始、结束、发送、接受,响应和非响应函数是否正确 ,我用示波器去读取波形的时候,我是能够看到看到相应的时钟线和数据线信号,但是我无法获取我操控芯片的状态。
void SDA_Out()//set the P1 port become a both_way input 202494
{
P1M1=00;
P1M0=00;
}
void SDA_In(){
P1M1=00;
P1M0=00;
}
/*
function: communication start function.
*/
void IIC_Start()
{
SDA_Out();//design input of port model,,,design the port ,make the port to two_way imput
SCL_8238=1;//improve the SCL,prepare for start,,,
SDA_8238=1;
delay_us(2);//IC have delay,because have C and R
SDA_8238=0;// spark the IIC communication
delay_us(2);
SCL_8238=0;//clock in high to read data_,this is prepare.
delay_us(2);
}
/*
function : communication end
*/
void IIC_Stop(){
SDA_Out();// control master transmit data_ to salve
SCL_8238=0;
delay_us(2);
SDA_8238=0;
delay_us(2);
SCL_8238=1;
SDA_8238=1;//end the communication.
delay_us(2);
}
/*
function:afer transmit data_ from master to slave ,slave will retrun a ack.
*/
void IIC_Ack()
{
SCL_8238=0;//ocuppy the bus
SDA_Out();//data_ from slave to master ,choose the data_ transmit way.
delay_us(2);
SDA_8238=0;
SCL_8238=1;//keep the data_ no change.
delay_us(2);
SCL_8238=0;//ack respose is end
// SDA_High();//CPU release the data_ line
}
/*
function:simulation the master no ack
*/
void IIC_Nack()
{
SCL_8238=0;//SCl
SDA_Out();//SDA become a input
SDA_8238=1;//don't respose the ack
delay_us(2);
SCL_8238=1;//keep the data_
delay_us(2);//
SCL_8238=0;//end the response
SDA_8238=1;//CPU release the data_ bus
}
char IIC_Wait_Ack()
{
short int ErrorTime=0;
SDA_In();//choose data_line as slave to master;
SDA_8238=1;delay_us(2);
SCL_8238=1;delay_us(2);
while(P16)//SDA_Read feedback data_ , use SDA save.202494 change 8P16
{
ErrorTime++;
if(ErrorTime>250)
{
IIC_Stop();
return 1;
}
}
SCL_8238=0;//pull the clock line ,wait data_ change
delay_us(1);
return 0;
}
void IIC_Send(char byte)
{
int t;
P_SW2 = 0x80;
SDA_Out();//202494
SCL_8238=0;
usart_init();
for(t=0;t<8;t++)
{
if(byte&0X80)
{
SDA_8238=1;
}
else
{
SDA_8238=0;
}
byte<<=1;
delay_us(2);
SCL_8238=1;//keep the data_,don't change the data_
delay_us(2);
SCL_8238=0;//to prepare for next data_ transmit
}
}
unsigned char IIC_Read(){
int i;
unsigned char receive=0;
SDA_Out();
for(i=0;i<8;i++){
SCL_8238=0;
delay_us(2);
SCL_8238=1;//202494
receive=(receive<<1)|P16;//202494 P16 SDA_Read is data_ bus zero or one. return data_ receive and it must be a constance.
delay_us(2);
}
return receive;
}
main()
{
unsigned char Counter,Register_Data;
P_SW2 = 0x80;
EA = 1;
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
usart_init();
IIC_Start();//start
IIC_Send(0x20); // transmit address + write 0x60
IIC_Ack();
IIC_Send(0x00); //transmit high address of storage
IIC_Ack();
IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack();
IIC_Send(0x0); //transmit high address of storage 2024828
IIC_Ack();
IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmit high address of storage 2024828
IIC_Ack(); IIC_Send(0xF7); //transmit high address of storage 2024828
// IIC_Ack();
// IIC_Send(0x20);//send the data_ of address
// IIC_Ack();//gain the ack.
// IIC_Send(0x20);
// IIC_Ack();
// IIC_Send(0x10);
IIC_Nack();
IIC_Stop(); //stop
delay_us(10);
// Delay10us(100);
//the second data_ is valid
IIC_Start(); //start
IIC_Send(0x21); //device address +write
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Counter=IIC_Read();
IIC_Ack();
Counter=IIC_Read();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
Register_Data = IIC_Read();
IIC_Ack();
IIC_Nack();
IIC_Stop();
usart_send_char(11);
usart_send_char(0x11);
usart_send_char(Counter);
usart_send_char(Counter>>8);
delay_us(10);
}
|
-
|