ARM? Cortex? M4 Cookbook
上QQ阅读APP看书,第一时间看更新

Writing to the GLCD

Although the LED flashing programs we've written so far have served to provide a tutorial introduction to C, you are probably ready for something a little more exciting. The Graphic LCD (GLCD) touchscreen provides an interactive interface based on a 320 x 240 pixel color display. Keil provides a library of functions to write characters and bit-mapped graphics to the screen.

Getting ready

  1. Create a new folder and rename it helloLCD_c2v0. Invoke uVision5, and create a new project.
  2. After selecting the target device (STM32F407IGHx), use the RTE manager to select the MCBSTM32F400 target board, and check the following software components: Board SupportGraphic LCD, CMSIS → CORE, CMSIS → RTOS (API)KeilRTX, DeviceStartup, DeviceSTM32Cube Framework (API)Classic. Finally, left-click on Resolve and OK.

How to do it…

  1. Create a new C source file called helloLCD.c, and enter the following statements. Although hidden by a fold, don't forget to add the boilerplate code we described in the recipe helloBlinky_c2v0.
    /*--------------------------------------------------
     * Recipe:  helloLCD_c2v0
     * Name:    helloLCD.c
     * Purpose: LCD Touchscreen Demo
     *--------------------------------------------------
     *
     * Modification History
     * 06.02.14 Created
     * 08.12.15 Updated (uVision5.17 & DFP2.6.0)
     *
     * Dr Mark Fisher, CMP, UEA, Norwich, UK
     *--------------------------------------------------*/
    
    #include "stm32f4xx_hal.h"
    #include "GLCD_Config.h"
    #include "Board_GLCD.h"
    
    #define wait_delay HAL_Delay
    
    extern GLCD_FONT     GLCD_Font_6x8;
    extern GLCD_FONT     GLCD_Font_16x24;
    
    #ifdef __RTX
    ______________________________________________________
    
    /* Function Prototypes */
    void SystemClock_Config(void);
    
    /**
      * System Clock Configuration
      */
    void SystemClock_Config(void) {
    ______________________________________________________
    
    /**
      * Main function
      */
    int main ( ) {
      unsigned int count;   
      
      HAL_Init ();   /* Init Hardware Abstraction Layer */
      SystemClock_Config ();           /* Config Clocks */
    
      GLCD_Initialize();             
      GLCD_SetBackgroundColor (GLCD_COLOR_WHITE);
      GLCD_ClearScreen ();  
      GLCD_SetBackgroundColor (GLCD_COLOR_BLUE);
      GLCD_SetForegroundColor (GLCD_COLOR_WHITE);
      GLCD_SetFont (&GLCD_Font_16x24);
      GLCD_DrawString (0, 0*24, " CORTEX-M4 COOKBOOK ");
      GLCD_DrawString (0, 1*24, "  PACKT Publishing  ");
      GLCD_SetBackgroundColor (GLCD_COLOR_WHITE);   
      GLCD_SetForegroundColor (GLCD_COLOR_BLUE);
    
      for (;;) {
        if (count==0)
          GLCD_DrawString (0, 2*24, "     Hello LCD!     ");
        else 
          GLCD_DrawString (0, 2*24, "                    ");                 
    
        wait_delay( 100 );            
        count = ( count+1 )%2;
      }  /* end for */
    }
  2. Build, download, and run the program.

How it works…

The functions beginning GLCD_ are defined in the file GLCD_MCBSTM32F400.c. We need to open this, and read the comments in the function headers to understand how to use them. The header file Boadd_LCD.h that is included by the pre-processor contains the function prototype declarations. The header file GLCD_Config.h provides macros that define named colors (like, GLCD_COLOR_BLACK) and constants such as GLCD_WIDTH / HEIGHT. GLCD_MCBSTM32F400.c is the latest in a series of GLCD drivers provided by Keil, and it represents a CMSIS v2.0-compliant revision of earlier versions.

The function GLCD_DrawString (uint32_t x, uint32_t y, const char *str) declared in file Board_GLCD.h takes three input arguments (args). The first two position the text on the screen, and the last arg is a pointer to an array of characters to be written (usually a literal value defined using quotes " " in the function call). Before calling GLCD_DrawString ( ), we must first set the character font to be used by the calling function, GLCD_SetFont (GLCD_FONT *font), and pass a pointer to the font used. There are two font sizes defined in file GLCD_Fonts.c. An array of characters terminated by a NULL character is called a string. You may wonder why we didn't need to use the & operator to recover an address and assign a pointer as we illustrated earlier. The short answer is that arrays are always referenced using pointers, so there is no need, but we'll discuss the matter further in Chapter 3, Assembly Language Programming.

The macro definition #define wait_delay HAL_Delay provides a pseudonym for the function HAL_Delay ( ) declared in the file st32f4xx_HAL.h. This is a more accurate delay based on a timer rather than an instruction loop.