GammaRay86
New Member
Hey guys,
I want to know some things about some PIC registers and hopefully some of you can help me.
I'm working on the A/D conversion on the PIC16F684. Here is my code so far:
This code does not seem to be working. It kinda works in that the LED blinks on and off but not at the 1.8 V like it should. It seems to turn off ONLY at values of like 2 V. But is on ABOVE and BELOW that which my code clearly does not say. So I need to ask some questions.
1. Do I need to set port A to be initially 0? e.g. PORTA = 0?
2. What is the difference between the ANSEL register and the CHS bits on ADCON0?
3. Do I need to set a delay between turning the ADC on and making the Go/Done bit 1?
4. Am I doing the AD value calculation correct assuming left justification? I assume the ADRESH bits should be multiplied by 2^8 (256) and 2^9 (512)?
5. I set the clock to internal RC. Is this okay?
5. ANYTHING anyone can see that is wrong? I saw some code where a comparator was shut off before the ADC. Is this required?
Thanks for any help
I want to know some things about some PIC registers and hopefully some of you can help me.
I'm working on the A/D conversion on the PIC16F684. Here is my code so far:
Code:
#pragma chip PIC16F684
#include "delay.h"
void main(void){
int adval, a, b; // adval is the ADC value
TRISA.0 = 1; // Set RA0/AN0 (pin 13) as I/P
TRISC.2 = 0; // Set RC2/AN6 (pin 8) as a dig O/P
ANSEL.0 = 1; // Set RA0/AN0 (pin 13) as analog I/P
ADCON1 = 0b.0011.0000; // Clock set to internal Frc
while(1){
//Do the sensor conversion
delay_us(50); // Set delay of 50 us for sampling (TAD)
ADCON0 = 0b.1000.0011; // Right justified, Vref = Vdd,
// Read from AN0 (pin 13), Go/Done = 1, ADC Enabled
while(ADCON0.1 == 1){} // A/D conversion started, continue to check Go/Done, when 0 it is completed
// Bit 1 of ADCON0 is cleared when finished
a = ADRESH.1; // Read bit 1 of ADRESH into variable a
a = a * 512; // Multiply "a" by the position of that bit (2^9)
b = ADRESH.0; // Read bit 0 of ADRESH into variable b
b = b * 256; // Multiply "b" by the position of that bit (2^8)
adval = ADRESL + a + b; // ADC value is the sum of ADRESL, a, and b
if(adval <369){ // LED on at RC2 (pin 8) when AN0 < 1.8V, off > 1.8V
PORTC = 0b.0000.0100;
}
else {
PORTC = 0b.0000.0000;
}
}
}
This code does not seem to be working. It kinda works in that the LED blinks on and off but not at the 1.8 V like it should. It seems to turn off ONLY at values of like 2 V. But is on ABOVE and BELOW that which my code clearly does not say. So I need to ask some questions.
1. Do I need to set port A to be initially 0? e.g. PORTA = 0?
2. What is the difference between the ANSEL register and the CHS bits on ADCON0?
3. Do I need to set a delay between turning the ADC on and making the Go/Done bit 1?
4. Am I doing the AD value calculation correct assuming left justification? I assume the ADRESH bits should be multiplied by 2^8 (256) and 2^9 (512)?
5. I set the clock to internal RC. Is this okay?
5. ANYTHING anyone can see that is wrong? I saw some code where a comparator was shut off before the ADC. Is this required?
Thanks for any help