头文件如下:
#ifndef __IIC_H__
#define __IIC_H__
#include "stm8l15x_gpio.h"
#define SCL_OUT_INIT() GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_OD_Low_Fast)
#define SCL_OUT_HIGH() GPIO_WriteBit(GPIOC,GPIO_Pin_1,SET)
#define SCL_OUT_LOW() GPIO_WriteBit(GPIOC,GPIO_Pin_1,RESET)
#define SDA_OUT_INIT() GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_OD_Low_Fast)
#define SDA_OUT_HIGH() GPIO_WriteBit(GPIOC,GPIO_Pin_0,SET)
#define SDA_OUT_LOW() GPIO_WriteBit(GPIOC,GPIO_Pin_0,RESET)
#define SDA_IN_INIT() GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_In_FL_No_IT)
#define SDA_IN_DATA() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)
#define IIC_HIGH 0x80
#define IIC_LOW 0
extern void iic_init();
extern void iic_single_byte_write(unsigned char Daddr, unsigned char addr, unsigned char data);
extern unsigned char iic_single_byte_read(unsigned char Daddr, unsigned char addr);
#endif /* __IIC_H__ */
函数定义如下:
#include "IIC.h"
extern void delay(uint32_t);
//IIC初始化函数
void iic_init(){
//初始化引脚为输出
SCL_OUT_INIT();
SDA_OUT_INIT();
delay(20);
//初始化状态为高电平
SCL_OUT_HIGH();
SDA_OUT_HIGH();
delay(20);
}
//IIC起始信号
void iic_start() {
SCL_OUT_INIT();
SDA_OUT_INIT();
//SDA = 1
SDA_OUT_HIGH();
delay(2);
//SCL = 1
SCL_OUT_HIGH();
delay(2);
//SDA = 0
SDA_OUT_LOW();
delay(2);
//SCL = 0
// SCL_OUT_LOW();
// delay(2);
}
//IIC终止信号
void iic_stop() {
SCL_OUT_INIT();
SDA_OUT_INIT();
//SDA = 0
SDA_OUT_LOW();
delay(2);
//SCL = 1
SCL_OUT_HIGH();
delay(2);
//SDA = 1
SDA_OUT_HIGH();
delay(2);
}
//IIC应答信号
void iic_respons(){
unsigned char i=0;
SCL_OUT_INIT();
// SDA_OUT_INIT();
//SCL = 1
SCL_OUT_HIGH();
delay(2);
//SDA初始化为输入
SDA_IN_INIT();
while((SDA_IN_DATA()==1)&& (i<250))i++;
SDA_OUT_INIT();
//SCL = 0
SCL_OUT_LOW();
delay(2);
}
//void respons() //应答
//{
//uchar i;
//scl = 1;
//delay();
//while((sda==1)&&(i<250))i++;
//scl = 0;
//delay();
//}
//void NoRespons()//不应答
//{ sda=1;
//delay();
//scl=1;
//delay();
//scl=0;
//delay();
//}
void iic_no_respons(){
SCL_OUT_INIT();
SDA_OUT_INIT();
//SDA=1
SDA_OUT_HIGH();
delay(2);
//SCL = 1
SCL_OUT_HIGH();
delay(2);
//SCL = 0
SCL_OUT_LOW();
delay(2);
}
void iic_write_byte(unsigned char data) {
unsigned char i, temp;
SCL_OUT_INIT();
SDA_OUT_INIT();
for (i = 0; i < 8; i++) {
temp = data;
temp = temp & 0x80;
SCL_OUT_LOW();
delay(2);
if(IIC_HIGH == temp) {
SDA_OUT_HIGH();
}
else {
SDA_OUT_LOW();
}
delay(2);
SCL_OUT_HIGH();
delay(2);
data = data<<1;
}
SCL_OUT_LOW();
delay(2);
SDA_OUT_HIGH();
delay(2);
}
unsigned char iic_read_byte() {
unsigned char i, temp=0, data=0;
SCL_OUT_INIT();
SDA_OUT_INIT();
SCL_OUT_LOW();
delay(2);
SDA_OUT_HIGH();
delay(2);
//SDA初始化为输入
SDA_IN_INIT();
delay(2);
for (i = 0; i < 8; i++) {
SCL_OUT_HIGH();
delay(5);
temp = SDA_IN_DATA();
data = (data << 1) | temp;
SCL_OUT_LOW();
delay(5);
}
//SDA重新初始化为输出
SDA_OUT_INIT();
delay(5);
return data;
}
void iic_single_byte_write(unsigned char Daddr, unsigned char addr, unsigned char data){
iic_start();
iic_write_byte(Daddr);
iic_respons();
iic_write_byte(addr);
iic_respons();
iic_write_byte(data);
iic_respons();
iic_stop();
}
unsigned char iic_single_byte_read(unsigned char Daddr, unsigned char addr){
unsigned char data;
iic_start();
iic_write_byte(Daddr);
iic_respons();
iic_write_byte(addr);
iic_respons();
iic_start();
iic_write_byte(Daddr+1);
iic_respons();
data = iic_read_byte();
iic_no_respons();
iic_stop();
return data;
}
当前问题是:
读取数据部分非0xff,且固定,应该可以判定读正常;
写新数据后读出的数据仍为写入前的数据,
波形均正常。麻烦有高手帮忙解决下,谢谢!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
延时函数发来看看
一周热门 更多>