O que o Jorge mandou vale a pena ler, sobre a tensão de referência, principalmente:
[attachthumb=1]
Melhorei o programa e a simulação, agora lê e envia tres entradas analógicas. O terminnal mostra o envio sequenciado de AN0 (0% = 0), AN1 (50%=7F) e AN2 (100%=FF).
[attachthumb=2]
Programa assembly:
list P=16F877A
#include "P16F877A.INC"
__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON
;__CONFIG 0x3F32
errorlevel -302 ; turn off bank bits warning
;
; Seleção de entradas do MUX (ADDCON0)
;
#DEFINE EAN_0 B'01000001' ; f/8, AN0, DONE, A/D On
#DEFINE EAN_1 B'01001001' ; f/8, AN1, DONE, A/D On
#DEFINE EAN_2 B'01010001' ; f/8, AN2, DONE, A/D On
;
; Variáveis da RAM internas
;
cblock H'20'
ld_loop_ex
ld_loop_in
endc
;
; Inicio do Programa
;
ORG 0h
; Inicializaçao do PIC, do display e da porta serial
bcf STATUS,RP1
bsf STATUS,RP0 ; Habilita banco 1
clrf TRISD ; porta do display, de saída
movlw B'00000010' ; setup do conversor A/D
movwf ADCON1 ; left justify result
; port A: only analogue inputs
;
; Ajusta parametros da UART (usando cristal externo de 4MHz)
; Baud Rate = 9600 bps, No Parity, 1 Stop Bit
;
movlw 0x19 ; 0x19=9600 bps / 0x0C=19200 bps
movwf SPBRG
movlw b'00100100' ; TX_Enable, assync. mode, brgh = high (2)
movwf TXSTA ; enable Async Transmission, BRGH=1
movlw b'10111111' ; RC6 saida, outros bits da PORTC sao de entrada
movwf TRISC
bcf STATUS,RP0 ; Habilita banco 0
clrf PORTD ; zera saídas do display
; movlw EAN_0 ; seleciona entrada analógica
; movwf ADCON0
movlw b'01000000' ; Setup Port C
movwf PORTC ; RC6(TX)=1 others are 0
movlw b'10000000' ; habilita a UART
movwf RCSTA
;
; Loop principal
;
start:
movlw EAN_0 ; entrada analógica 0
call read_ad
call display
call send
call delay1s
movlw EAN_1 ; entrada analógica 1
call read_ad
call display
call send
call delay1s
movlw EAN_2 ; entrada analógica 2
call read_ad
call display
call send
call delay1s
goto start
;
; Conversor A/D, palavra ADDCON0 em W
;
read_ad:
movwf ADCON0
bsf ADCON0,GO ; Iniciar conversao
wait:
btfsc ADCON0,GO ; Testa se terminou conversao
goto wait
movf ADRESH,W
return
;
; Display de LEDs
;
display:
movwf PORTD
return
;
; Envia byte (contido em W) pela UART e espera terminar
;
send:
movwf TXREG ; send data in W
TransWt:
bsf STATUS,RP0 ; banco 1
WtHere:
btfss TXSTA,TRMT ; Transmissao terminou se TRUE
goto WtHere ; Senao, espera
bcf STATUS,RP0 ; banco 0
return
;
;-----------------------------------------------------------------------------
;
; Delay for about 1s
;
delay1s:
movlw D'250'
call waitx400
movlw D'250'
call waitx400
movlw D'250'
goto waitx400
;
;-----------------------------------------------------------------------------
;
; Delay for about 250 ms
;
delay250ms:
movlw D'250'
goto waitx400
;
;-----------------------------------------------------------------------------
;
; Delay for about 400x cycles
;
; Se w=1, temporiza 1ms @ fck = 4MHz
;
waitx400:
movwf ld_loop_ex
ld_loopex:
movlw (400-4)/4
movwf ld_loop_in
ld_loopin:
decf ld_loop_in,F
skpz
goto ld_loopin
decf ld_loop_ex,F
skpz
goto ld_loopex
return
END