Files
8051_Segment_1/Segment_1.c
2018-05-06 01:49:17 +08:00

89 lines
1.5 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
};
// 主程式
int main(){
// 設定哪顆亮
Slect_Seg(~0x01); // 0xfe 就是 11111110
// 不讓程式結束的無窮迴圈
while(1) {
// 選數字從 0 - 9
int i;
for(i = 0; i < 10; i++) {
Display_Seg(NUM[i]);
delay(1500); // 延時 1.5 秒
}
}
}
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++);
}