Files
8051_Segment_2/Segment_2.c
2018-05-06 01:52:40 +08:00

95 lines
1.6 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(int);
// 宣告七段顯示器函式
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
};
char code POS[6] = {
~0x01, // 0xfe 就是 11111110
~0x02, // 0000 0010
~0x04, // 0000 0100
~0x08, // 0000 1000
~0x10, // 0001 0000
~0x20 // 0010 0000
};
// 主程式
int main(){
int i, j = 0;
// 不讓程式結束的無窮迴圈
while(1) {
Slect_Seg(POS[i]); // 設定哪顆亮
Display_Seg(NUM[j]); // 選數字
delay(1500); // 延時 1.5 秒
i = (i + 1) % 6;
j = (j + 1) % 10;
}
}
void Slect_Seg(unsigned char number) {
// 關閉 D 型正反器(預防措施)
P2_7 = 0;
// 指定哪顆亮
P0 = number;
// 開啟控制哪顆亮的 D 型正反器
P2_7 = 1;
// 延時(吃哪顆亮)
delay(1);
// 關閉 D 型正反器(讓吃進去的那顆固定亮)
P2_7 = 0;
}
void Display_Seg(unsigned char display) {
// 關閉 D 型正反器(預防措施)
P2_6 = 0;
// 指定號碼
P0 = display;
// 開啟控制哪顆亮的 D 型正反器
P2_6 = 1;
// 延時(吃號碼)
delay(1);
// 關閉 D 型正反器(讓吃進去的號碼固定住)
P2_6 = 0;
}
// 延遲
void delay(int time) {
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 120; j++);
}