PIC12F685 is an 8-pin DIP PIC microcontroller. However, it has a lot peripherals such as 10-bit ADC, timer, large program memory and EEPROM.
It has up to 6 inputs/outputs, if we internal RC oscillator, and disabling the master clear reset (/MCLR) pin.
Since it has only 6 inputs/outputs, we need more output expanding. I use six 74HC595 shift register to drive 6 digits seven-segment display.
The overall 74HC595 combination interface to the PIC12F685 with only three pins:
serial clock
serial data
and latch pin
The PIC12F685 shifts data to the display system serially using these three pins.
LM35 is an analog temperature sensor, comes with three pins:
- DC supply voltage
- analog temperature data out
- DC supply ground
The DC supply voltage ranges from +4 V to +30 V. But it is typically supplied at +5 V.
The analog temperature output is +10 mV per degree Celsius. With a 10-bit ADC module of PIC12F685 could precisely convert the analog temperature.
In this application, I use the temperature sensor to condition a DC fan. The fan will turn on if the temperature exceed 30 degree Celsius.
In this situation the system could only read the non negative temperature data, due to insufficient analog configuration pins.
The source code is written in CCS PICC compiler. The compiler has a lot of libraries. The 74HC595 is implemented with the compiler ready-to-use library.
#include<12F683.h>
//set the ADC to 10-bit
#device adc=10
#fuses INTRC_IO,NOWDT,NOMCLR
#use delay(clock=4M)
#define EXP_OUT_ENABLE PIN_A2
#define EXP_OUT_CLOCK PIN_A0
#define EXP_OUT_DO PIN_A1
#define NUMBER_OF_74595 6
#include<74595.c>
#define FAN PIN_A5
void main(){
unsigned char shiftData[6];
unsigned char ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned int16 converter;
long adcResult;
float temperature;
setup_oscillator(OSC_4MHz);
output_low(FAN);
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(3);
delay_ms(10);
read_adc(adc_start_only);
while(1){
adcResult=read_adc();
while(!adc_done());
//convert the analog voltage to temperature
temperature = (5.0*adcResult/1024)*1000;
converter=(int16)temperature;
//display the temperature data
shiftData[0]=ssd[converter/1000];
if(converter<1000) shiftData[0]=0;
shiftData[1]=ssd[(converter%1000)/100];
if(converter<100) shiftData[1]=0;
shiftData[2]=ssd[(converter%100)/10]|0x80;
shiftData[3]=ssd[converter%10];
shiftData[4]=0b01100011;
shiftData[5]=0b00111001;
write_expanded_outputs(&shiftData);
//conditioning the fan
if((temperature/10)>30) output_high(FAN);
else output_low(FAN);
delay_ms(1000);
}
}
The overall program require 44% of the 2048 word of the PIC12F683 program memory.
Simulation screen shot, The temperature reading is 29.7 degree Celsius. The DC fan is turned off. |
No comments:
Post a Comment