本帖最后由 abc9981 于 2017-8-16 14:05 编辑
之前参加研讨会新唐送了两块开发板,躺在抽屉里快生根了,这次拿出来耍耍。
一块是nano103s,另一块是M451
nano103s附板是Nu-Link_Me V3.0目前应该是较新的烧录模块,而且还带有串口功能 ,烧录跟串口直接解决了,不用再接一个串口TTL
做过1602的应该都知道,1602是5v的电平驱动,而nano103是3v的驱动查询芯片手册配置兼容引脚控制
根据手册信息,选用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,实在是方便
接下来就是写代码了
程序主要运用的是GPIO的功能,加上串口,跟延时处理,就可以搞定了
主要自己写的就是LCD1602的驱动程序了
LCD1602.H
#ifndef __LCD1602_H__
#define __LCD1602_H__
#include "Nano103.h"
#define uchar unsigned char
#define uint unsigned int
#define ushort unsigned short
#define rs PB9
#define rw PB10
#define ep PB11
void lcd_init(void);
void lcd_pos(uchar x,uchar y);
void wnchar(uchar weizhi,uchar zifu[]);
#endif
LCD1602.C
#include "Lcd1602.h"
void delay4us()
{
CLK_SysTickDelay(20);
}
/**********忙测试**********/
//uchar Busy_Check()
//{
// uchar LCD_Status;
// rs=0;
// rw=1;
// ep=1;
// delay4us();
// LCD_Status=P0&0x80;
// ep=0;
// return LCD_Status;
//}
/*************写指令************/
void lcd_wcmd(uchar cmd)
{
// while(Busy_Check());
uint data;
data = (uint)(cmd & 0xf0)<<4;
data += (cmd &0x0f);
rs=0;
rw=0;
ep=0;
delay4us();
PC->DOUT = data;
delay4us();
ep=1;
delay4us();
ep=0;
}
/************指定位置******************/
void lcd_pos(uchar x,uchar y) //设置液晶显示位置,x=0为第一行,1为第2行
{
uchar pos;
if(x == 0)
x = 0x80;
else if(x == 1)
x = 0x80 + 0x40;
pos = x + y;
lcd_wcmd(pos);
}
/**************写数据******************/
void lcd_wdat(uchar dat)
{
// while(Busy_Check());
uint data;
data = (uint)(dat & 0xf0)<<4;
data += (dat &0x0f);
rs=1;
rw=0;
ep=0;
PC->DOUT = data;
delay4us();
ep=1;
delay4us();
ep=0;
}
/************写一串字符**********************/
void wnchar(uchar weizhi,uchar zifu[])//写N个字符
{
uchar i;
lcd_pos(weizhi,0);
for(i=0; ;i++)
{
lcd_wdat(zifu[i]);
if(zifu[i+1]=='\0')
break;
}
}
/**********初始化***************/
void lcd_init()
{
lcd_wcmd(0x38);
delay4us();
lcd_wcmd(0x0c);
delay4us();
lcd_wcmd(0x06);
delay4us();
lcd_wcmd(0x01);
delay4us();
}
main.c
/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 4 $
* $Date: 15/12/27 1:12p $
* [url=home.php?mod=space&uid=247401]@brief[/url] A project template for Nano103 MCU.
*
* @note
* Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "Nano103.h"
#include "Lcd1602.h"
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable External XTAL (4~24 MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Waiting for 12MHz clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Switch HCLK clock source to HXT */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT,CLK_HCLK_CLK_DIVIDER(1));
/* Enable UART clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Select UART clock source from HXT */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_UART0_CLK_DIVIDER(1));
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
SystemCoreClockUpdate();
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk);
SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB0MFP_UART0_RXD | SYS_GPB_MFPL_PB1MFP_UART0_TXD);
/* Lock protected registers */
SYS_LockReg();
}
/*
* This is a template project for Nano103 series MCU. Users could based on this project to create their
* own application without worry about the IAR/Keil project settings.
*
* This template application uses external crystal as HCLK source and configures UART0 to print out
* "Hello World", users may need to do extra system configuration based on their system design.
*/
int main()
{
uchar data[]="Hello,21ic.com!";
uchar data1[]="MyID:abc9981";
SYS_Init();
/* Init UART to 115200-8n1 for print message */
UART_Open(UART0, 115200);
/* Connect UART to PC, and open a terminal tool to receive following message */
printf("八月,新唐你好\r\n");
printf("Hello,21ic.com!\r\n");
/* Got no where to go, just loop forever */
GPIO_SetMode(PA, BIT14, GPIO_PMD_OUTPUT);
GPIO_SetMode(PB, BIT9|BIT10|BIT11, GPIO_PMD_OUTPUT);
GPIO_SetMode(PC, 0x00000f0f, GPIO_PMD_OUTPUT);
printf("LCD1602 INIT!\r\n");
lcd_init();
CLK_SysTickDelay(1000000);
printf("Display 'Hello,21ic.com!'\r\n");
wnchar(0,data) ;
printf("Display 'MyID:abc9981'\r\n");
wnchar(1,data1) ;
printf("MyID:abc9981\r\n");
while(1)
{
PA->DOUT = 0x00; /* Output low */
CLK_SysTickDelay(1000000);
PA->DOUT = 0x4000; /* Output low */
CLK_SysTickDelay(1000000);
}
}
代码搞定,现在就是成果展示了
串口数据
LCD显示
LCD1602通常都是在学校的时候用的,这次又一次拿出了1602感觉好像又回到了读书的时候,真的很怀念
现在大学生电子设计大赛正在进行,祝所有参赛选手能获得好成绩!
附件太大了,可以前去官网下载nano103s相关资料:http://www.nuvoton.com.cn/hq/products/microcontrollers/arm-cortex-m0-mcus/nano103-base-series/?__locale=zh
|