这是ucosii操作系统设计,3人抢答器,使用国信长天CT117E开发板,单片机是stm32f103rbt6,使用外设有LCD屏幕,数码管,LED灯,按键以及蜂鸣器
单片机源程序如下:
- #include "led.h"
- #include "lcd.h"
- #include "key.h"
- #include "exti_a.h"
- #include "sys.h"
- #include "includes.h"
- #include "stm32f10x.h"
- #include "seg.h"
- #include "buzz.h"
- #define TASK_STK_SIZE 512 //任务堆栈长度
- OS_STK TaskStartStk[512]; //定义任务堆栈区//--------------------------------------------------------------------------------------------
- RCC_ClocksTypeDef rcc_clocks; //定义一个全局使用的芯片时钟频率结构变量
- OS_STK Task_LED1_Stk[TASK_STK_SIZE]; //定义任务堆栈区
- OS_STK Task_LED2_Stk[TASK_STK_SIZE]; //定义任务堆栈区
- OS_STK Task_LED3_Stk[TASK_STK_SIZE]; //定义任务堆栈区
- OS_STK Task_LED4_Stk[TASK_STK_SIZE]; //定义任务堆栈区
- OS_STK Task_COUNT_Stk[TASK_STK_SIZE]; //定义任务堆栈区
- OS_STK Task_DIS_Stk[TASK_STK_SIZE]; //定义任务堆栈区
- INT8U err; //错误类型变量
- //--------------------------------------------------------------------------------------------
- OS_EVENT *KEY1_Down; //声明KEY1键按下信号量
- OS_EVENT *KEY2_Down; //声明KEY2键按下信号量
- OS_EVENT *KEY3_Down; //声明KEY3键按下信号量
- OS_EVENT *KEY4_Down; //声明KEY4键按下信号量
- //--------------------------------------------------------------------------------------------
- void TaskStart (void *pdata);
- void Task_KEY1 (void *pdata); //声明任务
- void Task_KEY2 (void *pdata); //声明任务
- void Task_KEY3 (void *pdata); //声明任务
- void Task_KEY4 (void *pdata); //声明任务
- void Task_COUNT (void *pdata); //声明任务
- void Task_DIS (void *pdata); //声明任务
- u8 flag=0;
- u8 play1=0;
- u8 play2=0;
- u8 play3=0;
- u8 cnt=30;
- u8 wflag=0;
- void SysTick_Init (void)
- {
- RCC_GetClocksFreq(&rcc_clocks); //读取系统时钟频率
-
- //设置嘀嗒节拍中断周期为rcc_clocks.HCLK_Frequency/OS_TICKS_PER_SEC(1mS)
- //设置嘀嗒节拍中断优先级为最低抢占优先级
- SysTick_Config(rcc_clocks.HCLK_Frequency / OS_TICKS_PER_SEC);
- }
- //--------------------------------------------------------------------------------------------
- int main(void)
- {
- OSInit( ); //初始化uCOS_II
-
- //创建起始任务TaskStart(优先级0)
- OSTaskCreate(TaskStart, (void *) 0, &TaskStartStk[512 - 1], 0);
-
- OSStart( ); //启动多任务管理
- }
- void TaskStart (void *pdata)
- {
- pdata = pdata;
-
- /*开始:CPU工作参数及外设工作状态初始化*/
- //设置优先级分组:抢占优先级3位(8级)和响应优先级1位(2级)
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
- //设置向量表的位置和偏移:在FLASH中偏移为0
- NVIC_SetVectorTable(NVIC_VectTab_FLASH,0);
-
- SysTick_Init(); //设置uC/OS的时钟
- STM3210B_LCD_Init();
- LCD_SetTextColor(Black);
- LCD_SetBackColor(White);
- LCD_Clear(White);
- LCD_DisplayStringLine(Line2,"Xuhaipeng");
- LCD_DisplayStringLine(Line3,"scorecard");
- LCD_DisplayStringLine(Line4,"play1:00");
- LCD_DisplayStringLine(Line5,"play2:00");
- LCD_DisplayStringLine(Line6,"play3:00");
- OSTimeDlyHMSM(0, 0, 0, 200); //等待0.5秒
- LED_Init(); //LED输出初始化
- LED_Control(LEDALL,0);
- Key_Init(); //按键输入初始化
- EXTI_InitInit(); //外部中断初始化
- STM3210B_SEG_Init();
- SEG_DisplayValue(0,0,0);
- /*结束:CPU工作参数及外设工作状态初始化*/
-
- /*开始:创建工作任务*/
- OSTaskCreate(Task_KEY1, (void *)0, &Task_LED1_Stk[TASK_STK_SIZE - 1], 1);
- OSTaskCreate(Task_KEY2, (void *)0, &Task_LED2_Stk[TASK_STK_SIZE - 1], 2);
- OSTaskCreate(Task_KEY3, (void *)0, &Task_LED3_Stk[TASK_STK_SIZE - 1], 3);
- OSTaskCreate(Task_KEY4, (void *)0, &Task_LED4_Stk[TASK_STK_SIZE - 1], 4);
- OSTaskCreate(Task_COUNT, (void *)0, &Task_COUNT_Stk[TASK_STK_SIZE - 1],5);
- OSTaskCreate(Task_DIS, (void *)0, &Task_DIS_Stk[TASK_STK_SIZE - 1], 6);
- /*结束:创建工作任务*/
-
- /*开始:创建信号量*/
- KEY1_Down = OSSemCreate(0); //创建KEY1按下信号量
- KEY2_Down = OSSemCreate(0); //创建KEY2按下信号量
- KEY3_Down = OSSemCreate(0); //创建KEY3按下信号量
- KEY4_Down = OSSemCreate(0); //创建KEY4按下信号量
- /*结束:创建信号量*/
-
- OSTaskDel(OS_PRIO_SELF);
- }
- //--------------------------------------------------------------------------------------------
- //KEY1处理任务
- void Task_KEY1 (void *pdata)
- {
- pdata = pdata;
-
- for (;;)
- {
- OSSemPend(KEY1_Down, 0, &err); //等待键按下消息
-
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- if (!GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN))
- { //如果KYE输入仍为低则确认键按下
- LED_Control(LED1,1);//led亮
- BuzzOn();
- if(flag==1)
- {
- flag=0;
- if(cnt>20)play1=play1+2;
- else play1=play1+1;
- }
- if(play1>=10) wflag=1;
- OSTimeDlyHMSM(0, 0, 0, 500); //等待0.5秒
- LED_Control(LEDALL,0);
- BuzzOff();
- }
- while (!GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN)) //等待键松开
- {
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- };
- }
- }
- //--------------------------------------------------------------------------------------------
- //KEY2处理任务
- void Task_KEY2 (void *pdata)
- {
- pdata = pdata;
-
- for (;;)
- {
- OSSemPend(KEY2_Down, 0, &err); //等待键按下消息
-
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- if (!GPIO_ReadInputDataBit(KEY2_PORT, KEY2_PIN))
- { //如果KYE输入仍为低则确认键按下
- LED_Control(LED2,1);//led亮
- BuzzOn();
- if(flag==1)
- {
- flag=0;
- if(cnt>20)play2=play2+2;
- else play2=play2+1;
- }
- if(play2>=10) wflag=2;
- OSTimeDlyHMSM(0, 0, 0, 500); //等待0.5秒
- LED_Control(LEDALL,0);
- BuzzOff();
- }
- while (!GPIO_ReadInputDataBit(KEY2_PORT, KEY2_PIN)) //等待键松开
- {
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- };
- }
- }
- //--------------------------------------------------------------------------------------------
- //KEY3处理任务
- void Task_KEY3 (void *pdata)
- {
- pdata = pdata;
-
- for (;;)
- {
- OSSemPend(KEY3_Down, 0, &err); //等待键按下消息
-
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- if (!GPIO_ReadInputDataBit(KEY3_PORT, KEY3_PIN))
- { //如果KYE输入仍为低则确认键按下
- LED_Control(LED3,1);//led亮
- BuzzOn();
- if(flag==1)
- {
- flag=0;
- if(cnt>20)play3=play3+2;
- else play3=play3+1;
- }
- if(play3>=10) wflag=3;
- OSTimeDlyHMSM(0, 0, 0, 500); //等待0.5秒
- LED_Control(LEDALL,0);
- BuzzOff();
- }
- while (!GPIO_ReadInputDataBit(KEY3_PORT ,KEY3_PIN)) //等待键松开
- {
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- };
- }
- }
- //--------------------------------------------------------------------------------------------
- //KEY4处理任务
- void Task_KEY4(void *pdata)
- {
- pdata = pdata;
-
- for (;;)
- {
- OSSemPend(KEY4_Down, 0, &err); //等待键按下消息
-
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- if (!GPIO_ReadInputDataBit(KEY4_PORT, KEY4_PIN))
- { //如果KYE输入仍为低则确认键按下
- LED_Control(LED4,1);//led亮
- flag=1;cnt=30;
- OSTimeDlyHMSM(0, 0, 0, 500); //等待0.5秒
- LED_Control(LEDALL,0);
- }
- while (!GPIO_ReadInputDataBit(KEY4_PORT ,KEY4_PIN)) //等待键松开
- {
- OSTimeDlyHMSM(0, 0, 0, 20); //等待20mS
- };
- }
- }
- void Task_COUNT(void *pdata)
- {
- for(;;)
- {
- if(flag==1) cnt-- ;
- OSTimeDlyHMSM(0, 0,1,0);
- if(cnt==0) flag=0;
- SEG_DisplayValue(0,cnt/10,cnt%10);
- }
- }
- void Task_DIS(void *pdata)
- {
- u8 str1[20];
- u8 str2[20];
- for(;;)
- {
- snprintf((char*)str1, sizeof(str1), "play1:%02d", play1);
- snprintf((char*)str2, sizeof(str2), "play2:%02d", play2);
- LCD_DisplayStringLine(Line4,str1);
- LCD_DisplayStringLine(Line5,str2);
- if(wflag==1){
- flag=0;
- cnt=0;
- LCD_DisplayStringLine(Line7,"play1 win");
- }else if(wflag==2){
- flag=0;
- cnt=0;
- LCD_DisplayStringLine(Line7,"play2 win");
- }else if(wflag==3){
- flag=0;
- cnt=0;
- LCD_DisplayStringLine(Line7,"play3 win");
- }
- }
- }
复制代码
所有资料51hei提供下载:
qiangdaqi.7z
(236.33 KB, 下载次数: 31)
|