Jump to content

Recommended Posts

Hi there. I'm reasonably aufait with pic devices, but I seem to have hit a wall doing basic a to d conversion. I'm using a 18F4520, and using the AN0 input. I include the code here. I've broken the adcon0-2 registers to individual bits, hopefully making things more readable (or maybe not)!

The problem is, the the display doesn't reflect the position of the variable resistor I have on AN0 (which obviously has the ends tied to Vdd and Vss respectively. Nothing changes when I move the vr too! I'm missing something obvious here.

#include <system.h>
#include <stdio.h>

//Those pesky config bits!
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PWRT = ON
#pragma config PBADEN = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma CLOCK_FREQ 20000000

extern void init_display(void);
extern void cls(void);
extern void lprint(char*);
extern void gotoxy(char,char);

void main()
trisa = 255;
trisb = 0;    //Used by display
char disp[80];
unsigned short value;    //Value returned by conversion
clear_bit (adcon0, CHS0);    //AN0
clear_bit (adcon0, CHS1);    //
clear_bit (adcon0, CHS2);    //
clear_bit (adcon0, CHS3);    //

clear_bit (adcon1, PCFG0);    //Just AN0 a to d
set_bit (adcon1, PCFG1);    //
set_bit (adcon1, PCFG2);    //
set_bit (adcon1, PCFG3);    //
clear_bit (adcon1, VCFG0);    //+ve ref
clear_bit (adcon1, VCFG1);     //0v ref

set_bit (adcon2, ACQT0);    //Aquisition time 
clear_bit (adcon2, ACQT1);    //
set_bit (adcon2, ACQT2);    //
set_bit (adcon2, ADCS0);    //Clock
clear_bit(adcon2, ADCS1);    //
set_bit (adcon2, ADCS2);    //
set_bit (adcon2, ADFM);        //Justification

set_bit(adcon0,ADON);    //Turn on
set_bit(adcon0, GO);
if(test_bit(adcon0, GO != 0)) goto wait;

value = (ADRESH << 😎 | ADRESL;  //The emoji should read << 8, followed by a right bracket!
sprintf(disp, "Value is: %u.", value);
goto next;




Edited by matfrygb
I'm supplying more info.
Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...