Files
8051_TimerDelay_1/TimerDelay_1.c
2018-05-14 01:36:01 +08:00

137 lines
2.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//同一顆七節碼顯示 0 - 9每個號碼停留 1.5 秒
#include <reg51.h>
// 宣告 D 型正反器街接腳
sbit P2_6 = P2^6;
sbit P2_7 = P2^7;
// 宣告延時函式
void delay_1ms();
void delay_1500ms();
// 宣告七段顯示器函式
void Slect_Seg(unsigned char number);
void Display_Seg(unsigned char display);
//宣告數字的表示
char code NUM[10] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
// 主程式
int main(){
// 設定哪顆亮
Slect_Seg(~0x01); // 0xfe 就是 11111110
// 不讓程式結束的無窮迴圈
while(1) {
// 選數字從 0 - 9
int i;
for(i = 0; i < 10; i++) {
Display_Seg(NUM[i]);
delay_1500ms(); // 延時 1.5 秒
}
}
}
void Slect_Seg(unsigned char number) {
// 關閉 D 型正反器(預防措施)
P2_7 = 0;
// 指定哪顆亮
P0 = number;
// 開啟控制哪顆亮的 D 型正反器
P2_7 = 1;
// 延時(吃哪顆亮)
delay_1ms();
// 關閉 D 型正反器(讓吃進去的那顆固定亮)
P2_7 = 0;
}
void Display_Seg(unsigned char display) {
// 關閉 D 型正反器(預防措施)
P2_6 = 0;
// 指定號碼
P0 = display;
// 開啟控制哪顆亮的 D 型正反器
P2_6 = 1;
// 延時(吃號碼)
delay_1ms();
// 關閉 D 型正反器(讓吃進去的號碼固定住)
P2_6 = 0;
}
// 延遲 1ms
void delay_1ms(void) {
//設定為 mode1
TMOD = 0x10;
//設定初始值
TF1 = 0;
TR1 = 0;
TL1 = (65536 - 921) % 256;
TH1 = (65536 - 921) / 256;
//開啟計時器
TR1 = 1;
//當 TF1 沒有溢位
while(TF1 == 0);
//關閉計時器
TR1 = 0;
//將 TF1 歸零
TF1 = 0;
}
// 延遲 1500ms
void delay_1500ms(void) {
int k = 0;
//設定為 mode1
TMOD = 0x10;
while(k < 150) {
//設定初始值
TF1 = 0;
TR1 = 0;
TL1 = (65536 - 9216) % 256;
TH1 = (65536 - 9216) / 256;
//開啟計時器
TR1 = 1;
//當 TF1 沒有溢位
while(TF1 == 0);
k = k + 1;
}
//關閉計時器
TR1 = 0;
//將 TF1 歸零
TF1 = 0;
}