之前调试失败的主要原因找到了,i2c_data_transmit(I2C0,Address); 不能这样传送从机的地址,我是受了软件IIC的影响,以为连续送两次就行;必须用下面的语句传送从机地址,i2c_data_transmit是用来传数据的;
// /* send slave address to I2C bus*/
// i2c_master_addressing(I2C0,I2C1_SLAVE_ADDRESS7,I2C_RECEIVER);
// /* wait until ADDSEND bit is set*/
// while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
// /* clear ADDSEND bit */
// i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
#include "gd32e23x_i2c.h"
#include "gd32e23x.h"
#include "gd32e23x_it.h"
#include
#include
#include
#define TIMEOUT 50
const LEDENCODE[] = {0x03F,0x006,0x05B,0x04F,0x066,0x06D,0x07D,0x007,0x07F,0x06F};
uint8_t i;
void myGPIO_init(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOA);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ,GPIO_PIN_0);//PB6_42 I2C0_SCL GPIO_OTYPE_PP推挽输出
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_0);
}
//主函数处理
int main(void)
{
systick_config();
myGPIO_init();
delay_ms(10);
i2c_init();
while(1)
{
gpio_bit_set(GPIOA,GPIO_PIN_0); //PA0测试脚
delay_ms(25);
gpio_bit_reset(GPIOA,GPIO_PIN_0);
delay_ms(25);
GN1650_Show();
}
}
/********** 数码管显示 **********/
void GN1650_Show(void)
{
static uint16_t value=0;
GN1650_Wrt_RAM(0x68,LEDENCODE[value/1000%10]);//high //0x48 系统指令 0x41//4级亮度 8段显示 显示开
GN1650_Wrt_RAM(0x6A,LEDENCODE[value/100%10]); //| LEDDOT);
GN1650_Wrt_RAM(0x6C,LEDENCODE[value/10%10]);
GN1650_Wrt_RAM(0x6E,LEDENCODE[value%10]); //low
GN1650_Wrt_RAM(0x48,0x01);
value++;
if(value>9999) value=0;
}
void GN1650_Wrt_RAM(uint8_t Address,uint8_t Data)
{
/**** 使用了I2C0(主模式)写功能 ****/
/* wait until I2C bus is idle */
while( i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));//如果I2C0繁忙,空等
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0); // 在I2C0上生成一个起始位
/* wait until SBSEND bit is set */
while( !i2c_flag_get(I2C0, I2C_FLAG_SBSEND)); // 如果起始位还没有发送,空等
i2c_master_addressing(I2C0, Address, I2C_TRANSMITTER); //cmd
while( !i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)); //while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* wait until the RBNE bit is set */
i2c_data_transmit(I2C0,Data); //data
while( !i2c_flag_get(I2C0, I2C_FLAG_TBE));
/* send a stop condition to I2C bus*/
i2c_stop_on_bus(I2C0);
while( I2C_CTL0(I2C0)&0x200);
return; //ok
}
/*!
\brief cofigure the I2C0 interfaces
*/
void i2c_init(void)
{
i2c_deinit(I2C0);
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
/* connect PB6 to I2C0_SCL */
gpio_af_set(GPIOB,GPIO_AF_1,GPIO_PIN_6);
/* connect PB7 to I2C0_SDA */
gpio_af_set(GPIOB,GPIO_AF_1,GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ,GPIO_PIN_6);//PB6_42 I2C0_SCL
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ,GPIO_PIN_7);//PB7_43 I2C0_SDA
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
/* configure I2C0 clock */
i2c_clock_config(I2C0,1000000,I2C_DTCY_2);//可以跑到1MHz
/* configure I2C0 address */
//i2c_mode_addr_config(I2C0,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0);//GN1650每一位都有一个地址,规格书没有写GN1650的地址
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_I2CMODE_ENABLE);
/* enable I2C0 */
i2c_enable(I2C0);
}
不要一直等标志位,等不到就执行下一条语句
#define TIMEOUT 50
const LEDENCODE[] = {0x03F,0x006,0x05B,0x04F,0x066,0x06D,0x07D,0x007,0x07F,0x06F};
uint8_t i;
void myGPIO_init(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOA);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ,GPIO_PIN_0);//PB6_42 I2C0_SCL GPIO_OTYPE_PP推挽输出
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_0);
}
//主函数
int main(void)
{
systick_config();
myGPIO_init();
delay_ms(10);
i2c_init();
while(1)
{
gpio_bit_set(GPIOA,GPIO_PIN_0);
delay_ms(25);
gpio_bit_reset(GPIOA,GPIO_PIN_0);
delay_ms(25);
GN1650_Show();
}
}
/********** 数码管显示 **********/
void GN1650_Show(void)
{
static uint16_t value=0;
GN1650_Wrt_RAM(0x68,LEDENCODE[value/1000%10]);//high //0x48 系统指令 0x41//4级亮度 8段显示 显示开
GN1650_Wrt_RAM(0x6A,LEDENCODE[value/100%10]); //| LEDDOT);
GN1650_Wrt_RAM(0x6C,LEDENCODE[value/10%10]);
GN1650_Wrt_RAM(0x6E,LEDENCODE[value%10]); //low
GN1650_Wrt_RAM(0x48,0x01);
value++;
if(value>9999) value=0;
}
void GN1650_Wrt_RAM(uint8_t Address,uint8_t Data)
{
uint16_t timeout;
/**** 使用了I2C0(主模式)写功能 ****/
/* wait until I2C bus is idle */
timeout=TIMEOUT;
while(timeout-- && i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0); // 在I2C0上生成一个起始位
/* wait until SBSEND bit is set */
timeout=TIMEOUT;
while(timeout-- && !i2c_flag_get(I2C0, I2C_FLAG_SBSEND)); // 如果起始位还没有发送,空等
// /* send slave address to I2C bus*/
// i2c_master_addressing(I2C0,I2C1_SLAVE_ADDRESS7,I2C_RECEIVER);
// /* wait until ADDSEND bit is set*/
// while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
// /* clear ADDSEND bit */
// i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
i2c_master_addressing(I2C0, Address, I2C_TRANSMITTER); //cmd
timeout=TIMEOUT;
while(timeout-- && !i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)); //while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* wait until the RBNE bit is set */
i2c_data_transmit(I2C0,Data); //data
timeout=TIMEOUT;
while(timeout-- && !i2c_flag_get(I2C0, I2C_FLAG_TBE));
/* send a stop condition to I2C bus*/
i2c_stop_on_bus(I2C0);
timeout=TIMEOUT;
while(timeout-- && I2C_CTL0(I2C0)&0x200);
return; //ok
}
/*!
\brief cofigure the I2C0 interfaces
*/
void i2c_init(void)
{
i2c_deinit(I2C0);
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
/* connect PB6 to I2C0_SCL */
gpio_af_set(GPIOB,GPIO_AF_1,GPIO_PIN_6);
/* connect PB7 to I2C0_SDA */
gpio_af_set(GPIOB,GPIO_AF_1,GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ,GPIO_PIN_6);//PB6_42 I2C0_SCL
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ,GPIO_PIN_7);//PB7_43 I2C0_SDA
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
/* configure I2C0 clock */
i2c_clock_config(I2C0,1000000,I2C_DTCY_2);
/* configure I2C0 address */
//i2c_mode_addr_config(I2C0,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0);
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_I2CMODE_ENABLE);
/* enable I2C0 */
i2c_enable(I2C0);
}
超时就不做了,
#define TIMEOUT 50
const LEDENCODE[] = {0x03F,0x006,0x05B,0x04F,0x066,0x06D,0x07D,0x007,0x07F,0x06F};
uint8_t i;
void myGPIO_init(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOA);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ,GPIO_PIN_0);//PB6_42 I2C0_SCL GPIO_OTYPE_PP推挽输出
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_0);
}
//主函数处理
int main(void)
{
systick_config();
myGPIO_init();
delay_ms(10);
i2c_init();
while(1)
{
gpio_bit_set(GPIOA,GPIO_PIN_0);
delay_ms(25);
gpio_bit_reset(GPIOA,GPIO_PIN_0);
delay_ms(25);
GN1650_Show();
}
}
/********** 数码管显示 **********/
void GN1650_Show(void)
{
static uint16_t value=0;
GN1650_Wrt_RAM(0x68,LEDENCODE[value/1000%10]);//high //0x48 系统指令 0x41//4级亮度 8段显示 显示开
GN1650_Wrt_RAM(0x6A,LEDENCODE[value/100%10]); //| LEDDOT);
GN1650_Wrt_RAM(0x6C,LEDENCODE[value/10%10]);
GN1650_Wrt_RAM(0x6E,LEDENCODE[value%10]); //low
GN1650_Wrt_RAM(0x48,0x01);
value++;
if(value>9999) value=0;
}
void GN1650_Wrt_RAM(uint8_t Address,uint8_t Data)
{
uint16_t timeout;
/**** 使用了I2C0(主模式)写功能 ****/
/* wait until I2C bus is idle */
timeout=TIMEOUT;
while(timeout-- && i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));//如果I2C0繁忙,空等
if(timeout==0) goto end_1;
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0); // 在I2C0上生成一个起始位
/* wait until SBSEND bit is set */
timeout=TIMEOUT;
while(timeout-- && !i2c_flag_get(I2C0, I2C_FLAG_SBSEND)); // 如果起始位还没有发送,空等
if(timeout==0) goto end_1;
i2c_master_addressing(I2C0, Address, I2C_TRANSMITTER); //cmd
timeout=TIMEOUT;
while(timeout-- && !i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)); //while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* wait until the RBNE bit is set */
i2c_data_transmit(I2C0,Data); //data
timeout=TIMEOUT;
while(timeout-- && !i2c_flag_get(I2C0, I2C_FLAG_TBE));
if(timeout==0) goto end_1;
/* send a stop condition to I2C bus*/
i2c_stop_on_bus(I2C0);
timeout=TIMEOUT;
while(timeout-- && I2C_CTL0(I2C0)&0x200);
if(timeout==0) goto end_1;
return; //ok
end_1: //wrong
i2c_init(); //IIc wrong lets iic init
}
/*!
\brief cofigure the I2C0 interfaces
*/
void i2c_init(void)
{
i2c_deinit(I2C0);
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
/* connect PB6 to I2C0_SCL */
gpio_af_set(GPIOB,GPIO_AF_1,GPIO_PIN_6);
/* connect PB7 to I2C0_SDA */
gpio_af_set(GPIOB,GPIO_AF_1,GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ,GPIO_PIN_6);//PB6_42 I2C0_SCL
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ,GPIO_PIN_7);//PB7_43 I2C0_SDA
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
/* configure I2C0 clock */
i2c_clock_config(I2C0,1000000,I2C_DTCY_2);
/* configure I2C0 address */
//i2c_mode_addr_config(I2C0,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0);
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_I2CMODE_ENABLE);
/* enable I2C0 */
i2c_enable(I2C0);
/*** 清寄存器标志,IIC死掉后重启,提高抗干扰能力 ***/
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
i2c_flag_clear(I2C0, I2C_FLAG_SMBTO);
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
}
只点亮一位数码管;0xFF是显示的数据;
GN1650_Wrt_RAM(0x68,0xFF);//high //0x48 系统指令 0x41//4级亮度 8段显示 显示开
GN1650_Wrt_RAM(0x48,0x01);
绿色就是测试电平,紫色D7是时钟,黄色D4是数据,绿色点是START,橙色点是STOP;放大后;
GN1650的规格书,SCL高电平时传数据;SCL第九位ACK,SDA低电平;0110 1000转成16进制=68H;1111 1111转成16进制=FFH;0x48和0x01是同样的分析方法;这样可以检验数据传输正确与否;逻辑分析仪的波形图和GN1650的时序图是否对应的上;
除了CLK、GND,找其他通道连接数码管的SCL和SDA,GND接数码管的负极;
测试脚只输出高低电平;
GN1650
花了半天搞定,所以不能只写代码,不看芯片规格书;也不能只看规格书,不用代码验证;
最后,连续插拔数码管几十次,硬件IIC确实容易死掉(芯片没有死,因为我的测试脚有高低电平输出),只有重新上电运行;
以上就是使用GN1650的过程。
如有不清楚的可以看看《调试篇》的三篇IIC合集