stm32 ucos LED light delayed flashing + button flashing + serial port output

Publisher:神秘行者Latest update time:2018-06-18 Source: eefocusKeywords:stm32  ucos  LED Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#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;
  }
}


Keywords:stm32  ucos  LED Reference address:stm32 ucos LED light delayed flashing + button flashing + serial port output

Previous article:[HAL library learning path] 7. Timer interrupt
Next article:STM32F103 LED light simple program implementation

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号