/******************************************************************** * FileName: Servo Motor.c * Processor: PIC18F4520 * Compiler: MPLAB C18 v.3.36 * * This file uses the timer 0 to set an interruprt event at 50 Hz. * * * * Author Date Comment *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /** Processor Header Files *****************************************/ #include #include #include /** Define Constants Here ******************************************/ #define BUTTON_PRESSED 0 #define BUTTON_NOT_PRESSED 1 #define PIN_ON 1 #define PIN_OFF 0 /** Local Function Prototypes **************************************/ void low_isr(void); void high_isr(void); // ============================================================ // Configuration Bits // For details on PIC18F configuration bit settings, see // PIC18 Configuration Settings in MPLAB-IDE Help // ============================================================ #pragma config OSC = INTIO67 // Use the internal oscillator #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF #pragma config XINST = OFF /** Declare Interrupt Vector Sections ****************************/ #pragma code high_vector=0x08 void interrupt_at_high_vector(void) { _asm goto high_isr _endasm } #pragma code low_vector=0x18 void interrupt_at_low_vector(void) { _asm goto low_isr _endasm } /** Global Variables *********************************************/ /***************************************************************** * Function: void main(void) ******************************************************************/ #pragma code void main (void) { // Run the clock at 500 kHz (I could've picked about anything) OSCCONbits.IRCF2 = 0; OSCCONbits.IRCF1 = 1; OSCCONbits.IRCF0 = 1; // Set up the timer with a 1:4 prescaler with 16 bits resolution // Therefore the timer0 freq is 500 kHz / 4 / 4 = 31.25 kHz OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_4 ); // Should take a little over 2 seconds to overflow the counter from TMR0 = 0 // If you write in a different starting value for TMR0 it'll overflow sooner WriteTimer0(65530); // Start the timer at a some high value to get to the ISR the first time asap // Enable Global interrupts INTCONbits.GIE = 1; // Enable High priority interrupt // Set up the digital IO pins ADCON1 = 0x0F; // Make sure the pins are digital not analog TRISA = 0x0F; // Make RA0:RA3 inputs TRISB = 0x00; // Make RB0 an output PORTB = 0x00; // Clear the bits to start with while (1) { // A blank while loop, think of all the things you could do here! // When you use an interrupt the main loop is free for something else } } /***************************************************************** * Function: void high_isr(void) * Overview: This interrupt changes the state of RB3:RB0 pins when * the timer zero overflows (0xFFFF -> 0x0000) and triggers * this interrupt code to run ******************************************************************/ #pragma interrupt high_isr // declare function as high priority isr void high_isr(void) { if(INTCONbits.TMR0IF) // Check wether it was the timer interrupt that got us here (better be since that's the only interrupt right now) { INTCONbits.TMR0IF = 0; // Clear interrupt flag for TIMER Zero WriteTimer0(64911); } } /****************************************************************** * Function: void low_isr(void) * Input: * Output: * Overview: ********************************************************************/ #pragma interruptlow low_isr // declare function as low priority isr void low_isr(void) { }