Cheetos
Member level 3

i have a problem with my code in my PIC. it has to receive a data then it will operate. the problem is, once it has received the data, some other functions seemed to be ignored. for example, if i input "H" it will turn on. If i input "N" it must turn off but it does not. what i would like to happen is that my PIC will listen again once it has received "H" and shall do the function it needs to do. please help
void main() {
long at1,at2; // 1-inside,2-outside
int hot,on=0,adj,x;
char zrx,irx;
TRISA=0xFF; // all pins are inputs
TRISB=0x00; // all pins are outputs
ADCON1=0x80;
TRISC=0xC0; // only TX pin is made output on port C
RCSTA.SPEN=1;
RCSTA.CREN=1;
TXSTA.TXEN=0;
INTCON.GIE=1;
INTCON.PEIE=1;
PIE1.RCIE=0;
PIR1.RCIF=0;
Usart_Init(9600);
Delay_ms(3000);
at1=dec2temp(Adc_read(0));
at2=dec2temp(Adc_read(1))-1; // -1 to compensate +1 in reading due to 10mV difference
while(1){
if(PIR1.RCIF==1){
irx=Usart_Read();
zrx=irx;
RCSTA.CREN=0;
PIR1.RCIF=0;
}
//N - empty (78), H - present (72), S - asleep (83)
if(at2<=26){
if(on==1){
PORTB=0x80;
Delay_ms(10);
PORTB=0x00;
on=0;
}
}
// else{
if((zrx==78)&&(on==1)){
if(tsat<24){
adj=24-tsat;
for(x=1;x<=adj;x++){
PORTB=0x40;
Delay_ms(1000);
PORTB=0x00;
tsat++;
}
}
if(tsat>24){
adj=tsat-24;
for(x=1;x<=adj;x++){
PORTB=0x20;
Delay_ms(1000);
PORTB=0x00;
tsat=tsat-1;
}
}
PORTB=0x80;
Delay_ms(10);
PORTB=0x00;
on=0;
}
if((zrx==72)&&(on==0)){
PORTB=0x80;
Delay_ms(10);
PORTB=0x00;
on=1;
}
if(on==1){
if(at2>=30){ // determines higher temp to determine if the temp outside is cooler than the temp inside
hot=1;
}
else{
hot=0;
}
if(zrx==83){
hot=2;
}
tadj(hot,at1);
}
//}
PIR1.RCIF==0;
RCSTA.CREN=1;
delay_ms(1000);
}
}
void main() {
long at1,at2; // 1-inside,2-outside
int hot,on=0,adj,x;
char zrx,irx;
TRISA=0xFF; // all pins are inputs
TRISB=0x00; // all pins are outputs
ADCON1=0x80;
TRISC=0xC0; // only TX pin is made output on port C
RCSTA.SPEN=1;
RCSTA.CREN=1;
TXSTA.TXEN=0;
INTCON.GIE=1;
INTCON.PEIE=1;
PIE1.RCIE=0;
PIR1.RCIF=0;
Usart_Init(9600);
Delay_ms(3000);
at1=dec2temp(Adc_read(0));
at2=dec2temp(Adc_read(1))-1; // -1 to compensate +1 in reading due to 10mV difference
while(1){
if(PIR1.RCIF==1){
irx=Usart_Read();
zrx=irx;
RCSTA.CREN=0;
PIR1.RCIF=0;
}
//N - empty (78), H - present (72), S - asleep (83)
if(at2<=26){
if(on==1){
PORTB=0x80;
Delay_ms(10);
PORTB=0x00;
on=0;
}
}
// else{
if((zrx==78)&&(on==1)){
if(tsat<24){
adj=24-tsat;
for(x=1;x<=adj;x++){
PORTB=0x40;
Delay_ms(1000);
PORTB=0x00;
tsat++;
}
}
if(tsat>24){
adj=tsat-24;
for(x=1;x<=adj;x++){
PORTB=0x20;
Delay_ms(1000);
PORTB=0x00;
tsat=tsat-1;
}
}
PORTB=0x80;
Delay_ms(10);
PORTB=0x00;
on=0;
}
if((zrx==72)&&(on==0)){
PORTB=0x80;
Delay_ms(10);
PORTB=0x00;
on=1;
}
if(on==1){
if(at2>=30){ // determines higher temp to determine if the temp outside is cooler than the temp inside
hot=1;
}
else{
hot=0;
}
if(zrx==83){
hot=2;
}
tadj(hot,at1);
}
//}
PIR1.RCIF==0;
RCSTA.CREN=1;
delay_ms(1000);
}
}