Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Please help me in making codes in PWM temperature controlled fan via PIC12F675

Status
Not open for further replies.

Jabara16

New Member
Hi, im a newbie in programming and im actually doing in my project but it seems i cant understand it as i browse in google i find about this forum and it fascinated me because theres a lot of people really willing to help others in programming.

I want to make a PWM temperature controlled fan using PIC12F675 and LM35 as my sensor, moreover, I need to use microC using C langauge. please help me

Equipment and Specs:
Temp Sensor: LM35 (Precission Centrigrade Temperature Sensor)
Output voltage = 0mv + 10.0mv/ Degree Celsius
Microcontroller: PIC12675
Output voltage = 0.004882812v/increment
A/D Converter 10 bits = 1023


Conditions:
When the temperature rises the PWM(duty cycle) of the fan will rise proportional to the temperature
When it is operating in the 30-49 degrees Celsius the green LED turns on but if the temperature exceeded at 49 degrees and above the PWM must be zero and the red LED must turn on

PWM Conditions
Temperature (Degree Celsius) PWM (duty cycle)
Less than or equal to 34 30%
35-37 35%
38 40%
39 50%
40 60%
41 70%
42 80%
43-46 90%
47-49 100%
Greater than or equal to 50 shutdown



Computations:

Let A = 10mV B=0.004882812 C= 49/100

Sample calculations
@35degrees
Output LM35 = 35*A = 0.35
A/D Converter output of PIC = 0.35/B = 71.68bits = 71bits (the PIC12F675 can only read whole numbers)
Temperature Reading of PIC
71*C = 34.79 degrees celcius is almost equivalent to 35degrees

@36degrees
Output LM3 = 36/10mV = 0.36
A/D Converter output of PIC = 0.36/B = 73.728 = 73
Temperature Reading of PIC
73*C = 36.12672
@40degrees
Output LM3 = 40*A = 0.4
A/D Converter output of PIC = 0.4/B = 81.92
Temperature Reading of PIC
81*C = 39.69degrees

@41
Output LM3 = 41*A = 0.41
A/D Converter output of PIC = 0.41/B = 83.968
Temperature Reading of PIC
83*C = 40.67degrees

@42
Output LM3 = 42*A = 0.42
A/D Converter output of PIC = 0.42/B = 86.016
Temperature Reading of PIC
86*C = 42.14

@43
Output LM3 = 43*A = 0.43
A/D Converter output of PIC = 0.43/B = 88.064
Temperature Reading of PIC
88*C = 43.12


@44
Output LM3 = 44*A = 0.44
A/D Converter output of PIC = 0.44/B = 90.112
Temperature Reading of PIC
90*C = 44.1

@45
Output LM3 = 45*A = 0.45
A/D Converter output of PIC = 0.45/B = 92.16
Temperature Reading of PIC
86*C = 45.06
 
The pic12f675 has no CCP module, so you'll need to do this in software...

There is a software implementation here https://www.electro-tech-online.com/threads/help-needed-pwm-generation-using-pic16f676.132771/

You need to change a few things to suit your needs.. The LM35 is easy to connect...


Sir Ian,

My professor wants my program to run as follows:

1. the fan is running 30%PWM for normal temperature ( temp<=34 degrees celcius)
2. as the temperature goes higher the PWM also increases directly proportional to it
3. if the temperature goes higher or equal to 50 degrees the fan will shut down or lets say that the PWM will be zero


