Author Topic: ADC on PIC12F1571 no response  (Read 1154 times)

0 Members and 1 Guest are viewing this topic.

Offline step_sTopic starter

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
ADC on PIC12F1571 no response
« on: June 19, 2017, 12:11:57 am »
Hi guys.
Wanted to program a simple ADC, and I remember using the code before, but now it doesn't seem to work.
I have hooked up a pot to RA4, and wanted it to just pull RA2 high or low, depending on the threshold.
The ADC doesn't seem to be running, so I'm a little confused as to what part of the code that's not working. Everything else on the PIC is working like it should.

Code: [Select]
unsigned int ADC;

void main(void) {
   
    TRISA = 0b00010000; //RA4 input
    ANSELA = 0b00010000; //RA4 analog
    ODCONA = 0b00000000; //No open-drain
    OSCCON = 0b11110000; //32MHz internal
   
    ADCON0 = 0b00001101; //Set pin R4 AN3, Enable ADC
    ADCON1 = 0b10000000; //Right justified, Fosc/2, VDD as ref+
   
   
    while(1)
    {
        GO_nDONE = 1; //Initiate ADC
       
        while(GO_nDONE); //Wait for ADC
       
        ADC = (ADRESH << 8 | ADRESL); //Gather result in 1 integer
       
        if (ADC >= 100) { //If voltage is high, set RA2 high
            RA2 = 1;
        }
        if (ADC <= 100) { //If voltage is low, set RA2 low
            RA2 = 0;
        }
    }
   
    return;
}
Hope you can help.
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: ADC on PIC12F1571 no response
« Reply #1 on: June 19, 2017, 01:25:52 am »
ADCON1 = 0b10000000; //Right justified, Fosc/2, VDD as ref+

I think you have to use Fosc/32 with a 32MHz osc.
« Last Edit: June 19, 2017, 01:27:37 am by StillTrying »
.  That took much longer than I thought it would.
 
The following users thanked this post: step_s


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf