我是靠谱客的博主 孝顺糖豆,最近开发中收集的这篇文章主要介绍用一个数组(buffer)模拟寄存器进行IIC读写,STM32F10X 做从机实现硬件IIC I2C,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
在网上找了半天,大部分人都是用两个数组,一个TxBuffer,一个RxBuffer。为什么没有人用一个数组呢????
本文用一个数组实现模拟寄存器进行IIC读写,STM32F10X 做从机实现硬件IIC。**
编辑器用的Keil5。mian.c就不发出来了就常见的各种初始化。前边常用的IIC配置网上都有,主要是中断做了修改。****
#include "myiic.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "usart.h"
#define I2C_ADDR 0x03 //自己定义一个自己喜欢的
uint8_t data = 0;
uint8_t rx_count = 0;
int c=0;
u8 flag=0;
uint8_t Buffer_Rx_IIC1[256]={0x2b};//接收缓存
uint8_t Buffer_Tx_IIC1[256]={0x3a};//接收缓存
uint8_t Rx_Idx_IIC1=0;//接收计数
uint8_t Tx_Idx_IIC1=0;//发送计数
void I2C1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
I2C_DeInit(I2C1);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;//I2C必须开漏输出,实现线与逻辑
GPIO_Init(GPIOB, &GPIO_InitStructure);
// configure I2C1
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C_ADDR;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress= I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;//错误中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// enable I2C1
I2C_ITConfig(I2C1, I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF, ENABLE);
I2C_Cmd(I2C1, ENABLE);
}
void I2C1_EV_IRQHandler(void)
{
__IO uint32_t SR1Register =0;
__IO uint32_t SR2Register =0;
SR1Register = I2C1->SR1;
SR2Register = I2C1->SR2;
/* I2C1是从机(MSL = 0) */
if((SR2Register &0x0001) != 0x0001)
{
/* 主机已发送地址,地址为被置位·(ADDR = 1: EV1(包括发送和接收)) */
if((SR1Register & 0x0002) == 0x0002)
{
/* 清除标志位 */
SR1Register = 0;
SR2Register = 0;
Rx_Idx_IIC1=0;
Tx_Idx_IIC1=0;
}
/* 接收数据(RXNE = 1: EV2) *///这里是重中之重。
if((SR1Register & 0x0040) == 0x0040)
{ flag+=1;
Buffer_Rx_IIC1[Rx_Idx_IIC1++]= I2C1->DR;
data=Buffer_Rx_IIC1[--Rx_Idx_IIC1]
if (flag>=2)//
{
uint8_t index=Buffer_Rx_IIC1[0];
Buffer_Tx_IIC1[index]=data;
}
SR1Register = 0;
SR2Register = 0;
}
/* 检测到停止条件(STOPF =1: EV4) */
if((SR1Register & 0x0010) == 0x0010)
{ flag=0;
I2C1->CR1 |= 0x0001;
SR1Register = 0;
SR2Register = 0;
Rx_Idx_IIC1=0;
Tx_Idx_IIC1=0;
}
/* 发送数据(TxE = 1: EV3、EV3-1) */
if((SR1Register & 0x0080) == 0x0080)
{ int i;
flag=0;
I2C1->DR = Buffer_Tx_IIC1[data];
SR1Register = 0;
SR2Register = 0;
}
/* 检测到非应答(AF =1: EV3-2) */
if(( SR1Register & 0x0400) == 0x0400)
{
I2C1->SR1 &= 0xFDFF;
SR1Register = 0;
SR2Register = 0;
}
}
}
void I2C1_ER_IRQHandler(void)
{
__IO uint32_t SR1Register =0;
__IO uint32_t SR2Register =0;
SR1Register = I2C1->SR1;
SR2Register = I2C1->SR2;
if(I2C_GetITStatus(I2C1, I2C_IT_SMBALERT)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_PECERR)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_OVR)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_AF)) {
I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
}
else if(I2C_GetITStatus(I2C1, I2C_IT_ARLO)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_BERR)) {
}
I2C1->CR1 |= 0x0001;
SR1Register = 0;
SR2Register = 0;
}
硬件IIC看起来似乎很麻烦,但是按照官方的时序走很简单。
这里贴出来官方手册的从机接收和发送时序。
好了,第一篇博客到此结束,有问题留言哦!!
最后
以上就是孝顺糖豆为你收集整理的用一个数组(buffer)模拟寄存器进行IIC读写,STM32F10X 做从机实现硬件IIC I2C的全部内容,希望文章能够帮你解决用一个数组(buffer)模拟寄存器进行IIC读写,STM32F10X 做从机实现硬件IIC I2C所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复