“#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "beep.h"
#include "key.h"
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "timer.h"
#include "sram.h"
#include "string.h"
#include "malloc.h"
#include "math.h"
//#include "exti.h"
#include "semphr.h"
// 128 x 64
#define SCREEN_WIDTH 256
#define SCREEN_HEIGHT 128
#define MAX_SNAKE_LENGTH 128
typedef enum {
DIR_UP,
DIR_DOWN,
DIR_LEFT,
DIR_RIGHT
} Direction;
typedef struct {
int x;
int y;
} Point;
// total task
#define START_TASK_PRIO 1
//Mem size
#define START_STK_SIZE 1024
//total task handler
TaskHandle_t StartTask_Handler;
//total task function
void start_task(void *pvParameters);
//KEY task
#define KEY_TASK_PRIO 2
//Mem size
#define KEY_STK_SIZE 256
//handler
TaskHandle_t KEYTask_Handler;
//key task function
void key_task(void *pvParameters);
//LED0 task
#define LED0_TASK_PRIO 3
//Mem size
#define LED0_STK_SIZE 128
//handler
TaskHandle_t LED0Task_Handler;
//led0 task function
void led0_task(void *pvParameters);
//LED1 task
#define LED1_TASK_PRIO 3
//Mem size
#define LED1_STK_SIZE 128
//handler
TaskHandle_t LED1Task_Handler;
//led1 task function
void led1_task(void *pvParameters);
//BEEP task
#define BEEP_TASK_PRIO 3
//Mem size
#define BEEP_STK_SIZE 128
//handler
TaskHandle_t BEEPTask_Handler;
//beep task function
void beep_task(void *pvParameters);
//LCD task
#define LCD_TASK_PRIO 3
//Mem size
#define LCD_STK_SIZE 128
//handler
TaskHandle_t LCDTask_Handler;
//lcd task function
void lcd_task(void *pvParameters);
//Generate Snake Game task
//task function
void GenerateSnakeGame(void *pvParameters);
//GenerateSnakeGame task
//v Control Task
void vControlTask(void *pvParameters);
//software timer control task
#define TIMERCONTROL_TASK_PRIO 2
#define TIMERCONTROL_STK_SIZE 256
TaskHandle_t TimerControlTask_Handler;
void timercontrol_task(void *pvParameters);
int lcd_discolor[14]={WHITE, BLACK, BLUE, BRED,
GRED, GBLUE, RED, MAGENTA,
GREEN, CYAN, YELLOW,BROWN,
BRRED, GRAY };
////////////////////////////////////////////////////////
TimerHandle_t AutoReloadTimer_Handle; //ÖÜÆÚ¶¨Ê±Æ÷¾ä±ú
TimerHandle_t AutoReloadTimer1_Handle; //ÖÜÆÚ¶¨Ê±Æ÷¾ä±ú1
TimerHandle_t OneShotTimer_Handle; //µ¥´Î¶¨Ê±Æ÷¾ä±ú
void AutoReloadCallback(TimerHandle_t xTimer); //ÖÜÆÚ¶¨Ê±Æ÷»Øµ÷º¯Êý
void AutoReloadCallback1(TimerHandle_t xTimer); //ÖÜÆÚ¶¨Ê±Æ÷»Øµ÷º¯Êý1
void OneShotCallback(TimerHandle_t xTimer); //µ¥´Î¶¨Ê±Æ÷»Øµ÷º¯Êý
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é4
delay_init(168); //³õʼ»¯ÑÓʱº¯Êý
uart_init(115200); //³õʼ»¯´®¿Ú
LED_Init(); //³õʼ»¯LED¶Ë¿Ú
BEEP_Init();
KEY_Init();
LCD_Init(); //³õʼ»¯LCD
TIM3_Int_Init(5000-1,8400-1);//timer3 reset
TIM2_Int_Init(10000-1,8400-1);//timer2 reset
//creat totol task
xTaskCreate((TaskFunction_t )start_task, //tatol task function
(const char* )"start_task", //tatol task name
(uint16_t )START_STK_SIZE, //Mem size
(void* )NULL, //
(UBaseType_t )START_TASK_PRIO, //total task prio
(TaskHandle_t* )&StartTask_Handler); //total task handler
vTaskStartScheduler(); //running total task
}
//total task function
void start_task(void *pvParameters)
{
taskENTER_CRITICAL(); //mem
// //create LED0 task
// xTaskCreate((TaskFunction_t )led0_task,
// (const char* )"led0_task",
// (uint16_t )LED0_STK_SIZE,
// (void* )NULL,
// (UBaseType_t )LED0_TASK_PRIO,
// (TaskHandle_t* )&LED0Task_Handler);
// //create beep task
// xTaskCreate((TaskFunction_t )beep_task,
// (const char* )"beep_task",
// (uint16_t )BEEP_STK_SIZE,
// (void* )NULL,
// (UBaseType_t )BEEP_TASK_PRIO,
// (TaskHandle_t* )&BEEPTask_Handler);
// //create LED1 task
// xTaskCreate((TaskFunction_t )led1_task,
// (const char* )"led1_task",
// (uint16_t )LED1_STK_SIZE,
// (void* )NULL,
// (UBaseType_t )LED1_TASK_PRIO,
// (TaskHandle_t* )&LED1Task_Handler);
// //create LCD task
// xTaskCreate((TaskFunction_t )lcd_task,
// (const char* )"lcd_task",
// (uint16_t )LCD_STK_SIZE,
// (void* )NULL,
// (UBaseType_t )LCD_TASK_PRIO,
// (TaskHandle_t* )&LCDTask_Handler);
// //USART
// xTaskCreate((TaskFunction_t )float_task, //name of function needle
// (const char* )"float_task", //name of function
// (uint16_t )FLOAT_STK_SIZE, //size of memory
// (void* )NULL, //object
// (UBaseType_t )FLOAT_TASK_PRIO, //prio
// (TaskHandle_t* )&FLOATTask_Handler); //handler
//software timer control task
// xTaskCreate((TaskFunction_t )timercontrol_task,
// (const char* )"timercontrol_task",
// (uint16_t )TIMERCONTROL_STK_SIZE,
// (void* )NULL,
// (UBaseType_t )TIMERCONTROL_TASK_PRIO,
// (TaskHandle_t* )&TimerControlTask_Handler);
// //KEY
// xTaskCreate((TaskFunction_t )key_task, //name of function needle
// (const char* )"key_task", //name of function
// (uint16_t )KEY_STK_SIZE, //size of memory
// (void* )NULL, //object
// (UBaseType_t )KEY_TASK_PRIO, //prio
// (TaskHandle_t* )&KEYTask_Handler); //handler
//Auto Timer setup
AutoReloadTimer_Handle=xTimerCreate((const char* )"AutoReloadTimer",
(TickType_t )1000,
(UBaseType_t )pdTRUE,
(void* )1,
(TimerCallbackFunction_t)AutoReloadCallback); //
//Auto Timer1 setup
AutoReloadTimer1_Handle=xTimerCreate((const char* )"AutoReloadTimer1",
(TickType_t )2000,
(UBaseType_t )pdTRUE,
(void* )1,
(TimerCallbackFunction_t)AutoReloadCallback1);
//One Timer setup
OneShotTimer_Handle=xTimerCreate((const char* )"OneShotTimer",
(TickType_t )2000,
(UBaseType_t )pdFALSE,
(void* )2,
(TimerCallbackFunction_t)OneShotCallback); //
xTaskCreate(GenerateSnakeGame, "Snake", configMINIMAL_STACK_SIZE * 2, NULL, 1, NULL);
xTaskCreate(vControlTask, "Control", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskDelete(StartTask_Handler); //
taskEXIT_CRITICAL(); //
}
//LED0 task function
void led0_task(void *pvParameters)
{
while(1)
{
LED0=!LED0;
vTaskDelay(1000);
}
}
//LED1 task function
void led1_task(void *pvParameters)
{
while(1)
{
LED1=!LED1;
vTaskDelay(2000);
}
}
//BEEP task function
void beep_task(void *pvParameters)
{
while(1)
{
BEEP=!BEEP;
vTaskDelay(10000);
}
}
//LCD task function
void lcd_task(void *pvParameters)
{
u8 i=0;
POINT_COLOR = RED;
while(1)
{
for(;i<50;LCD_Draw_Circle(100,80,i++));
}
}
//USART task function
void float_task(void *pvParameters)
{
static float float_num=0.0000;
while(1)
{
if(float_num<=10.0)
{
float_num+=0.0001;
printf("float_num is: %.4f\r\n",float_num);
vTaskDelay(300);
}
else
{
float_num=0.0000;
vTaskDelay(300);
}
}
}
//KEY task function
void key_task(void *pvParameters)
{
while(1)
{
// u8 key=0;
// key=KEY_Scan(0); //get key value
// switch(key)
// {
// case 4 /*WKUP_PRES*/: //4 is WKUP_PRES KEY is touched
// vTaskSuspend(LED0Task_Handler);
// vTaskSuspend(LED1Task_Handler);
// break;
// case 1/*KEY0_PRES*/: //4 is KEY0_PRES KEY is touched
// vTaskSuspend(BEEPTask_Handler);
// break;
// case 2/*KEY1_PRES*/: //4 is KEY1_PRES KEY is touched
// vTaskSuspend(FLOATTask_Handler);
// break;
// case 3/*KEY2_PRES*/: //4 is KEY2_PRES KEY is touched
// vTaskResume(BEEPTask_Handler);
// vTaskResume(FLOATTask_Handler);
// vTaskResume(LED0Task_Handler);
// vTaskResume(LED1Task_Handler);
// break;
// }
vTaskDelay(20);
}
}
//TimerControl task
void timercontrol_task(void *pvParameters)
{
u8 key;
while(1)
{
if((AutoReloadTimer_Handle!=NULL)&&(OneShotTimer_Handle!=NULL))
{
key = KEY_Scan(0);
switch(key)
{
case WKUP_PRES: //
xTimerStart(AutoReloadTimer_Handle,0);
printf("Start AutoBackTIMER!\r\n");
break;
case KEY0_PRES:
xTimerStart(OneShotTimer_Handle,0);
printf("Start OneTIMER!\r\n");
break;
case KEY1_PRES:
xTimerStop(AutoReloadTimer_Handle,0);
xTimerStop(OneShotTimer_Handle,0);
printf("Stop AutoBackTIMER and OneTIMER!\r\n");
break;
case KEY2_PRES:
xTimerStart(AutoReloadTimer1_Handle,0);
printf("Start AutoBackTIMER1!\r\n");
break;
}
}
vTaskDelay(10);
}
}
//auto timer callback function
void AutoReloadCallback(TimerHandle_t xTimer)
{
static u8 tmr1_num=0;
tmr1_num++; //
LCD_ShowxNum(70,111,tmr1_num,3,16,0x80); //
LCD_Fill(6,131,114,313,lcd_discolor[tmr1_num%14]);//
}
//auto timer1 callback function
void AutoReloadCallback1(TimerHandle_t xTimer)
{
static u8 tmr2_num=0;
tmr2_num++; //
LCD_ShowxNum(60,90,tmr2_num,3,16,0x80); //
POINT_COLOR=tmr2_num%14;
LCD_DrawRectangle(30,30,80,90);//
}
//one timer callback function
void OneShotCallback(TimerHandle_t xTimer)
{
static u8 tmr2_num = 0;
tmr2_num++; //
LCD_ShowxNum(190,111,tmr2_num,3,16,0x80); //
LCD_Fill(126,131,233,313,lcd_discolor[tmr2_num%14]); //
}
Point snake[MAX_SNAKE_LENGTH];
int snakeLength;
Direction currentDir;
Point food;
//Game over
void GameOver(void) {
vTaskSuspendAll(); //
//
}
//draw game
void DrawGame(void) {
LCD_Clear(WHITE);
for (int i = 0; i < snakeLength; i++) {
FillRect(snake[i].x * 8, snake[i].y * 8, 8, 8, WHITE);
}
//
FillRect(food.x * 8, food.y * 8, 8, 8, RED);
// UpdateDisplay(); //
}
//Generate Snake
void GenerateSnakeGame(void *pvParameters) {
snake[0].x = 10;
snake[0].y = 5;
snakeLength = 1;
currentDir = DIR_RIGHT;
//generate food
food.x = rand() % (SCREEN_WIDTH / 8); //
food.y = rand() % (SCREEN_HEIGHT / 8);
while (1) {
//now length
for (int i = snakeLength; i > 0; i--) {
snake[i] = snake[i - 1];
}
//now direct
switch (currentDir) {
case DIR_UP: snake[0].y--; break;
case DIR_DOWN: snake[0].y++; break;
case DIR_LEFT: snake[0].x--; break;
case DIR_RIGHT: snake[0].x++; break;
}
//touch wall
if (snake[0].x < 0 || snake[0].x >= (SCREEN_WIDTH / 8) ||
snake[0].y < 0 || snake[0].y >= (SCREEN_HEIGHT / 8)) {
GameOver();
break;
}
if (snake[0].x == food.x && snake[0].y == food.y) {
snakeLength++;
food.x = rand() % (SCREEN_WIDTH / 8);
food.y = rand() % (SCREEN_HEIGHT / 8);
}
DrawGame();
vTaskDelay(pdMS_TO_TICKS(200)); //
}
}
u8 key;
void vControlTask(void *pvParameters) {
while (1) {
//
key=KEY_Scan(0);
if (key==WKUP_PRES) {
currentDir = DIR_UP;
} else if (key==KEY1_PRES) {
currentDir = DIR_DOWN;
} else if (key==KEY2_PRES) {
currentDir = DIR_LEFT;
} else if (key==KEY0_PRES) {
currentDir = DIR_RIGHT;
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
”请分析以上代码,根据以上代码来修改及完善功能