#include "stm32f10x.h" // Device header
#include "stdio.h"
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
USART_InitTypeDef USART_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure; //按键中断初始化
NVIC_InitTypeDef NVIC_InitStructure;
uint16_t flag; //直接定义个标志位
uint16_t avoid_untilflag; //避免持续发送
uint16_t key_on_flag; //按键按下标志位
void Rcc_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE); //初始化GPIO时钟和串口引脚
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //初始化定时器时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //初始化串口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启按键中断时钟
}
void LED_init() //初始化灯的GPIO 直接打开例程中GPIO选择main.c 复制过来都可以用了
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_0 | GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_All); //先关掉所有灯
}
void Key_init() //直接历程中EXTI拉过来就是按键1
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM4_init() //初始化定时器4 直接找例程中TIMEBASE 复制粘贴过来
{
NVIC_InitTypeDef NVIC_InitStructure; //NVIC结构体初始化 直接找例程中TIMEBASE 复制粘贴过来
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1*(10000-1); //直接计数20000次 自己可以算等于2s 不会自己慢慢琢磨
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //36MHZ直接分频一次100us
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //配置中断模式
TIM_Cmd(TIM4,ENABLE);
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_init() //直接找例程中USART 复制粘贴过来 最需要改引脚即可 这个等于串口PA9,PA10固定不会变
{
// GPIO_InitTypeDef GPIO_InitStructure; //可有可无 最上边已经定义全局GPIO
/* Configure USARTy TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTy RX as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
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);
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC); //可以先清除一次防止发错
}
int fputc(int ch/*c*/, FILE *f/*stream*/) //直接从USART例程中的printf中找出来拉过来
{
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
int main()
{
Rcc_Init();
LED_init();
TIM4_init();
USART_init();
Key_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
GPIO_ResetBits(GPIOB,GPIO_Pin_5); //红灯亮
GPIO_ResetBits(GPIOB,GPIO_Pin_0); //绿灯亮
printf("黄灯亮\r\n"); //黄+绿=黄
while(1)
{
if(key_on_flag==1)
{
if(avoid_untilflag==0&&flag==0) //只让执行一次 防止串口卡死
{
avoid_untilflag=1;
GPIO_SetBits(GPIOB,GPIO_Pin_All); //全灭
GPIO_ResetBits(GPIOB,GPIO_Pin_5); //红灯亮
printf("红灯亮\r\n");
}
if(avoid_untilflag==1&&flag==1) //只让执行一次 防止串口卡死
{
avoid_untilflag=2;
GPIO_SetBits(GPIOB,GPIO_Pin_All); //全灭
GPIO_ResetBits(GPIOB,GPIO_Pin_0); //绿灯亮
printf("绿灯亮\r\n");
}
if(avoid_untilflag==2&&flag==2) //只让执行一次 防止串口卡死
{
avoid_untilflag=0;
GPIO_SetBits(GPIOB,GPIO_Pin_All); //全灭
GPIO_ResetBits(GPIOB,GPIO_Pin_1); //蓝灯亮
printf("蓝灯亮\r\n");
}
}
else
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5); //红灯亮
GPIO_ResetBits(GPIOB,GPIO_Pin_0); //绿灯亮
}
}
}
void EXTI0_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
{
if(++key_on_flag==2)
key_on_flag=0;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
void TIM4_IRQHandler() //中断处理函数 例程中也有
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET)
{
if(++flag==3)
flag=0;
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
}
}