[应用方案] 新塘M0系列在LCD上显示中文字符方法

[复制链接]
801|6
 楼主| wanduzi 发表于 2020-3-29 11:50 | 显示全部楼层 |阅读模式
EC_M051_Display_Chinese_Characters_On_LCD_V1.00.zip (490.02 KB, 下载次数: 11)


 楼主| wanduzi 发表于 2020-3-29 11:54 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2014 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include <stdio.h>
  7. #include "M051Series.h"
  8. #include "lcd_Driver.h"


  9. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  10. #define PLL_CLOCK           50000000


  11. void SYS_Init(void)
  12. {
  13.     /*---------------------------------------------------------------------------------------------------------*/
  14.     /* Init System Clock                                                                                       */
  15.     /*---------------------------------------------------------------------------------------------------------*/

  16.     /* Enable Internal RC 22.1184MHz clock */
  17.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  18.     /* Waiting for Internal RC clock ready */
  19.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  20.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  21.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  22.     /* Enable external XTAL 12MHz clock */
  23.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  24.     /* Waiting for external XTAL clock ready */
  25.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  26.     /* Set core clock as PLL_CLOCK from PLL */
  27.     CLK_SetCoreClock(PLL_CLOCK);

  28.     /* Enable IP clock */
  29.     CLK_EnableModuleClock(UART0_MODULE);

  30.     /* IP clock source */
  31.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));


  32.     /* Update System Core Clock */
  33.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  34.     //SystemCoreClockUpdate();
  35.     PllClock        = PLL_CLOCK;            // PLL
  36.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  37.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

  38.     /*---------------------------------------------------------------------------------------------------------*/
  39.     /* Init I/O Multi-function                                                                                 */
  40.     /*---------------------------------------------------------------------------------------------------------*/

  41.     /* Set P3 multi-function pins for UART0 RXD and TXD.*/
  42.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;



  43. }

  44. /*---------------------------------------------------------------------------------------------------------*/
  45. /* Init UART                                                                                               */
  46. /*---------------------------------------------------------------------------------------------------------*/
  47. void UART0_Init()
  48. {
  49.     /* Reset IP */
  50.     SYS_ResetModule(UART0_RST);

  51.     /* Configure UART0 and set UART0 Baudrate */
  52.     UART_Open(UART0, 115200);
  53. }


  54. /*---------------------------------------------------------------------------------------------------------*/
  55. /* MAIN function                                                                                           */
  56. /*---------------------------------------------------------------------------------------------------------*/

  57. int main(void)
  58. {

  59.     /* Unlock protected registers */
  60.     SYS_UnlockReg();

  61.     /* Init System, IP clock and multi-function I/O */
  62.     SYS_Init();

  63.     /* Lock protected registers */
  64.     SYS_LockReg();

  65.     /* Init UART0 for printf */
  66.     UART0_Init();

  67.     printf("LCD demo start.\n");
  68.     /*---------------------------------------------------------------------------------------------------------*/
  69.     /* Init LCD                                                                                */
  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     LCD_Init();
  72.     LCD_EnableBackLight();
  73.     LCD_ClearScreen();

  74.     LCD_Print(0, "This is LCD demo");

  75.     ShowChar(1, 4, 'N');
  76.     ShowChar(1, 5, 'u');
  77.     ShowChar(1, 6, 'v');
  78.     ShowChar(1, 7, 'o');
  79.     ShowChar(1, 8, 't');
  80.     ShowChar(1, 9, 'o');
  81.     ShowChar(1, 10, 'n');

  82.     ShowCharCH(2, 2, 0); //in the third row and third column of LCD show  '新'
  83.     ShowCharCH(2, 3, 1); //in the third row and fourth column of LCD show '唐'
  84.     ShowCharCH(2, 4, 2); //in the third row and fifth column of LCD show  '电'
  85.     ShowCharCH(2, 5, 3); //in the third row and sexth column of LCD show  '子'


  86.     while (1);
  87. }




 楼主| wanduzi 发表于 2020-3-29 11:55 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     LCD_Driver.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.0
  4. * $Revision: 4 $
  5. * $Date: 19/06/20 10:49a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 series LCD Module library source file
  7. *
  8. * @note
  9. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include "M051Series.h"
  12. #include "LCD_Driver.h"

  13. extern  const char Ascii[];
  14. extern  const char CHCode[];
  15. /**
  16.   * @brief  For SPI write method
  17.   * @param  u32Data    Data will be written by SPI0
  18.   * [url=home.php?mod=space&uid=266161]@return[/url] None
  19.   */
  20. static __INLINE void SpiWrite(uint32_t u32Data)
  21. {
  22.     SPI0->TX0 = u32Data;
  23.     SPI0->CNTRL |= SPI_CNTRL_GO_BUSY_Msk;

  24.     while ((SPI0->CNTRL & SPI_CNTRL_GO_BUSY_Msk));

  25.     SPI0->CNTRL |= SPI_CNTRL_IF_Msk;
  26. }

  27. /**
  28.   * @brief  Use SPI0 interface to configure LCD module
  29.   * @param  None
  30.   * @return None
  31.   */
  32. void LCD_Init(void)
  33. {
  34.     /* Use SPI0 for LCD */
  35.     SYS->P1_MFP &= ~(SYS_MFP_P17_Msk | SYS_MFP_P16_Msk | SYS_MFP_P15_Msk | SYS_MFP_P14_Msk);
  36.     SYS->P1_MFP |= SYS_MFP_P17_SPICLK0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P14_SPISS0;

  37.     CLK->APBCLK |= CLK_APBCLK_SPI0_EN_Msk;

  38.     SYS->IPRSTC2 |= SYS_IPRSTC2_SPI0_RST_Msk;
  39.     SYS->IPRSTC2 &= (~SYS_IPRSTC2_SPI0_RST_Msk);

  40.     /* Initial SPI0 data format and SPI0 clock */
  41.     SPI0->CNTRL = SPI_CNTRL_CLKP_Msk | SPI_CNTRL_TX_NEG_Msk | (9 << SPI_CNTRL_TX_BIT_LEN_Pos);
  42.     SPI0->DIVIDER = ((12000000 / 2000000) >> 1) - 1;

  43.     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
  44.     SPI0->SSR = SPI_SSR_AUTOSS_Msk | SPI_SSR_SSR_Msk ;

  45.     // Set BR
  46.     SpiWrite(0xEB);

  47.     // Set PM
  48.     SpiWrite(0x81);
  49.     SpiWrite(0xA0);

  50.     SpiWrite(0xC0);

  51.     // Set Display Enable
  52.     SpiWrite(0xAF);
  53. }

  54. /**
  55.   * @brief Configure start address of LCD
  56.   * @param PA PA value for LCD
  57.   * @param CA CA value for LCD
  58.   * @return None
  59.   */
  60. static void SetPACA(uint8_t PA, uint8_t CA)
  61. {
  62.     // Set PA
  63.     SpiWrite(0xB0 | PA);

  64.     // Set CA MSB
  65.     SpiWrite(0x10 | (CA >> 4) & 0xF);

  66.     // Set CA LSB
  67.     SpiWrite(0x00 | (CA & 0xF));
  68. }

  69. /**
  70.   * @brief Show a char on LCD
  71.   * @param x X position
  72.   * @param y Y position
  73.   * @param ascii_word    ASCII character that will be shown on LCD
  74.   * @return None
  75.   */
  76. void ShowChar(uint8_t x, uint8_t y, uint8_t ascii_word)
  77. {
  78.     int i = 0, k = 0;
  79.     unsigned char temp;
  80.     k = (ascii_word - 32) * 16;

  81.     for (i = 0; i < 8; i++)
  82.     {
  83.         SetPACA((x * 2), (129 - (y * 8) - i));
  84.         temp = Ascii[k + i];
  85.         SpiWrite(0x100 | temp);
  86.     }

  87.     for (i = 0; i < 8; i++)
  88.     {
  89.         SetPACA((x * 2) + 1, (129 - (y * 8) - i));
  90.         temp = Ascii[k + i + 8];
  91.         SpiWrite(0x100 | temp);
  92.     }
  93. }


  94. /**
  95.   * @brief show 16×16 Chinese character on LCD
  96.   * @param x X position,x=0、1、2、4
  97.   * @param y Y position,y=0、1、2、4、5、6、7
  98.   * @param index    Chinese character library in CHCode array
  99.   * @return None
  100.   */

  101. void ShowCharCH(uint8_t x, uint8_t y, uint16_t index)
  102. {
  103.     int i = 0, k = 0;
  104.     unsigned char temp;

  105.     k = index * 32;

  106.     /*Display 8×16 points in the upper half*/
  107.     for (i = 0; i < 16; i++)
  108.     {
  109.         SetPACA((x * 2), (129 - (y * 16) - i));
  110.         temp = CHCode[k + i];
  111.         SpiWrite(0x100 | temp);
  112.     }

  113.     /*Show 8×16 points in the lower half*/
  114.     for (i = 0; i < 16; i++)
  115.     {
  116.         SetPACA((x * 2) + 1, (129 - (y * 16) - i));
  117.         temp = CHCode[k + i + 16];
  118.         SpiWrite(0x100 | temp);
  119.     }
  120. }
  121. /**
  122.   * @brief Enable back-light of LCD
  123.   * @param None
  124.   * @return None
  125.   */
  126. void LCD_EnableBackLight(void)
  127. {
  128.     GPIO_SetMode(P1, 0x02, GPIO_PMD_OUTPUT);
  129.     P11 = 0;
  130. }

  131. /**
  132.   * @brief Disable back-light of LCD
  133.   * @param None
  134.   * @return None
  135.   */
  136. void LCD_DisableBackLight(void)
  137. {
  138.     GPIO_SetMode(P1, 0x02, GPIO_PMD_OUTPUT);
  139.     P11 = 1;
  140. }

  141. /**
  142.   * @brief Show a string on specific line
  143.   * @param line Line number
  144.   * @param str string
  145.   * @return None
  146.   */
  147. void LCD_Print(uint8_t line, char *str)
  148. {
  149.     int i = 0;

  150.     do
  151.     {
  152.         ShowChar(line, i, *str++);
  153.         i++;

  154.         if (i > 15)
  155.             break;
  156.     } while (*str != '\0');
  157. }

  158. /**
  159.   * @brief Clear screen to background color
  160.   * @param None
  161.   * @return None
  162.   */
  163. void LCD_ClearScreen(void)
  164. {
  165.     int i = 0;
  166.     /*CLEAR ALL PANEL*/
  167.     SetPACA(0x0, 0x0);

  168.     for (i = 0; i < 132 * 8; i++)
  169.     {
  170.         SpiWrite(0x100);
  171.     }

  172.     SpiWrite(0x10f);
  173. }


 楼主| wanduzi 发表于 2020-3-29 11:56 | 显示全部楼层
这个例子非常棒。
heisexingqisi 发表于 2020-3-29 13:20 | 显示全部楼层
LCD是什么型号的,看看。
darklighttt 发表于 2020-3-30 09:09 | 显示全部楼层
没有一点中文介绍,有点不大能看懂。不过程序看上去还是很规范的。
磨砂 发表于 2020-4-6 15:46 | 显示全部楼层
非常感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

156

主题

1882

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部