본문 바로가기

컴퓨터/STM32 primer 2

STM32 Primer2 LED 제어하기


이번시간에는 CircleOS API함수를 사용하여 LED제어를 해 보도록 하겠습니다.

조이스틱을 이용하여 조이스틱을 위로하면 빨간색 LED가 켜지고 아래로 하면 빨간색이 끄지고,
오른쪽으로 하면 녹색 LED가 켜지고, 왼쪽으로 하면 녹색이 끄지도록 했습니다.

선택버튼을 누르면 프로그램이 종료합니다.


 /********************** (C) COPYRIGHT 2007-2009 RAISONANCE ********************
* File Name          :  Application.c
* Author             :
* Date First Issued  :
* Description        :  Circle_App CircleOS application template.
* Revision           :
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "circle_api.h"

/* Private defines -----------------------------------------------------------*/

// The following should be the minimal CircleOS version needed by your application
#define NEEDEDVERSION "V 3.7"

/* Private functions ---------------------------------------------------------*/
static enum MENU_code MsgVersion(void);

/* Public variables ----------------------------------------------------------*/
const char Application_Name[8+1] = {"My App"};      // Max 8 characters


/*******************************************************************************
* Function Name  : Application_Ini
* Description    : Initialization function of Circle_App. This function will
*                  be called only once by CircleOS.
* Input          : None
* Return         : MENU_CONTINUE_COMMAND
*******************************************************************************/
enum MENU_code Application_Ini(void)
    {
    // Ensure that the current OS version is recent enough
    if(strcmp(UTIL_GetVersion(), NEEDEDVERSION) < 0)
        {
        return MsgVersion();
        }


    // TODO: Write your application initialization function here.
    DRAW_Clear ();


    return MENU_CONTINUE_COMMAND;
    }

/*******************************************************************************
* Function Name  : Application_Handler
* Description    : Management of the Circle_App.
*
* Input          : None
* Return         : MENU_CONTINUE
*******************************************************************************/
enum MENU_code Application_Handler(void)
    {


    // TODO: Write your application handling here.
  
    switch(JOYSTICK_GetState()) {
        case JOYSTICK_LEFT:
            LED_Set(LED_GREEN, LED_OFF);
            DRAW_DisplayString (10, 10, "GREEN OFF", 12);
            break;
        case JOYSTICK_RIGHT:
            LED_Set(LED_GREEN, LED_ON);
            DRAW_DisplayString (10, 10, "GREEN ON", 12);
            break;
        case JOYSTICK_DOWN:
            LED_Set(LED_RED, LED_OFF);
            DRAW_DisplayString (10, 10, "RED OFF", 12);
            break;
        case JOYSTICK_UP:
            LED_Set(LED_RED, LED_ON);
            DRAW_DisplayString (10, 10, "RED ON", 12);
            break;
    }

 

    // This routine will get called repeatedly by CircleOS, until we
    // return MENU_LEAVE

#if 1
    // If the button is pressed, the application is exited
    if(BUTTON_GetState() == BUTTON_PUSHED)
        {
        BUTTON_WaitForRelease();
        return MENU_Quit();
        }
#endif

    return MENU_CONTINUE;   // Returning MENU_LEAVE will quit to CircleOS
    }

/*******************************************************************************
* Function Name  : MsgVersion
* Description    : Display the current CircleOS version and the version needed
*                  exit to main menu after 4 secondes
*
* Input          : None
* Return         : MENU_REFRESH
*******************************************************************************/
static enum MENU_code MsgVersion(void)
   {
   int hh,mm,ss,ss2;
  
   DRAW_DisplayString(5,60,"CircleOS",17);
   DRAW_DisplayString(80,60,UTIL_GetVersion(),6);
   DRAW_DisplayString(5,34,NEEDEDVERSION,6);
   DRAW_DisplayString(50,34," required",12);
  
   RTC_GetTime( &hh, &mm, &ss);
   ss = ss + 4;                  // 4 secondes
   ss = ss%60;
  
   do
        {
        RTC_GetTime( &hh, &mm, &ss2 );
        }
   while ( ss2 != ss );           // do while < 4 secondes
  
   DRAW_Clear();
   return MENU_REFRESH;
   }