标题:
单片机驱动LCM1604四行液晶例程
[打印本页]
作者:
IdeaMing
时间:
2021-6-18 14:31
标题:
单片机驱动LCM1604四行液晶例程
51hei图片_20210618143308.jpg
(4.99 MB, 下载次数: 82)
下载附件
2021-6-18 14:33 上传
单片机源程序如下:
/* Module---------------------------------------------------------------------*/
#define _MAIN_MODULE_
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
#define DATAPORT P0
sbit LCD_RS = P1^0;//0指令 1数据
sbit LCD_RW = P1^1;//0写 1读
sbit LCD_EN = P1^2;//使能信号脚
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
code const uint8_t revision_date[12] = __DATE__; //固件生成日期
code const uint8_t revision_time[16] = __TIME__; //固件生成时间
code const uint8_t revision_ver[13]={" Ver:1.0 \r\n"};//版本信息
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
unsigned int time_ms;
unsigned char seg[]={
" LCD1604 Demo "
" By Liming "
" 2021-06-15 "
" PM: --:--:-- "
};
void IO_Init(void)
{
P0M0 = 0x00;P0M1 = 0X00;
P1M0 = 0x07;P1M1 = 0X00;
}
/*******************************************************************************
* @brief None
* @param None
* @retval None
****************************************************************Author:Liming**/
void TIM0_Init(void) //1毫秒@22.1184MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x9A; //设置定时初值
TH0 = 0xA9; //设置定时初值
TF0 = 0; //清除TF0标志
ET0 = 1; //定时器0中断开启
TR0 = 1; //定时器0开始计时
EA = 1;
}
void TIM0_int (void) interrupt 1
{
if(time_ms>0)time_ms--;
if(task1s)task1s--;
}
void Delay(unsigned int t)
{
time_ms = t;
while(time_ms);
}
uint8_t LCD1604_ReadSta(void)
{
uint8_t res;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;Delay(5);
res = DATAPORT;
LCD_EN = 0;
return res;
}
void LCD1604_BusyWait(void)
{
/*当P0^7为低电平时,P0口为空闲状态*/
while((LCD1604_ReadSta()&0x80)==0x80);
Delay(1);
}
void LCD1604_WriteCmd(unsigned char Cmd)
{
LCD1604_BusyWait();
LCD_RS = 0;
LCD_RW = 0;
DATAPORT = Cmd;
LCD_EN = 1;Delay(10);
LCD_EN = 0;
}
void LCD1604_WriteDat(unsigned char Dat)
{
LCD1604_BusyWait();
LCD_RS = 1;
LCD_RW = 0;
DATAPORT = Dat;
LCD_EN = 1;
Delay(10);
LCD_EN = 0;
}
void initial(void){
LCD1604_WriteCmd(0x38);
LCD1604_WriteCmd(0x01);
LCD1604_WriteCmd(0x06);
LCD1604_WriteCmd(0x0c);
}
void main(void){
unsigned char data_index;
unsigned char i;
TIM0_Init(); //SysTick 1ms
initial(); //初始化LCD
while(1){
if(task1s==0)
{
task1s = 1000;sec++;
if(sec>=60){
sec = 0;min++;
if(min>=60){
min = 0;hour++;
if(hour>=12)hour = 0;
}
}
seg[54] = (hour/10)+'0';
seg[55] = (hour%10)+'0';
seg[57] = (min/10)+'0';
seg[58] = (min%10)+'0';
seg[60] = (sec/10)+'0';
seg[61] = (sec%10)+'0';
data_index = 0;
LCD1604_WriteCmd(0x80);//第一行
while(data_index<=15){LCD1604_WriteDat(seg[data_index]);data_index++;}
LCD1604_WriteCmd(0xc0);//第二行
while(data_index<=31){LCD1604_WriteDat(seg[data_index]);data_index++;}
LCD1604_WriteCmd(0x90);//第三行
while(data_index<=47){LCD1604_WriteDat(seg[data_index]);data_index++;}
LCD1604_WriteCmd(0xd0);//第四行
while(data_index<=63){LCD1604_WriteDat(seg[data_index]);data_index++;}
}
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1