View previous topic :: View next topic |
Author |
Message |
jwoo
Joined: 22 Dec 2017 Posts: 5
|
Temperature controlled fan using PIC 16F877A |
Posted: Mon Jan 01, 2018 2:34 pm |
|
|
Hello, I use 12V fan(2 wire), L293D, I want to adjust the speed of the fan according to the temperature. Fan is running but fan speed does not change according to temperature. I do not understand what the problem is, I'm happy to help.
Code: | #define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections
#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#device ADC=10
#use delay(clock = 4MHz)
#include <lcd.c>
char temperature[] = " 00.0 C";
unsigned int16 temp;
void motor();
void main(){
setup_adc(ADC_CLOCK_DIV_8); // Divisor for 4 MHz crystal
setup_adc_ports(AN0); // Configure AN0 pin as analog
set_adc_channel(0); // Select channel 0 (AN0)
delay_us(20);
lcd_init();
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 125, 1); //Setting timers for PWM
set_timer1(0);
lcd_putc('\f'); // Clear LCD
lcd_gotoxy(1, 1); // Go to column 1 row 1
printf(lcd_putc, "Temp:");
while(TRUE){
delay_ms(1000);
temp = read_adc() * 0.489; // Read analog voltage and convert it to degree celsius (0.489 = 500/1023)
if (temp > 99)
temperature[0] = 1 + 48; // Put 1 (of hundred)
else
temperature[0] = ' '; // Put space
temperature[1] = (temp / 10) % 10 + 48;
temperature[2] = temp % 10 + 48;
temperature[5] = 223; // Degree symbol
lcd_gotoxy(6, 1); // Go to column 5 row 2
printf(lcd_putc, temperature); // Display LM35 temperature result
motor(); }
}
void motor()
{
if(temperature<10)
{
setup_ccp1(CCP_OFF);
}
if(temperature>10)
{
setup_ccp1(CCP_PWM);
if(temperature>=10&&temperature<=29)
{
set_pwm1_duty((int16)204); //20% duty cycle PWM
}
else if(temperature>=30&&temperature<=35)
{
set_pwm1_duty((int16)510); //50% duty cycle PWM
}
else if(temperature>=36&&temperature<=45)
{
set_pwm1_duty((int16)715); //70% duty cycle PWM
}
else if(temperature>=46&&temperature<=150)
{
set_pwm1_duty((int16)919); //90% duty cycle PWM
}
}}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Jan 01, 2018 3:07 pm |
|
|
'temperature' is declared as a character array.
Your test test it as if it is a numeric value. It isn't.
temperature<10 won't work at all.
temperature' where you use it will be the address of the array in memory. Won't change.....
You have the numeric value in 'temp'. It is this value that you need to be testing against. |
|
|
jwoo
Joined: 22 Dec 2017 Posts: 5
|
|
Posted: Mon Jan 01, 2018 3:39 pm |
|
|
Ttelmah wrote: | 'temperature' is declared as a character array.
Your test test it as if it is a numeric value. It isn't.
temperature<10 won't work at all.
temperature' where you use it will be the address of the array in memory. Won't change.....
You have the numeric value in 'temp'. It is this value that you need to be testing against. |
Yeaa my fool, thank you. Now it works properly. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jan 02, 2018 2:09 am |
|
|
Perhaps the lesson, is to think about your names.
If you called the text array 'degree_text', and the temperature numeric value 'temperature', it'd have been obvious where it was going wrong...
Glad it worked. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jan 02, 2018 11:16 am |
|
|
re: 'fan'
50% duty cycle won't give you 50% speed or volume of air, same holds true for 30%. you need to make some tests to get the real numbers.
also
if the 'fan' has a tachometer output ( aka PC CPU cooling fan) it's easy to get the actual RPM of the fan and control the speed.
just stuff to know and ponder about.
Jay |
|
|
|