Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with PIC code

Status
Not open for further replies.

Cheetos

Member level 3
Joined
May 26, 2011
Messages
57
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,700
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);
}
}
 

Try this inside the while(1) and remove the part where you receive! i don´t know if RCIF is the flag you have on your PIC but i think it is common to all :S

Code:
if(RCIF){     				   	// Receive Data 232
irx=Usart_Read();
zrx=irx;
}

good luck
 

please help me, once it has received 'H' my pic operates, but once i transmit 'N' it does not listen to what i am transmitting anymore
 

Try something simpler just to check it is working or not... test this. the main point is that if you send 1 RB0 pin will be active and with the 2 it will desactivate



Code:
unsigned int 		converter_ascii_to_dec;


while(1)
{
if(RCIF){     				   	// Receive Data 232
irx=Usart_Read();
zrx=irx;
converter_ascii_to_dec=zrx-0x30;          // converts ascii to decimal numbers
}								// Conversion from Ascii to Decimal numbers
if (converter_ascii_to_dec==1){
RB1=1;
}								// End input ==1
if (converter_ascii_to_dec==2){
RB0=0;
}								
}


Get it properly on your code and it should work (it works for me) after that just think a bit and do whatever you want with this sample code
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top