Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. /*
  2. * File: testing.c
  3. * Author: Bennett and Bec
  4. *
  5. * Created on 15 October 2019, 14:59
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "ConfigRegsPIC18f4520.h"
  11. #include <timers.h>
  12. //#include <math.h>
  13. //#include <string.h>
  14. #include <p18f4520.h>
  15.  
  16.  
  17. //void encoderISR(void);
  18. void lowPriorityISR (void);
  19. void low_interrupt(void);
  20. void configureISR(void);
  21.  
  22. //Define the low priority interrupt at 0x0018
  23. //start the service routine for low priority interrupt
  24. //#pragma interruptlow lowPriorityISR
  25. //void lowPriorityISR (void){
  26. // encoderISR();
  27. //}
  28.  
  29. #pragma code lowPriorityInterruptAddress=0x0018
  30. void low_interrupt(void){
  31. //All that needs to happen in the interrupt is to goto the service routine
  32. _asm
  33. GOTO lowPriorityISR
  34. _endasm
  35. }
  36.  
  37. //#pragma code //default code section
  38.  
  39. //#define M_PI 3.14159265358979323846264338327950288
  40.  
  41. // Function declarations
  42. void configurePORTB(void);
  43.  
  44. volatile unsigned int trigger = 0;
  45. volatile unsigned int trigger2 = 0;
  46. volatile unsigned int x = 0;
  47.  
  48. //unsigned char x;
  49. #pragma code
  50. void main(void) {
  51.  
  52.  
  53. configurePORTB();
  54. configureISR();
  55.  
  56.  
  57.  
  58. while(1) {
  59. //x = PORTBbits.RB5;
  60. //x = LATBbits.LATB4;
  61. }
  62.  
  63. }
  64.  
  65.  
  66.  
  67. void configurePORTB(void)
  68. {
  69. TRISBbits.RB4 = 0;
  70. TRISBbits.RB6 = 0;
  71. TRISBbits.RB7 = 0;
  72.  
  73. // PORTBbits.RB4 = 0;
  74. // PORTBbits.RB6 = 0;
  75. // PORTBbits.RB7 = 0;
  76.  
  77. TRISBbits.RB5 = 1; // set RB4 to input
  78. INTCON2bits.RBPU = 0; // clear pull up to enable
  79. ADCON1 = 0xFF; // Check why***
  80. }
  81.  
  82. void configureISR(void)
  83. {
  84.  
  85. RCONbits.IPEN = 1; // enable priority interrupts
  86.  
  87. INTCONbits.RBIE = 1; // enables interrupt on PORTB change
  88. INTCON2bits.RBIP = 0; // low priority interrupt
  89.  
  90. //INTCON2bits.INTEDG0 = 1; // interrupt on rising edge (RB0)
  91. //INTCON2bits.INTEDG1 = 1;
  92.  
  93.  
  94. T1CON = 0b10000001; // enable timer 1, prescaler of 1, 16 bit resolution
  95. //PIE1bits.TMR1IE = 1; // enable timer 1 overflow interrupt
  96. //INTCONbits.PEIE = 1;
  97. //INTCONbits.GIE = 1;
  98.  
  99.  
  100. //PIE1bits.TMR1IE = 1; // enable timer 1 overflow interrupt
  101.  
  102. INTCONbits.GIEH = 1; //Enable high priority interrupts
  103. INTCONbits.GIEL = 1; //Enable low priority interrupts
  104.  
  105. }
  106. #pragma interruptlow lowPriorityISR
  107. void lowPriorityISR(void)
  108. {
  109. // if(PIR1bits.TMR1IF == 1)
  110. // {
  111. // trigger2 = 1;
  112. // //encoder_overflow_counter++; // increment counter
  113. // //if(encoder_overflow_counter)
  114. // PIR1bits.TMR1IF = 0; // clear overflow bit flag
  115. // }
  116. //trigger2 = 1;
  117. if(INTCONbits.RBIF == 1)
  118. {
  119. trigger = 1;
  120. // // find time difference
  121. // encoder_left_t2 = ReadTimer1();
  122. // encoder_time_diff = encoder_overflow_counter*(2^16) + encoder_left_t2 - encoder_left_t1;
  123. // encoder_left_t1 = encoder_left_t2;
  124. // encoder_overflow_counter = 0;
  125.  
  126.  
  127. x = PORTBbits.RB5; // Need to read PORTB before clearing the mismatch
  128. INTCONbits.RBIF = 0; // Clear interrupt flag
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement