本帖最后由 woshilee 于 2014-11-26 18:53 编辑
我用STM32F103的I2C1作为主接收器,I2C2作为从发送器,程序如下,程序总是会卡在判断ADDR位那里,就是红 {MOD}部分,小弟菜鸟,不知道对I2C的理解哪里不对,就是无法运行,很捉急,谢谢大神帮忙了
#include <stm32f10x.h>
#include <stdio.h>
u8 Getbyte=0;
u8 i=0;
u8 flag1=0,flag2=0,sendflag=0;
/*****************************************************
函数: void Delay(vu32 nCount)
参数: vu32 nCount 延时时间
描述: 延时指定时间
返回: 无
******************************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
/********************USART1配置函数************************/
void USART1_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200; //串口的波特率,例如115200 最高达4.5Mbits/s
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据字长度(8位或9位)
USART_InitStructure.USART_StopBits = USART_StopBits_1; //可配置的停止位-支持1或2个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //双工模式,使能发送和接收
USART_Init(USART1, &USART_InitStructure); // 调用STM32的USART初始化底层函数
USART_Cmd(USART1,ENABLE);
}
void I2C_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
/* I2C configuration */
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0xA1;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
I2C_AcknowledgeConfig(I2C1, ENABLE);
I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0xA2;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Init(I2C2, &I2C_InitStructure);
I2C_Cmd(I2C2, ENABLE);
/*允许1字节1应答模式*/
I2C_AcknowledgeConfig(I2C2, ENABLE);
}
/********************GPIO配置函数************************/
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//定义 GPIO_InitTypeDef类型结构体GPIO_InitStructure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_10|GPIO_Pin_11; //PB.6,PB.7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //复用开漏输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/********************时钟配置函数************************/
void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE); //使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1|RCC_APB1Periph_I2C2, ENABLE); //使能usart1时钟
}
/********************printf函数支持函数************************/
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
return ch;
}
int fgetc(FILE *fp)
{
int ch = 0;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{
}
ch = (int)USART1->DR & 0xFF;
putchar(ch); //回显
return ch;
}
/********************主函数************************/
int main(void)
{
RCC_Config();
GPIO_Config();
USART1_Config();
I2C_Config();
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);// 在BUSY=0时发送起发送起始条件,进入主模式
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));// 发生以后读SR1并写DR,((uint32_t)0x00030001) /* BUSY总线忙, MSL主模式 and SB起始条件已发送 flag */
I2C_Send7bitAddress(I2C1, 0xA2, I2C_Direction_Receiver);//发送地址,最后一位为1写,最后一位为0读
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_ADDR) == RESET);
I2C_SendData(I2C2, 0xff);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_RXNE) == RESET);
Getbyte=I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_AcknowledgeConfig(I2C2, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
printf("
%d
",Getbyte);
while(1);
}
解决了送你十块钱话费,谢了,坐等
此帖出自
小平头技术问答
这个文档中没有程序呀,只是简单的说了一下
一周热门 更多>