'how to combine ADRESH and ADRESL on 12 bit ADC
MICRO: PIC18LF47K42 compiler: XC8 application: MPLABX
Hi, im trying to combine the values in my 12 bit ADC. they go into ADRESH and ADRESL. my ADC is set up for right-justify which does formating as so: ADRESH:(----MSB,x,x,x) ADRESL: (X,X,X,X,X,X,X,LSB)
It's my first tiem ever working with an ADC and admitably i dont know very muc habout them. But from inspecting the value in my result register i can tell i dont have a great resolution. Im pretty sure its becasue of how im combining ADRESH and ADRESL. how could i do this? again its a 12Bit ADC. its being tested wit han LDR, when theres no light i get 0, whic his fine but when theres slight light it goes t oa decimal value of 48, so i increase the brightness slightly again and it stays at 48. until a large jump to whic hit jumps to 64 or something.
#include "myIncludes.h"
volatile unsigned char ZCDSoftwareFlag = 0;
volatile unsigned char switchValue = 0;
void main(void)
{
portInit();
triac = 0;
unsigned char result;
adcInit();
while(1)
{
__delay_us(4);
ADCON0bits.GO = 1; //Start conversion
while (ADCON0bits.GO); //Wait for conversion done
result = ADRESH;
result = result << 8;
result = result |ADRESL;
}
}
And heres the ADC init function
void adcInit(void)
{
ADCON0bits.FM = 1; //right-justify
ADCON0bits.CS = 1; //ADCRC Clock
ADPCH = 0x00; //RA0 is Analog channel
ADCON0bits.ON = 1; //Turn ADC On
ADCON0bits.GO = 1; //Start conversion
}
Solution 1:[1]
You try to put an 12Bit result in a 8 Bit variable. Switch it to 16Bit.
uint16_t result;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |