?? lcd_1602.c
字號:
/* DS12C887時鐘C語言編程 作者:賴楚君 時間:從2009-5-12至2009-
程序流程說明
一、LCD_1602驅動程序
1、1ms延時函數
2、讀LCD函數
3、檢測忙碌標志位函數
4、寫LCD函數
5、字符顯示定位函數
6、輸出并顯示字符函數
7、初始化LCD函數
二、DS12C887驅動程序
1、地址替換函數
2、讀DS12C887函數
3、寫DS12C887函數
4、初始化DS12C887函數
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
/* 一、LCD_1602驅動程序
端口引腳定義 */
sfr LCD1602_DATA_PORT = 0x80;//P0口
sbit RS = P2^5;//命令/數據選擇端(H/L)
sbit RW = P2^6;//讀/寫選擇端(H/L)
sbit EN = P2^7;//下降沿觸發
/*1ms延時函數*/
void Delay_ms(uint i)
{
uint j;
for(;i>0;i--)
for(j=125;j>0;j++);
}
/*讀LCD函數*/
uchar Read_LCD(bit Style)
{
uchar Port;
RS = Style;
RW = 1;//讀時高電平有效
EN = 1;
Port = LCD1602_DATA_PORT;
EN = 0;
RS = ~Style;
RW = 0;
return Port;
}
/*檢測忙碌標志位函數*/
void Check_BusyFlag()
{
uint Retry;
for(Retry=1000;Retry>0;Retry--)
{
if(Read_LCD(0)&0x80==0) break;//BusyFlag=0表示不忙碌
}
}
/*寫LCD函數*/
#define LCD_Command 0 //寫指令宏定義
#define LCD_Data 1 //寫數據宏定義
void Write_LCD(bit Style,uchar Data )
{
Check_BusyFlag();//寫LCD前要先檢測忙碌標志位,而讀LCD前不用檢測該位
RS = Style;
RW = 0;//寫時低電平有效
EN = 1;
LCD1602_DATA_PORT = Data;
EN = 0;
RS = ~Style;
RW = 1;
}
/*字符顯示定位函數*/
void Goto_XY(uchar X,uchar Y)
{
if(Y==0) Write_LCD(LCD_Command,0x80|X);//第一行顯示
if(Y==1) Write_LCD(LCD_Command,0xC0|X);//第二行顯示
}
/*輸出并顯示字符函數*/
void Output_String(uchar *Str)
{
while(*Str!='\0')//不為空字符串時輸出
{
Write_LCD(LCD_Data,*Str);
Str++;
//Delay_ms(1);//加延時可以實現打字效果
}
}
/*初始化LCD函數*/
void Init_LCD()
{
Write_LCD(LCD_Command,0x38);//寫指令0x38h(16x2顯示,5x7點陣,8位數據接口)
Write_LCD(LCD_Command,0x38);
Write_LCD(LCD_Command,0x08);//關閉顯示
Write_LCD(LCD_Command,0x01);//清屏
Write_LCD(LCD_Command,0x0C);//開啟顯示且顯示光標
}
/* 二、DS12C887驅動程序
端口引腳定義(因特爾模式)
sfr DS12C887_DATA_PORT = 0x90;//P1口
sbit ALE = P2^4;//AS Pin
sbit RD_ = P2^3;//DS Pin
sbit WR_ = P2^2;//R/W pin
sbit CS = P2^1;//CS Pin
地址替換函數
因為DS12C887的基地址為7F00H
uint Replay_Address(uchar Address)
{
uint Replay;
Replay = 0x7F00 + Address;
return Replay;
}
讀DS12C887函數
uchar Read_DS12C887(uint Address)
{
uchar Port;
ALE = 1;
RD_ = 1;
WR_ = 1;
CS = 0;
DS12C887_DATA_PORT = Address;//讀取地址
ALE = 0;
RD_ = 0;
Port = DS12C887_DATA_PORT;//讀取數據
RD_ = 1;
CS = 1;
ALE = 1;
return Port;
}
寫DS12C887函數
void Write_DS12C887(uint Address,uchar Data)
{
ALE = 1;
RD_ = 1;
WR_ = 1;
CS = 0;
DS12C887_DATA_PORT = Address;
ALE = 0;
WR_ = 0;
DS12C887_DATA_PORT = Data;
WR_ = 1;
CS = 1;
ALE = 1;
}
初始化DS12C887函數
void Init_DS12C887()
{
Write_DS12C887(Replay_Address(0x0A),0x20);//對寄存器A進行設置:打開振蕩器并使RTC計時;SWQ禁止
Write_DS12C887(Replay_Address(0x0B),0x06);//對寄存器B進行設置:時鐘、日歷格式為二進制;24小時模式
//Write_DS12C887(Replay_Address(0x0C),0x06);//對寄存器C進行設置:
if(Read_DS12C887(Replay_Address(0x0D)) == 0)//讀寄存器D-bit7,如為0則DS12C887內部鋰電池電能耗盡,并在LCD_1602顯示"Warning:Battery Few"
{
Init_LCD();
Goto_XY(4,0);
Output_String("Warning:");
Goto_XY(2,1);
Output_String("Battery Few");
while(1);
}
}
時間處理函數
DS12C887時間地址宏定義
#define Second 0x00//秒
#define Second_Alarm 0x01//秒鬧鐘
#define Minute 0x02//分
#define Minute_Alarm 0x03//分鬧鐘
#define Hour 0x04//時
#define Hour_Alarm 0x05//時鬧鐘
#define Week 0x06//星期
#define Data 0x07//日
#define Month 0x08//月
#define Year 0x09//年
#define Century 0x32//世紀
void Time_Process()*/
/*DS12C887+LCD_1602時鐘主函數*/
void main()
{
Init_LCD();
Goto_XY(0,0);
Output_String("2009-05-14");
Goto_XY(0,1);
Output_String("09:05:20");
while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -