CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Temperature controlled fan using PIC 16F877A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jwoo



Joined: 22 Dec 2017
Posts: 5

View user's profile Send private message

Temperature controlled fan using PIC 16F877A
PostPosted: Mon Jan 01, 2018 2:34 pm     Reply with quote

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: 19186

View user's profile Send private message

PostPosted: Mon Jan 01, 2018 3:07 pm     Reply with quote

'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

View user's profile Send private message

PostPosted: Mon Jan 01, 2018 3:39 pm     Reply with quote

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: 19186

View user's profile Send private message

PostPosted: Tue Jan 02, 2018 2:09 am     Reply with quote

Smile

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: 9073
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jan 02, 2018 11:16 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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