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

fan motor PWM

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
davidnclare



Joined: 08 Sep 2010
Posts: 9

View user's profile Send private message

fan motor PWM
PostPosted: Wed Oct 06, 2010 2:31 pm     Reply with quote

My first real program, bits borrowed of here and by reading the CCS reference manual and the PIC data sheet.
I hope it may be of interest to somebody.
My first post also.


Code:
//this is a little test PWM program to drive a DC fan.
// the fan allready has some control circuit built into it.
//I susspect it has PIC and MOSFET controlling it.
//Some have been failing on site so I need to test it in the workshop.
//The fan manufacturers specified the PWM should be 1Khz
//Fan spec  DC% 0 to 2 invalid run at default
//              5% motor RPM = 0
//             10% motor RPM = Minium
//             90% motor RPM = Maximum
//             95% reverse at marimum RPM
//           98 to 100% invalid run at default
// default speed 4600RPM for 24Vdc fans
// I know this program is not the best but it was a good learning experience
// to produce it. I do like making electronics do stuff.
#include <16f818.h>
#device ADC=8
#device ICD=TRUE

#use delay (internal=8mhz)
#define RIGHT_LED PIN_B0
#define LEFT_LED PIN_B1

void main(){
      int16 value,potinput,scale;
      value=25;
      //Timer 2 set up 124 is calculated duty resolution 500 max 1024 10bit
      setup_timer_2(T2_DIV_BY_16,124,1);
      setup_ccp1(CCP_PWM);
      setup_port_a(AN0);
      setup_adc(ADC_CLOCK_DIV_16);
      set_adc_channel(0);
      delay_us(20);
     
while(TRUE){
      output_low(RIGHT_LED); //Flashing led's so I know the program is running
      output_high(LEFT_LED); //I could have used output_toggle()
      delay_ms (250);
      output_high(RIGHT_LED);
      output_low(LEFT_LED);
      delay_ms (250);
      potinput=read_adc();
      //potinput 8 bit ADC therefore 0 to 255
      //max pwm 90%, 90% of 500 is 450 so max is 450-25=425
      //425=255*1.6+25
      //value=25 min5%
      scale=potinput*1.6;
      set_pwm1_duty(scale+value); 
}

}
dpechman



Joined: 04 Dec 2007
Posts: 43

View user's profile Send private message Visit poster's website

PostPosted: Fri Feb 11, 2011 5:42 am     Reply with quote

here a variation using software controlled pwm

Code:

/*
FAN SPEED CONTROL
SERIAL RECEIVE FORMAT(in X%): "speed=X%;"
*/

#include <16F877A.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT

#define FAN      PIN_B2

#include <stdlib.h>

char string[10], c[31];
boolean reading=false;
char valorstring[10];
int index=0, startreading=0, endreading=0, tamanho=0, speed=0, time=0;

#use rs232(baud=9600, rcv=SERIAL_RX, xmit=SERIAL_TX) 

#int_TIMER0
void TIMER0_isr(){
   if(++time >= speed){
      output_low(FAN);
   }
   else{
      output_high(FAN);
   }
   
   if(time >= 100) time = 0;
}

#int_RDA
void rda_isr() {
   int i;
   if(kbhit()){         
     
       c[++index]=getc();
       if(c[index]=='#' || index > 30){
         index=0;
       }
       
       if(c[index]=='='){
         inicioleitura=index;
         reading=true;
       }
       
       if(c[index]=='%' && reading==true)
       {
         fimleitura = index;
         reading = false;
         size = endreading - startreading;
         
         z=0;
         
         for(y = startreading+1; y < endreading + size; y++){
            valorstring[z++] = c[y];
         }

         strcpy(string, valorstring);

         speed = atoi(string);

         c="";
         
         y=0;
         
       }
   }
}

void main(){

   setup_timer_0(RTCC_INTERNAL | RTCC_8_BIT | RTCC_DIV_64);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   while( true )
   {
     
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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