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.

[PIC] Relay on/off only two times

Status
Not open for further replies.

CAUVERY

Newbie level 1
Joined
Sep 11, 2016
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
33
Hello everyone,am beginner to the microcontroller programming. am working on temperature controlled heater relay using pic16f887 in hitech compiler and display the temperature in seven segment. am using LM35 sensor.so when the process starts heater relay will be on, whenever temperature is greater than 100 the heater relay should switch off(when heater relay off--temperature drops below 100 after certain time and again relay on). am getting correct output up to this level of code. now i want to switch on/off heater only two times i.e when temperature goes greater than 100 for third time the relay should permanently switch off.so i add flag varaible to count the operation but unfortunately in my code increment operator doesn't work inside the condition and relay on/off occurs only first time. what should do to on/off twice.

In simple words I can say.
(1st time--process starts--temperature rising--relay on--temperature rise>100--relay off--temperature drops below 100--again relay on--2nd time--temperature rise>100--relay off permanently(dont care about the temperature drop)

if i get guidance what can be done to solve this issue, it will be more helpful, here i attached my code..


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
74
75
76
77
78
#include<htc.h>
__CONFIG(0x20F2);
__CONFIG(0x3055);
#define _XTAL_FREQ 12000000
 
#define D1 RD0
#define D2 RD1
#define D3 RD2
#define D4 RD3
 
void ADC_Initialize()
{
  ADCON0 = 0b01010001; 
  ADCON1 = 0b11000000; 
}
 
unsigned int ADC_Read(unsigned char channel)
{
  GO_nDONE = 1;                   
  while(GO_nDONE);                
  return ((ADRESH<<8)+ADRESL);    
}
 
 
void main()
{
unsigned int i,a,b,c,d,e,f,g,h,adc;
int flag=0;
unsigned int seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
 
TRISC=0x00;
TRISD=0x10;
 
 
ADC_Initialize();
 
while(1)
{
 
  adc = (ADC_Read(4));
   k = adc*0.488281;
 
 
if(k>100)                                       //temperature greater than 100
{
flag=flag+1;                                   // increment
if(flag>=0 && flag<4)                   
RD6=0;                                          //relay permanently off
}
else
{
RD6=1;                                        //temperature less than 100-relay on
}
 
 
 
a=k%10;
b=k/10;
PORTC=seg[a];D1=1; 
__delay_ms(5);D1=0; 
 
c=b%10;
d=b/10;
PORTC=seg[c];D2=1; 
__delay_ms(5);D2=0; 
 
e=d%10; 
f=d/10;   
PORTC=seg[e];D3=1; 
__delay_ms(5);D3=0;
 
g=f%10; 
h=f/10;   
PORTC=seg[g];D4=1; 
__delay_ms(5);D4=0;
 
}
}

 
Last edited by a moderator:

Falgs have only 0 or 1 value. You should use and say counters.
 
Hi cauvery,

Actually you didn't have any counter variable in the code.
For your application, you have to count the occurrences

Just try this,

char counter;

if(k > 100)
{
counter++;
if(counter >= 3)
break ;

// Ur default code here

}

Also note this code terminates the entire process. You may need to handle certain other cases too
 
On powering up, does your relay behave like (ON - OFF - ON) ? I guess, after that, the relay does not get OFF. If this is the case, then look at your code. When the temperature goes above 100 the condition "if(k>100) " meets and the counter i.e. flag increment takes place. This increment goes on until "k" comes below 100. By that time the counter becomes more than 4 . Now when "k" goes below 100, the relay gets ON and "k" goes above 100. But since the flag has already gone beyond 4, the condition "if(flag>=0 && flag<4)" does not meet causing non-execution of "RD6=0". Thus the relay does not get OFF. Have a look to this point and your circuit operation.
 

(1st time--process starts--temperature rising--relay on--temperature rise>100--relay off--temperature drops below 100--again relay on--2nd time--temperature rise>100--relay off permanently(dont care about the temperature drop)

I cannot see the purpose of the flag variable, the only thing it is doing is just allow you control relay during flag=1 to 3. Considering your code do no show any declaration of this variable, if it is 16 bits length you will have to wait a lot for the next interation.

Code:
flag=flag+1;                                   // increment
if(flag>=0 && flag<4)                   
RD6=0;                                          //relay permanently off
}
else
{
RD6=1;                                        //temperature less than 100-relay on
}

I would also remark the lack of a hysteresis control, but perhaps not totally necessary for this case.
 

Because you are driving a relay, you MUST set some hysteresis: relay should be turned on when the temp falls below 98 and must be turned off when the temp rises above 100.

The hysteresis amount must be set by the sensitivity of the temp sensor plus some margin for chatter.

Also check lines: 46-48 and manually step through the loop and see what you are doing...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top