Ricezione segnale infrarosso
Ciao a tutti,
sto sviluppando un'applicazione all'infrarosso con protocollo di trasmissione molto simile a quello di SONY ma leggermente modificato. Simile perché le tempistiche di 1, 0 e start sono le stesse ma ciò che cambia è la lunghezza dei bit (16 bit), l'invio di due segnali di Start e la frequenza a 40KHz. Detto ciò, sto programmando un 16F1827 e per decodificare il segnale utilizzo il modulo ECCP (Compare Capture PWM) in modalità capture. Il PIC funziona con quarzo da 8Mhz e il PLL è disabiliato. Il programma funziona ovvero riesco a decodificare giustamente il segnale, ma dopo un po di decodifiche il programma si blocca e non capisco il perché, l'unico modo per farlo riprendere è un reset dal pin MCLR. E' una settimana e mezza che ci sono sopra a questo problema e non ne vengo a capo. Di seguito riporto il codice:
Qualcuno sa come aiutarmi per cortesia ?
Saluti,
lcua31989
sto sviluppando un'applicazione all'infrarosso con protocollo di trasmissione molto simile a quello di SONY ma leggermente modificato. Simile perché le tempistiche di 1, 0 e start sono le stesse ma ciò che cambia è la lunghezza dei bit (16 bit), l'invio di due segnali di Start e la frequenza a 40KHz. Detto ciò, sto programmando un 16F1827 e per decodificare il segnale utilizzo il modulo ECCP (Compare Capture PWM) in modalità capture. Il PIC funziona con quarzo da 8Mhz e il PLL è disabiliato. Il programma funziona ovvero riesco a decodificare giustamente il segnale, ma dopo un po di decodifiche il programma si blocca e non capisco il perché, l'unico modo per farlo riprendere è un reset dal pin MCLR. E' una settimana e mezza che ci sono sopra a questo problema e non ne vengo a capo. Di seguito riporto il codice:
- Codice: Seleziona tutto
#include "EEprom_Mapping.h"
#include "Remote_Mapping.h"
#define RISING 0B00000101
#define FALLING 0B00000100
#define START_EVENT 4 //one start signal: 3000uS -> (2400uS high, 600uS low)
#define ONE_EVENT 2 // Logic one signals: 1200uS high and 600 uS low
#define ZERO_EVENT 1 // Logic zero signals: 600uS high and 600 uS low
#define FIRST_START 0
#define SECOND_START 1
#define TOOGLE 2
#define CAPTURE_DATA 3
typedef struct rx_buffer //data struct for signal receiving
{
unsigned short int start;
unsigned short int toogle;
unsigned short int device;
unsigned short int button;
unsigned short int data_rady;
int packet;
} buffer;
typedef struct zero_timer //data struct for the timer 0
{
unsigned short int timer;
} t_zero;
typedef struct one_timer //data struct for the timer 1
{
unsigned short int h_timer;
unsigned short int l_timer;
} t_one;
unsigned short int counter=0, rx_status=FIRST_START, en_counter=0, check=0, entrato=0, bit_counter=0, counter_reply;
unsigned int bit_load = 8192, ms_seconds=0, delay=0;
char txt[20];
buffer rx_data={0, 2, 0, 0, 0, 0};
t_one timer_one_a = {0xFB, 0x50}; //600uS
t_zero timer_zero_a = {21};
void initPic();
unsigned short int splitPacket();
unsigned short int getButtonPressed();
unsigned short int checkPacket();
void playBuzzer(unsigned short int message);
void setTimer0();
void clearTimer0();
void Interrupt()
{
//timer 1 is in overflow every 600 uS
if(PIR1.TMR1IF)
{
if(en_counter==1) //if enable counter is one
{
counter += 1; //count up
}
if(counter>5) //if counter is grater of 5
rx_status = FIRST_START; //reset the main routine to decode the signal
TMR1H = timer_one_a.h_timer; //restet timer 1
TMR1L = timer_one_a.l_timer; //restet timer 1
PIR1.TMR1IF = 0; //restet Interrupt timer 1
}
//This Interrupt routine measure the translations H -> L only
if(PIR1.CCP1IF)
{
if(CCP1CON == FALLING) //if a translation H -> L is available
{
CCP1CON = RISING; //Will be measured the L -> H translation
en_counter = 1; //enable counter
}
else if(CCP1CON == RISING) //if a translation L -> H is available
{
CCP1CON = FALLING; //Will be measured the H -> L translation
en_counter = 0; //disable the counter
}
switch(rx_status)
{
case FIRST_START: //routine able to detect the first start bit
rx_data.data_rady = 0; //Data is not rady
if(counter == START_EVENT) //if the first start bit has been sent
{
counter = 0; //clear the counter
rx_status = SECOND_START; //Check the next transmission
rx_data.start = 1; //Detected the first start bit
}
else
{
rx_status = FIRST_START;
rx_data.start = 0;
}
break;
case SECOND_START: //routine able to detect the second start bit
if(counter == START_EVENT) //if the second start bit has been sent
{
counter = 0; //clear the counter
rx_status = CAPTURE_DATA; //check the next data byte
rx_data.start = 2; //Dtected the second start bit
rx_data.toogle = 0; //clear and restore all variables
rx_data.device = 0;
rx_data.button = 0;
rx_data.data_rady = 0;
rx_data.packet = 0;
bit_load = 8192;
}
else
{
rx_status = SECOND_START;
rx_data.start = 0;
}
break;
case CAPTURE_DATA: //routin able to detect the data byte: toogle, add, cmd
if(counter==ZERO_EVENT) //if the zero bit has been sent
{
counter = 0; //clear counter
rx_data.packet = rx_data.packet | 0; //add zero to the packet
bit_load = bit_load >> 1; //shift right the next bit to load
}
else if(counter==ONE_EVENT) //if the one bit has been sent
{
counter = 0; //clear counter
rx_data.packet = rx_data.packet | bit_load; //add one to the pkt
bit_load = bit_load >> 1; //shift right the load next bit to load
}
if(bit_load == 0) //if all data frame has been sent
{
rx_data.data_rady = 1; //data is rady to spit
rx_status = FIRST_START; //restore the status
counter = 0; //clear counter
CCP1CON = FALLING; //restore the CCP module as falling rise edge
en_counter = 0; //disable counter
}
break;
}
PIR1.CCP1IF = 0; //reset interrupt
}
}
void main()
{
unsigned short int messaggio=0;
initPic();
Sound_Play(880, 500);
Uart1_init(9600);
UART1_Write_Text("RS232 rady\r\n");
while(1)
{
check = checkPacket();
}
}
void playBuzzer(unsigned short int message)
{
//write code
}
unsigned short int getButtonPressed()
{
unsigned short int button = 0;
button = rx_data.button;
return button;
}
unsigned short int checkPacket()
{
unsigned short int returned_value=0;
if(rx_data.data_rady)
{
rx_data.data_rady = 0;
IntToStr(rx_data.packet, txt);
Uart1_Write_Text(txt);
Uart1_Write_Text("\r\n");
}
void initPic()
{
TRISA = 0B00000011;
PORTA = 0;
ANSELA = 0B00000011;
WPUA = 0B00000000;
ADCON0 = 0B00000000;
ADCON1 = 0B00000000;
TRISB = 0B000001111;
PORTB = 0;
ANSELB =0B00000000;
WPUB = 0B00000000;
APFCON0 = 0B00000001; //ccp1 in RB0 pin
APFCON1 = 0B00000001; //tx pin in RB5 pin
CCP1CON = FALLING; //define CCP module as capture mode each falling edge
INTCON = 0b11000000; //Enable interrupts
PIE1.CCP1IE = 0; //Disable cccp1ie interrup
PIE1.TMR1IE = 0; //Disable tmr1ie interrupt
PIR1.TMR1IF = 0; //clear tmr1if interrupt flasg
PIR1.CCP1IF = 0; //clear ccp1if interrupt flag
TMR1H = timer_one_a.h_timer; //configurations of the timer 1
TMR1L = timer_one_a.l_timer;
T1CON = 0b00000001;
PIE1.CCP1IE = 1; //Enable cccp1ie interrup
PIE1.TMR1IE = 1; //Enable tmr1ie interrupt
PIR1.TMR1IF = 0; //clear tmr1if interrupt flasg
PIR1.CCP1IF = 0; //clear ccp1if interrupt flag
Sound_Init(&PORTA, 4);
}
Qualcuno sa come aiutarmi per cortesia ?
Saluti,
lcua31989