|
LCD1602 時(shí)間顯示
2019-12-08.png (172.25 KB, 下載次數(shù): 80)
下載附件
2019-12-8 18:22 上傳
Proteus
AT89C51單片機(jī)源程序如下: main.c
- /**
- * @file main.c
- * @author Kiyotaka
- * @brief Unix timestamp on LCD1602
- * @version 0.1
- * @date 2019-12-08
- *
- * @copyright Copyright (c) 2019
- *
- */
- #include "main.h"
- #include "lcd1602.h"
- void Delay1000ms(void);
- unsigned long int timestamp = 157579655;
- int main(void) {
- LCD1602_Init();
- LCD1602_Printf(0, 0, "Unix timestamp:");
- while (1) {
- LCD1602_Printf(0, 1, "%ld", timestamp++);
- Delay1000ms();
- }
- }
- void Delay1000ms(void) { //@12.000MHz
- unsigned char i = 8;
- unsigned char j = 154;
- unsigned char k = 122;
- _nop_();
- do {
- do {
- while (--k);
- } while (--j);
- } while (--i);
- }
復(fù)制代碼
lcd1602.c - /**
- * @file lcd1602.c
- * @author Kiyotaka
- * @brief LCD1602 driver
- * @version 0.1
- * @date 2019-12-08
- *
- * @copyright Copyright (c) 2019
- *
- */
- #include "lcd1602.h"
- typedef enum { GPIO_PIN_RESET = 0, GPIO_PIN_SET } GPIO_PinState;
- #define RS_CMD GPIO_PIN_RESET
- #define RS_DATA GPIO_PIN_SET
- /**
- * @brief 延時(shí)函數(shù)
- *
- * @param us 延時(shí)時(shí)間
- * @return None
- */
- static void Delay(int us) {
- while (us-- > 0) {
- _nop_();
- }
- }
- /**
- * @brief 液晶I/O 口復(fù)位
- *
- * @return None
- */
- static void PinReset(void) {
- LCD1602_E_Pin = GPIO_PIN_RESET;
- LCD1602_RS_Pin = GPIO_PIN_RESET;
- LCD1602_RW_Pin = GPIO_PIN_RESET;
- LCD1602_Port = 0xFF;
- }
- /**
- * @brief LCD1602 等待液晶空閑
- *
- *@return None
- */
- static void WaitIdleTime(void) {
- LCD1602_RS_Pin = RS_CMD;
- LCD1602_RW_Pin = GPIO_PIN_SET;
- LCD1602_Port = 0x00;
- LCD1602_E_Pin = GPIO_PIN_SET;
- do {
- Delay(100);
- } while ((LCD1602_Port & 0x80) >> 7);
- PinReset();
- }
- /**
- * @brief LCD1602 寫數(shù)據(jù)/指令函數(shù)
- *
- * @param val 8位數(shù)據(jù)
- * @param is_data true 是數(shù)據(jù), false 是指令
- * @return None
- */
- static void Write(unsigned char val, bit is_data) {
- bit interrupt_stat = EA;
- EA = 0;
- WaitIdleTime();
- LCD1602_RS_Pin = is_data;
- LCD1602_Port = val;
- LCD1602_E_Pin = GPIO_PIN_SET;
- Delay(100);
- PinReset();
- EA = interrupt_stat;
- }
- /**
- * @brief LCD1602 初始化
- *
- * @return None
- */
- void LCD1602_Init(void) {
- PinReset();
- Write(0x38, RS_CMD);
- Write(0x0C, RS_CMD);
- Write(0x06, RS_CMD);
- Write(0x01, RS_CMD);
- }
- /**
- * @brief LCD1602 顯示函數(shù)
- *
- * @param addr_x (0~15) 橫坐標(biāo)
- * @param addr_y (0~1) 縱坐標(biāo)
- * @param fmt 格式化字符串
- * @param ...
- * @return int
- */
- int LCD1602_Printf(int addr_x, int addr_y, const char *fmt, ...) {
- char buf[20] = {0};
- char *p = buf;
- int ret = 0;
- va_list args;
- va_start(args, fmt);
- ret = vsprintf(p, fmt, args);
- va_end(args);
- Write(0x80 + 0x40 * addr_y + addr_x, RS_CMD);
- while (*p != '\0') {
- if (*p == '\f') {
- Write(0x01, RS_CMD);
- } else if (isprint(*p) != 0) {
- Write(*p, RS_DATA);
- }
- p++;
- }
- return ret;
- }
復(fù)制代碼
驅(qū)動(dòng)程序(Keil + Proteus)
lcd1602.zip
(47.8 KB, 下載次數(shù): 24)
2019-12-8 18:20 上傳
點(diǎn)擊文件名下載附件
lcd驅(qū)動(dòng) 下載積分: 黑幣 -5
如果注釋為亂碼,Keil的Encoding設(shè)置成UTF-8
2019-12-08 (2).png (130.69 KB, 下載次數(shù): 57)
下載附件
2019-12-8 18:37 上傳
|
評(píng)分
-
查看全部評(píng)分
|