Adc in pic16f877a...

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

Testing ADC code in MP lab is this possible??
Don't know it will work but it is according to data sheet ....

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000


#define DAD PORTB


main(){
    TRISB=0X00;
TRISA = 0xff ;
ADCON1=0b00000000;
ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
while(1){
    ADIF=0;
    ADIE=1;
    PEIE=1;
    GIE=1;
    
    __delay_ms(1);
GO_DONE=1;
    __delay_ms(1);

    DAD=ADRESH;
    }
}
 

ErnieM

Joined Apr 24, 2011
8,415
MPLAB SIM Help said:
General Limitations

Limitations that apply to all devices are listed below. Some devices may have specific limitations.

  • For devices with the listed peripherals, these peripherals are NOT supported:

    D/A converter (DAC) and Op Amp (OPA).
That's my story and I'm sticking to it.
 

spinnaker

Joined Oct 29, 2009
7,830
Hi,

Testing ADC code in MP lab is this possible??
Don't know it will work but it is according to data sheet ....

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000


#define DAD PORTB


main(){
    TRISB=0X00;
TRISA = 0xff ;
ADCON1=0b00000000;
ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
while(1){
    ADIF=0;
    ADIE=1;
    PEIE=1;
    GIE=1;
    
    __delay_ms(1);
GO_DONE=1;
    __delay_ms(1);

    DAD=ADRESH;
    }
}

ls what possible? Do you mean simulator? Just programing for ADC? What? Put some effort into your questions.. You have been told this many times.
 

ErnieM

Joined Apr 24, 2011
8,415
OK, do you find any error in this code will this code will work fine??
You have it backwards. We are not a testing nor inspection unit. YOU test the code. If you have a problem you report such to us, then we may look to see why it happens.

Your general scheme of copying the left justified contents of the result into a port is sound.
 

spinnaker

Joined Oct 29, 2009
7,830
You have it backwards. We are not a testing nor inspection unit. YOU test the code. If you have a problem you report such to us, then we may look to see why it happens.

Your general scheme of copying the left justified contents of the result into a port is sound.
Im. not going to even bother answering this member anymore. Op has been around long enough to know better. Rarely does the member put any effort into the questions,half the time you have to guess what he wants. I will still monitor the ops threads but until things improve I am done trying to help this member.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
YOU test the code. If you have a problem you report such to us, then we may look to see why it happens.
Hi again,

I have tested the code i found that PORTB is giving output to 8 LED's according to analog input by voltage divider ...
but when i change the voltage via R of voltage divider there is no update in led's output but when the uc is restarted there is change in output.
so, i think there is problem of updating the output.
 

takao21203

Joined Apr 28, 2012
3,702
Is it really so hard to connect a 4 digit LED display to a PIC, and observe the A/D result? It is just 12 wires, after all.

I never use a incircuit debugger, and only rarely I use the simulator. I used it more often with assembler.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Is it really so hard to connect a 4 digit LED display to a PIC, and observe the A/D result? It is just 12 wires, after all.

I never use a incircuit debugger, and only rarely I use the simulator. I used it more often with assembler.
??????????????????????????
 

t06afre

Joined May 11, 2009
5,934
Do you have an in circuit debugger? That will tell you right away what the problem is.
The Op do not have a programmer that support this. But by using MPLAB SIM you can do a lot of code checking using single stepping,watches and breakpoints. I have pointed the OP countless times to use this tool. And also given links to tutorials. But sad to say. I do not think any of this has caught his attention at all.
It looks like the OP has looked at the datasheet. But not with much attention to details. This code will enable ADC interrupt
Rich (BB code):
ADIF=0;
    ADIE=1;
    PEIE=1;
    GIE=1;
But with no ISR to handle the interrupt. The code will not work. And it will be stuck in limbo due to no return from interrupt code segment. The code might work without the latter code segment.
 

sharkz

Joined Mar 26, 2013
1
Hi again,

I have tested the code i found that PORTB is giving output to 8 LED's according to analog input by voltage divider ...
but when i change the voltage via R of voltage divider there is no update in led's output but when the uc is restarted there is change in output.
so, i think there is problem of updating the output.
ADC output is 10 bit so changes in most significant 2-bit might not show. This might be the reason.
 

tshuck

Joined Oct 18, 2012
3,534
ADC output is 10 bit so changes in most significant 2-bit might not show. This might be the reason.
Not quite. the ADC data is left justified, the 2 least significant bits are not shown, with a Vref of 5v, that is a bit weight of ~20mV, not likely to be the case that Ritesh can reliably alter his ADC input sub 20mV.
 

tshuck

Joined Oct 18, 2012
3,534
You have turned on your ADC interrupt, yet there is no code at the interrupt vector (0x0004). Your code does not return from that. I would suggest you take out the interrupt do that you can figure out how to use the ADC first and add the interrupt back once you understand them better.
 

ErnieM

Joined Apr 24, 2011
8,415
The Op do not have a programmer that support this. But by using MPLAB SIM you can do a lot of code checking using single stepping,watches and breakpoints. I have pointed the OP countless times to use this tool. And also given links to tutorials. But sad to say. I do not think any of this has caught his attention at all.
It looks like the OP has looked at the datasheet. But not with much attention to details. This code will enable ADC interrupt
Rich (BB code):
ADIF=0;
    ADIE=1;
    PEIE=1;
    GIE=1;
But with no ISR to handle the interrupt. The code will not work. And it will be stuck in limbo due to no return from interrupt code segment. The code might work without the latter code segment.
I missed the interrupt getting turned on. DO NOT TURN THER INTERRUPOTS ON, unless of course you have an interrupt routine to handle such. Otherwise your code will have a major corruption and may get stuck somewhere.

At least comment out the GIE=1; line.
 

RG23

Joined Dec 6, 2010
304
Use a debugger like Pickit2

Put a breakpoint in your program where you are storing the digital value

and run the program

Then check the file registers where the value is saved

Doing this you will get an idea whether you are going in the right direction and then you can manipulate accordingly
 
Top