[SOLVED] PIC16F877a temperature control program using CCS C [if statement not executed]

Status
Not open for further replies.

mnqobilozo

Newbie level 3
Joined
Sep 2, 2014
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
39
i am designing a UPS for a school project using a PIC16F877A as a controller to monitor the temperature by displaying temperature on a 2X16 LCD and run a motor if a temperature of 70 degrees is reached and stops when the temperature drops to 50 degrees. i also inserted a voltage detector to detect an output on the inverter and send a 5 volts signal to PIN_B2 of the pic if voltage is present, so that if there is no voltage, the pic should switch a relay through PIN_B4 to supply the load from the mains supply bypassing the UPS.
i have succeeded on the other tasks but i fail to switch the relay accordingly.

here is my program written in C language using CCS compiler.

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
/* 
PIC16F877A and LM35 Based Temperature Monitor 
 
MACHI M.C 
STD.no 20403715
*/ 
 
#include <16f877a.h> 
#device adc=10    // Set ADC resolution to 10Bit
#fuses XT,NOLVP,NOWDT,NOPROTECT 
#use delay(crystal=4000000)
#use rs232(baud=960,xmit=PIN_C6,rcv=PIN_C7,ERRORS)
#include <lcd.c>
 
#define  LOAD  PIN_B7
#define  THRES 70.0     // load switching threshold in Celsius
#define  OFF 50.0  // fan stops when temparature reaches value in degrees
#define ADC_OFF 0   // ADC off
#define  RELAY PIN_B4
#define  R2    PIN_B2
#define  R1    PIN_B1
#include <stdlib.h>
int16 digital_reading;  // ADC resolution is 10Bit, an 8Bit integer is not enough to hold the reading
float temp;
 
 
 
void main()
{
  
   /* ADC Initialization */
   setup_adc(ADC_CLOCK_INTERNAL); // initialize ADC with a sampling rate of Crystal/4 MHz
   setup_adc_ports(RA0_ANALOG);   // set PIN_A0 as analog input channel
   set_adc_channel(0);            // point ADC to channel 0 for ADC reading
   delay_ms(1);                   // ADC module is slow, needs some time to adjust.
  
   disable_interrupts(GLOBAL);   // DISABE ALL INTERRUPTS.
   SET_TRIS_B(0X0F);             //set RB0,RB1,RB2 & RB3 as inputs, RB4,RB5,RB6 & RB7 as output.
 output_low(RELAY);            // relay initialy off
   if (input(PIN_B2)==0)
   {
   output_high(PIN_B4);
   }
   else if (input(PIN_B2)==1)
   {
   output_low(RELAY);
   }
   
   /* Peripherals Configurations */
   lcd_init();                   // Turn LCD ON, along with other initialization commands
   output_low(LOAD);             // the load is initially OFF
   lcd_gotoxy(1,1);              // point LCD cursor to col1 row1
   lcd_putc("Temperature is:");  // print on LCD
   {
   while(1) // infinite loop
   {
      digital_reading = read_adc();    // capture current temperature reading
      delay_us(100);                   // 0.1ms delay for ADC stabilization
      temp = digital_reading * 0.4883; // convert reading to Celsius
      lcd_gotoxy(1,2);                 // point LCD cursor to col1 row2
      printf(lcd_putc,"%2.1f C",temp); // print value on LCD
 
      if(temp>=THRES)
       Do 
       {
         output_high(LOAD);   // Control Load
       }
      while (temp>=OFF);
      else  output_low(LOAD);
      delay_ms(1000);   // 1 second delay between readings
   }
   }
}

 
Last edited by a moderator:

Do you have a transistor driving the relay? PIC cannot supply current needed by a relay if connected directly to PIC.
 

Do you have a transistor driving the relay? PIC cannot supply current needed by a relay if connected directly to PIC.

yes i do. for the statement:


Code C - [expand]
1
2
3
4
5
6
7
8
if (input(PIN_B2)==0)
   {
   output_high(PIN_B4);
   }
   else if (input(PIN_B2)==1)
   {
   output_low(RELAY);
   }

the pic executes output_high(PIN_B4) not accounting the statement if (input(PIN_B2)==0).
 
Last edited by a moderator:

Split the code into several parts to debug the code.

Post your schematic.

In your code, you are checking the PIN_B2 once. Did you measure the voltage on PIN_B2 is high or low with a multimeter.

Do a blink LED test, it will solve 90% of your problem.

Best wishes
 

I'm only using a mobile phone to read the message so I can't check easily but it looks like the ADC conversion is returning a number which you then convert to volts by multiplying by 0.4883. That is correct for volts but the LM35 returns 10mV/C so unless you are amplifying the voltage it may not be reaching the threshold you expect.

Brian.
 

Thank you all for your replies guys. my problem is solved. the program expected the conditions to be true in order to proceed to the next statement. i solved it.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…