51单片机用lm016l显示闹钟的的C语言程序
时间: 2023-12-23 08:05:55 浏览: 77
51单片机编写的闹钟程序.doc
好的,下面是一个简单的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 alarm_hour, alarm_minute;
unsigned char hour, minute;
char str[9];
lcd_init();
alarm_hour = 8; // 设置闹钟小时
alarm_minute = 30; // 设置闹钟分钟
while(1)
{
hour = get_hour(); // 获取当前小时
minute = get_minute(); // 获取当前分钟
sprintf(str, "%02d:%02d", hour, minute); // 格式化时间字符串
lcd_write_string(0, 0, str); // 在第一行显示时间
if(hour == alarm_hour && minute == alarm_minute) // 如果到了闹钟时间
{
lcd_write_string(1, 0, "Alarm!"); // 在第二行显示闹钟提示信息
delay(500); // 延时500毫秒
lcd_write_string(1, 0, " "); // 清除闹钟提示信息
delay(500); // 延时500毫秒
}
delay(1000); // 延时1秒
}
}
```
这个程序与时钟程序相似,不同之处在于多了一个闹钟的判断和提示。需要根据实际需求实现获取当前时间的函数`get_hour()`和`get_minute()`,同时需要设置闹钟的小时和分钟。提示信息可以根据实际情况进行修改。
阅读全文