Simple LED with timer on PIC18 C18 Compiler

Discussion in 'Embedded Systems and Microcontrollers' started by skybox, Jul 28, 2009.

  1. skybox

    Thread Starter Active Member

    Mar 2, 2009
    68
    1
    Hi guys,

    I can't get this to work. I am using the internal oscillator at 31 kHz and have a prescale value of 1:128 with the 8-bit timer. If I did my math right, this should take about 4 seconds for the timer interrupt flag to recieve a 1 after overflow. If someone can give me tips and/or advice, it would be greatly appreicated!!!

    Thanks!

    Here is my code:
    Code ( (Unknown Language)):
    1.  
    2. #include <p18f14k50.h>
    3.  
    4. //pragma codes
    5. #pragma code high_vector=0x08
    6. #pragma config FOSC = IRC //enable the internal oscillator
    7.  
    8.  
    9.  
    10.  
    11. //function prototypes
    12. void oscillator(void);
    13. void timer(void);
    14. void high_isr(void);
    15.  
    16. void interrupt_at_high_vector(void)
    17. {
    18.    _asm goto high_isr _endasm
    19. }
    20.  
    21. /*******Oscillator function - This function will set the oscillator to 31 kHz***********/
    22. void oscillator(void)
    23. {
    24. //Set oscillator to 31 kHz
    25. OSCCONbits.IRCF2 = 0;
    26. OSCCONbits.IRCF1 = 0;
    27. OSCCONbits.IRCF0 = 0;
    28.  
    29. //Device clock derived directly from the LFINTOSC internal oscillator
    30. OSCTUNEbits.INTSRC = 0;
    31. }
    32. /*******End Oscillator Function********************************************************/
    33.  
    34.  
    35. /*******Timer function - Will start the timer and set pre-scalar to 1:128**********/
    36. void timer(void)
    37. {
    38. //Pre-scalar value of 1:128. This should give about 4 seconds for the timer0 interrupt flag to go high
    39. T0CONbits.T0PS2 = 1;
    40. T0CONbits.T0PS1 = 1;
    41. T0CONbits.T0PS0 = 0;
    42.  
    43. T0CONbits.TMR0ON = 1; //Enables Timer0
    44. }
    45. /*******End Timer Function********************************************************/
    46.  
    47. void main(void)
    48. {
    49.         INTCONbits.GIE = 1; //enable high priority global interrupts
    50.         RCONbits.IPEN = 1; //enable priority levels on interrupts
    51.        
    52.         INTCONbits.TMR0IF = 0; //clears timer interrupt flag
    53.         INTCONbits.TMR0IE = 1; //enable Timer0 interrupt bit
    54.         T0CONbits.T08BIT = 1; //Set Timer0 as an 8-bit timer
    55.         INTCON2bits.TMR0IP = 1; //Timer0 Interrupt bit set to high priority
    56.        
    57.         TRISC = 0x00; //set all PORTC bits as an output
    58.        
    59.         oscillator();
    60.         timer();
    61. }    
    62.  
    63.        
    64. //High Interrupt Vector function
    65. #pragma interrupt high_isr            // declare function as high priority isr
    66. void high_isr(void)
    67. {
    68.     if(INTCONbits.TMR0IF = 1)
    69.         {
    70.             LATCbits.LATC0=1 //Turn on LED at RC0
    71.         }
    72. }
    73.  
     
Loading...