stm32f103c8t6最小系统PA6和PA7初始化为软件模拟IIC代码
时间: 2023-08-30 15:05:17 浏览: 110
以下是将stm32f103c8t6最小系统中的PA6和PA7初始化为软件模拟IIC的代码:
```c
#include "stm32f10x.h"
#define SDA_H GPIOA->BSRR = GPIO_Pin_7
#define SDA_L GPIOA->BRR = GPIO_Pin_7
#define SCL_H GPIOA->BSRR = GPIO_Pin_6
#define SCL_L GPIOA->BRR = GPIO_Pin_6
#define SDA_READ GPIOA->IDR & GPIO_Pin_7
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SDA_H;
SCL_H;
}
void IIC_Start(void)
{
SDA_H;
SCL_H;
SDA_L;
SCL_L;
}
void IIC_Stop(void)
{
SDA_L;
SCL_H;
SDA_H;
}
void IIC_Ack(void)
{
SDA_L;
SCL_H;
SCL_L;
SDA_H;
}
void IIC_NAck(void)
{
SDA_H;
SCL_H;
SCL_L;
}
uint8_t IIC_WaitAck(void)
{
uint8_t ucErrTime = 0;
SDA_H;
SCL_H;
while (SDA_READ)
{
ucErrTime++;
if (ucErrTime > 250)
{
IIC_Stop();
return 1;
}
}
SCL_L;
return 0;
}
void IIC_SendByte(uint8_t txd)
{
uint8_t t;
SDA_OUT();
SDA_L;
for (t = 0; t < 8; t++)
{
if ((txd & 0x80) >> 7)
SDA_H;
else
SDA_L;
txd <<= 1;
SCL_H;
SCL_L;
}
}
uint8_t IIC_ReadByte(uint8_t ack)
{
uint8_t i, receive = 0;
SDA_IN();
for (i = 0; i < 8; i++)
{
SCL_H;
receive <<= 1;
if (SDA_READ)
receive++;
SCL_L;
}
if (ack)
IIC_Ack();
else
IIC_NAck();
return receive;
}
```
这个代码包含了软件模拟IIC的初始化函数,开始和停止函数,发送和接收字节的函数。
阅读全文