#define MAIN_Fosc 22118400L
#include "STC15Fxxxx.H"
sbit P_HC595_SER = P4^0;
sbit P_HC595_RCLK = P5^4;
sbit P_HC595_SRCLK = P4^3;
u8 hour,minute,second;
void DisplayRTC(void);
void RTC(void);
void delay_ms(u8 ms);
void DisableHC595(void);
void Initialize_LCD(void);
void Write_AC(u8 hang,u8 lie);
void Write_DIS_Data(u8 DIS_Data);
void ClearLine(u8 row);
u8 BIN_ASCII(u8 tmp);
void PutString(u8 row, u8 column, u8 *puts);
void WriteChar(u8 row, u8 column, u8 dat);
void main(void)
{
P0M1 = 0; P0M0 = 0; //设置为准双向口
P1M1 = 0; P1M0 = 0; //设置为准双向口
P2M1 = 0; P2M0 = 0; //设置为准双向口
P3M1 = 0; P3M0 = 0; //设置为准双向口
P4M1 = 0; P4M0 = 0; //设置为准双向口
P5M1 = 0; P5M0 = 0; //设置为准双向口
P6M1 = 0; P6M0 = 0; //设置为准双向口
P7M1 = 0; P7M0 = 0; //设置为准双向口
Initialize_LCD();
ClearLine(0);
ClearLine(1);
P2M1 &= ~(1<<4);
P2M0 |= (1<<4);
P24 = 1;
DisableHC595();
PutString(0,0,"---Clock demo---");
hour = 12;
minute = 0;
second = 0;
DisplayRTC();
while(1)
{
delay_ms(250);
delay_ms(250);
delay_ms(250);
delay_ms(250);
RTC();
DisplayRTC();
}
}
void delay_ms(u8 ms)
{
unsigned int i;
do{
i = MAIN_Fosc / 13000;
while(--i) ;
}while(--ms);
}
void DisableHC595(void)
{
u8 i;
P_HC595_SER = 1;
for(i=0; i<20; i++)
{
P_HC595_SRCLK = 1;
P_HC595_SRCLK = 0;
}
P_HC595_RCLK = 1;
P_HC595_RCLK = 0;
P_HC595_RCLK = 1;
P_HC595_RCLK = 0;
}
void DisplayRTC(void)
{
if(hour >= 10) WriteChar(1,4,hour / 10 + '0');
else WriteChar(1,4,' ');
WriteChar(1,5,hour % 10 +'0');
WriteChar(1,6,'-');
WriteChar(1,7,minute / 10+'0');
WriteChar(1,8,minute % 10+'0');
WriteChar(1,9,'-');
WriteChar(1,10,second / 10 +'0');
WriteChar(1,11,second % 10 +'0');
}
void RTC(void)
{
if(++second >= 60)
{
second = 0;
if(++minute >= 60)
{
minute = 0;
if(++hour >= 24) hour = 0;
}
}
}
#define LineLength 16
sfr LCD_BUS = 0x80;
sbit LCD_B7 = LCD_BUS^7;
sbit LCD_B6 = LCD_BUS^6;
sbit LCD_B5 = LCD_BUS^5;
sbit LCD_B4 = LCD_BUS^4;
sbit LCD_B3 = LCD_BUS^3;
sbit LCD_B2 = LCD_BUS^2;
sbit LCD_B1 = LCD_BUS^1;
sbit LCD_B0 = LCD_BUS^0;
sbit LCD_ENA = P2^7;
sbit LCD_RW = P2^6;
sbit LCD_RS = P2^5;
#define C_CLEAR 0x01
#define C_HOME 0x02
#define C_CUR_L 0x04
#define C_RIGHT 0x05
#define C_CUR_R 0x06
#define C_LEFT 0x07
#define C_OFF 0x08
#define C_ON 0x0C
#define C_FLASH 0x0D
#define C_CURSOR 0x0E
#define C_FLASH_ALL 0x0F
#define C_CURSOR_LEFT 0x10
#define C_CURSOR_RIGHT 0x10
#define C_PICTURE_LEFT 0x10
#define C_PICTURE_RIGHT 0x10
#define C_BIT8 0x30
#define C_BIT4 0x20
#define C_L1DOT7 0x30
#define C_L1DOT10 0x34
#define C_L2DOT7 0x38
#define C_4bitL2DOT7 0x28
#define C_CGADDRESS0 0x40
#define C_DDADDRESS0 0x80
#define LCD_DelayNop() NOP(15)
#define LCD_BusData(dat) LCD_BUS = dat
void CheckBusy(void)
{
u16 i;
for(i=0; i<5000; i++) {if(!LCD_B7) break;}
}
void IniSendCMD(u8 cmd)
{
LCD_RW = 0;
LCD_BusData(cmd);
LCD_DelayNop();
LCD_ENA = 1;
LCD_DelayNop();
LCD_ENA = 0;
LCD_BusData(0xff);
}
void Write_CMD(u8 cmd)
{
LCD_RS = 0;
LCD_RW = 1;
LCD_BusData(0xff);
LCD_DelayNop();
LCD_ENA = 1;
CheckBusy();
LCD_ENA = 0;
LCD_RW = 0;
LCD_BusData(cmd);
LCD_DelayNop();
LCD_ENA = 1;
LCD_DelayNop();
LCD_ENA = 0;
LCD_BusData(0xff);
}
void Write_DIS_Data(u8 dat)
{
LCD_RS = 0;
LCD_RW = 1;
LCD_BusData(0xff);
LCD_DelayNop();
LCD_ENA = 1;
CheckBusy();
LCD_ENA = 0;
LCD_RW = 0;
LCD_RS = 1;
LCD_BusData(dat);
LCD_DelayNop();
LCD_ENA = 1;
LCD_DelayNop();
LCD_ENA = 0;
LCD_BusData(0xff);
}
void Initialize_LCD(void)
{
LCD_ENA = 0;
LCD_RS = 0;
LCD_RW = 0;
delay_ms(100);
IniSendCMD(C_BIT8);
delay_ms(10);
Write_CMD(C_L2DOT7);
delay_ms(6);
Write_CMD(C_CLEAR);
Write_CMD(C_CUR_R);
Write_CMD(C_ON);
}
void ClearLine(u8 row)
{
u8 i;
Write_CMD(((row & 1) << 6) | 0x80);
for(i=0; i void WriteChar(u8 row, u8 column, u8 dat) { Write_CMD((((row & 1) << 6) + column) | 0x80); Write_DIS_Data(dat); } void PutString(u8 row, u8 column, u8 *puts) { Write_CMD((((row & 1) << 6) + column) | 0x80); for ( ; *puts != 0; puts++) { Write_DIS_Data(*puts); if(++column >= LineLength) break; } }
相关文章