Last active
December 11, 2015 18:58
-
-
Save omerk/4645120 to your computer and use it in GitHub Desktop.
Playing with the ADC on a PIC10F222
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <xc.h> | |
#include <stdint.h> | |
#define _XTAL_FREQ 4000000 // required for __delay_ms() | |
#define DELAY 1000 | |
#define THRESHOLD 100 | |
#pragma config MCLRE=OFF, MCPU=OFF, CP=OFF, WDTE=OFF, IOSCFS=4MHZ | |
void | |
blink () | |
{ | |
GPIObits.GP0 = 1; | |
__delay_ms(300); | |
GPIObits.GP0 = 0; | |
__delay_ms(300); | |
} | |
int | |
main (void) | |
{ | |
TRISGPIO = 0b00000010; // GP1 input | |
// GP1 = analog, GP0 = digital | |
ADCON0bits.ANS1 = 1; | |
ADCON0bits.ANS0 = 0; | |
// select AN1 | |
ADCON0bits.CHS1 = 0; | |
ADCON0bits.CHS0 = 1; | |
// turn ADC on | |
ADCON0bits.ADON = 1; | |
blink(); | |
for(;;){ | |
// start conversion | |
ADCON0bits.GO = 1; | |
// wait until conversion finishes | |
while (ADCON0bits.nDONE); | |
// read result and set output as necessary | |
if(ADRES > THRESHOLD){ | |
GPIObits.GP0 = 1; | |
} else { | |
GPIObits.GP0 = 0; | |
} | |
//blink(); | |
__delay_ms(DELAY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nope, simply a case of using the wrong value for the ADC mux. Spotted by @MarkAtMicrohip: "Looks like you have CHS0 and CHS1 backward. You're always looking at the internal voltage reference." 😞