Maker Pro
Maker Pro

PIC decoding NEC signal with TIMER1 problem

Hello,

I'm trying to decode NEC remote control signal with my PIC18F4550, I started learning this couple days ago.
I've tried CCP capture mode, then IOC, then INT0, but all these ways were hard for me to start.

By searching I decided to follow a strategy by a programmer doing the project on his website, with different compiler.

I tried to copy his way but didn't work for me, with some modifications and tweaks I started to get some readings.

But the problem is that all the buttons give the same code! I don't know why.

I get like 0xFFFFFFF00.

I don't know what's wrong, but this is the best I could get, and this is my code in MPLAB XC8.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DECODING function:

uint32_t PIN_POLLING_VS1838b(uint16_t *arr)
{
TMR1=0;
while ((!PORTBbits.RB2) && (TMR1 < 10000)); // test 9ms low 9182
if ((TMR1 > 10000) || (TMR1 < 9000))
return 1;
TMR1 = 0;
while ((PORTBbits.RB2) && (TMR1 < 5000)); // test 4.5ms high ~4480
if ((TMR1 > 4600) || (TMR1 < 4300))
return 2;
TMR1 = 0;
/////////////////////////////////////////////////////////////////////
for(i=0;i<32;i++){ // 2.25ms & 1.25ms test
TMR1 = 0;
while ((!PORTBbits.RB2) && (TMR1 < 800));
TMR1 = 0;
while ((PORTBbits.RB2) && (TMR1 < 2000));
//arr=TMR1;
if((TMR1 < 700) && (TMR1 > 400))
ir_code_INT &= ~(1<<i);
else if((TMR1 < 2000) && (TMR1 > 1000))
ir_code_INT |= (1<<i);
}
/////////////////////////////////////////////////////////////////////
return ir_code_INT;//
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MAIN function:

uint8_t val_arr[20],i_sprint[2];
uint16_t TMR1_RET[32];
uint32_t capture_val,ir_code_REC;
uint8_t i;

void main(void) {
INTOSC_4MHz;
AN_DISABLE;
//DISABLE_INT;
//ENABLE_INT_IOC;

i2c_init();
i2c_start();
LCD_Init();
TRISBbits.RB2=1;
TRISDbits.RD2=0;
TRISDbits.RD3=0;
INTCONbits.GIE=0;
INTCONbits.PEIE=0;
INTCONbits.INT0IE=0;
INTCON2bits.INTEDG0=0;
INTCON2bits.RBPU=1;
T1CON=0x80; // timer1 16-bits
T1CONbits.TMR1ON=1; // turn on timer1
while(1){

while (PORTBbits.RB2);
capture_val = PIN_POLLING_VS1838b(TMR1_RET);
sprintf(val_arr,"%lx",capture_val);
LCD_string("remote code:");
move_cursor(2,0);
LCD_string(val_arr);
__delay_ms(500);
sendCMD(clear_display);

}
}
 
Top