[SOLVED] PIC12F683 with LM35 ...

Status
Not open for further replies.

7eshmat

Newbie level 4
Joined
Jun 24, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
44
i need the c code for connecting LM35 heat sensor with PIC12F683 and the output will control a fan via PWM "Pin5/ccp1"

temperature Range 30c to 45c

30c >>> 30% PWM
45c and Above >>> 100% PWM

thanks
 

Okay tpetar, thx for ur reply ... here is the code i wrote ,,, could u plz tell me whats wrong with it ...

Code:
[CODE]
#define Temp35 71
#define Temp25 51        
unsigned int Ch0;

void main() {
CMCON0 = 7; //Disable comparator
ANSEL = 1;
TRISIO.B2 = 0;
PWM1_Init(5000);
PWM1_Set_Duty(255);
PWM1_Start();

while (1){
		Ch0 = ADC_Read(0); //Gets reading from channel 0
		if (Ch0 > Temp25 ,Ch0 <Temp35){
                   PWM1_Set_Duty(127);
		}
		else{}

		if(Ch0 > Temp35){
                       PWM1_Set_Duty(200);
		}
		else{}
	}
}
[/CODE]
 

so i changed it to this, its not working properly ... the waveform on oscop is changing rapidly !!!

Code:
ANSEL = 0x01;  
TRISIO = 0x01;  
CMCON0 = 0x07;

ADCON0 = 1;
ADC_Init();

i have another qn. i need to make 3 speeds for the output PWM
temp < temp25
temp25 < temp < temp35 ..... " i cant figure the code for this case"
temp > temp35

- - - Updated - - -

i Also need to know the difference between

Code:
ADC_Get_Sample();
and
Code:
ADC_Read();
 
Last edited:

There is no need for ADC Initialization.

Change

if (Ch0 > Temp25 ,Ch0 <Temp35){

to

if ((Ch0 > Temp25) && (Ch0 < Temp35)){

//I am assuming that you are using 8 bit adc and max value will be 255 for 150 deg C.

Code C - [expand]
1
2
3
if(Ch0 < Temp25){//Do something}
elseif((Ch0 > Temp25) && (ch0 < Temp35)){//Do something}
elseif(Ch0 > Temp35){//Do something}




Also make sure that PWM_Set_Duty() is called only once when temp changes. Don't call it repetedly.
 
Last edited:
thanks alot jayanth.devarayanadurga ... Really helped me

and here is the final code for anyone need to use it

Code:
#define Temp35 71
#define Temp25 51        
unsigned int Ch0;

void main() {

ANSEL = 0x01;  
TRISIO = 0x01;  
CMCON0 = 0x07;
ADCON0 = 0x90;

PWM1_Init(5000);
PWM1_Set_Duty(255);
PWM1_Start();

while (1){

                Ch0 = ADC_Read(0); //Gets reading from channel 0
                Delay_ms(100);
                if (Ch0 < Temp25){
                   PWM1_Set_Duty(63);
                }
                if ((Ch0 > Temp25) && (Ch0 < Temp35)){
                   PWM1_Set_Duty(127);
                }
                if(Ch0 >= Temp35){
                   PWM1_Set_Duty(255);
                }
        }
}
 

I told you to call PWM1_Set_Duty() only once when ch0 changes.

and i did so ... !!!
if Ch0 changes ,,, then PWM1_Set_Duty() changes ... if not ,then the duty will remain the same

if you see anything wrong ,will u plz post the code in your mind

thanks
 

Consider this code of yours


Code C - [expand]
1
2
3
4
5
6
7
8
9
if (Ch0 < Temp25){
                   PWM1_Set_Duty(63);
                }
                if ((Ch0 > Temp25) && (Ch0 < Temp35)){
                   PWM1_Set_Duty(127);
                }
                if(Ch0 >= Temp35){
                   PWM1_Set_Duty(255);
                }



If Ch0 is < Temp25 then PWM duty is set to 63 and in the next ans successive execution of while(1) loop Ch0 will be still the same value and the if(Ch0 < Temp35) will be executed in every while(1) loop and so PWM duty is repetedly set. Use a variable like oldval and check if oldval != Ch0. If condition is true then set pwm duty and then assign oldval = Ch0.
 

so i did what u told me and i got to this ...

Code:
char val;
.
.
.
while (1){
        Ch0 = ADC_Read(0); //Gets reading from channel 0
                Delay_ms(50);
                if (Ch0 < Temp30){
                   if (val != Ch0){
                      PWM1_Set_Duty(0);
                      val = Ch0;
                      }
                }
                if ((Ch0 >= Temp30) && (Ch0 < Temp35)){
                   if (val != Ch0){
                      PWM1_Set_Duty(80);
                      val = Ch0;
                      }
so will it work ,, or im still wrong
 

You can place if(val != Ch0) and val = Ch0 before the two if statements. It works. Make val as unsigned int as Ch0 is unsigned int.

so will this work ?!!

Code:
                if (val != Ch0)
                   val = Ch0 ;{
                   if (Ch0 < Temp30){
                      PWM1_Set_Duty(0);
                      }
                }
                if (val != Ch0)
                   val = Ch0;{
                   if ((Ch0 >= Temp30) && (Ch0 < Temp35)){
                      PWM1_Set_Duty(80);
                      }
                }
                if (val != Ch0)
                   val = Ch0;{
                   if ((Ch0 >= Temp35) && (Ch0 < Temp40)){
                      PWM1_Set_Duty(127);
                      }

- - - Updated - - -

when i did this it didnt work

Code:
                if (val != Ch0){
                   val = Ch0 ;
                   if (Ch0 < Temp30){
                      PWM1_Set_Duty(0);
                      }
                }
                if (val != Ch0){
                   val = Ch0;
                   if ((Ch0 >= Temp30) && (Ch0 < Temp35)){
                      PWM1_Set_Duty(80);
                      }
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
unsigned int Ch0 = 0, val = 0;
 
...
 
if (val != Ch0){
 
    if(Ch0 < Temp30)
            PWM1_Set_Duty(0);
    else if((Ch0 >= Temp30) && (Ch0 < Temp35))
            PWM1_Set_Duty(80);
 
    val = Ch0;
}

 
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…