下面是程序的全部,在project向导中仅仅选用软件仿真。
#include /* for EnableInterrupts macro */
#include /* include peripheral declarations */
typedef unsigned char UCHAR;
typedef unsigned int UINT;
#define LED1 PTB_PTB7
#define LED2 PTB_PTB6
#define LED3 PTB_PTB5
#define RESET PTB_PTB4
#define SHIFT PTB_PTB3
#define LATCH PTB_PTB2
#define A PTB_PTB0
UCHAR near ADResult, ADCEnd = 0;
UCHAR near LED1Buf = 0,LED2Buf = 0,LED3Buf = 0;
void interrupt 16 ADC(void){
ADCEnd = 1;
ADResult = ADR;
}
void ShowLED(UCHAR LEDBuf, UCHAR LEDNum){
/***************************
-- -- --
| | | | | |
-- -- --
| | | | | |
-- -- . --
1 2 3
****************************/
UCHAR near LEDTemp, i, ConverValue;
RESET = 0;
SHIFT = 0;
LATCH = 0;
RESET = 1;
switch (LEDBuf) {
case 0:
ConverValue = 0xFC;
break;
case 1:
ConverValue = 0x60;
break;
case 2:
ConverValue = 0xDA;
break;
case 3:
ConverValue = 0xF2;
break;
case 4:
ConverValue = 0x66;
break;
case 5:
ConverValue = 0xB6;
break;
case 6:
ConverValue = 0xBE;
break;
case 7:
ConverValue = 0xE0;
break;
case 8:
ConverValue = 0xFE;
break;
case 9:
ConverValue = 0xF6;
break;
}
if(LEDNum == 2){
ConverValue |= 0x01;
}
for(i = 0; i < 8; i++){
SHIFT = 0;
LEDTemp = 0x7F | ConverValue;
LEDBuf <<= 1;
if(LEDTemp == 0xFF){
A = 1;
}else{
A = 0;
}
SHIFT = 1;
}
LATCH = 1;
if(LEDNum == 1){
LED1 = 0;
LED2 = 1;
LED3 = 1;
}else if(LEDNum == 2){
LED1 = 1;
LED2 = 0;
LED3 = 1;
}else{
LED1 = 1;
LED2 = 1;
LED3 = 0;
}
}
void ShowConvert(void){
volatile unsigned int near TrueValue ;
TrueValue =(UINT) (ADResult * 314.7) / 255;
TrueValue = TrueValue ;
LED1Buf = (UCHAR)(TrueValue/100);
LED2Buf = (UCHAR)((TrueValue - LED1Buf * 100)/10);
LED3Buf = (UCHAR)(TrueValue - LED1Buf * 100 - LED2Buf *10);
}
void Wait6ms(void){
UCHAR near Temp;
TMODH = 0x2;
TMODL = 0x58;
TSC = 0x05;
do{
__asm nop;
}while(TSC_TOF == 0);
Temp = TSC;
TSC_TOF = 0;
TSC_TSTOP = 1;
}
void main(void) {
UCHAR near i;
EnableInterrupts; /* enable interrupts */
CONFIG2 = 0x00;
CONFIG1 = 0x38;
DDRB = 0xFF;
DDRA = 0x18;
ADICLK = 0x40;
//ADSCR = 0x41;
PTA_PTA4 = 1;
RESET = 0;
LED1 = 1;
LED2 = 1;
LED3 = 1;
/* include your code here */
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
ADSCR = 0x41;
do{
__asm nop;
}while(ADCEnd == 0);
ADCEnd = 0;
ShowConvert();
for(i = 0; i < 50; i++){
ShowLED(LED1Buf,1);
__RESET_WATCHDOG();
Wait6ms();
ShowLED(LED2Buf,2);
__RESET_WATCHDOG();
Wait6ms();
ShowLED(LED3Buf,3);
__RESET_WATCHDOG();
Wait6ms();
}
} /* loop forever */
/* please make sure that you never leave this function */
} |