1.硬件2.程序#define IO1 0x0600 #define IO2 0x0400 #define IO3 0x0600 #define ADR_244 IO3 //LCD Registers addresses #define LCD_CMD_WR (IO10x00) #define LCD_DATA_WR (IO10x02) #define LCD_BUSY_RD (IO10x04) #define LCD_DATA_RD (IO10x06) /* 可编程定时/计数器接口8253A */ #define ADR_TIMER_CONTROL (IO2 0x06) #define ADR_TIMER_DATA0 (IO2 0x00) #define ADR_TIMER_DATA1 (IO2 0x02) #define ADR_TIMER_DATA2 (IO2 0x04) #define TIMER_COUNTER0 0x00 #define TIMER_COUNTER1 0x40 #define TIMER_COUNTER2 0x80 #define TIMER_LATCH 0x00 #define TIMER_LSB 0x10 #define TIMER_MSB 0x20 #define TIMER_LSB_MSB 0x30 #define TIMER_MODE0 0x00 #define TIMER_MODE1 0x02 #define TIMER_MODE2 0x04 #define TIMER_MODE3 0x06 #define TIMER_MODE4 0x08 #define TIMER_MODE5 0x09 #define TIMER_BCD 0x01 unsigned char Str1[] Current Time; unsigned char Str2[]Set ; unsigned char HMS_String[] 12:30:00 ; unsigned char Hour12,Minute30,Second0; unsigned char Settime; unsigned char Change_H_or_M1; void outp(unsigned int addr, char data) // 输出一字节到I/O端口 { __asm { mov dx, addr mov al, data out dx, al } } char inp(unsigned int addr) // 从I/O端口输入一字节 { char result; __asm { mov dx, addr in al, dx mov result, al } return result; } //设置中断失量表 void set_int(unsigned char int_no, void * service_proc) { _asm { push es xor ax, ax mov es, ax mov al, int_no xor ah, ah shl ax, 1 shl ax, 1 mov si, ax mov ax, service_proc mov es:[si], ax inc si inc si mov bx, cs mov es:[si], bx pop es } } //中断处理函数 /* void _interrupt _far nmi_handler(void) { if(Second60) { Second0; if(Minute60) { Minute0; if(Hour24) { Hour0;Minute0;Second0; } } } } void setup_nmi(void) // 在中断失量表添加2号中断失量 { set_int(0x02, (void *)nmi_handler); // 设置8253A定时/计数器 outp(ADR_TIMER_CONTROL, TIMER_COUNTER0 | TIMER_MODE3 | TIMER_LSB_MSB|TIMER_BCD); outp(ADR_TIMER_DATA0, 0x00); outp(ADR_TIMER_DATA0, 0x01); outp(ADR_TIMER_CONTROL, TIMER_COUNTER2 | TIMER_MODE2 | TIMER_LSB_MSB|TIMER_BCD); outp(ADR_TIMER_DATA2, 0x00); outp(ADR_TIMER_DATA2, 0x10); } */ //LCD忙等待 void WaitForEnable( void ) { unsigned char result; do{ resultinp(LCD_BUSY_RD); }while(result0x80); } //LCD写命令 void LcdWriteCommand( unsigned char cmd) { WaitForEnable(); outp(LCD_CMD_WR,cmd); } //LCD写数据 void LcdWriteData( char data ) { WaitForEnable(); outp(LCD_DATA_WR,data); } void LcdReset( void ) { LcdWriteCommand( 0x38); LcdWriteCommand( 0x0c); LcdWriteCommand( 0x06); LcdWriteCommand( 0x01); } void lcd_pos(unsigned char pos) { LcdWriteCommand( pos|0x80); } void lcd_out(unsigned char *row) { unsigned char *p; prow; while(*p!\0) { LcdWriteData(*p); p; } } void delay(unsigned int x) {unsigned char i; while(x--) {for(i0; i120;i){}} } //时分秒转换为字符串 void hms_str(unsigned char h,unsigned char m,unsigned char s) { HMS_String[4]h/100; HMS_String[5]h%100; HMS_String[7]m/100; HMS_String[8]m%100; HMS_String[10]s/100; HMS_String[11]s%100; } //调整时间 void Change_Time() { Settime0; if(inp(ADR_244)!0x0f) { lcd_pos(0x00); lcd_out(Str2); Settime1; } while(Settime) { if(!(inp(ADR_244)0x01)) { while(!(inp(ADR_244)0x01)){} Change_H_or_M!Change_H_or_M; if(!Change_H_or_M) {Str2[4] ;Str2[5] ;Str2[7];Str2[8];} else {Str2[4];Str2[5];Str2[7] ;Str2[8] ;} lcd_pos(0x00);lcd_out(Str2); } if(!(inp(ADR_244)0x02)) { while(!(inp(ADR_244)0x02)){} if(Change_H_or_M1) {if(Hour24)Hour0;} else {if(Minute60)Minute0;} } if(!(inp(ADR_244)0x04)) { while(!(inp(ADR_244)0x04)){} if(Change_H_or_M1) {if(--Hour0xff)Hour23;} else {if(--Minute0xff)Minute59;} } if(!(inp(ADR_244)0x08)) { while(!(inp(ADR_244)0x08)){} lcd_pos(0x00); lcd_out(Str1); Settime0; } hms_str(Hour,Minute,Second); lcd_pos(0x40); lcd_out(HMS_String); } } void main(void) { // setup_nmi(); LcdReset(); lcd_pos(0x00); lcd_out(Str1); lcd_pos(0x40); lcd_out(HMS_String); while(1) { //hms_str(Hour,Minute,Second); lcd_pos(0x40); lcd_out(HMS_String); outp(0x800,0xff); delay(100); // LcdReset(); lcd_pos(0x00); lcd_out(Str1); outp(0x800,0x00); delay(100); outp(0x600,0xff); delay(100); //Change_Time(); } } char end_flag[5]{0x55,0x55,0x55,0x55,0x55};3.测试