Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

simple adc using pic16f877a problem

Status
Not open for further replies.

amitha

Newbie level 1
Joined
Oct 9, 2008
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,289
Hi ,i am trying a simple adc (8bit only) using pic16f877a n trying to output the result in portc but i am not getting any output when simulated in proteus isis 7. Please do show me where is my mistake. tq


Code:
ADCON1 EQU 9FH
ADCON0 EQU 1FH
go EQU 2
ADRESL EQU 9EH
PORTC EQU 07H 
TRISC EQU 87H


ORG 0x000
goto main

main


start
movlw b'11000000' ; configure all channel as analog n vref =vdd n gnd
banksel ADCON1
movwf ADCON1
movlw b'01010001' ; ch2 as anolog input n fosc/16
banksel ADCON0
movwf ADCON0
movlw b'00000000'
movwf TRISC
clrf  PORTC

loop
        banksel ADCON0
        bsf ADCON0,go
back    btfsc ADCON0,go
        goto back
        movf ADRESL,w
        movwf PORTC
        goto delay
        goto loop


delay
        movlw d'255'
        movwf 20h
looping nop
        nop
        decfsz 20h
        goto looping
        return

end
 

Go for C language, its much easier to control ADC..
 

Do one thing. Before assigning the values to all registers DO clear i mean initialize all the registers which you are using.......... This might help you a lot.... :cool::wink:
 

why make life difficult, use C

Here is a sample code for mikroC
Code:
unsigned short temp_res;

void main() {
  USART_Init(19200);  // Initalize USART (9600 baud rate, 1 stop bit, ...

  // Select Vref and analog inputs, in order to use ADC_Read
  ADCON1 = 0;        // All porta pins as analog, VDD as Vref
  TRISA  = 0xFF;     // PORTA is input

  do {

    // Read ADC results and send the upper byte via USART
    temp_res = ADC_Read(2) >> 2;
    USART_Write(temp_res);
    Delay_ms(100);
  } while (1);       // endless loop
}
 

Hi,

Try this version - have included a longer delay so you can see the changes better.



Code:
    list p=16F877A,r=hex,n=80,x=off,st=off
    include <P16F877A.inc>
    errorlevel -302        ; no bank warnings
    __config H'3931'    ; 4meg xtal

        cblock    0x20
        d1
        d2
        d3
        endc

        ORG 0x000
        goto main

main    nop


start
        banksel ADCON1
        movlw    b'10000000'        ; set portA as all adc input, bit 7 r justify
        movwf    ADCON1    


        banksel ADCON0
        movlw     b'11000001'     ; select Frc, ADC channel 0, Turn Adc On
        movwf     ADCON0

        

;        movlw b'11000000' ; configure all channel as analog n vref =vdd n gnd
;        banksel ADCON1
;        movwf ADCON1
;        movlw b'01010001' ; ch2 as anolog input n fosc/8  AT 4 MEG
;        banksel ADCON0
;        movwf ADCON0

        movlw b'00000000'    ; make PortC outputs
        movwf TRISC
        clrf  PORTC

loop
        bsf ADCON0,GO

back    btfsc ADCON0,GO
        goto back
        banksel    ADRESL
        movf ADRESL,W
        banksel    PORTC
        movwf PORTC
        goto delay250
        goto loop


delay
        movlw d'255'
        movwf d1
looping nop
        nop
        decfsz d1,F
        goto looping
        return


delay250    movlw    d'250'                ; delay 250 ms 
            movwf    d1
dly250_1    movlw    0xC7
            movwf    d2
            movlw    0x01
            movwf    d3
dly250_2
            decfsz    d2, f
            goto    dly250_3
            decfsz    d3, f
dly250_3    goto    dly250_2
            decfsz    d1    ,f
            goto    dly250_1
            retlw    0x00



            end
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top