/*
* Project name:
Design of a PIC12F683 based temperature controlled DC fan
* Revision History:
29th September 2015:
- initial release (omega6)[url] https://www.edaboard.com/threads/343938/[/url]
09th October 2015:
- modified release (Philippe LE GUEN) [url]https://pleguen.fr/[/url]
* Description:
A simple example of using the ADC library.
ADC results is used to command a fan motor (DC 12V).
* Test configuration:
MCU: PIC12F683
[url]https://ww1.microchip.com/downloads/en/DeviceDoc/41211D_.pdf[/url]
dev.board: EasyPIC7
[url]https://www.mikroe.com/easypic/[/url]
Oscillator: Internal Oscillator (HFINTOSC), 8.0000 MHz (no crystal)
Ext. Modules: a DC12V motor fan
SW: mikroC PRO for PIC
[url]https://www.mikroe.com/mikroc/pic/[/url]
* NOTES:
- The motor fan is supply by a +12V auxiliary, commanded by a transistor NPN BC547 with the PWM signal on GP2.
*/
//const unsigned short VREF = 5.0; // The current value is +5,0V, so is equal to +5000mV...
const unsigned short VREF = 4.90; // Warning ! The VDD power on EaysiPIC7 is 4,90V (measured)
unsigned int adc_sample, Temp_C, Temp_Old, offset;
void main() {
ADC_Init(); // Initialize ADC
ADCON0 = 0b10000011; // Set VDD as Vref, ANO as analog read, Right justified (very important for a good AD Conversion !)
ANSEL = 0b00110000; // bit 6-4 ADCS<2:0>: A/D Conversion Clock Select bits (see datasheet)
// x11 = FRC (clock derived from a dedicated internal oscillator = 500 kHz max)
TRISIO.B0 = 1; // Set pin 7 (ANO) as input, used for ADC read
TRISIO.B2 = 0; // Set pin 5 (CCP1) as output, used for PWM
PWM1_Init(5000); // Initialize PWM at 19.61kHz (per pic spec-sheet allows 10bit resolution)
PWM1_Start();
while(1){
//Read ADC Channel 1
adc_sample = ADC_Get_Sample(0); // Get 10-bit results of AD conversion
//adc_sample = ADC_Read(0);
// for indication, VREF is a value in Volt...
Temp_C = (adc_sample * VREF)/10.240; // Calculate temperature in Celsuis
// The LM35 delivers +246mV at room temperature of +22.7°C case (measured)
// which corresponds to +10,83700441mV/°C (10mV/°C in theory), due to component tolerance...
if(Temp_C != Temp_Old) {
//if(Temp_C >= 30)
if(Temp_C > 21) {
PWM1_Set_Duty(255); // Set PWM to 100%
}
/*if(20 <= Temp_C <= 21) { // you can adapted this code for your application testing... (is perfectible)
PWM1_Set_Duty(127);
}*/
else
if(Temp_C < 20) {
PWM1_Set_Duty(0); // Turn off PWM
}
Temp_Old = Temp_C;
}
Delay_ms(500);
}
}