HR7P193单片机IIC例程代码
时间: 2023-11-17 16:04:21 浏览: 56
以下是HR7P193单片机IIC例程代码:
```c
#include "HR7P193.h"
#include "iic.h"
void IIC_Init()
{
SDA = 1;
SCL = 1;
}
void IIC_Start()
{
SDA = 1;
SCL = 1;
wait();
SDA = 0;
wait();
SCL = 0;
}
void IIC_Stop()
{
SDA = 0;
SCL = 1;
wait();
SDA = 1;
wait();
}
void IIC_WriteByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(dat & 0x80)
SDA = 1;
else
SDA = 0;
dat <<= 1;
wait();
SCL = 1;
wait();
SCL = 0;
}
SDA = 1;
wait();
SCL = 1;
wait();
SCL = 0;
}
unsigned char IIC_ReadByte(unsigned char ack)
{
unsigned char i, dat = 0;
SDA = 1;
for(i=0;i<8;i++)
{
wait();
SCL = 1;
dat <<= 1;
if(SDA)
dat |= 0x01;
wait();
SCL = 0;
}
if(ack)
SDA = 0;
else
SDA = 1;
wait();
SCL = 1;
wait();
SCL = 0;
SDA = 1;
return dat;
}
```
注意,在使用这个例程代码之前,需要先定义 SDA 和 SCL 的引脚号码。
阅读全文