|
|
View previous topic :: View next topic |
Author |
Message |
davidnclare
Joined: 08 Sep 2010 Posts: 9
|
fan motor PWM |
Posted: Wed Oct 06, 2010 2:31 pm |
|
|
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
|
|
Posted: Fri Feb 11, 2011 5:42 am |
|
|
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 )
{
}
}
|
|
|
|
|
|
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
|