proteus仿真之DS1302+LCD1602顯示試驗
proteus仿真之DS1302+LCD1602顯示試驗
仿真效果圖為:
C語言源程序如下:
/*
51單片機:DS1302+LCD1602 Proteus 仿真程序。
功能:LCD1602時鐘與日期的顯示。
仿真結果:LCD1602顯示設定的時間與日期。
*/
#include
/**********LCD1602接口程序**********/
#defineLCD_PORTP1 //液晶LCD1602數(shù)據
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
char data str1[16]="Date: ";
char data str2[16]="Time: ";
sbit SCK = P3^6; // DS1302時鐘線
sbit SDA = P3^4; // DS1302數(shù)據線
sbit RST = P3^5; // DS1302復位線
//DS1302 復位重定義
#define RST_CLR RST=0//電平置低
#define RST_SET RST=1//電平置高
//DS1302 數(shù)據
#define SDA_CLR SDA=0//電平置低
#define SDA_SET SDA=1//電平置高
#define SDA_RD SDA //電平讀取
//DS1302 時鐘
#define SCK_CLR SCK=0//時鐘信號
#define SCK_SET SCK=1//電平置高
#define DS1302_SEC 0x80//秒數(shù)據地址
#define DS1302_MIN 0x82//分數(shù)據地址
#define DS1302_HOUR 0x84//時數(shù)據地址
#define DS1302_DATE 0x86//日數(shù)據地址
#define DS1302_MON 0x88//月數(shù)據地址
#define DS1302_DAY 0x8a//星期數(shù)據地址
#define DS1302_YEAR 0x8c//年數(shù)據地址
#define DS1302_CTRL 0x8e//控制數(shù)據地址
#define DS1302_CHARGE 0x90//涓流充電
bit ReadRTC_Flag; //讀DS1302標志。1為讀 0為不讀。
unsigned char time_buf1[8] = {40,14,2,16,23,59,50,7};// -年月日時分秒周 2014-02-14 10:59:50 7周
unsigned char time_buf[8] ; // -年月日時分秒周
void DS1302_Init(void);
void DS1302_Write_Byte(unsigned char addr, unsigned char d);
unsigned char DS1302_Read_Byte(unsigned char addr) ;
void DS1302_Read_Time(void);
void DS1302_Write_Time(void);
void InitTIMER0(void);//inital timer0
void Delay_1ms(unsigned char i);
void Delay_10us(unsigned char i);
void Write_Cmd(unsigned char cmd);
void Write_Dat(unsigned char dat);
void Addr_x_y(unsigned char x,bit y);
void Show_Char(unsigned char x,bit y,unsigned char p);
void Show_String(unsigned char x,bit y,char *ptr);
void LCD_Init(void);
void main(void)
{
LCD_Init();
DS1302_Init();
DS1302_Write_Time();
InitTIMER0();
//P2=0xff; //51默認為輸入
while(1)
{
if(ReadRTC_Flag==1)
{
ReadRTC_Flag=0;
DS1302_Read_Time();
}
str1[5]=time_buf1[1]/10 + '0';//年 數(shù)據的轉換,
str1[6]=time_buf1[1]%10 + '0';//因我們采用數(shù)碼管0~9的顯示,將數(shù)據分開
str1[7]=0x2d; //加入"-"
str1[8]=time_buf1[2]/10 + '0';//月
str1[9]=time_buf1[2]%10 + '0';
str1[10]=0x2d;
str1[11]=time_buf1[3]/10 + '0';//日
str1[12]=time_buf1[3]%10 + '0';
str1[13]=0x20;
str1[14]='W';
str1[15]=time_buf1[7] + '0';//周幾
str2[5]=time_buf1[4]/10 + '0';//時 數(shù)據的轉換,
str2[6]=time_buf1[4]%10 + '0';//因我們采用數(shù)碼管0~9的顯示,將數(shù)據分開
str2[7]=0x3a; //加入"-"
str2[8]=time_buf1[5]/10 + '0';//分
str2[9]=time_buf1[5]%10 + '0';
str2[10]=0x3a;
str2[11]=time_buf1[6]/10 + '0';//秒
str2[12]=time_buf1[6]%10 + '0';
Show_String(0,0,str1);
Show_String(0,1,str2);
}
}
/********************************/
void Delay_1ms(unsigned char i) //最小延時1ms
{
unsigned char j;
while(i--)
for(j=0;j<125; j++);
}
void Delay_10us(unsigned char i) //最小延時10us
{
unsigned char j;
while(i--)
for(j=0;j<10; j++);
}
void Write_Cmd(unsigned char cmd) //寫指令
{
Delay_10us(5);
E=0;
RS=0;
RW=0;
LCD_PORT = cmd;
Delay_10us(5); //>40us
E=1;
Delay_1ms(2); //>150us
E=0;
Delay_10us(4); //>25+10us
}
void Write_Dat(unsigned char dat) //寫數(shù)據
{
Delay_10us(5);
E=0;
RS=1;
RW=0;
LCD_PORT = dat;
Delay_10us(5);
E=1;
Delay_10us(5);
E=0;
Delay_10us(4);
}
void Addr_x_y(unsigned char x,bit y) //寫坐標,定位置
{
unsigned char temp=0x80;//默認最高位:D7為1 即以0x80開始。
if(y) //y :0為第一行 1為第二行
{
temp"=0x40;
}
temp|=x;
Write_Cmd(temp);
}
void Show_Char(unsigned char x,bit y,unsigned char p)
//在指定位置顯示一個字符。
{
Addr_x_y(x,y);
Write_Dat(p);
}
void Show_String(unsigned char x,bit y,char *ptr)
{
unsigned char i;
for (i=0;i<16;i++)
Show_Char(x++,y,*(ptr+i));//循環(huán)顯示16個字符
}
void LCD_Init(void) //1602初始化代碼
{
Delay_1ms(1500);
Write_Cmd(0x38);
Delay_1ms(5);
Write_Cmd(0x38);
Delay_1ms(5);
Write_Cmd(0x38);
Delay_1ms(5);
Write_Cmd(0x38);
Write_Cmd(0x08);
Write_Cmd(0x06);
Write_Cmd(0x0c);
Write_Cmd(0x01);
}
/*------------------------------------------------
DS1302初始化
------------------------------------------------*/
void DS1302_Init(void)
{
RST_CLR; //RST腳置低
SCK_CLR; //SCK腳置低
DS1302_Write_Byte(DS1302_SEC,0x00);
}
/*------------------------------------------------
向DS1302寫入一字節(jié)數(shù)據
------------------------------------------------*/
void DS1302_Write_Byte(unsigned char addr, unsigned char dat)
{
unsigned char i;
RST_SET;
addr = addr & 0xFE; //寫地址 最低位為W寫,低電平
for (i = 0; i < 8; i++)
{
if (addr & 0x01)
{
SDA_SET;
}
else
{
SDA_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
//寫入數(shù)據:dat
for (i = 0; i < 8; i ++)
{
if (dat & 0x01)
{
SDA_SET;
}
else
{
SDA_CLR;
}
SCK_SET;
SCK_CLR;
dat = dat >> 1;
}
RST_CLR; //停止DS1302總線
}
/*------------------------------------------------
從DS1302讀出一字節(jié)數(shù)據
------------------------------------------------*/
unsigned char DS1302_Read_Byte(unsigned char addr)
{
unsigned char i;
unsigned char temp;
RST_SET;
addr = addr | 0x01;//最低RD,有效為高電平
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
SDA_SET;
}
else
{
SDA_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
//輸出數(shù)據:temp
for (i = 0; i < 8; i ++)
{
temp = temp >> 1;
if (SDA_RD)
{
temp |= 0x80;
}
else
{
temp &= 0x7F;
}
SCK_SET;
SCK_CLR;
}
RST_CLR; //停止DS1302總線
return temp;
}
/*------------------------------------------------
從DS1302讀出時鐘數(shù)據
------------------------------------------------*/
void DS1302_Read_Time(void)
{
unsigned char i,tmp;
time_buf[1]=DS1302_Read_Byte(DS1302_YEAR);//年
time_buf[2]=DS1302_Read_Byte(DS1302_MON);//月
time_buf[3]=DS1302_Read_Byte(DS1302_DATE);//日
time_buf[4]=DS1302_Read_Byte(DS1302_HOUR);//時
time_buf[5]=DS1302_Read_Byte(DS1302_MIN);//分
time_buf[6]=(DS1302_Read_Byte(DS1302_SEC))&0x7F;//秒
time_buf[7]=DS1302_Read_Byte(DS1302_DAY);//周
for(i=0;i<8;i++)
{ //BCD處理
tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]%16;
time_buf1[i]=time_buf1[i]+tmp*10;
}
}
/*------------------------------------------------
向DS1302寫入時鐘數(shù)據
------------------------------------------------*/
void DS1302_Write_Time(void)
{
unsigned char i,tmp;
for(i=0;i<8;i++)
{ //BCD處理
tmp=time_buf1[i]/10;
time_buf[i]=time_buf1[i]%10;
time_buf[i]=time_buf[i]+tmp*16;
}
DS1302_Write_Byte(DS1302_CTRL,0x00);//關閉寫保護
DS1302_Write_Byte(DS1302_SEC,0x80);//暫停
//DS1302_Write_Byte(DS1302_CHARGE,0xa9);//涓流充電
DS1302_Write_Byte(DS1302_YEAR,time_buf[1]);//年
DS1302_Write_Byte(DS1302_MON,time_buf[2]);//月
DS1302_Write_Byte(DS1302_DATE,time_buf[3]);//日
DS1302_Write_Byte(DS1302_HOUR,time_buf[4]);//時
DS1302_Write_Byte(DS1302_MIN,time_buf[5]);//分
DS1302_Write_Byte(DS1302_SEC,time_buf[6]);//秒
DS1302_Write_Byte(DS1302_DAY,time_buf[7]);//周
DS1302_Write_Byte(DS1302_CTRL,0x80);//打開寫保護
}
/*------------------------------------------------
Timer0 初始化,開中斷,2ms定時
------------------------------------------------*/
void InitTIMER0(void)
{
TMOD|=0x01; //定時器設置 16位
TH0=(65536-2000)/256;//定時時間 2ms
TL0=(65536-2000)%256;
EA=1;
ET0=1;
TR0=1;
}
void tim0_isr(void) interrupt 1 // timer0 中斷
{
static unsigned char num;
TH0=(65536-2000)/256;//重新賦值 2ms
TL0=(65536-2000)%256;
num++;
if(num==50) //大致100ms
{
num=0;
ReadRTC_Flag=1; //讀標志位置1
}
}