打印
[应用方案]

新塘M0系列在LCD上显示中文字符方法

[复制链接]
528|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wanduzi|  楼主 | 2020-3-29 11:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
EC_M051_Display_Chinese_Characters_On_LCD_V1.00.zip (490.02 KB)


使用特权

评论回复
沙发
wanduzi|  楼主 | 2020-3-29 11:54 | 只看该作者
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2014 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "M051Series.h"
#include "lcd_Driver.h"


#define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
#define PLL_CLOCK           50000000


void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

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

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

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

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

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

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

    /* Enable IP clock */
    CLK_EnableModuleClock(UART0_MODULE);

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


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

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

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



}

/*---------------------------------------------------------------------------------------------------------*/
/* Init UART                                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
void UART0_Init()
{
    /* Reset IP */
    SYS_ResetModule(UART0_RST);

    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
}


/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function                                                                                           */
/*---------------------------------------------------------------------------------------------------------*/

int main(void)
{

    /* Unlock protected registers */
    SYS_UnlockReg();

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

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 for printf */
    UART0_Init();

    printf("LCD demo start.\n");
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init LCD                                                                                */
    /*---------------------------------------------------------------------------------------------------------*/
    LCD_Init();
    LCD_EnableBackLight();
    LCD_ClearScreen();

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

    ShowChar(1, 4, 'N');
    ShowChar(1, 5, 'u');
    ShowChar(1, 6, 'v');
    ShowChar(1, 7, 'o');
    ShowChar(1, 8, 't');
    ShowChar(1, 9, 'o');
    ShowChar(1, 10, 'n');

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


    while (1);
}




使用特权

评论回复
板凳
wanduzi|  楼主 | 2020-3-29 11:55 | 只看该作者
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url]     LCD_Driver.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V3.0
* $Revision: 4 $
* $Date: 19/06/20 10:49a $
* [url=home.php?mod=space&uid=247401]@brief[/url]    M051 series LCD Module library source file
*
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "M051Series.h"
#include "LCD_Driver.h"

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

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

    SPI0->CNTRL |= SPI_CNTRL_IF_Msk;
}

/**
  * @brief  Use SPI0 interface to configure LCD module
  * @param  None
  * @return None
  */
void LCD_Init(void)
{
    /* Use SPI0 for LCD */
    SYS->P1_MFP &= ~(SYS_MFP_P17_Msk | SYS_MFP_P16_Msk | SYS_MFP_P15_Msk | SYS_MFP_P14_Msk);
    SYS->P1_MFP |= SYS_MFP_P17_SPICLK0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P14_SPISS0;

    CLK->APBCLK |= CLK_APBCLK_SPI0_EN_Msk;

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

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

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

    // Set BR
    SpiWrite(0xEB);

    // Set PM
    SpiWrite(0x81);
    SpiWrite(0xA0);

    SpiWrite(0xC0);

    // Set Display Enable
    SpiWrite(0xAF);
}

/**
  * @brief Configure start address of LCD
  * @param PA PA value for LCD
  * @param CA CA value for LCD
  * @return None
  */
static void SetPACA(uint8_t PA, uint8_t CA)
{
    // Set PA
    SpiWrite(0xB0 | PA);

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

    // Set CA LSB
    SpiWrite(0x00 | (CA & 0xF));
}

/**
  * @brief Show a char on LCD
  * @param x X position
  * @param y Y position
  * @param ascii_word    ASCII character that will be shown on LCD
  * @return None
  */
void ShowChar(uint8_t x, uint8_t y, uint8_t ascii_word)
{
    int i = 0, k = 0;
    unsigned char temp;
    k = (ascii_word - 32) * 16;

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

    for (i = 0; i < 8; i++)
    {
        SetPACA((x * 2) + 1, (129 - (y * 8) - i));
        temp = Ascii[k + i + 8];
        SpiWrite(0x100 | temp);
    }
}


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

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

    k = index * 32;

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

    /*Show 8×16 points in the lower half*/
    for (i = 0; i < 16; i++)
    {
        SetPACA((x * 2) + 1, (129 - (y * 16) - i));
        temp = CHCode[k + i + 16];
        SpiWrite(0x100 | temp);
    }
}
/**
  * @brief Enable back-light of LCD
  * @param None
  * @return None
  */
void LCD_EnableBackLight(void)
{
    GPIO_SetMode(P1, 0x02, GPIO_PMD_OUTPUT);
    P11 = 0;
}

/**
  * @brief Disable back-light of LCD
  * @param None
  * @return None
  */
void LCD_DisableBackLight(void)
{
    GPIO_SetMode(P1, 0x02, GPIO_PMD_OUTPUT);
    P11 = 1;
}

/**
  * @brief Show a string on specific line
  * @param line Line number
  * @param str string
  * @return None
  */
void LCD_Print(uint8_t line, char *str)
{
    int i = 0;

    do
    {
        ShowChar(line, i, *str++);
        i++;

        if (i > 15)
            break;
    } while (*str != '\0');
}

/**
  * @brief Clear screen to background color
  * @param None
  * @return None
  */
void LCD_ClearScreen(void)
{
    int i = 0;
    /*CLEAR ALL PANEL*/
    SetPACA(0x0, 0x0);

    for (i = 0; i < 132 * 8; i++)
    {
        SpiWrite(0x100);
    }

    SpiWrite(0x10f);
}


使用特权

评论回复
地板
wanduzi|  楼主 | 2020-3-29 11:56 | 只看该作者
这个例子非常棒。

使用特权

评论回复
5
heisexingqisi| | 2020-3-29 13:20 | 只看该作者
LCD是什么型号的,看看。

使用特权

评论回复
6
darklighttt| | 2020-3-30 09:09 | 只看该作者
没有一点中文介绍,有点不大能看懂。不过程序看上去还是很规范的。

使用特权

评论回复
7
磨砂| | 2020-4-6 15:46 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

136

主题

1685

帖子

3

粉丝