I would like to ask a question about the programming of DS1302. I brought another control module in the program of DS1302.Here the timer2 is setted to interrupt,but it can’t response after brought the timer t0 interruptions. Then as a result: the t0 can’t be controlled when interrupting.How to solve the problems?
The datasheet of DS1302: http://www.kynix.com/uploadfiles/pdf0125/DS1302N.pdf
program:
Code: Select all
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar dis_time_buf[16]={0};
sbit pwm_v=P0^7;//Horizontal signal
sbit pwm_h=P1^7;//Perpendicular to the direction of the signal
uchar sev;
uchar seh;
uchar count;//t0 the time of interruption
//LCD pin definition
sbit RS = P1^0; // define the port
sbit RW = P1^1;
sbit EN = P2^5;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
//DS1302 pin definition
sbit RST=P2^0;
sbit IO=P2^1;
sbit SCK=P2^2;
//DS1302 address definition
#define ds1302_sec_add 0x80 //seconds data address
#define ds1302_min_add 0x82 //seconds data address
#define ds1302_hr_add 0x84 //times data address
#define ds1302_date_add 0x86 //days data address
#define ds1302_month_add 0x88 //months data address
#define ds1302_day_add 0x8a //weeks data address
#define ds1302_year_add 0x8c //years data address
#define ds1302_control_add 0x8e //controlling data address
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
//Initial time definition
uchar time_buf[8] ={0x20,0x17,0x04,0x08,0x21,0x38,0x55,0x06};//
/*------------------------------------------------
uSdelay function,include input value unsigned char t,not returned value
unsigned char is the variable of characters,the degrees of value is
0~255 crystal oscillator=12M,delaying
T=tx2+5uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS delay function,include input value unsigned char t,not returned value
unsigned char is the variable of characters,the degrees of value is
0~255 crystal oscillator=12M,
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//delay 1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
//function
bitLCD_Check_Busy(void)
{
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);//0x80;0x00
}
//the writing function of ordering
voidLCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); /wait if busying
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
//the function of writing data
voidLCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy());//wait if busying
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
//clear function
voidLCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
//writing characters function
voidLCD_write_char(unsigned char x,unsigned char y,unsigned char Data)
{
if(y == 0) //set coordinate
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
//LCD initialize function
voidLCD_Init(void)
{
LCD_Write_Com(0x38); /*display the set module*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*close shows*/
LCD_Write_Com(0x01); /*display clear*/
LCD_Write_Com(0x06); /*show the move set*/
DelayMs(5);
LCD_Write_Com(0x0C); /*display opening*/
}
//DS1302initialize function
void ds1302_init(void)
{
RST=0; //RST
SCK=0; //SCK
}
//write the first data in DS1302
void ds1302_write_byte(uchar addr, uchar d)
{
uchari;
RST=1; //start DS1302 line
//write the aim address:addr
addr= addr & 0xFE; //
for(i = 0; i < 8; i ++) {
if(addr & 0x01) {
IO=1;
}
else{
IO=0;
}
SCK=1; //produce time
SCK=0;
addr= addr >> 1;
}
//writing data:d
for(i = 0; i < 8; i ++) {
if(d & 0x01) {
IO=1;
}
else{
IO=0;
}
SCK=1; /produce time
SCK=0;
d= d >> 1;
}
RST=0; //stop DS1302 line
}
void Display(void)
{
LCD_write_char(3,0,dis_time_buf[0]+'0');
LCD_write_char(4,0,dis_time_buf[1]+'0');
LCD_write_char(5,0,dis_time_buf[2]+'0');
LCD_write_char(6,0,dis_time_buf[3]+'0');
LCD_write_char(7,0,'/');
LCD_write_char(8,0,dis_time_buf[4]+'0');
LCD_write_char(9,0,dis_time_buf[5]+'0');
LCD_write_char(10,0,'/');
LCD_write_char(11,0,dis_time_buf[6]+'0');
LCD_write_char(12,0,dis_time_buf[7]+'0');
LCD_write_char(15,0,dis_time_buf[14]+'0');
//show in the second
LCD_write_char(3,1,dis_time_buf[8]+'0');
LCD_write_char(4,1,dis_time_buf[9]+'0');
LCD_write_char(5,1,':');
LCD_write_char(6,1,dis_time_buf[10]+'0');
LCD_write_char(7,1,dis_time_buf[11]+'0');
LCD_write_char(8,1,':');
LCD_write_char(9,1,dis_time_buf[12]+'0');
LCD_write_char(10,1,dis_time_buf[13]+'0');
}
//timer 0 intilization
void t0( )
{
TMOD=0x01;
TH0=0xfe;
TL0=0x33;//0.5ms
EA=1;
ET0=1;
PT0=1;//
TR0=1;
}
//timer 0 interrupt function
void Time0( )interrupt 1
{
TH0=0xfe;
TL0=0x33;//装初值0.5ms
if(count<sev)//
pwm_v=1;
else
pwm_v=0;
if(count<seh)//
pwm_h=1;
else
pwm_h=0;
count=(count+1);
count=count%40;//cycle is 20ms
}
//timer 2 interrupt function
void Timer2() interrupt 5 //定时器2是5号中断
{
static uchar t;
TF2=0;
t++;
if(t==4) //间隔200ms(50ms*4)读取一次时间
{
t=0;
ds1302_read_time(); //the reading time
dis_time_buf[0]=(time_buf[0]>>4); //year
dis_time_buf[1]=(time_buf[0]&0x0f);
dis_time_buf[2]=(time_buf[1]>>4);
dis_time_buf[3]=(time_buf[1]&0x0f);
dis_time_buf[4]=(time_buf[2]>>4); //month
dis_time_buf[5]=(time_buf[2]&0x0f);
dis_time_buf[6]=(time_buf[3]>>4); //day
dis_time_buf[7]=(time_buf[3]&0x0f);
dis_time_buf[14]=(time_buf[7]&0x07); //month
//show in the second
dis_time_buf[8]=(time_buf[4]>>4); //time
dis_time_buf[9]=(time_buf[4]&0x0f);
dis_time_buf[10]=(time_buf[5]>>4); //second
dis_time_buf[11]=(time_buf[5]&0x0f);
dis_time_buf[12]=(time_buf[6]>>4); //second
dis_time_buf[13]=(time_buf[6]&0x0f);
}
}
Wishes~