test
- #include "stm32f4xx.h"
- //key gpio a 0 e 2 3 4
- //led gpio f 9 10 e 13 14
- //beep gpio f 8
- //位带操作
- //寄存器位带别名 = 0x42000000 + (寄存器的地址-0x40000000)*32 + 引脚编号*4
- #define PAin(n) *(volatile uint32_t *)(0x42000000+(((uint32_t)&GPIOA->IDR - 0x40000000)<<5) + (n<<2))
- #define PEin(n) *(volatile uint32_t *)(0x42000000+(((uint32_t)&GPIOE->IDR - 0x40000000)<<5) + (n<<2))
- #define PFout(n) *(volatile uint32_t *)(0x42000000+(((uint32_t)&GPIOF->ODR - 0x40000000)<<5) + (n<<2))
- #define PFin(n) *(volatile uint32_t *)(0x42000000+(((uint32_t)&GPIOF->IDR - 0x40000000)<<5) + (n<<2))
- #define PEout(n) *(volatile uint32_t *)(0x42000000+((uint32_t)&GPIOE->ODR - 0x40000000)*32+n*4)
- #define LED0 PFout(9)
- #define LED1 PFout(10)
- #define LED2 PEout(13)
- #define LED3 PEout(14)
- //定义需要配置的硬件结构体
- static GPIO_InitTypeDef GPIO_InitStruct;
- static TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- static NVIC_InitTypeDef NVIC_InitStruct;
- //初始化LED
- void init_led_beep()
- {
- //1、使能AHB1硬件时钟
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOE, ENABLE);
- //2、硬件配置
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //端口模式配置为输出
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //输出配置为推挽输出
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10 | GPIO_Pin_8; //配置引脚编号为9号引脚
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //快速50Mhz输出速度
- GPIO_Init(GPIOF, &GPIO_InitStruct);
-
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
- GPIO_Init(GPIOE, &GPIO_InitStruct);
- //3、使能硬件工作
- //GPIO_SetBits(GPIOF, GPIO_Pin_9|GPIO_Pin_10);//设置GPIOF9引脚为高电平,灯灭
- PFout(8) = 0;
- PFout(9) = 1;
- PFout(10) = 1;
- PEout(13) = 1;
- PEout(14) = 1;
- }
- void TIm1_init(void)
- {
- //使能TIM1的硬件时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- //TIM1的硬件配置
- //配置定时器1分频值、计数值等等
- TIM_TimeBaseStructure.TIM_Period = (10000/2)-1; //计数值 168000000/16800 = 10000hz/2,决定定时时间1/2秒
- TIM_TimeBaseStructure.TIM_Prescaler = 16800-1; //预分频值 16800-1 + 1 = 16800
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数的方法
- // TIM_TimeBaseStructure.TIM_ClockDivision = 0; //在F407是不支持
-
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- TIM_ClearFlag(TIM1,TIM_FLAG_Update); //必须先清除配置时候产生的更新标志
- //配置定时器1中断的触发方式:时间更新
- TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
-
- //开启TIM1的NVIC中断
- NVIC_InitStruct.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn ; //定时器1的请求通道
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x02; //响应优先级
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能该通道中断,失能
- NVIC_Init(&NVIC_InitStruct);
-
- //使能定时器3工作
- TIM_Cmd(TIM1, ENABLE);
- }
- void TIM1_UP_TIM10_IRQHandler(void)
- {
- //判断TIM3是否有中断请求
- if(TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)
- {
- LED1 ^= 1;
-
- //清空标志位,告诉CPU,已经完成当前中断处理,可以响应新的中断请求
- TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
- }
- }
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟
- init_led_beep();
- TIm1_init();
- while(1)
- {}
- return 0;
- }
|