76 lines
1.2 KiB
C
Executable File
76 lines
1.2 KiB
C
Executable File
// Timer 0 take as interrupt
|
|
// Timer 1 take as delay
|
|
|
|
#include <reg51.h>
|
|
|
|
// 宣告延時函式
|
|
void delay10ms(void);
|
|
|
|
unsigned int timer0_counter = 150;
|
|
|
|
void main(void){
|
|
|
|
//開啟 Timer0 和 Timer1
|
|
TMOD = 0x11;
|
|
|
|
//interrupt control
|
|
TL0 = (65536 - 9216) % 256;
|
|
TH0 = (65536 - 9216) / 256;
|
|
TF0 = 0;
|
|
TR0 = 1; //開啟計時器 timer0
|
|
ET0 = 1; //開啟 TF0 的中斷模組開關
|
|
EA = 1; //開啟中斷模組總開關
|
|
|
|
delay10ms();
|
|
|
|
while(1){
|
|
// do LED move up to down per 1.5 sec
|
|
// using Timer 1 Mode 1 to generate 10ms delay
|
|
}
|
|
}
|
|
|
|
|
|
void delay10ms(void){
|
|
//設定初始值
|
|
TF1 = 0;
|
|
TR1 = 0;
|
|
TL1 = (65536-9216) % 256;
|
|
TH1 = (65536-9216) / 256;
|
|
|
|
//開啟計時器 timer1
|
|
TR1 = 1;
|
|
|
|
//當 TF1 沒有溢位
|
|
while(TF1 == 0);
|
|
|
|
//關閉計時器
|
|
TR1 = 0;
|
|
|
|
//將 TF1 歸零
|
|
TF1 = 0;
|
|
}
|
|
|
|
|
|
// timer0 在計時到的時候(TF0 = 1)會呼叫的中斷副函式
|
|
void my_timer0(void) interrupt 1 {
|
|
|
|
//設定初始值
|
|
TL0 = (65536 - 9216) % 256;
|
|
TH0 = (65536 - 9216) / 256;
|
|
TF0 = 0;
|
|
|
|
// timer0_counter 一直遞減
|
|
timer0_counter--;
|
|
|
|
|
|
//當 timer0_counter 為 0 時,實作七段顯示器
|
|
if (timer0_counter == 0){
|
|
// 實作七段顯示器
|
|
|
|
|
|
//將 timer0_counter 的值設回 150
|
|
timer0_counter = 150;
|
|
|
|
}
|
|
}
|