Skip to content

Instantly share code, notes, and snippets.

@bkerley
Forked from anonymous/PWMfanController.c
Created October 20, 2011 18:54
Show Gist options
  • Save bkerley/1301963 to your computer and use it in GitHub Desktop.
Save bkerley/1301963 to your computer and use it in GitHub Desktop.
/*This code for a PIC12F615 compiles in mikroC for PIC http://www.mikroe.com
It takes an analog value from a dash-mounted potentiometer and drives a MOSFET proportional to the input. The MOSFET pulses the low-side of a blower motor in
a 1997 Mitsubishi Mirage, providing 128 speeds*/
void main() {
int anin=0, ramped=255;
unsigned long temp = 0;
TRISIO = 0x01; //set GPIO0 as input
ANSEL = 0x01; //use AN0 (0C for 2 and 3
//C1ON_bit = 0;
//GPIO = 0x00;
PWM1_Init(18000);
PWM1_Set_Duty(0);
PWM1_Start();
Delay_ms(1000); //Delay fan startup
while(1) {
PWM1_Set_Duty(ramped);
temp = ADC_Read(0); //Read in 10-bit analog value
anin = temp >> 3; //Shift the 10-bit value to only 7 bits
//and store in a 1-byte variable
anin += 0b10000000; //Add offset so anin is between 128 and 255
if (anin<ramped) {
ramped-=1; //If desired (anin) is low, decrement ramped
}
if (anin>ramped) {
ramped+=1; //If desired (anin) is higher, increment ramped
}
PWM1_Set_Duty(ramped);
Delay_ms(20); //slow down the ramping
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment