xhizt
Newbie level 1
- Joined
- Apr 8, 2013
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,307
Hi, I'm having a hard time decoding NEC protocol. This is my assignment and I'm new to microcontrollers. I'm working with mikroC PRO for PIC and ISIS Proteus.
I made NEC signal with address/inverse/command/inverse and measured it in Proteus, used IRLINK to transfer it to GP3 and nothing works.
I used GPIO.GP1 ON/OFF just to check on oscilloscope what happens and it appears my interrupt misses first pulse.
Any suggestions, comments or different approaches on this problem are welcome
I made NEC signal with address/inverse/command/inverse and measured it in Proteus, used IRLINK to transfer it to GP3 and nothing works.
I used GPIO.GP1 ON/OFF just to check on oscilloscope what happens and it appears my interrupt misses first pulse.
Any suggestions, comments or different approaches on this problem are welcome
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 // NEC IR DECODER _MCLRE_OFF;_PWRTE_OFF;_CP_OFF;_CPD_OFF;_FOSC_INTRCIO; static unsigned long counter; static unsigned input_data, bit_count; static unsigned char address=0,address_inverse=0; static unsigned char command=0,command_inverse=0; unsigned char dataready = 0; unsigned long time; unsigned int edge = 0; enum { Start, Wait, Capture }; char Current_state = Start; void interrupt(){ if(INTCON.T0IF) // check the timer0 over flow interrupt flag { counter++; } else if(INTCON.GPIF){ GPIO.GP1=1;GPIO.GP1=0; time = ((counter<<8)+TMR0); // time since last interrupt counter = 0; TMR0 = 0; switch (Current_state){ case Start: if(time >= 8000 && time < 10000) { //wait 9ms leading pulse bit_count = 0; input_data = 0; Current_state = Wait; } break; case Wait: if(time >= 4000){ // wait for 4.5ms Current_state = Capture; } else{ Current_state = Start; } break; case Capture: //9ms + 4.5ms passed edge++; if (edge == 2){ //edge waits for 2 interrupts ( skips rising edge ) input_data = input_data<<1; // shift buffer if(time<=1300){ input_data = input_data | 0x0; // add 0 to received data bit_count++; } else{ input_data = input_data | 0x1; // add 1 to received data bit_count++; } edge = 0; } //complete 32 bit if(bit_count == 32){ address = (input_data>>24)& 0xFF; //extract the data from the buffer address_inverse = (input_data>>16)& 0xFF; command = (input_data>>8) & 0xFF; command_inverse = (input_data) & 0xFF; input_data=0; //clear the buffer if((!(address & address_inverse)) && (!(command & command_inverse))) // check NEC { dataready = 1; } else { dataready = 0; // wrong code } Current_state = Start; } break; default: Current_state = Start; } INTCON.GPIF = 0; //clear interrupt flag. } INTCON.T0IF = 0; // clear the timer0 interrupt flag } void main() { CMCON = 7; //Disable Comparator ANSEL = 0; //All digital I/O TRISIO = 0x8; //Configure I/O Pin GPIO = 0x00; //Set all output to 0 OPTION_REG = 0x88; //pullups are disabled , timer0 prescale 1:1 IOC = 0x8; //interrupt on change is only to the GPIO3 INTCON.T0IE = 1; // Timer0 overflow interrupt enable INTCON.T0IF = 0; // clear the timer0 intrrupt flags INTCON.GPIE = 1; // external interrupt on GP3 is enabled INTCON.GPIF = 0; // clear the external interrupt flag INTCON.PEIE = 1; //enable interrupt INTCON.GIE = 1; //enable global interrupt TMR0 = 0; while(1){ if(dataready){ if(command == 0b01010000){GPIO.GP0 = 1; }//test dataready = 0; } } }