GD32F103的I2C模块有I2C0和I2C1;本程序使用I2C0功能模块;
I2C0引脚复用和重映射:
当I2C0_REMAP=0时,I2C0引脚复用功能,I2C0_SCL映射到PB6引脚,I2C0_SDA映射到PB7引脚;
当I2C0_REMAP=1时,I2C0引脚重映射,I2C0_SCL映射到PB8引脚,I2C0_SDA映射到PB9引脚;
I2C1引脚只有复用引脚:
I2C1_SCL映射到PB10引脚,I2C1_SDA映射到PB11引脚;
I2C标速模式:最快100kbit/s; 系统复位以后,I2C默认工作在从机模式下; I2C接口: main.c如下:
I2C快速模式:最快400kbit/s;
当clkspeed<=100000位/秒,配置I2C为标速模式;
当100000位/秒
主机和从机模式切换如下:
通过软件设置START位置1,使I2C在总线上发送一个START起始位之后,I2C变为主机模式;
通过软件设置STOP位置1,使I2C在总线上发送一个STOP结束位后,I2C就会自动变为从机模式;
串行数据SDA和串行时钟SCL;
START起始位定义:在SCL为高时,SDA线上出现一个从高到低的电平转换;
STOP结束位定义:在SCL为高时,SDA线上出现一个从低到高的电平转换;
I2C主机:负责产生START起始位和STOP结束位,并且负责产生SCL时钟;#include "24LC256.h"
/*
GD32F103的I2C模块有I2C0和I2C1;本程序使用I2C0功能模块;
I2C0引脚复用和重映射:
当I2C0_REMAP=0时,I2C0引脚复用功能,I2C0_SCL映射到PB6引脚,I2C0_SDA映射到PB7引脚;
当I2C0_REMAP=1时,I2C0引脚重映射,I2C0_SCL映射到PB8引脚,I2C0_SDA映射到PB9引脚;
I2C1引脚只有复用引脚:
I2C1_SCL映射到PB10引脚,I2C1_SDA映射到PB11引脚;
I2C标速模式:最快100kbit/s;
I2C快速模式:最快400kbit/s;
当clkspeed<=100000位/秒,配置I2C为标速模式;
当100000位/秒
#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t,bool
#include "delay.h"
//#include "IWDG.h"
//#include "WWDGT.h"
#include "stdio.h" //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()
#include "string.h" //使能strcpy(),strlen(),memset()
#include "UART3.h"
#include "24LC256.h"
const char CPU_Reset_REG[]="\r\nCPU reset!\r\n";
const char CPU_Is_Running_REG[]="\r\nCPU is running!\r\n";
int main(void)
{
uint8_t d;
uint16_t i;
u8 buf[11];
//NVIC_PRIGROUP_PRE4_SUB0:抢占优先级为4bit(取值为0~15),子优先级为0bit(没有响应优先级)
//NVIC_PRIGROUP_PRE3_SUB1:抢占优先级为3bit(取值为0~7),子优先级为1bit(取值为0~1)
//NVIC_PRIGROUP_PRE2_SUB2:抢占优先级为2bit(取值为0~3),子优先级为2bit(取值为0~3)
//NVIC_PRIGROUP_PRE1_SUB3:抢占优先级为1bit(取值为0~1),子优先级为3bit(取值为0~7)
//NVIC_PRIGROUP_PRE0_SUB4:抢占优先级为0bit(没有抢占优先级),子优先级为3bit(取值为0~15)
nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);//设置系统中断优先级"抢占优先级为4bit,子优先级为0bit"
INTX_ENABLE();//开启所有中断
GD32F103_UART3_Init(115200);
printf("%s",CPU_Reset_REG);//调试串口输出"\r\nCPU reset!\r\n"
delay_init();
EEPROM_PIN_Init();
//I2C0_SCL映射到PB8引脚,I2C0_SDA映射到PB9引脚,配置I2C为快速模式400KHz
strcpy((char*)buf,"9876543210");
EEPROM_Buffer_Data_Write(buf,10,0);
memset(buf,0,sizeof(buf));//清除buf[]
EEPROM_Buffer_Data_Read(buf,10,0);
printf("\r\nbuf1[]=%s",buf);
for(i=0;i<10;i++)
{
d=EEPROM_U8_Data_Read1(i);
printf("\r\nd=%c",d);
}
EEPROM_U8_Data_Write('0',0);
EEPROM_U8_Data_Write('1',1);
EEPROM_U8_Data_Write('2',2);
EEPROM_U8_Data_Write('3',3);
EEPROM_U8_Data_Write('4',4);
EEPROM_U8_Data_Write('5',5);
EEPROM_U8_Data_Write('6',6);
EEPROM_U8_Data_Write('7',7);
EEPROM_U8_Data_Write('8',8);
EEPROM_U8_Data_Write('9',9);
memset(buf,0,sizeof(buf));//清除buf[]
EEPROM_Buffer_Data_Read(buf,10,0);
printf("\r\nbuf2[]=%s",buf);
for(i=0;i<10;i++)
{
d=EEPROM_U8_Data_Read1(i);
printf("\r\nd=%c",d);
}
while(1)
{
}
}