gd32f103 模拟i2c
代码如下:
void at24cxx_init(void)
{
Soft_I2C_Init();
}
void Soft_I2C_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
IIC_SCL(1);
IIC_SDA(1);
}
void iic_delay_us(uint32_t nus)
{
uint8_t i= 0;
while(nus--)
{
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
}
}
//产生IIC起始信号
void iic_start(void)
{
SDA_OUT();
IIC_SDA(1);
IIC_SCL(1);
IIC_SDA(0);
IIC_SCL(0);
}
//产生IIC停止信号
void iic_stop(void)
{
SDA_OUT();//sda线输出
IIC_SDA(0);//STOP:when CLK is high DATA change form low to high
IIC_SCL(1);
IIC_SDA(1);//发送I2C总线结束信号
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
uint8_t iic_wait_ack(void)
{
uint8_t ucErrTime=0;
SDA_IN(); //SDA设置为输入
IIC_SDA(1);iic_delay_us(1);
IIC_SCL(1);iic_delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
iic_stop();
return 1;
}
}
IIC_SCL(0);//时钟输出0
return 0;
}
//产生ACK应答
void iic_ack(void)
{
IIC_SCL(0);
SDA_OUT();
IIC_SDA(0);
IIC_SCL(1);
IIC_SCL(0);
}
//不产生ACK应答
void iic_nack(void)
{
IIC_SCL(0);
SDA_OUT();
IIC_SDA(1);
IIC_SCL(1);
IIC_SCL(0);
}
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答
void iic_send_byte(uint8_t txd)
{
uint8_t i = 0;
uint8_t byte = txd;
uint8_t sda = 0;
SDA_OUT();
for (i = 0; i < 8; i++)
{
IIC_SCL(0);
if (byte & 0x80)
{
IIC_SDA(1);;
}
else
{
IIC_SDA(0);;
}
IIC_SCL(1);
byte <<= 1;
}
IIC_SCL(0);
SDA_IN();
IIC_SCL(1);
sda=gpio_input_bit_get(GPIOA, GPIO_PIN_4);
if (sda)
{
IIC_SCL(0);
SDA_OUT();
}
IIC_SCL(0);
SDA_OUT();
IIC_SDA(1) ;
}
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
uint8_t iic_read_byte(uint8_t ack)
{
unsigned char i,receive=0;
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++ )
{
IIC_SCL(0);
iic_delay_us(2);
IIC_SCL(1);
receive<<=1;
if(READ_SDA)receive++;
iic_delay_us(1);
}
if (!ack)
iic_nack();//发送nACK
else
iic_ack(); //发送ACK
return receive;
}
void DRV_WriteByte(uint8_t slaveAddr, uint8_t byteAddr,uint8_t data)
{
iic_start();
iic_send_byte(slaveAddr);
iic_send_byte(byteAddr);
iic_send_byte(data);
iic_stop();
}
gd32f103 TLC59116.c驱动
代码如下:
#include "TLC59116.h"
#include "i2c.h"
void TLC59116_Init(uint8_t ic)
{
DRV_WriteByte(ic,TLC59116_MODE1, 0x01);
DRV_WriteByte(ic,TLC59116_MODE2, 0x00);
DRV_WriteByte(ic,TLC59116_LEDOUT0, 0xff);
DRV_WriteByte(ic,TLC59116_LEDOUT1, 0xff);
DRV_WriteByte(ic,TLC59116_LEDOUT2, 0xff);
DRV_WriteByte(ic,TLC59116_LEDOUT3, 0xff);
DRV_WriteByte(ic,TLC59116_PWM0, 0x00);
DRV_WriteByte(ic,TLC59116_PWM1, 0x00);
DRV_WriteByte(ic,TLC59116_PWM2, 0x00);
DRV_WriteByte(ic,TLC59116_PWM3, 0x00);
DRV_WriteByte(ic,TLC59116_PWM4, 0x00);
DRV_WriteByte(ic,TLC59116_PWM5, 0x00);
DRV_WriteByte(ic,TLC59116_PWM6, 0x00);
DRV_WriteByte(ic,TLC59116_PWM7, 0x00);
DRV_WriteByte(ic,TLC59116_PWM8, 0x00);
DRV_WriteByte(ic,TLC59116_PWM9, 0x00);
DRV_WriteByte(ic,TLC59116_PWM10, 0x00);
DRV_WriteByte(ic,TLC59116_PWM11, 0x00);
DRV_WriteByte(ic,TLC59116_PWM12, 0x00);
DRV_WriteByte(ic,TLC59116_PWM13, 0x00);
DRV_WriteByte(ic,TLC59116_PWM14, 0x00);
DRV_WriteByte(ic,TLC59116_PWM15, 0x00);
}
gd32f103 TLC59116.h驱动
代码如下:
#ifndef __TLC59116_H__
#define __TLC59116_H__
#include "gd32f10x.h"
#include <stdio.h>
#define TLC59116_BASEADDR 0b1100000
#define TLC59116_ALLCALL 0b1101000
#define TLC59116_SUB1 0b1101001
#define TLC59116_SUB2 0b1101010
#define TLC59116_SUB3 0b1101100
#define TLC59116_RESET 0b1101011
#define TLC59116_MODE1 0x00
#define TLC59116_MODE2 0x01
#define TLC59116_PWM0 0x02
#define TLC59116_PWM1 0x03
#define TLC59116_PWM2 0x04
#define TLC59116_PWM3 0x05
#define TLC59116_PWM4 0x06
#define TLC59116_PWM5 0x07
#define TLC59116_PWM6 0x08
#define TLC59116_PWM7 0x09
#define TLC59116_PWM8 0x0A
#define TLC59116_PWM9 0x0B
#define TLC59116_PWM10 0x0C
#define TLC59116_PWM11 0x0D
#define TLC59116_PWM12 0x0E
#define TLC59116_PWM13 0x0F
#define TLC59116_PWM14 0x10
#define TLC59116_PWM15 0x11
#define TLC59116_GRPPWM 0x12
#define TLC59116_GRPFREQ 0x13
#define TLC59116_LEDOUT0 0x14
#define TLC59116_LEDOUT1 0x15
#define TLC59116_LEDOUT2 0x16
#define TLC59116_LEDOUT3 0x17
#define TLC59116_SUBADR1 0x18
#define TLC59116_SUBADR2 0x19
#define TLC59116_SUBADR3 0x1A
#define TLC59116_ALLCALLADR 0x1B
#define TLC59116_IREF 0x1C
#define TLC59116_EFLAG1 0x1D
#define TLC59116_EFLAG2 0x1E
#define TLC59116_1 0xc0 //1100 0000
#define TLC59116_2 0xc2 //1100 0010
#define TLC59116_3 0xc4 //1100 0100
void TLC59116_Init(uint8_t ic);
#endif
版权声明:本文为CSDN博主「samtyo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/samtyo/article/details/122303282
gd32f103 模拟i2c
代码如下:
void at24cxx_init(void)
{
Soft_I2C_Init();
}
void Soft_I2C_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
IIC_SCL(1);
IIC_SDA(1);
}
void iic_delay_us(uint32_t nus)
{
uint8_t i= 0;
while(nus--)
{
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
}
}
//产生IIC起始信号
void iic_start(void)
{
SDA_OUT();
IIC_SDA(1);
IIC_SCL(1);
IIC_SDA(0);
IIC_SCL(0);
}
//产生IIC停止信号
void iic_stop(void)
{
SDA_OUT();//sda线输出
IIC_SDA(0);//STOP:when CLK is high DATA change form low to high
IIC_SCL(1);
IIC_SDA(1);//发送I2C总线结束信号
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
uint8_t iic_wait_ack(void)
{
uint8_t ucErrTime=0;
SDA_IN(); //SDA设置为输入
IIC_SDA(1);iic_delay_us(1);
IIC_SCL(1);iic_delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
iic_stop();
return 1;
}
}
IIC_SCL(0);//时钟输出0
return 0;
}
//产生ACK应答
void iic_ack(void)
{
IIC_SCL(0);
SDA_OUT();
IIC_SDA(0);
IIC_SCL(1);
IIC_SCL(0);
}
//不产生ACK应答
void iic_nack(void)
{
IIC_SCL(0);
SDA_OUT();
IIC_SDA(1);
IIC_SCL(1);
IIC_SCL(0);
}
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答
void iic_send_byte(uint8_t txd)
{
uint8_t i = 0;
uint8_t byte = txd;
uint8_t sda = 0;
SDA_OUT();
for (i = 0; i < 8; i++)
{
IIC_SCL(0);
if (byte & 0x80)
{
IIC_SDA(1);;
}
else
{
IIC_SDA(0);;
}
IIC_SCL(1);
byte <<= 1;
}
IIC_SCL(0);
SDA_IN();
IIC_SCL(1);
sda=gpio_input_bit_get(GPIOA, GPIO_PIN_4);
if (sda)
{
IIC_SCL(0);
SDA_OUT();
}
IIC_SCL(0);
SDA_OUT();
IIC_SDA(1) ;
}
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
uint8_t iic_read_byte(uint8_t ack)
{
unsigned char i,receive=0;
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++ )
{
IIC_SCL(0);
iic_delay_us(2);
IIC_SCL(1);
receive<<=1;
if(READ_SDA)receive++;
iic_delay_us(1);
}
if (!ack)
iic_nack();//发送nACK
else
iic_ack(); //发送ACK
return receive;
}
void DRV_WriteByte(uint8_t slaveAddr, uint8_t byteAddr,uint8_t data)
{
iic_start();
iic_send_byte(slaveAddr);
iic_send_byte(byteAddr);
iic_send_byte(data);
iic_stop();
}
gd32f103 TLC59116.c驱动
代码如下:
#include "TLC59116.h"
#include "i2c.h"
void TLC59116_Init(uint8_t ic)
{
DRV_WriteByte(ic,TLC59116_MODE1, 0x01);
DRV_WriteByte(ic,TLC59116_MODE2, 0x00);
DRV_WriteByte(ic,TLC59116_LEDOUT0, 0xff);
DRV_WriteByte(ic,TLC59116_LEDOUT1, 0xff);
DRV_WriteByte(ic,TLC59116_LEDOUT2, 0xff);
DRV_WriteByte(ic,TLC59116_LEDOUT3, 0xff);
DRV_WriteByte(ic,TLC59116_PWM0, 0x00);
DRV_WriteByte(ic,TLC59116_PWM1, 0x00);
DRV_WriteByte(ic,TLC59116_PWM2, 0x00);
DRV_WriteByte(ic,TLC59116_PWM3, 0x00);
DRV_WriteByte(ic,TLC59116_PWM4, 0x00);
DRV_WriteByte(ic,TLC59116_PWM5, 0x00);
DRV_WriteByte(ic,TLC59116_PWM6, 0x00);
DRV_WriteByte(ic,TLC59116_PWM7, 0x00);
DRV_WriteByte(ic,TLC59116_PWM8, 0x00);
DRV_WriteByte(ic,TLC59116_PWM9, 0x00);
DRV_WriteByte(ic,TLC59116_PWM10, 0x00);
DRV_WriteByte(ic,TLC59116_PWM11, 0x00);
DRV_WriteByte(ic,TLC59116_PWM12, 0x00);
DRV_WriteByte(ic,TLC59116_PWM13, 0x00);
DRV_WriteByte(ic,TLC59116_PWM14, 0x00);
DRV_WriteByte(ic,TLC59116_PWM15, 0x00);
}
gd32f103 TLC59116.h驱动
代码如下:
#ifndef __TLC59116_H__
#define __TLC59116_H__
#include "gd32f10x.h"
#include <stdio.h>
#define TLC59116_BASEADDR 0b1100000
#define TLC59116_ALLCALL 0b1101000
#define TLC59116_SUB1 0b1101001
#define TLC59116_SUB2 0b1101010
#define TLC59116_SUB3 0b1101100
#define TLC59116_RESET 0b1101011
#define TLC59116_MODE1 0x00
#define TLC59116_MODE2 0x01
#define TLC59116_PWM0 0x02
#define TLC59116_PWM1 0x03
#define TLC59116_PWM2 0x04
#define TLC59116_PWM3 0x05
#define TLC59116_PWM4 0x06
#define TLC59116_PWM5 0x07
#define TLC59116_PWM6 0x08
#define TLC59116_PWM7 0x09
#define TLC59116_PWM8 0x0A
#define TLC59116_PWM9 0x0B
#define TLC59116_PWM10 0x0C
#define TLC59116_PWM11 0x0D
#define TLC59116_PWM12 0x0E
#define TLC59116_PWM13 0x0F
#define TLC59116_PWM14 0x10
#define TLC59116_PWM15 0x11
#define TLC59116_GRPPWM 0x12
#define TLC59116_GRPFREQ 0x13
#define TLC59116_LEDOUT0 0x14
#define TLC59116_LEDOUT1 0x15
#define TLC59116_LEDOUT2 0x16
#define TLC59116_LEDOUT3 0x17
#define TLC59116_SUBADR1 0x18
#define TLC59116_SUBADR2 0x19
#define TLC59116_SUBADR3 0x1A
#define TLC59116_ALLCALLADR 0x1B
#define TLC59116_IREF 0x1C
#define TLC59116_EFLAG1 0x1D
#define TLC59116_EFLAG2 0x1E
#define TLC59116_1 0xc0 //1100 0000
#define TLC59116_2 0xc2 //1100 0010
#define TLC59116_3 0xc4 //1100 0100
void TLC59116_Init(uint8_t ic);
#endif
版权声明:本文为CSDN博主「samtyo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/samtyo/article/details/122303282
暂无评论