[应用方案] 八月新唐+点亮LCD1602

[复制链接]
3256|21
 楼主| abc9981 发表于 2017-8-14 15:56 | 显示全部楼层 |阅读模式
本帖最后由 abc9981 于 2017-8-16 14:05 编辑

之前参加研讨会新唐送了两块开发板,躺在抽屉里快生根了,这次拿出来耍耍。
IMG_20170812_172932R.jpg
IMG_20170812_173032R.jpg
一块是nano103s,另一块是M451
nano103s附板是Nu-Link_Me V3.0目前应该是较新的烧录模块,而且还带有串口功能 ,烧录跟串口直接解决了,不用再接一个串口TTL
做过1602的应该都知道,1602是5v的电平驱动,而nano103是3v的驱动查询芯片手册配置兼容引脚控制
QQ截图20170814153446.jpg

根据手册信息,选用PC.0~PC.3作为data口低4位,PC.8~PC.11作为data口高4位,rs PB.9 rw PB.10 ep PB.11。
选择好引脚后就可以接线了。

Nu-Link_Me V3.0的优势就是板载的串口TTL,将四个拨码开关拨到NO,就可以在电脑中直接链接UART0,实在是方便
IMG_20170814_154406R.jpg

接下来就是写代码了
程序主要运用的是GPIO的功能,加上串口,跟延时处理,就可以搞定了

主要自己写的就是LCD1602的驱动程序了
LCD1602.H
  1. #ifndef __LCD1602_H__
  2. #define __LCD1602_H__

  3. #include "Nano103.h"

  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. #define ushort        unsigned short

  7. #define rs         PB9
  8. #define rw        PB10
  9. #define ep        PB11


  10. void lcd_init(void);
  11. void lcd_pos(uchar x,uchar y);
  12. void wnchar(uchar weizhi,uchar zifu[]);

  13. #endif
LCD1602.C
  1. #include "Lcd1602.h"



  2. void delay4us()
  3. {
  4.         CLK_SysTickDelay(20);
  5. }

  6. /**********忙测试**********/
  7. //uchar Busy_Check()
  8. //{
  9. //        uchar LCD_Status;
  10. //        rs=0;
  11. //        rw=1;
  12. //        ep=1;
  13. //        delay4us();
  14. //        LCD_Status=P0&0x80;
  15. //        ep=0;
  16. //        return LCD_Status;
  17. //}

  18. /*************写指令************/
  19. void lcd_wcmd(uchar cmd)
  20. {
  21. //        while(Busy_Check());
  22.         uint data;
  23.         data = (uint)(cmd & 0xf0)<<4;
  24.         data += (cmd &0x0f);
  25.         rs=0;
  26.         rw=0;
  27.         ep=0;
  28.         delay4us();
  29.         PC->DOUT = data;
  30.         delay4us();
  31.         ep=1;
  32.         delay4us();
  33.         ep=0;
  34. }

  35. /************指定位置******************/
  36. void lcd_pos(uchar x,uchar y) //设置液晶显示位置,x=0为第一行,1为第2行
  37. {
  38.         uchar pos;
  39.         if(x == 0)
  40.                 x = 0x80;
  41.         else if(x == 1)
  42.                 x = 0x80 + 0x40;
  43.         pos = x + y;
  44.         lcd_wcmd(pos);
  45. }

  46. /**************写数据******************/
  47. void lcd_wdat(uchar dat)
  48. {
  49. //        while(Busy_Check());
  50.         uint data;
  51.         data = (uint)(dat & 0xf0)<<4;
  52.         data += (dat &0x0f);
  53.         rs=1;
  54.         rw=0;
  55.         ep=0;
  56.         PC->DOUT = data;
  57.         delay4us();
  58.         ep=1;
  59.         delay4us();
  60.         ep=0;
  61. }

  62. /************写一串字符**********************/
  63. void wnchar(uchar weizhi,uchar zifu[])//写N个字符
  64. {
  65.         uchar i;
  66.         lcd_pos(weizhi,0);
  67.         for(i=0; ;i++)
  68.         {
  69.                 lcd_wdat(zifu[i]);
  70.                 if(zifu[i+1]=='\0')
  71.                 break;
  72.         }
  73. }


  74. /**********初始化***************/
  75. void lcd_init()
  76. {
  77.         lcd_wcmd(0x38);
  78.         delay4us();
  79.         lcd_wcmd(0x0c);
  80.         delay4us();
  81.         lcd_wcmd(0x06);
  82.         delay4us();
  83.         lcd_wcmd(0x01);
  84.         delay4us();
  85. }
