#include <p16f628.h>
//******************************************************************
// Virtual AD 2003
// Julio Cesar Palacios Reyes
//
// PIC 16F628
//
// V_AD Return a value Between 0 and 69
// and voltage_value = (V_AD * VDD) / 96
//
// It works with the voltage reference and comparator modules.
//
// It has 4 chanels (A0, A1, A2, A4)
//*******************************************************************
#define A0_C1out 0x02
#define A1_C2out 0x02
#define A3_C1out 0x0A
#define A2_C2out 0x0A
#define C1out 0x40
#define C2out 0x80
#define VR_Low_mode 0x20
#define VR_High_mode 0x00
#define Voltage_reference_on 0x80
#define MAX_VALUE 96
//****************************************************************
V_AD(char chanel)
{
char VR_mode,maskcomp,VR_value;
//config comparator reference module
//Four input multiplexed to to comparators
//AX_CYout: conect PORTA input pin AX to CY comparator output.
if(chanel==0) {cmcon=A0_C1out;
maskcomp=C1out;}
if(chanel==1) {cmcon=A1_C2out;
maskcomp=C2out;}
if(chanel==2) {cmcon=A2_C2out;
maskcomp=C2out;}
if(chanel==3) {cmcon=A3_C1out;
maskcomp=C1out;}
//Voltage Reference in low mode
VR_mode=VR_Low_mode;
// find the closer Voltage Reference value to the signal input
// for(VR_value= 0v to .71(Vdd))
for(VR_value=0x00;VR_value<=0x0F;VR_value++)
{
if(VR_value==6 && VR_mode==VR_Low_mode) {VR_value=0;
VR_mode=VR_High_mode;}
//config Voltage Reference Module
vrcon=Voltage_reference_on | VR_mode | VR_value;
//Read the comparator output and if it is higer or equal than
//the input signal return the value
if(cmcon & maskcomp){if(VR_mode==VR_Low_mode) return(4*VR_value);
else return(24+3*VR_value);}
}
// if not
return(MAX_VALUE);
}
//*****************************************************************
//Example:Read Chanel 0 and Send the value to PORTB
main()
{
//trisb as output and trisa as input
trisb=0x00;
trisa=0xFF;
while(1)
{
//Call virtual AD with chanel 0 (A0)
// and send the result to portB
portb=V_AD(0);
delay_ms(200);
}
}
Copyright © 2002-2006 SourceBoost Technologies