can you help me in making the codes? please? or if ever do you have codes there and the hex file? please? i needed it badly :(
 
I don't mean any disrespect when I say this but!!! I haven't the time to do everybody's stuff for them..... You write what you need... I'll take a look at it and suggest / help you then.

The whole Idea is YOU need to learn this stuff.... Me doing it for you won't teach you anything..

The link I sent you has enough code in for you to change it and use it in your project...
 
I don't mean any disrespect when I say this but!!! I haven't the time to do everybody's stuff for them..... You write what you need... I'll take a look at it and suggest / help you then.

The whole Idea is YOU need to learn this stuff.... Me doing it for you won't teach you anything..

The link I sent you has enough code in for you to change it and use it in your project...



yeah man, your right. but i just need it badly :( im sorry
 
Why do you need it badly..... Usually its a homework project that students just shelve until 2 days before it's due in....

Is this the case.....

yeah your also correct about that too, but mine is a different scenario i dont have any knowledge at all in programming just basic codes like "if else then", my proffessor gave it to us (as a group were 2 actually) last monday but my group mate leave me behind yesterday because he needs to handle things in Chicago (personal problems) so he leaves also yesterday and he will arrive next week but the deadline of this project is on friday :( but i don't know were to start, i don't know were to go. i dont want to fail man in college this is one of our final requirements :( please. im begging you
 
anyone please help me :( i dont know ho wot do it, i only know the things that needed to be programmed but i dont know hot to program it in microC and C language :(
 
I can't help with mikroC.... I use Microchips own stuff

This was written in XC8 so it will need adjustments......
Code:
#include <htc.h>  
#define _XTAL_FREQ  4000000
#pragma config FOSC = 4;		// This equates to INTOSC + GPIO4 and 5
#pragma config WDTE = OFF		// Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF		// Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF		// Internal MCLR
#pragma config BOREN = ON		// Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF			// Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF		// Data Code Protection bit (Data memory code protection is disabled)
 
 
int OnPulse = 217;
int OffPulse = 38;
char TOG = 0;
 

void interrupt ISR(void)
	{
    T0IF = 0;
	TOG = ~TOG;
	if(TOG)						// Remember that TMR0 has a 
		{						// count of 255... Therefore
		GPIO5 = 1;				// to maintain a steady period
		TMR0 = OnPulse;			// The on time + the off time
		}						// must equal 255
	else
		{
		GPIO5 = 0;				// If On = 100 then off must be 155
		TMR0 = OffPulse;		// If on = 155 then off must be 100
		}
	}

int GetAdc(int ch)
	{
	ADCON0 = 0x81;					// 10 bit & turn on
	ADCON0 |= ch<<3;				// channel select
	GO = 1;
	while(GO);						// wait till done
	return (int)ADRESH *256 + ADRESL ;
	} 

int map(int x)
	{
	int retval = 0;					// Map to
	if(x < 100)	retval = 253;		// varying
	if(x < 95)	retval = 229;		// percentages
	if(x < 86)	retval = 204;		// of temp reading
	if(x < 84)	retval = 178;		//
	if(x < 82)	retval = 153;
	if(x < 80)	retval = 127;
	if(x < 78)	retval = 102;
	if(x < 76)	retval = 89;
	if(x < 70)	retval = 76;
	return retval;
	}
 
void main()
	{
	int result;
	TRISIO0 = 1;					// Input
	TRISIO5 = 0;					// output
	TMR0 = OnPulse;					// set initial timer 
	ANSEL = 0x11;					// FOSC/32 &  CH0
	OPTION_REG = 0;					// no prescaler
	T0IE = 1;						// Timer Interrupt on
	GIE = 1;						// Global Interrupt on
 
	while(1)
        {
 		result = GetAdc(0);			// get from LM35 connected on AN0
		result = map(result);		// map to percentage
		OffPulse = result;			// Set off time
		OnPulse = 255 - (unsigned char)result;	// set on time (cast to be sure)
        }
   }
 
float fLM,fPICAD, fPICC; // float specifies decimal numbers
float fA,fB,fC; // float specifies decimal numbers
int current_duty;


void PWM1_Init(const long freq)
{
PWM1_Init(20000); // Initialize PWM1 module at 20KHz
}

void main() {
fA = (float)10; //10mV
fB = 0.004882812F; //F stands for float number meaning its a decimal number
fC = (float)49/100; //float keyword means that the answer will be a decimal number

PWM1_Init;
current_duty = 0; // initial value for current_duty

while (1) {
if (fLM = .30)
{
fPICAD = .30/fB;
fPICC = fPICAD*fC;
current_duty = 30;

}
if(fLM = .31)
{
fPICAD = .31/fB;
fPICC = fPICAD*fC;
current_duty = 30;

}
if(fLM = .32)
{
fPICAD = .32/fB;
fPICC = fPICAD*fC;
current_duty = 30;

}
if(fLM = .33)
{
fPICAD = .33/fB;
fPICC = fPICAD*fC;
current_duty = 30;

}
if(fLM = .34)
{
fPICAD = .34/fB;
fPICC = fPICAD*fC;
current_duty = 30;

}
if(fLM = .35)
{
fPICAD = .35/fB;
fPICC = fPICAD*fC;
current_duty = 35;

}
if(fLM = .36)
{
fPICAD = .36/fB;
fPICC = fPICAD*fC;
current_duty = 35;

}
if(fLM = .37)
{
fPICAD = .31/fB;
fPICC = fPICAD*fC;
current_duty = 35;

}
if(fLM = .38)
{
fPICAD = .38/fB;
fPICC = fPICAD*fC;
current_duty = 40;

}
if(fLM = .39)
{
fPICAD = .39/fB;
fPICC = fPICAD*fC;
current_duty = 50;

}
if(fLM = .40)
{
fPICAD = .40/fB;
fPICC = fPICAD*fC;
current_duty = 60;

}
if(fLM = .41)
{
fPICAD = .41/fB;
fPICC = fPICAD*fC;
current_duty = 70;

}
if(fLM = .42)
{
fPICAD = .42/fB;
fPICC = fPICAD*fC;
current_duty = 80;

}
if(fLM = .43)
{
fPICAD = .43/fB;
fPICC = fPICAD*fC;
current_duty = 90;

}
if(fLM = .44)
{
fPICAD = .31/fB;
fPICC = fPICAD*fC;
current_duty = 90;

}
if(fLM = .45)
{
fPICAD = .45/fB;
fPICC = fPICAD*fC;
current_duty = 90;

}
if(fLM = .46)
{
fPICAD = .46/fB;
fPICC = fPICAD*fC;
current_duty = 90;

}
if(fLM = .47)
{
fPICAD = .47/fB;
fPICC = fPICAD*fC;
current_duty = 100;

}
if(fLM = .48)
{
fPICAD = .48/fB;
fPICC = fPICAD*fC;
current_duty = 100;

}
if(fLM = .49)
{
fPICAD = .49/fB;
fPICC = fPICAD*fC;
current_duty = 100;

}
if(fLM = .50)
{
fPICAD = .50/fB;
fPICC = fPICAD*fC;
current_duty = 0;

}
}
}






is that correct? please help me. :( im using PIC12F675
 
I can't help with mikroC.... I use Microchips own stuff

This was written in XC8 so it will need adjustments......
Code:
#include <htc.h>  
#define _XTAL_FREQ  4000000
#pragma config FOSC = 4;		// This equates to INTOSC + GPIO4 and 5
#pragma config WDTE = OFF		// Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF		// Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF		// Internal MCLR
#pragma config BOREN = ON		// Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF			// Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF		// Data Code Protection bit (Data memory code protection is disabled)
 
 
int OnPulse = 217;
int OffPulse = 38;
char TOG = 0;
 

void interrupt ISR(void)
	{
    T0IF = 0;
	TOG = ~TOG;
	if(TOG)						// Remember that TMR0 has a 
		{						// count of 255... Therefore
		GPIO5 = 1;				// to maintain a steady period
		TMR0 = OnPulse;			// The on time + the off time
		}						// must equal 255
	else
		{
		GPIO5 = 0;				// If On = 100 then off must be 155
		TMR0 = OffPulse;		// If on = 155 then off must be 100
		}
	}

int GetAdc(int ch)
	{
	ADCON0 = 0x81;					// 10 bit & turn on
	ADCON0 |= ch<<3;				// channel select
	GO = 1;
	while(GO);						// wait till done
	return (int)ADRESH *256 + ADRESL ;
	} 

int map(int x)
	{
	int retval = 0;					// Map to
	if(x < 100)	retval = 253;		// varying
	if(x < 95)	retval = 229;		// percentages
	if(x < 86)	retval = 204;		// of temp reading
	if(x < 84)	retval = 178;		//
	if(x < 82)	retval = 153;
	if(x < 80)	retval = 127;
	if(x < 78)	retval = 102;
	if(x < 76)	retval = 89;
	if(x < 70)	retval = 76;
	return retval;
	}
 
void main()
	{
	int result;
	TRISIO0 = 1;					// Input
	TRISIO5 = 0;					// output
	TMR0 = OnPulse;					// set initial timer 
	ANSEL = 0x11;					// FOSC/32 &  CH0
	OPTION_REG = 0;					// no prescaler
	T0IE = 1;						// Timer Interrupt on
	GIE = 1;						// Global Interrupt on
 
	while(1)
        {
 		result = GetAdc(0);			// get from LM35 connected on AN0
		result = map(result);		// map to percentage
		OffPulse = result;			// Set off time
		OnPulse = 255 - (unsigned char)result;	// set on time (cast to be sure)
        }
   }



siree, how do you program works (sorry i cant understand the program, im a newbie in this field :( )? is it also applicable if i burn it in PIC12F675?
 
Sir if ever are you familiar with PicBasic Pro?

Nope.... C is a far better language....

I have commented throughout.... If you download the free version on XC8... It will compile and give you a hex file...
 
ow i need to download the free version of XC8 then. Siree in your program what pin is the input and the output? is it the pin 2 of PIC12F675 is the output and the pin 3 is the input?
 
Sir Ian, by the way. thank you for your help and time, even if you don't know me you helped me, i appreciate it seriously. I pray my project works with the help of your program :) im currently downloading the free version of XC8 right now
 
Sir Ian, i already finished installing the XC8 but i can't open the program there is no application in the Microchip folder it only has the "UNINSTALL MPLAB XC8" shortcut :( how come :((
 
Status
Not open for further replies.

Latest threads

Back
Top