我想用ECT的OC中断做一准确电子钟,其间主要用到了display(x,y)显示函数和一个中断函数。
为方便看,特把display函数分成两个单独的分钟显示函数displaymin(x)和小时显示函数displayhour(y)
出现的问题:
如果只显示分钟displaymin(x)准确做到过一分钟数字增加1,但一加上显示小时的功能函数displayhour(y)分钟数字的递加好像就得过1分半钟(我那另一块表作比较)
所用单片机为9s12dg128,总线频率8M
程序如下,希望高手指点问题的出处
#include <hidef.h> /* common defines and macros */ #include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
void interrupt 15 timch7(); void displaymin(volatile unsigned char x); void displayhour(volatile unsigned char y); void delay();
unsigned char a[10]={ 0xc0,0xf3,0xa4,0xa1,0x93,0x89,0x88,0xe3,0x80,0x81 }; volatile int i=0; volatile int j=0; volatile unsigned char min=9; volatile unsigned char hour=23;
void main(void) { /* put your own code here */ TSCR1=0x90; //使能TEN,快速清零标志TFFCA TSCR2=0x0f; //分频系数128,TCRE=1 TC7=125; //2ms中断 TIOS=0x80; //管脚7为OC7 TIE=0x80; //使能中断TI7 DDRA=0xff; EnableInterrupts; for(;;) {} /* wait forever */ }
void interrupt 15 timch7(){ i+=1; if(i==500){ i=0; j+=1; } if(j==60){ j=0; min+=1; } if(min==60){ min=0; hour+=1; } if(hour==24) hour=0; TC7=125; displaymin(min); displayhour(hour); }
void displaymin(volatile unsigned char x){ if(x<10){ PORTA=a[x]; DDRP=0x01; delay(); PORTA=a[0]; DDRP=0x02; delay(); PORTA=0xff; } else{ PORTA=a[x%10]; DDRP=0x01; delay(); PORTA=a[x/10]; DDRP=0x02; delay(); PORTA=0xff; } }
void displayhour(volatile unsigned char y) { if(y<10){ PORTA=a[y]; DDRP=0x04; delay(); PORTA=a[0]; DDRP=0x08; delay(); PORTA=0xff; } else{ PORTA=a[y%10]; DDRP=0x04; delay(); PORTA=a[y/10]; DDRP=0x08; delay(); PORTA=0xff; } } void delay(){ int k; for(k=0;k<1000;k++); }
|