首页 > 其他 > 详细

04 GPIO模拟SPI驱动 128*64LCD

时间:2020-09-24 10:21:34      阅读:44      评论:0      收藏:0      [点我收藏+]

1 硬件背景

  板子是新唐的 NuMaker-M031SE 

  液晶屏幕 驱动IC:UC1701

  硬件连接对照图:

  技术分享图片

2 软件

   2.1液晶屏驱动的代码

  

  1 #include "stdio.h"
  2 #include "uc1701_Analog.h"
  3 #include "NuMicro.h"
  4 
  5 /*
  6   写指令到LCD模块
  7 */
  8 void Transform_Cmd(unsigned data)
  9 {
 10   CD = 0;
 11   SPI_WRITE_TX(SPI0, data);
 12     while(SPI_IS_BUSY(SPI0));
 13 }
 14 /*
 15   写数据到LCD模块
 16 */
 17 void Transform_Data(unsigned data)
 18 {
 19   CD = 1;
 20     SPI_WRITE_TX(SPI0, data);
 21     while(SPI_IS_BUSY(SPI0));
 22 }
 23 
 24 void delay( int i)
 25 {
 26     int j, k;
 27     for(j=0;j<i;j++)
 28         for(k=0;k<110;k++);
 29 }
 30 
 31 /*
 32     LCD模块初始化
 33 */
 34 void LCD_Init()
 35 {
 36   RST = 0;
 37   delay(100); 
 38   RST = 1;
 39   delay(20);
 40   Transform_Cmd(0xe2);
 41   delay(5);
 42   Transform_Cmd(0x2c);
 43   delay(5);
 44   Transform_Cmd(0x2e);
 45   delay(5);
 46   Transform_Cmd(0x2f);
 47   delay(5);
 48   Transform_Cmd(0x23);
 49   Transform_Cmd(0x81);
 50   Transform_Cmd(0x28);
 51   Transform_Cmd(0xa2);
 52   Transform_Cmd(0xc8);
 53   Transform_Cmd(0xa0);
 54   Transform_Cmd(0x40);
 55   Transform_Cmd(0xaf);
 56 }
 57 
 58 /*
 59   指定字符起始的 页码值 page 和列 column
 60 */
 61 void Lcd_address(unsigned char page , unsigned char column )
 62 {
 63   column = column - 1;  //从数字1 开始计数
 64   page = page - 1;            //从数字1 开始计数
 65   Transform_Cmd(0xb0 + page);         //设置页开始的地址
 66   Transform_Cmd( ((column>>4)&0x0f) + 0x10 ); //设置行开始的地址  高四位
 67   Transform_Cmd(column&0x0f);          //低四位
 68 }
 69 
 70 /*
 71     全屏清屏
 72 */
 73 void Clear_screen()
 74 {
 75   unsigned char i,j;
 76    for(i=0;i<9;i++)
 77    {
 78       Lcd_address(1+i,1);
 79       for(j=0;j<132;j++)
 80       {
 81         Transform_Data(0x00);
 82                 
 83       }
 84    }   
 85 }
 86 
 87 /*
 88 显示 16x16 汉字
 89 */
 90 void Display_16x16( unsigned char page, unsigned column ,unsigned char *dp )
 91 {
 92    unsigned char i,j;
 93    for(j=0;j<2;j++)
 94    {
 95       Lcd_address(page+j,column);
 96       for(i=0;i<16;i++)
 97       {
 98         Transform_Data(*dp);
 99         dp++;
100       }
101    }
102 }

 2.2主函数代码

  

  1 /******************************************************************************
  2  * @file     main.c
  3  * @version  V1.00
  4  * $Revision: 11 $
  5  * $Date: 18/07/19 2:18p $
  6  * @brief    Implement SPI Master loop back transfer. This sample code needs to
  7  *           connect MISO_0 pin and MOSI_0 pin together. It will compare the
  8  *           received data with transmitted data.
  9  *
 10  * @note
 11  * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
 12 *****************************************************************************/
 13 #include <stdio.h>
 14 #include "NuMicro.h"
 15 #include "uc1701_Analog.h"
 16 
 17 #define SPI_CLK_FREQ    10000000  //Speed = 100K
 18 
 19 
 20 
 21 unsigned char tail[]={
 22 0x00,0x04,0x04,0x04,0x84,0x44,0x34,0x4F,0x94,0x24,0x44,0x84,0x84,0x04,0x00,0x00, 
 23 0x00,0x60,0x39,0x01,0x00,0x3c,0x40,0x42,0x4c,0x40,0x40,0x70,0x04,0x09,0x31,0x00, 
 24 };
 25 
 26 /* Function prototype declaration */
 27 void SYS_Init(void);
 28 void SPI_Init(void);
 29 
 30 static void delay_ms(uint16_t nDelay)
 31 {
 32     uint16_t nIndex;
 33 
 34     for(nIndex = 0; nIndex < nDelay; nIndex++)
 35 
 36     {
 37         CLK_SysTickDelay(1000);//delay one ms
 38     }
 39 }
 40 /* ------------- */
 41 /* Main function */
 42 /* ------------- */
 43 int main(void)
 44 {
 45 
 46     /* Unlock protected registers */
 47     SYS_UnlockReg();
 48     /* Init System, IP clock and multi-function I/O. */
 49     SYS_Init();
 50     /* Lock protected registers */
 51     SYS_LockReg();
 52 
 53     /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
 54     UART_Open(UART0, 115200);
 55 
 56         GPIO_SetMode(PB, BIT4|BIT5, GPIO_MODE_OUTPUT);    //ADD BY GSW   RST 和 CD
 57     /* Init SPI */
 58     SPI_Init();            
 59         LCD_Init();
 60         Clear_screen();            
 61         Display_16x16(3,10,tail);    
 62     while(1)
 63     {
 64              delay_ms(1);
 65     }
 66 }
 67 
 68 void SYS_Init(void)
 69 {
 70     /*---------------------------------------------------------------------------------------------------------*/
 71     /* Init System Clock                                                                                       */
 72     /*---------------------------------------------------------------------------------------------------------*/
 73     /* Enable HIRC clock */
 74     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
 75 
 76     /* Waiting for HIRC clock ready */
 77     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
 78 
 79     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
 80     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
 81 
 82     /* Select HIRC as the clock source of UART0 */
 83     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
 84 
 85     /* Select PCLK1 as the clock source of SPI0 */
 86     CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL2_SPI0SEL_PCLK1, MODULE_NoMsk);
 87 
 88     /* Enable UART peripheral clock */
 89     CLK_EnableModuleClock(UART0_MODULE);
 90 
 91     /* Enable SPI0 peripheral clock */
 92     CLK_EnableModuleClock(SPI0_MODULE);
 93 
 94     /*---------------------------------------------------------------------------------------------------------*/
 95     /* Init I/O Multi-function                                                                                 */
 96     /*---------------------------------------------------------------------------------------------------------*/
 97     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
 98     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
 99                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
