|
|
View previous topic :: View next topic |
Author |
Message |
newaprendiz
Joined: 09 Jul 2013 Posts: 1
|
Temperature Controlled Fan with LCD using 16F877A |
Posted: Sat Jul 13, 2013 3:38 am |
|
|
Hi guys. I am learning PIC programming with this little project. I used LM35 sensor to read analog temperature. 16F877A for A/D conversion, an LCD, and a fan. PIC produces proportional duty cycle for different different ranges i.e. if adc output is 27C, it produces 25% duty cycle which controls fan speed. Temperature and duty cycle is displayed on LCD.
Code: |
#include <16F877A.h>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4M)
#include <lcd.c>
//=============================================================================
int main()
{
int temp;
setup_adc_ports(RA0_analog);
setup_adc(adc_clock_internal);
set_adc_channel(0);
lcd_init();
printf(lcd_putc, "\f");
delay_ms(1000);
for(;;)
{
temp = (float) read_adc()/2;
if (temp >= 20 && temp <= 25)
{
setup_ccp1(ccp_pwm);
set_pwm1_duty(31);
setup_timer_2(t2_div_by_16, 127, 1);
printf(lcd_putc, "\f");
printf(lcd_putc, "Temperature:%d C", temp);
printf(lcd_putc, "\nDuty Cycle : %d", 25);
delay_ms(1000);
}
else if (temp >= 26 && temp <= 30)
{
setup_ccp1(ccp_pwm);
set_pwm1_duty(62);
setup_timer_2(t2_div_by_16, 127, 1);
printf(lcd_putc, "\f");
printf(lcd_putc, "Temperature:%d C", temp);
printf(lcd_putc, "\nDuty Cycle : %d ", 50);
delay_ms(1000);
}
else if (temp >= 31 && temp <= 35)
{
setup_ccp1(ccp_pwm);
set_pwm1_duty(93);
setup_timer_2(t2_div_by_16, 127, 1);
printf(lcd_putc, "\f");
printf(lcd_putc, "Temperature:%d C", temp);
printf(lcd_putc, "\nDuty Cycle : %d", 75);
delay_ms(1000);
}
else if (temp >= 36 && temp <= 40)
{
setup_ccp1(ccp_pwm);
set_pwm1_duty(124);
setup_timer_2(t2_div_by_16, 127, 1);
printf(lcd_putc, "\f");
printf(lcd_putc, "Temperature:%d C", temp);
printf(lcd_putc, "\nDuty Cycle : %d", 100);
delay_ms(1000);
}
else if (temp <= 24 && temp >= 40)
{
setup_ccp1(ccp_pwm);
set_pwm1_duty(0);
setup_timer_2(t2_div_by_16, 127, 1);
printf(lcd_putc, "\f");
printf(lcd_putc, "Temperature:%d C", temp);
printf(lcd_putc, "\nToo low/high temp !!");
output_high(pin_c0);
delay_ms(1000);
}
}
return 0;
}
|
The problem is fan and lcd part is not working on proteus (Haven't done it on hardware yet), No display on lcd.
I would really appreciate your help. Thanks.
Here is the schematic:
http://sdrv.ms/1dtH5Bu |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Jul 13, 2013 4:33 am |
|
|
Start by reading the sticky at the top of this forum.....
Proteus teaches you nearly nothing about how to get a PIC working. A series of problems. First PIC's differ from what they are 'meant' to do, and the simulator does not know this. Second, the simulator does not simulate the analog effects round the chip as digital signals change. third the simulator accepts what it is told, even if this is impossible (power clock etc..).
It is possible to use the isis simulator to help setting up some things, but only once you actually understand a PIC. Starting with the simulator, is a bit like starting trying to learn how to hammer nails, by putting your hand on the table, and practising by hitting your fingers.....
Now learn to debug things by getting _one_ thing working at a time. Start by simply flashing an LED. Is it flashing, and at the right speed. Then, can you display a 'hello world' message on the display?. If not, then look at the connections for this.
Step at a time. Get the display working. Then read a temperature and display this. Then try to control the fan. This is the 'core' part of debugging. Build yourself the real hardware and go step by step into this. Trying to do too much is a sure way of just wasting time.
There are quite a few problems with the code, but you need to start by getting the display working first. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Jul 13, 2013 5:12 am |
|
|
Mr. T is right again, you have to crawl before you can walk let alone run!
As a 'first project' you must break it down into small,unique sub-programs and get each one to work on it's own.While not a huge program,you an never 'debug' what's wrong.
Along with Mr. T's comments I'd like to add..
1) replace the variable name 'temp' to say 'LM35rawdata'.Since modern programs allow more than2 characters for a variable, giving variables 'self describing' names makes it a LOT easier to follow what they are,where they're from,what they do.Also 'temp' usually describles something 'temporary' not temperature.
2) You've told the compler to use the ADC in 10 bit mode, but save the result into an 8 bit variable.It's best to start in 8 bit mode.I've used the LM35 since the mid '80s in energy control products and you do not need 10 bit mode.
3)Read the datasheet very carefully,especially the ADC section. It is very,very rare that anyone uses the internal clock for the ADC.
4)In your 'If temperature is xxx, set PWM' logic , your last command is to delay for 1 second.You do this 5 times.Although it will work, better coding would be to have one delay at the end of the read-decide-display loop.
5)You turn on an LED (pin_C0) if the LM35 reading is too high, but never reset it when the temperature goes below the setpoint.
6)You've got a return (0); Definitely wrong and not required.
7) GET rid of Proteus, or at the very least NEVER believe what it says!
hth
jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|