Hello All,
I am having trouble getting the A2D to work on PIC16f1939. I am using CCS compiler 5.007.
The CCS comes with the A2D function that works fine but when I follow the manual method( more control) I couldn't get it work.
Can anyone point out what I am doing wrong? your help is appreciated it.
Thank you!
I am having trouble getting the A2D to work on PIC16f1939. I am using CCS compiler 5.007.
The CCS comes with the A2D function that works fine but when I follow the manual method( more control) I couldn't get it work.
Can anyone point out what I am doing wrong? your help is appreciated it.
Thank you!
Rich (BB code):
#include <16F1939.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOPROTECT, NOMCLR,VCAP_A6
#use delay(clock = 32000000)//16000000)//8000000)
#include "flex_lcd.c"
#use i2c(master, sda=PIN_C4, scl=PIN_C3)
#include <stdlib.h>
#use fast_io (B)
#byte GPIOB = 0xd
#bit GPIOB_1 = GPIOB.1
#byte TRISIOB = 0x08D
#bit TRISIOB_1= TRISIOB.1
#byte ANSELB = 0x18D
#bit ANSELB_1= ANSELB.1
#byte ADCON0=0x9d
#bit ADCON0_0=ADCON0.0
#bit ADCON0_1=ADCON0.1
#bit ADCON0_2=ADCON0.2
#bit ADCON0_3=ADCON0.3
#bit ADCON0_4=ADCON0.4
#bit ADCON0_5=ADCON0.5
#bit ADCON0_6=ADCON0.6
#byte ADCON1=0x9e
#bit ADCON1_7=ADCON0.7
#bit ADCON1_6=ADCON0.6
#bit ADCON1_5=ADCON0.5
#bit ADCON1_4=ADCON0.4
//PERIPHERAL INTERRUPT ENABLE REGISTER 1
#byte PIE1= 0x91
#bit PIE1_6=PIE1.6
#byte ADRESH=0x9c
#byte ADRESL=0x9B
//FUNCTION PROTOTYPE
void adc_init(void);
int16 sample(void);
void main()
{
int16 adc=0;
lcd_init();
adc_init();
While(1)
{
adc=sample();
delay_ms(10);
printf(lcd_putc,"\f%s%5lu","ADC:",adc);
}
}
void adc_init(void)
{
//CONFIGURE PORT RB1/AN10 AS DIGITAL:
TRISIOB_1=1; //CONFIGURE PIN RB1 AS INPUT.
//CONFIGURE PIN AS ANALOG
ANSELB_1=1; // SET UP PIN RB1 AS ANALOG.
//Select ADC conversion clock Fosc/64
ADCON1_6=1;
ADCON1_5=1;
ADCON1_4=0;
//Select Analog Channel 10 (AN10)
//ADCON0 |=01010000;
ADCON0_6=0;
ADCON0_5=1;
ADCON0_4=0;
ADCON0_3=1;
ADCON0_2=0;
//10 bit results should be right justified
ADCON1_7=1;
}
int16 sample(void)
{
// variable declaration
int high;
int low;
int16 adc;
//Disables the ADC interrupt
PIE1_6=0;
//Turn on the A2D converter
ADCON0_0=1;
//start the conversion (Go)
ADCON0_1=1;
//wait for the acquisition time
delay_us(60);
//when ADCON0_1=1 the A2D conversion is not done
while(ADCON0_1==1)
//wait till the conversion is finished
//Read the value of ADRESH and ADRESL
high= ADRESH;
low= ADRESL;
//store the resulting A2D conversion
//high and low byte into one 16 bit variable
adc=make16(high,low);
return(adc);
}