我想请问 我使用STC8H1K08,的串口模拟IIC,我这个代码有问题吗,为啥无法获取数据
请大佬帮我看一下 ,我的开始、结束、发送、接受,响应和非响应函数是否正确 ,我用示波器去读取波形的时候,我是能够看到看到相应的时钟线和数据线信号,但是我无法获取我操控芯片的状态。voidSDA_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 mastertransmit 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;//ocuppythe 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_Readfeedback 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 P16SDA_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); //transmithigh address of storage
IIC_Ack();
IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack();
IIC_Send(0x0); //transmithigh address of storage 2024828
IIC_Ack();
IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xFF); //transmithigh address of storage 2024828
IIC_Ack(); IIC_Send(0xF7); //transmithigh 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);
}
看AI8051U实验箱例程,IO模拟I2C控制AIC23B
sbit P_AIC23_SCL = P5^1; //2024-7-21
sbit P_AIC23_SDA = P5^0;
#define I2S_MASTER_TRANSMITTER //接收DAC, Uncomment the line below if you will use the I2S peripheral as a Master
// #define I2S_SLAVE_RECEIVER //发送ADC,Uncomment the line below if you will use the I2S peripheral as a Slave
/****************************/
void AIC23_Delay(void)
{
u16 dly;
dly = 10; //取1对应SCL H为0.5us,L为1.5us,读写仍正常.为了可靠,取8
while(--dly) ;
}
/****************************/
void AIC23_Start(void) //start the I2C, SDA High-to-low when SCL is high
{
P_AIC23_SDA = 1; //SDA = 1;
AIC23_Delay();
P_AIC23_SCL = 1; //SCL = 1;
AIC23_Delay();
AIC23_Delay();
AIC23_Delay();
AIC23_Delay();
P_AIC23_SDA = 0; //SDA = 0;
AIC23_Delay();
AIC23_Delay();
AIC23_Delay();
P_AIC23_SCL = 0; //SCL = 0;
AIC23_Delay();
}
void AIC23_Stop(void) //STOP the I2C, SDA Low-to-high when SCL is high
{
P_AIC23_SCL = 0; //SCL = 0;
AIC23_Delay();
P_AIC23_SDA = 0; //SDA = 0;
AIC23_Delay();
P_AIC23_SCL = 1; //SCL = 1;
AIC23_Delay();
AIC23_Delay();
AIC23_Delay();
P_AIC23_SDA = 1; //SDA = 1;
AIC23_Delay();
}
u8 AIC23_Check_ACK(void) //Check ACK, If status=0: right, if status=1:error
{
u8 status;
P_AIC23_SDA = 1; //SDA = 1;
AIC23_Delay();
P_AIC23_SCL = 1; //SCL = 1;
AIC23_Delay();
status = 0;
if(P_AIC23_SDA) status = 1;
P_AIC23_SCL = 0; //SCL = 0;
AIC23_Delay();
return status;
}
/****************************/
void AIC23_WriteAbyte(u8 dat) //write a byte to I2C
{
u8 i;
for(i=0; i<8; i++)
{
if(dat & 0x80) P_AIC23_SDA = 1; //SDA = 1;
else P_AIC23_SDA = 0; //SDA = 0;
AIC23_Delay();
P_AIC23_SCL = 1; //SCL = 1;
dat <<= 1;
AIC23_Delay();
P_AIC23_SCL = 0; //SCL = 0;
AIC23_Delay();
}
}
DebugLab 发表于 2024-9-7 15:53
看AI8051U实验箱例程,IO模拟I2C控制AIC23B
sbit P_AIC23_SCL = P5^1; //2024-7-21
您好请问这个AIC23B实验箱 例程在哪里可以看到
becomeAGeneral 发表于 2024-9-9 11:09
您好请问这个AIC23B实验箱 例程在哪里可以看到
https://www.stcaimcu.com/forum.p ... TUzNDg4MXwxfDE1MjU= 主机写命令/数据要用wait_ack来等待从机回应吧? DebugLab 发表于 2024-9-9 11:26
https://www.stcaimcu.com/forum.p ... TUzNDg4MXwxfDE1MjU=
没有 你说的程序,请问可以给一个有的吗
becomeAGeneral 发表于 2024-10-11 14:07
没有 你说的程序,请问可以给一个有的吗
Lkck8210 发表于 2024-9-9 17:27
主机写命令/数据要用wait_ack来等待从机回应吧?
您好我想请问一下这个等待回应是不是在一个时钟内去读取数据线上的状态 然后进行判断就好了 DebugLab 发表于 2024-10-11 15:36
谢谢 我想请问一下他这个代码是再什么频率下面延时多少秒
void AIC23_Delay(void)
{
u16 dly;
dly = 10; //取1对应SCL H为0.5us,L为1.5us,读写仍正常.为了可靠,取8
while(--dly) ;
}
页:
[1]