Files
8051_Interrupt_1/Interrupt_1.c
2018-05-13 18:27:58 +08:00

141 lines
2.2 KiB
C

// Timer 0 take as interrupt
// Timer 1 take as delay
#include <reg51.h>
// 宣告延時函式
void delay_1500ms(void);
unsigned int timer0_counter = 150;
bit lightoff = 1;
bit lighton = 0;
sbit P1_0 = P1^0;
sbit P1_1 = P1^1;
sbit P1_2 = P1^2;
sbit P1_3 = P1^3;
sbit P1_4 = P1^4;
sbit P1_5 = P1^5;
sbit P1_6 = P1^6;
sbit P1_7 = P1^7;
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; //開啟中斷模組總開關
while(1){
// do LED move up to down per 1.5 sec
// using Timer 1 Mode 1 to generate 10ms delay
P1 = 0xFF;
while (1) {
delay_1500ms();
P1_0 = lighton;
delay_1500ms();
P1_1 = lighton;
delay_1500ms();
P1_2 = lighton;
delay_1500ms();
P1_3 = lighton;
delay_1500ms();
P1_4 = lighton;
delay_1500ms();
P1_5 = lighton;
delay_1500ms();
P1_6 = lighton;
delay_1500ms();
P1_7 = lighton;
delay_1500ms();
P1_7 = lightoff;
delay_1500ms();
P1_6 = lightoff;
delay_1500ms();
P1_5 = lightoff;
delay_1500ms();
P1_4 = lightoff;
delay_1500ms();
P1_3 = lightoff;
delay_1500ms();
P1_2 = lightoff;
delay_1500ms();
P1_1 = lightoff;
delay_1500ms();
P1_0 = lightoff;
}
}
}
void delay_1500ms(void){
int time = 0;
while(time < 150) {
//設定初始值
TF1 = 0;
TR1 = 0;
TL1 = (65536-9216) % 256;
TH1 = (65536-9216) / 256;
//開啟計時器 timer1
TR1 = 1;
//當 TF1 沒有溢位
while(TF1 == 0);
//關閉計時器
TR1 = 0;
//將 TF1 歸零
TF1 = 0;
time++;
}
}
// 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;
}
}