Astralist
Newbie level 5
- Joined
- Oct 29, 2014
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 134
Hi, i'm currently building a frequency counter and also to plot adc graph using PIC18F4550
im using software on computer as a user interface.
right now i'm having problem in interrupt routines. it seems not firing.
this is my first time using timer and interrupt routine.
i've searched through the web, and datasheet. seems i'm still missing some config here.
please check my code, im using HI-Tech PIC18 9.63 PL2 on MPLAB 8.83 IDE.
XTAL freq = 20MHz, set to 48MHz by PLL for USB operation.
im using software on computer as a user interface.
right now i'm having problem in interrupt routines. it seems not firing.
this is my first time using timer and interrupt routine.
i've searched through the web, and datasheet. seems i'm still missing some config here.
please check my code, im using HI-Tech PIC18 9.63 PL2 on MPLAB 8.83 IDE.
XTAL freq = 20MHz, set to 48MHz by PLL for USB operation.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 #ifndef MAIN_C #define MAIN_C #include <htc.h> #include "usb.h" #include "HardwareProfile.h" #include "usb_function_hid.h" #include "genericHID.h" __CONFIG(1, USBPLL & IESODIS & FCMDIS & HSPLL & CPUDIV1 & PLLDIV5); __CONFIG(2, VREGEN & PWRTDIS & BOREN & BORV20 & WDTDIS & WDTPS32K); __CONFIG(3, PBDIGITAL & LPT1DIS & MCLREN); __CONFIG(4, XINSTDIS & STVREN & LVPDIS & ICPORTDIS & DEBUGDIS); __CONFIG(5, UNPROTECT); __CONFIG(6, UNPROTECT); __CONFIG(7, UNPROTECT); void InitialiseSystem(); void ProcessIO(); void SendPacket(); void SetADCON12_VddVref(); void SetADCON12_ExtVref(); void start_timer_count(); void interrupt ISR(); short do_TMR0_end_count = 0; short FreqRdy = 0; short toggle_LED = 0; unsigned long TMR1_counter = 0; unsigned long TMR0_counter = 0; unsigned short st_TMR1L ; unsigned short st_TMR1H ; unsigned long st_TMR1_ovfl = 0 ; void main(void) { InitialiseSystem(); start_timer_count(); while(1) { USBDeviceTasks(); if (FreqRdy == 1) { GIE = 0; PEIE = 0; } if (toggle_LED == 1) { //RC7 != RC7; toggle_LED = 0; } ProcessIO(); } } void start_timer_count() { GIE = 0; PEIE = 0; TMR1H = 0; TMR1IF = 0; TMR1L = 0; TMR1_counter = 0; TMR0 = 0; T1CON = 0b00000111; PSA = 1; TMR0IF = 0; TMR0IE = 1; TMR1IF = 0; TMR1IE = 1; TMR0ON = 1; GIE = 1; PEIE = 1; } void interrupt counter() { RC7 = 1; //to check if interrupt is firing if (TMR1IF == 1) { TMR1_counter++; TMR1IF = 0; } if (TMR0IF == 1) { TMR0_counter++; if(do_TMR0_end_count) { st_TMR1L = TMR1L; st_TMR1H = TMR1H; st_TMR1_ovfl = TMR1_counter; TMR1_counter = 0; TMR0_counter = 0; FreqRdy = 1; do_TMR0_end_count = 0; } else { if(TMR0_counter == 46875) { do_TMR0_end_count = 1; toggle_LED = 1; } } TMR0IF = 0; } } void InitialiseSystem() { TRISA = 0b00001111; TRISB = 0b00000000; TRISC = 0b00000011; TRISD = 0b00000000; TRISE = 0b0000; PORTA = 0b00000000; PORTB = 0b00000000; PORTC = 0b00000000; PORTD = 0b00000000; PORTE = 0b0000; UPUEN = 1; FSEN = 1; USBOutHandle = 0; USBInHandle = 0; USBDeviceInit(); } void SendPacket() { if(!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } } void SetADCON12_VddVref() { ADCON1 = 0b00001011; ADCON2 = 0b10001110; } void SetADCON12_ExtVref() { ADCON1 = 0b00011011; ADCON2 = 0b10001110; } void ProcessIO() { if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl == 1)) return; if(!HIDRxHandleBusy(USBOutHandle)) { switch(ReceivedDataBuffer[0]) { case 1: if (FreqRdy) { ToSendDataBuffer[0] = 1; ToSendDataBuffer[1] = st_TMR1L; ToSendDataBuffer[2] = st_TMR1H; ToSendDataBuffer[3] = st_TMR1_ovfl; SendPacket(); FreqRdy = 0; start_timer_count(); } else { ToSendDataBuffer[0] = 1; ToSendDataBuffer[1] = 0; ToSendDataBuffer[2] = 0; ToSendDataBuffer[3] = 0; SendPacket(); } break; case 2: SendPacket(); break; } USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64); } } #endif