keil5篮球计时器
时间: 2023-11-26 18:46:46 浏览: 83
篮球计时器
基于STM32的篮球比赛计时计分器的设计需要使用Keil5进行编程。下面是一个基于中断按键的篮球计时器的代码示例:
```c
#include "stm32f10x.h"
#include "lcd.h"
#define KEY1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)
#define KEY2 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)
#define KEY3 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)
uint16_t time = 600; // 比赛时间,单位为秒
uint16_t attack_time = 24; // 进攻时间,单位为秒
uint8_t pause_flag = 0; // 暂停标志位,0表示未暂停,1表示已暂停
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
if(!pause_flag)
{
time--;
attack_time--;
}
}
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
if(!pause_flag)
{
pause_flag = 1;
TIM_Cmd(TIM2, DISABLE);
}
else
{
pause_flag = 0;
TIM_Cmd(TIM2, ENABLE);
}
}
}
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
time = 600;
attack_time = 24;
pause_flag = 0;
TIM_Cmd(TIM2, ENABLE);
}
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line2);
attack_time = 24;
}
}
int main(void)
{
LCD_Init();
LCD_Clear(WHITE);
LCD_ShowString(0, 0, "Time:");
LCD_ShowString(0, 16, "Attack Time:");
LCD_ShowNum(48, 0, time / 60, 2, 16);
LCD_ShowNum(72, 0, time % 60, 2, 16);
LCD_ShowNum(96, 16, attack_time, 2, 16);
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_Init(&NVIC_InitStructure);
while(1)
{
LCD_ShowNum(48, 0, time / 60, 2, 16);
LCD_ShowNum(72, 0, time % 60, 2, 16);
LCD_ShowNum(96, 16, attack_time, 2, 16);
if(KEY1)
{
while(KEY1);
if(!pause_flag)
{
pause_flag = 1;
TIM_Cmd(TIM2, DISABLE);
}
else
{
pause_flag = 0;
TIM_Cmd(TIM2, ENABLE);
}
}
if(KEY2)
{
while(KEY2);
time = 600;
attack_time = 24;
pause_flag = 0;
TIM_Cmd(TIM2, ENABLE);
}
if(KEY3)
{
while(KEY3);
attack_time = 24;
}
}
}
```
该代码使用了定时器TIM2和外部中断EXTI0、EXTI1、EXTI2。其中,TIM2用于计时,EXTI0用于暂停/继续,EXTI1用于复位,EXTI2用于重置进攻时间。在主函数中,通过轮询按键状态来实现按键功能。同时,LCD显示比赛时间和进攻时间。
阅读全文