7、将程序烧写到单片机,观察实验现象
五、实验结果:
观察电路显示:
六、实验心得:
这次实验比较容易,在熟悉示例代码后,根据函数库以及变量说明,发现了需要完善的地方。设置中断,设置预分频器后,最终成功运行。这次试验熟悉了中断的使用以及分频器的设定。
经过这次的实验,我个人得到了不少的收获,一方面加深了自己对理论知识的认识,另一方面也提高了自己的实验逻辑思考能力及实验操作能力。
七、源代码如下:
#include
#pragma config WDT = OFF #pragma config OSC = HS #pragma config LVP = OFF
void isr_high(void);
volatile unsigned char Long_Count ; volatile unsigned char Direct_LED ; volatile unsigned char Dir_Count ;
void main(void) { TRISD=0x00;
PORTD=0b00000001;
//******************************************** // Exercise
// Enable Timer2 interrupt with high Priority //
//********************************************
// Set output port for LED driver // Set b0 of LED is On
//(Refer 18fxx2_cn(39564b) of Page 75-84)
RCONbits.IPEN=1; // Enable Interrupt Priority bit
IPR1bits.TMR2IP=1; // Set Timer2 for High Priority INTCONbits. GIEH=1;
// Enable High Priority Interrupt
/***********************************/ /* Interrupt Time
*/
/*(4/16Mhz) [16*10*(199+1)] = 8mS */
/***********************************/
//(Refer MPLAB C18函数库 of Page 50)
OpenTimer2 (TIMER_INT_ON &T2_PS_1_16 & T2_POST_1_10) ; PR2=199;
Long_Count=0; Direct_LED=0;
Dir_Count=0;
while(1);
}
//************************************************ //* #pragma Interrupt Declarations * //*
Function: isr_high_direct * //* Direct execution to the actual * //* high-priority interrupt code. * //************************************************ #pragma code isrhighcode = 0x0008
void isr_high_direct(void) {
_asm
// Turn On the Timer2 with Interrupt //(4/16Mhz) [16*10*(199+1)] = 8mS
// Loop Here!
//begin in-line assembly
goto isr_high _endasm
//go to isr_high function
//end in-line assembly
}
#pragma code
//************************************************ //* Function: isr_high(void) * //* High priority interrupt will * //* Save the received data to buffer Rec_Data * //************************************************ #pragma interrupt isr_high
void isr_high(void) { PIR1bits.TMR2IF=0;
if (Long_Count <= 6) Long_Count++; else {
Long_Count=0;
if (Direct_LED==0x00)
{ PORTD<<=1;
Dir_Count++; if (Dir_Count==7)
{ Dir_Count=0;
Direct_LED=0x1;
}
}
// Clear Timer2 interrupt Flag
// 8mS * 6 = 48mS // Time is 48mS, do the function
// Right or Left shift
// LED left shift
// End of LED position?
// Yes, set flag of right shift else { }
}
}
PORTD>>=1;
// LED right shift
Dir_Count++; if (Dir_Count==7) { Dir_Count=0; Direct_LED=0x00;
}
相关推荐: