51单片机用lm016l显示时钟的C语言程序
时间: 2023-10-05 20:11:30 浏览: 142
lM75 温度传感器
5星 · 资源好评率100%
好的,下面是一个简单的51单片机用LM016L显示时钟的C语言程序,仅供参考:
```
#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_EN P2_2
#define LCD_DATA P1
void delay(unsigned int ms)
{
unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 110; j++);
}
void lcd_init()
{
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
delay(15);
LCD_DATA = 0x38;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
delay(5);
LCD_DATA = 0x38;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
delay(5);
LCD_DATA = 0x0C;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
delay(5);
LCD_DATA = 0x01;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
delay(5);
}
void lcd_write_cmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_DATA = cmd;
LCD_EN = 1;
delay(1);
LCD_EN = 0;
delay(1);
}
void lcd_write_data(unsigned char dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DATA = dat;
LCD_EN = 1;
delay(1);
LCD_EN = 0;
delay(1);
}
void lcd_set_pos(unsigned char row, unsigned char col)
{
unsigned char pos;
if(row == 0)
pos = 0x80 + col;
else
pos = 0xC0 + col;
lcd_write_cmd(pos);
}
void lcd_write_string(unsigned char row, unsigned char col, unsigned char *str)
{
lcd_set_pos(row, col);
while(*str)
{
lcd_write_data(*str++);
}
}
void main()
{
unsigned char hour, minute, second;
char str[9];
lcd_init();
while(1)
{
hour = get_hour(); // 获取当前小时
minute = get_minute(); // 获取当前分钟
second = get_second(); // 获取当前秒数
sprintf(str, "%02d:%02d:%02d", hour, minute, second); // 格式化时间字符串
lcd_write_string(0, 0, str); // 在第一行显示时间
delay(1000); // 延时1秒
}
}
```
这个程序使用了一个简单的延时函数`delay()`,需要根据实际情况调整延时时间。同时,需要根据实际需求实现获取当前时间的函数`get_hour()`、`get_minute()`和`get_second()`。
阅读全文