Created
November 19, 2012 14:43
-
-
Save jacyzon/4111010 to your computer and use it in GitHub Desktop.
8051 exam2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* exam_2.c | |
* | |
* Created: 2012/11/16 下午 16:10:10 | |
* Author: Jason YiZhang Chen | |
*/ | |
#include <reg51.h> | |
//++++++++++++++++++++++++++++++中斷控制+++++++++++++++++++++++++++++++++// | |
#define __INT0_SWITCH__ 0 //INT0 中斷開關 0關 1開 | |
#define __INT0_TRIGGER__ 1 //INT0 觸發方式 0低準位 1負緣 | |
#define __INT0_IP__ 0 //INT0 優先權 0不具優先 1具優先 | |
#define __INT1_SWITCH__ 1 //INT1 中斷開關 0關 1開 | |
#define __INT1_TRIGGER__ 1 //INT1 觸發方式 0低準位 1負緣 | |
#define __INT1_IP__ 0 //INT1 優先權 0不具優先 1具優先 | |
#if __INT0_SWITCH__ == 1 //根據開啟INT0或INT1改變中斷向量代號 | |
#define __INT_VECTOR__ 0 | |
#else | |
#define __INT_VECTOR__ 2 | |
#endif | |
#define __TIMER1_SWITCH__ 1 //TIMER1 中斷開關 0關 1開 | |
#define __TIMER1_IP__ 0 //TIMER1 優先權 0不具優先 1具優先 | |
//++++++++++++++++++++++++++++++七段控制+++++++++++++++++++++++++++++++++// | |
#define SEG0 0 //組別0 | |
#define SEG1 10 //組別A (A=10 ~ F=15 ) | |
#define SEG2 1 //組別1 | |
#define SEG3 1 //組別1 | |
#define __SHIFT__ 0 //移動控制 0:左移 1:右移 | |
#define __INCDEC__ 1 //計數控制 0:上數 1:下數 | |
#define __SECINIT__ 25 //秒數初值(0~59) | |
#define __MININIT__ 10 //分數初值(0~59) | |
//++++++++++++++++++++++++++++++PORT定義+++++++++++++++++++++++++++++++++// | |
#define LED P0 //七段資料線 | |
#define LEDP P1 //七段掃瞄線 | |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// | |
#define T 50000 //timer記數次數 | |
//++++++++++++++++++++++++++++++FUNCTION+++++++++++++++++++++++++++++++++// | |
void init(); | |
void inc_time(); | |
void display(); | |
void set_display(unsigned char, unsigned char, unsigned char, unsigned char); | |
void delay_ms(unsigned int); | |
//++++++++++++++++++++++++++++++VARIABLE+++++++++++++++++++++++++++++++++// | |
unsigned char code Table[] = { ~0x3f, ~0x06, ~0x5b, ~0x4f, | |
~0x66, ~0x6d, ~0x7d, ~0x07, | |
~0x7f, ~0x6f, ~0x77, ~0x7c, | |
~0x39, ~0x5e, ~0x79, ~0x71 }; | |
unsigned char shift = 0, shiftSet = 0; //左移, 左移旗標 | |
unsigned char displayText[4]; | |
char min = __MININIT__, sec = __SECINIT__; | |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// | |
void main() { | |
set_display(Table[min/10], Table[min%10], Table[sec/10], Table[sec%10]); | |
init(); | |
while(1) | |
{ | |
display(); | |
} | |
} | |
void init() { | |
EA = 0; //中斷關閉 | |
P3 = 0xff; | |
/***************IE***************/ | |
EX0 = __INT0_SWITCH__; | |
EX1 = __INT1_SWITCH__; | |
ET1 = __TIMER1_SWITCH__; | |
/***************TCON***************/ | |
IT0 = __INT0_TRIGGER__; | |
IT1 = __INT1_TRIGGER__; | |
/***************IP***************/ | |
PX0 = __INT0_IP__; | |
PX1 = __INT1_IP__; | |
PT1 = __TIMER1_IP__; | |
/***************TIMER1設定***************/ | |
TMOD = 0x10; //TIMER1 16位元計時器 | |
TR1 = 0; | |
TH1 = (65536 - T) / 256; | |
TL1 = (65536 - T) % 256; | |
TR1 = 1; | |
EA = 1; | |
} | |
void timer1() interrupt 3 { | |
static unsigned int i = 0, count = 0, countTime = 0; | |
TR1 = 0; | |
if(++i == 20) { | |
i = 0; | |
inc_time(); | |
if(shiftSet == 0) | |
set_display(Table[min/10], Table[min%10], Table[sec/10], Table[sec%10]); | |
} | |
if(shiftSet == 1) { | |
if(++count == 10) { | |
count = 0; | |
#if __SHIFT__ == 0 | |
++shift; | |
#else | |
--shift; | |
#endif | |
if(++countTime == 20) { | |
countTime = 0; | |
shiftSet = 0; | |
shift = 0; | |
} | |
} | |
} | |
TH1 = (65536 - T) / 256; | |
TL1 = (65536 - T) % 256; | |
TR1 = 1; | |
} | |
void key_press_int() interrupt __INT_VECTOR__ { | |
if(shiftSet == 0) { | |
shiftSet = 1; | |
shift = 0; | |
set_display(Table[SEG0], Table[SEG1], Table[SEG2], Table[SEG3]); | |
} | |
else | |
display(); | |
} | |
void set_display(unsigned char s1, unsigned char s2, unsigned char s3, unsigned char s4) { | |
displayText[0] = s1; displayText[1] = s2; displayText[2] = s3; displayText[3] = s4; | |
} | |
void display() { | |
unsigned char i, j; | |
for(j = 0; j < 250; j++) { | |
for(i = 0; i < 4; i++) { | |
LEDP = ~(0x08 >> i); | |
switch (i) { | |
case 0: LED = displayText[(0+shift)%4]; break; | |
case 1: LED = displayText[(1+shift)%4]; break; | |
case 2: LED = displayText[(2+shift)%4]; break; | |
case 3: LED = displayText[(3+shift)%4]; break; | |
} | |
delay_ms(1); | |
LED = 0xff; | |
} | |
} | |
} | |
void inc_time() { | |
#if __INCDEC__ == 0 | |
if(++sec == 60) { | |
sec = 0; | |
if(++min == 60) { | |
min = __MININIT__; | |
sec = __SECINIT__; | |
} | |
} | |
#else | |
if(--sec == -1) { | |
sec = 59; | |
if(--min == -1) { | |
min = __MININIT__; | |
sec = __SECINIT__; | |
} | |
} | |
#endif | |
} | |
void delay_ms(unsigned int i) { | |
int count; | |
while(i != 0) { | |
count = 120; | |
while(count>0) count--; | |
i--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment