Advertisement
chayanforyou

PIC timer0

Jul 13th, 2021
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. void Interrupt()
  2. {
  3.   // Timer0 Interrupt - Freq = 38461.54 Hz - Period = 0.000026 seconds
  4.   if (INTCON.TMR0IF ==1) // timer 0 interrupt flag
  5.   {
  6.     GPIO.GP0 = ~GPIO.GP0;      // Toggle PORTB bit0 LED
  7.     INTCON.TMR0IF = 0;         // clear the flag
  8.     INTCON.TMR0IE = 1;         // reenable the interrupt
  9.     TMR0 = 243;           // reset the timer preset count
  10.   }
  11.  
  12. }
  13.  
  14. void main()
  15. {
  16.   CMCON = 0x07; // GP2:0 como IO
  17.   TRISIO = 0x00;    // PORT is all output...to show the interrupts
  18.   GPIO = 0;       // start with all outputs low
  19.  
  20. //Timer0 Registers Prescaler= 1 - TMR0 Preset = 230 - Freq = 38461.54 Hz - Period = 0.000026 seconds
  21. OPTION_REG.T0CS = 0;  // bit 5  TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
  22. OPTION_REG.T0SE = 0;  // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
  23. OPTION_REG.PSA = 1;   // bit 3  Prescaler Assignment bit...0 = Prescaler is assigned to the WDT
  24. OPTION_REG.PS2 = 0;   // bits 2-0  PS2:PS0: Prescaler Rate Select bits
  25. OPTION_REG.PS1 = 0;
  26. OPTION_REG.PS0 = 0;
  27. TMR0 = 243;             // preset for timer register
  28.  
  29. // Interrupt Registers
  30.   INTCON = 0;           // clear the interrpt control register
  31.   INTCON.TMR0IE = 1;        // bit5 TMR0 Overflow Interrupt Enable bit...1 = Enables the TMR0 interrupt
  32.   INTCON.TMR0IF = 0;        // bit2 clear timer 0 interrupt flag
  33.   INTCON.GIE = 1;           // bit7 global interrupt enable
  34.  
  35.   while(1)  //endless loop
  36.   {
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement