Advertisement
xerpi

Practica 7 CI

Nov 18th, 2014
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <xc.h>
  2. #include <stdio.h>
  3. #include "config.h"
  4. #include "GLCD.h"
  5.  
  6. #define TMR0_1_SEC_H 0x0B
  7. #define TMR0_1_SEC_L 0xDB
  8.  
  9. unsigned long long freq = 0;
  10. volatile unsigned long long ticks = 0;
  11.  
  12. void interrupt high_priority timer0_interrupt()
  13. {
  14.     if (INTCONbits.TMR0IE && INTCONbits.TMR0IF) {
  15.         INTCONbits.TMR0IF = 0;
  16.         freq = ticks + TMR1L + (TMR1H<<8);
  17.         ticks = 0;
  18.         //Reset TMR0
  19.         TMR0H = TMR0_1_SEC_H;
  20.         TMR0L = TMR0_1_SEC_L;
  21.         //Reset TMR1
  22.         TMR1H = 0;
  23.         TMR1L = 0;
  24.     }
  25.     return;
  26. }
  27.  
  28. void interrupt low_priority timer1_interrupt()
  29. {
  30.     if (PIE1bits.TMR1IE && PIR1bits.TMR1IF) {
  31.         PIR1bits.TMR1IF = 0;
  32.         ticks += 0xFFFF;
  33.     }
  34.     return;
  35. }
  36.  
  37. void main(void)
  38. {
  39.     TMR0H = TMR0_1_SEC_H;
  40.     TMR0L = TMR0_1_SEC_L;
  41.  
  42.     TMR1H = 0;
  43.     TMR1L = 0;
  44.  
  45.     INTCONbits.TMR0IF = 0; //Clear flag
  46.     INTCONbits.TMR0IE = 1; //Enable interrupt TIMER0
  47.     INTCON2bits.TMR0IP = 1; //High priority
  48.    
  49.     PIR1bits.TMR1IF = 0; //Clear flag
  50.     PIE1bits.TMR1IE = 1; //Enable interrupt TIMER1
  51.     IPR1bits.TMR1IP = 0; //Low priority
  52.  
  53.     RCONbits.IPEN = 1; //Enable priority interrupts
  54.  
  55.     T0CON = 0b10000100;  //Enables Timer0
  56.                 //Timer0 is configured as a 16-bit timer
  57.                 //Instruction cycle clock
  58.                 //Increment on falling edge transition on T0CKI pin
  59.                 //Timer0 prescaler is assigned
  60.                 //1:32 prescaler value
  61.  
  62.     T1CON = 0b10000011; //Enable read/w rite of T im er1 in 16-bit operation
  63.                 //1:1 prescale value
  64.                 //Timer1 oscillator is shut of
  65.                 //Synchronize external clock input
  66.                 //External clock from pin R C 0/T 1O SO /T 13C K I
  67.                 //Enables T im er1
  68.  
  69.     INTCONbits.GIE = 1; //Enable global interrupts
  70.     INTCONbits.GIEH = 1; // Enable high priority interrupts
  71.     INTCONbits.GIEL = 1; // Enable low priority interrupts
  72.  
  73.     TRISD = 0x00;
  74.     GLCDinit();
  75.     clearGLCD(0, 7, 0, 127);
  76.     char buf[32];
  77.     char buf2[32];
  78.  
  79.     while (1) {
  80.         sprintf(buf, "%i Hz", freq);
  81.         sprintf(buf2, "Ticks: %i ", ticks);
  82.         writeTxt(0, 0, buf);
  83.         writeTxt(1, 0, buf2);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement