搞了 三天了还没搞出来。。。求大神帮忙啊
#include"
STM32f10x.h"
#include"stm32f10x_rcc.h"
#include"stm32f10x_gpio.h"
#include"stm32f10x_
tim.h"
#include"delay.h"
#include"stm32f10x_i2c.h"
void time()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清中断标志位
TIM_TimeBaseStructure.TIM_Period = 2000;//装初值
TIM_TimeBaseStructure.TIM_Prescaler = 3599;//设置分频系数
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//设置时钟分频系数
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//设置定时器计数模式
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE );//使能定时器2 中断
/* Enables the TIM2 counter */
TIM_Cmd(TIM2, ENABLE);//使能定时器外设
}
void GPIOf()
{
GPIO_DeInit(GPIOF);
/* Configure all the GPIOA in Input Floating mode */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
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;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void rcc()
{
RCC_DeInit();
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//使能GPIOF的时钟
/* Enable BKP and TIM2 clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //使能定时器2 的时钟
//NVIC_enableIRQn();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
}
void i2c()
{
I2C_InitTypeDef I2C_InitStructure;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 =0x01;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress =I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 40000;
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
}
void NVIC_c()
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置中断优先级
NVIC_InitStruct.NVIC_IRQChannel=TIM2_IRQn;//设置中断优先级通道
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);//初始化中断
}
void i2()
{
uint16_t ReceivedData;
/* 没有被复位 代表现在总线正忙 出现复位 总线空闲,执行下面的语句 */
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)!=RESET);
/* 产生一个起始信号 */
I2C_GenerateSTART(I2C1, ENABLE);
/* 检查主模式是否被选择 EV5是否被置位 */
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
/* 发送从器件地址 */
I2C_Send7bitAddress(I2C1, 0xa0,I2C_Direction_Transmitter);
/* 判断从器件地址是否匹配 EV1是否被置位 */
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)!=SUCCESS);
I2C_SendData(I2C1, 0x00);
/* 等待从应答 */
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);
I2C_SendData(I2C1, 0x5d);
/* 等待从应答 */
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);
/* 发送停止信号 */
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_GenerateSTART(I2C1, ENABLE);
/* 检查主模式是否被选择 EV5是否被置位 */
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS);
/* 发送从器件地址 */
I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter);
/* 判断从器件地址是否匹配 EV1是否被置位 */
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)!=SUCCESS);
I2C_SendData(I2C1, 0x00);
/* 等待从应答 */
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);
I2C_SendData(I2C1, 0x5d);
/* 等待从应答 */
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);
/* 发送停止信号 */
I2C_GenerateSTOP(I2C1, ENABLE);
}
int main()
{
rcc();
time();
// NVIC_c();
GPIOf();
i2c();
GPIO_SetBits(GPIOF, GPIO_Pin_6);
while(1)
{
delay(1000);
i2();
}
}
void TIM2_IRQHandler()
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
if(GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_6)==1)
{
GPIO_ResetBits(GPIOF, GPIO_Pin_6); //不能用两个 if 因为 当满足前一个if时
}
else//输出寄存器的值已经被改变了
{
GPIO_SetBits(GPIOF, GPIO_Pin_6);//也就满足了后面if的条件,所以 电平被改变后的瞬间又被改变回来
}//所以 表现出来 人眼看到的情况就是 没有改变电平
}
https://blog.csdn.net/changshengxiao/article/details/88069376
一周热门 更多>