stm32 ucos LED灯延时闪烁+按键闪烁+串口输出
2018-06-18 来源:eefocus
#include 'main.h'
#include 'stdio.h'
USART_InitTypeDef USART_InitStructure;
#define RCC_KEY1 RCC_APB2Periph_GPIOD
#define GPIO__KEY1_Port GPIOD
#define KEY1 GPIO_Pin_3
#define RCC_KEY@ RCC_APB2Periph_GPIOA
#define GPIO_KEY2_Port GPIOA
#define KEY2 GPIO_Pin_8
#define RCC_KEY3 RCC_APB2Periph_GPIOC
#define GPIO_KEY3_PORT GPIOC
#define GPIO_KEY3 GPIO_Pin_13
#define RCC_KEY4 RCC_APB2Periph_GPIOA
#define GPIO_KEY4_PORT GPIOA
#define GPIO_KEY4 GPIO_Pin_0
#define NOKEY 0
#define KEY1 1
#define KEY2 2
#define KEY3 3
#define KEY4 4
INT8U KeyNum = 0;
static OS_STK TaskOSSTK[128];
static OS_STK Task1OSSTK[128];
static OS_STK Task2OSSTK[128];
static OS_STK Task3OSSTK[128];
static OS_STK Task4OSSTK[128];
static void Task0(void * pdata); //优先级6
static void Task1(void * pdata); //7
static void Task2(void * pdata); //8
static void Task3(void * pdata); //9
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);// USART1 可以换成 USART2 等
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
// 接收数据
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
int main()
{
SystemInit();
RCC_Configuration();
GPIO_Configuration()
USART_Configuration();
OSInit();
OSTaskCreate(Task0,(void *)0,(OS_STK *)&Task0OSSTK[128-1],6);
OSStart();
while(1);
}
static void Task0(void *pdata)
{
(void)pdata;
OS_CPU_SysTickInit();
OSTaskCreate(Task1, (void *)0, (OS_STK *)&Task1OSStk[128-1], 7);
OSTaskCreate(Task2, (void *)0, (OS_STK *)&Tas2OSStk[128-1], 8);
OSTaskCreate(Task3, (void *)0, (OS_STK *)&Task3OSStk[128-1], 9);
for(;;)
{
OSTimeDly(OS_TICKS_PER_SEC);
GPIO_SetBits(GPIOF, GPIO_Pin_6);
OSTimeDly(OS_TICKS_PER_SEC);
GPIO_ResetBits(GPIOF, GPIO_Pin_6);
}
}
static void Task1(void *pdata)
{
(void)pdata;
for(;;)
{
OSTimeDly(OS_TICKS_PER_SEC/2);
GPIO_SetBits(GPIOF, GPIO_Pin_7);
OSTimeDly(OS_TICKS_PER_SEC/2);
GPIO_ResetBits(GPIOF, GPIO_Pin_7);
}
}
static void Task2(void *pdata)
{
(void)pdata;
for(;;)
{ printf('灯3开始闪烁\r\n');
OSTimeDly(OS_TICKS_PER_SEC/5);
GPIO_SetBits(GPIOF, GPIO_Pin_8);
OSTimeDly(OS_TICKS_PER_SEC/5);
GPIO_ResetBits(GPIOF, GPIO_Pin_8);
}
}
static void Task3(void *pdata)
{
(void)pdata;
while(1)
{
KeyNum = ReadKeyDown();
if(KeyNum==KEY1)
{
OSTimeDly(OS_TICKS_PER_SEC/10);
GPIO_SetBits(GPIOF, GPIO_Pin_9);
OSTimeDly(OS_TICKS_PER_SEC/10);
GPIO_ResetBits(GPIOF, GPIO_Pin_9);
}
}
}
INT8U ReadKeyDown(void)
{
/* 1 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1))
{
return KEY1;
}
/* 2 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT, GPIO_KEY2))
{
return KEY2;
}
/* 3 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT, GPIO_KEY3))
{
return KEY3;
}
/* 4 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY4_PORT, GPIO_KEY4))
{
return KEY4;
}
/* No key is pressed */
else
{
return NOKEY;
}
}