Created
November 19, 2012 18:38
-
-
Save jacyzon/4112706 to your computer and use it in GitHub Desktop.
8051 exam4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* exam_4.c | |
* | |
* Created: 2012/11/20 下午 01:55:10 | |
* Author: Jason YiZhang Chen | |
*/ | |
#include <reg51.h> | |
#define Data P0 | |
sbit RS=0x96; //P10 LCD指令/資料控制, | |
sbit RW=0x91; //P11 LCD讀取/寫入控制, | |
sbit EN=0x97; //P12 LCD致能輸出 | |
//++++++++++++++++++++++++++++++位移控制+++++++++++++++++++++++++++++++++// | |
#define __MODEL1__ 1 //0不動 1右移 2左移 | |
#define __MODEL2__ 2 //0不動 1右移 2左移 | |
//++++++++++++++++++++++++++++++中斷控制+++++++++++++++++++++++++++++++++// | |
#define __TIMER1_SWITCH__ 1 //TIMER1 中斷開關 0關 1開 | |
#define __TIMER1_IP__ 0 //TIMER1 優先權 0不具優先 1具優先 | |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// | |
#define T 50000 //timer記數次數 | |
//++++++++++++++++++++++++++++++FUNCTION+++++++++++++++++++++++++++++++++// | |
void delay_ms(unsigned int i); | |
void LCD_Data(char); | |
void LCD_Cmd(unsigned char); | |
void LCD_init(); | |
void update_LCM(); | |
void dsplay_LCM(); | |
void init(); | |
void inc_time(); | |
char code Table[]={ | |
0x10,0x1f,0x02,0x0f,0x0a,0xff,0x02,0x00, //年 | |
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x13,0x00, //月 | |
0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00 }; //日 | |
char min = 0, sec = 0, hr = 0; | |
char msg1[] = "2012\00011\00116\00200:00"; | |
char msg2[] = "100360318 "; | |
unsigned char shift1 = 0, shift2 = 0; | |
void main() { | |
init(); | |
dsplay_LCM(); | |
while(1); | |
} | |
void timer1() interrupt 3 { | |
static unsigned int i = 0, count = 0; | |
TR1 = 0; | |
if(++i == 20) { | |
i = 0; | |
inc_time(); | |
} | |
if(++count == 3) { | |
count = 0; | |
#if __MODEL1__ == 1 | |
shift1--; | |
#elif __MODEL1__ == 2 | |
shift1++; | |
#endif | |
#if __MODEL2__ == 1 | |
shift2--; | |
#elif __MODEL2__ == 2 | |
shift2++; | |
#endif | |
update_LCM(); | |
} | |
TH1 = (65536 - T) / 256; | |
TL1 = (65536 - T) % 256; | |
TR1 = 1; | |
} | |
void inc_time() { | |
if(++sec == 60) { | |
sec = 0; | |
if(++min == 60) { | |
min = 0; | |
if(++hr == 24) { | |
hr = 0; | |
} | |
} | |
} | |
msg1[11] = hr/10 + 0x30; | |
msg1[12] = hr%10 + 0x30; | |
msg1[14] = min/10 + 0x30; | |
msg1[15] = min%10 + 0x30; | |
} | |
void init() { | |
EA = 0; //中斷關閉 | |
/***************IE***************/ | |
ET1 = __TIMER1_SWITCH__; | |
/***************IP***************/ | |
PT1 = __TIMER1_IP__; | |
/***************TIMER1設定***************/ | |
TMOD = 0x10; //TIMER1 16位元計時器 | |
TR1 = 0; | |
TH1 = (65536 - T) / 256; | |
TL1 = (65536 - T) % 256; | |
TR1 = 1; | |
EA = 1; | |
} | |
void update_LCM() { | |
unsigned char i; | |
LCD_Cmd(0x80); //游標由第一行開始顯示 | |
for(i = 0; i < 16; i++) | |
LCD_Data(msg1[(i+shift1)%16]); | |
LCD_Cmd(0xC0); //游標由第二行開始顯示 | |
for(i = 0; i < 16; i++) | |
LCD_Data(msg2[(i+shift2)%16]); | |
} | |
void dsplay_LCM() { | |
unsigned char i; | |
LCD_init(); //重置及清除LCD | |
for(i=0;i<24;i++) { //寫入年月日字型 | |
LCD_Cmd(0x40+i); //指定CGRAM位址 | |
LCD_Data(Table[i]); //寫入CGRAM資料 | |
} | |
update_LCM(); | |
} | |
void delay_ms(unsigned int i) { | |
int count; | |
while(i != 0) { | |
count = 120; | |
while(count>0) count--; | |
i--; | |
} | |
} | |
/********************************************** | |
*函數名稱: LCD_Data | |
*功能描述: 傳送資料到文字型LCD | |
*輸入參數:dat | |
**********************************************/ | |
void LCD_Data(char dat){ //傳送資料到LCD | |
Data=dat; //資料送到BUS | |
RS=1;RW=0;EN=1; //資料寫入到LCD內 | |
delay_ms(1); //LCD等待寫入完成 | |
EN=0; //禁能LCD | |
} | |
/********************************************** | |
*函數名稱: LCD_Cmd | |
*功能描述: 傳送命令到文字型LCD | |
*輸入參數:Cmd | |
**********************************************/ | |
void LCD_Cmd(unsigned char Cmd) { //傳送命令到LCD | |
Data=Cmd; //命令送到BUS | |
RS=0;RW=0;EN=1; //命令寫入到LCD內 | |
delay_ms(1); //LCD等待寫入完成 | |
EN=0; //禁能LCD | |
} | |
/********************************************** | |
*功能描述: 啟始化文字型LCD | |
**********************************************/ | |
void LCD_init(void) { //LCD的啟始程式 | |
LCD_Cmd(0x38); | |
delay_ms(1); | |
LCD_Cmd(0x38); | |
delay_ms(1); | |
/*0011 1000,8bit傳輸,顯示2行,5*7字型 | |
bit4:DL=1,8bit傳輸, | |
bit3:N=1,顯示2行 | |
bit2:F=0,5*7字型*/ | |
LCD_Cmd(0x0c); | |
delay_ms(1); /*bit2:D=1,顯示幕ON | |
bit1:C=0,不顯示游標 | |
bit0:B=0,游標不閃爍*/ | |
LCD_Cmd(0x06); | |
delay_ms(1); /*bit1:I/D=1,顯示完游標右移, | |
bit0:S=0,游標移位禁能*/ | |
LCD_Cmd(0x01); | |
delay_ms(1); //清除顯示幕 | |
LCD_Cmd(0x02); | |
delay_ms(1); //游標回原位 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment