Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

adc not working properly

Status
Not open for further replies.

maria258

New Member
whilst the compiling is successful, when put to the proteus simulation, it is not working as required. can someone help me to find the fault? could it be something regarding the tris bits? but i dont know whats the problem with that.
thanks
code is shown below


Code:
#include <pic.h>
 
 unsigned int itime;
 void delay (unsigned char itime);
 
 void main (void)
 {
 TRISA=0b10000000;    //RA0 input for analog
 TRISC=0b11000000; //RC0, RC1 are set as outputs
 TRISD=0x11111111;    //D is set as output
 
 ADCON1=0b10000000;
 //bit7:ADFM:right justified
 //bit6-4:unused, read as 0
 //bit3-0:all ports configured as analog channels
 
 ADCON0=0b0000001;
 //bit7-6:Fosc/2
 //bit5-3:using analog channel RA0/AN0
 //bit2:ADON not on
 //bit1:unimplemented
 //bit0: ADON converter module is operating
 
 while(1)
 {
 delay(100);
 //GODONE=1;    //start converting ***ADGO=1
 //while(GO);
 PORTC=ADRESL;
 PORTD=ADRESH;
 delay(100);
 }
 }
 
 void delay (unsigned char itime)
 {
     unsigned int i,j;
 
     for(i=0;i<itime;i++)
         for(j=0;j<200;j++);
 }
 
I don't know C, but unless things are reversed in the language/compiler there are some errors on the TRIS settings.

Most things I've ever encountered go MSB ---->>> LSB as you read it from right to left.

In actuality it looks like you have PORTA.7 set as in input, and all others as output, with a similar problem on TRISB. As an aside, you've got TRISD set to all input even though your comment shows them as outputs.

Plus you only list 7 bits (not 8) on your ADCON0 register.
 
TRISD=0x11111111; //D is set as output
this actualy sets the TRIS reg to input

for out put you need TRISD=0x00; //D is set as output


TRISA=0b10000000; //RA0 input for analog

this is also wrong and should be TRISA=0b00000001; //RA0 input for analog

a couple of things i noticed as well is you have confused input and output on the TRIS reg think of it like this 0=0ut 1 =1nput
also try not to mix your number bases it might be easier to use 0b rather than 0x
as wouldbeinventor says bit 0 of a port is on the right and bit 7 on the left 76543210
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top