main.c
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 4 $
  5. * $Date: 15/12/27 1:12p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    A project template for Nano103 MCU.
  7. *
  8. * @note
  9. * Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "Nano103.h"
  13. #include "Lcd1602.h"


  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/
  19.     /* Unlock protected registers */
  20.     SYS_UnlockReg();

  21.     /* Enable External XTAL (4~24 MHz) */
  22.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  23.     /* Waiting for 12MHz clock ready */
  24.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  25.     /* Switch HCLK clock source to HXT */
  26.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT,CLK_HCLK_CLK_DIVIDER(1));

  27.     /* Enable UART clock */
  28.     CLK_EnableModuleClock(UART0_MODULE);

  29.     /* Select UART clock source from HXT */
  30.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_UART0_CLK_DIVIDER(1));

  31.     /* Update System Core Clock */
  32.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  33.     SystemCoreClockUpdate();


  34.     /*---------------------------------------------------------------------------------------------------------*/
  35.     /* Init I/O Multi-function                                                                                 */
  36.     /*---------------------------------------------------------------------------------------------------------*/
  37.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  38.     SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk);
  39.     SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB0MFP_UART0_RXD | SYS_GPB_MFPL_PB1MFP_UART0_TXD);
  40.     /* Lock protected registers */
  41.     SYS_LockReg();
  42. }

  43. /*
  44. * This is a template project for Nano103 series MCU. Users could based on this project to create their
  45. * own application without worry about the IAR/Keil project settings.
  46. *
  47. * This template application uses external crystal as HCLK source and configures UART0 to print out
  48. * "Hello World", users may need to do extra system configuration based on their system design.
  49. */

  50. int main()
  51. {

  52.         uchar data[]="Hello,21ic.com!";
  53.         uchar data1[]="MyID:abc9981";
  54.     SYS_Init();
  55.     /* Init UART to 115200-8n1 for print message */
  56.     UART_Open(UART0, 115200);
  57.     /* Connect UART to PC, and open a terminal tool to receive following message */
  58.     printf("八月,新唐你好\r\n");
  59.         printf("Hello,21ic.com!\r\n");

  60.     /* Got no where to go, just loop forever */

  61.         GPIO_SetMode(PA, BIT14, GPIO_PMD_OUTPUT);
  62.         GPIO_SetMode(PB, BIT9|BIT10|BIT11, GPIO_PMD_OUTPUT);
  63.         GPIO_SetMode(PC, 0x00000f0f, GPIO_PMD_OUTPUT);
  64.         printf("LCD1602  INIT!\r\n");
  65.         lcd_init();
  66.         CLK_SysTickDelay(1000000);
  67.         printf("Display 'Hello,21ic.com!'\r\n");
  68.         wnchar(0,data) ;
  69.         printf("Display 'MyID:abc9981'\r\n");
  70.         wnchar(1,data1) ;
  71.         printf("MyID:abc9981\r\n");
  72.     while(1)
  73.         {
  74.                 PA->DOUT = 0x00;                /* Output low */
  75.             CLK_SysTickDelay(1000000);
  76.                 PA->DOUT = 0x4000;                /* Output low */
  77.             CLK_SysTickDelay(1000000);
  78.         }

  79. }
代码搞定,现在就是成果展示了

串口数据

QQ截图20170814150325.jpg

LCD显示
IMG_20170814_150250R.jpg


LCD1602通常都是在学校的时候用的,这次又一次拿出了1602感觉好像又回到了读书的时候,真的很怀念
现在大学生电子设计大赛正在进行,祝所有参赛选手能获得好成绩!





附件太大了,可以前去官网下载nano103s相关资料:http://www.nuvoton.com.cn/hq/products/microcontrollers/arm-cortex-m0-mcus/nano103-base-series/?__locale=zh



  
dongnanxibei 发表于 2017-8-14 16:40 | 显示全部楼层
我看看你们都有什么板子,新唐好久没送板子了。
 楼主| abc9981 发表于 2017-8-14 17:37 | 显示全部楼层
dongnanxibei 发表于 2017-8-14 16:40
我看看你们都有什么板子,新唐好久没送板子了。

现场研讨会一般不是都有板子的吗
yangweiping 发表于 2017-8-14 20:01 | 显示全部楼层
牛X,着实牛X
yiy 发表于 2017-8-14 21:11 | 显示全部楼层
这么多,牛X。
huangcunxiake 发表于 2017-8-14 22:29 | 显示全部楼层
复制一份,留着点我的LCD
huangcunxiake 发表于 2017-8-14 22:30 | 显示全部楼层
楼主,工程文件打包发一下不,学习学习你的代码。
21mengnan 发表于 2017-8-14 22:52 | 显示全部楼层
漂亮,土豪一枚。
 楼主| abc9981 发表于 2017-8-15 08:59 | 显示全部楼层
huangcunxiake 发表于 2017-8-14 22:30
楼主,工程文件打包发一下不,学习学习你的代码。

主要代码都贴出来了,其他的就是新唐的例程了,我还是把新唐的资料也发出来吧,免得你们还要去找
 楼主| abc9981 发表于 2017-8-15 09:01 | 显示全部楼层
21mengnan 发表于 2017-8-14 22:52
漂亮,土豪一枚。

板子新唐送的,LCD都是以前留下的,真的要算成本的花10元不到吧
zhuotuzi 发表于 2017-8-19 18:17 | 显示全部楼层
不错,这活动一出,好多值得我们学习的分享。
zhuotuzi 发表于 2017-8-19 18:17 | 显示全部楼层
都是货真价实的经验分享,学习学习。。
wanduzi 发表于 2017-8-19 21:56 | 显示全部楼层
好羡慕各位有板子的。
643757107 发表于 2017-8-20 16:20 | 显示全部楼层
这板子给力,之前我也有一块。
643757107 发表于 2017-8-20 16:20 | 显示全部楼层
051和451学习新唐单片机的最佳选择。
mintspring 发表于 2017-8-27 16:04 | 显示全部楼层
代码不错,拿走用用。
huahuagg 发表于 2017-8-27 18:51 | 显示全部楼层
PC->DOUT 这个可以放头文件H里面,统一一下比较好。
dongnanxibei 发表于 2017-8-27 21:10 | 显示全部楼层
这板子看着真漂亮。
allen969 发表于 2017-8-28 17:19 | 显示全部楼层
软件技术可以哦,我公司有新唐的软件案子外包,有兴趣加 QQ 872918265    SL哦
tanik 发表于 2017-10-5 22:30 | 显示全部楼层
货真价实的经验分享,学习学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

295

帖子

2

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