100 
101     /* Setup SPI0 multi-function pins */
102     /* PA.3 is SPI0_SS,   PA.2 is SPI0_CLK,
103        PA.1 is SPI0_MISO, PA.0 is SPI0_MOSI*/
104     SYS->GPA_MFPL = (SYS->GPA_MFPL & ~(SYS_GPA_MFPL_PA3MFP_Msk |
105                                        SYS_GPA_MFPL_PA2MFP_Msk |
106                                        SYS_GPA_MFPL_PA1MFP_Msk |
107                                        SYS_GPA_MFPL_PA0MFP_Msk)) |
108                     (SYS_GPA_MFPL_PA3MFP_SPI0_SS |
109                      SYS_GPA_MFPL_PA2MFP_SPI0_CLK |
110                      SYS_GPA_MFPL_PA1MFP_SPI0_MISO |
111                      SYS_GPA_MFPL_PA0MFP_SPI0_MOSI);
112 
113     /* Update System Core Clock */
114     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
115     SystemCoreClockUpdate();
116 }
117 
118 void SPI_Init(void)
119 {
120     /*---------------------------------------------------------------------------------------------------------*/
121     /* Init SPI                                                                                                */
122     /*---------------------------------------------------------------------------------------------------------*/
123     /* Configure as a master, clock idle low, 8-bit transaction, drive output on falling clock edge and latch input on rising edge. */
124     /* Set IP clock divider. SPI clock rate = 2MHz */
125     SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 8, SPI_CLK_FREQ);
126 
127     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
128     SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
129 }
130 
131 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/

3 遇到的问题

  这种模拟的方法很多,从51单片机到ST等都可以用这种模拟的方法来做。我分享一个外链,欢迎大家下载参考

       https://officeacid-my.sharepoint.com/:f:/g/personal/zt024_uid_men/ElFUgS2N35JLq2UifJMl3CMBxXC08CPj0J8bx6MwR2ZM3w?e=nYSv6R

       如果有问题欢迎大家留言讨论

04 GPIO模拟SPI驱动 128*64LCD

原文:https://www.cnblogs.com/Record-experience/p/13721062.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!