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.

Enable the A/D conversion on the other PIC16877A ports (Assembly)

Status
Not open for further replies.

Dragnovith

Junior Member level 1
Junior Member level 1
Joined
Apr 17, 2021
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
104
Hello, I would like to know how do I enable the a/d conversion on the other ports, for example, I am able to perform the conversion through this code through the AN0 port, but I made several modifications and I couldn't enable AN1,AN2,etc.. .

The principle of this is that I'm trying to put an LM35 on each of the RA0, RA1, RA2 and RA3 ports. Then I'll have to convert, so I need that part of the A/D. For the sensor that is connected to the RA0 port, it is working.

sensors.PNG


The code comments are in Portuguese (Brazil), anything I can transcribe into English.

Code:
Main
       bank1       ;     MUDA BANCO MEMORIA 1
        MOVLW   B'10000000'
;              1-UU----     Alinhado a direita   
;              -0UU----     FSC/   
;              --UU0000     Todas analogicas   
        MOVWF   ADCON1    ; valor justificado a esquerda   
        MOVLW   B'00000000'
    MOVWF   TRISB            
        MOVLW   B'00000000'
    MOVWF   TRISD            
        MOVLW   B'11111111'
    MOVWF   TRISA          
    MOVLW   B'00000000'    
;              --------       
        MOVWF   OPTION_REG     
        MOVLW    B'00000111' ; Desabilita os comparadores           
    MOVWF    CMCON           
   
    bank0       ; BANCO DE MEMORIA 0
        MOVLW   B'00000001' ; Configura ADC
;              00----U-    FSC/8   
;              -------1    Liga o conversor   
;              --000---    Canal AN0 entrada
    MOVWF   ADCON0

    MOVLW   B'00000000'   
    MOVWF   INTCON

        CLRF    PORTB       
    CLRF    D0
    CLRF    D1

D0 and D1 are the variables responsible for the unit and ten of the LM35, so I would have to create others for each of the other entries correct?
The complete code is in the .rar file
 

Attachments

  • sensor_code.rar
    2.1 KB · Views: 104
Last edited by a moderator:

Hi,

Read datasheet, the whole "ADC" section,
but especially "11.1 A/D Acquisition Requirements"

Thus you need to do the folowing steps:
* configure ADC channel, but don't start a conversion yet
* wait (acquisition time)
* start the conversion
* wait (conversion)
* read ADC result

Besides this you need the correct port setup

Klaus
 
It is lucky that you are using a Proteus simulation because the way you have connected the LCD character module is almost impossible to get working with real hardware. There are several other issues in your source code that will cause problems too.

In any case this is one way to read ADC channels other than AN0:
C:
;-----------------------------------------------------------
; INICIO DA CONVERSAO => CONVERSION START
;-----------------------------------------------------------

MEDIDA
; AN0 - PESO
READ_AN0:
    bank0
    BCF     ADCON0,3        ; --0 Seta AN0 para Leitura
    BCF     ADCON0,4        ; -00
READ_ADC:
    BCF     ADCON0,5        ; 0xx
    CALL    DELAY20         ; Espera 30us
    BSF     ADCON0,2        ; Inicia a Conversao
    CALL    FIM_CONVERSAO   ; Verifica o fim da conversao
    bank1
    MOVFW   ADRESL          ; Copia o valor convertido para W
    bank0
    MOVWF   T0              ; Envia o valor convertido para T0
    BCF     STATUS,C
    RRF     T0,1            ; Divide por 2
    MOVFW   T0
    CALL    TABELA_TEMP
    MOVWF   T0
    RETURN

READ_AN1:
    bank0
    BSF     ADCON0,3        ; --1 Seta AN1 para Leitura
    BCF     ADCON0,4        ; -01
    goto    READ_ADC
    
READ_AN2:
    bank0
    BCF     ADCON0,3        ; --0 Seta AN2 para Leitura
    BSF     ADCON0,4        ; -10
    goto    READ_ADC
    
READ_AN3:
    bank0
    BSF     ADCON0,3        ; --1 Seta AN3 para Leitura
    BSF     ADCON0,4        ; -11
    goto    READ_ADC
    
FIM_CONVERSAO
    BTFSC   ADCON0,2
    GOTO    FIM_CONVERSAO
    RETURN
 
It is lucky that you are using a Proteus simulation because the way you have connected the LCD character module is almost impossible to get working with real hardware. There are several other issues in your source code that will cause problems too.

In any case this is one way to read ADC channels other than AN0:
C:
;-----------------------------------------------------------
; INICIO DA CONVERSAO => CONVERSION START
;-----------------------------------------------------------

MEDIDA
; AN0 - PESO
READ_AN0:
    bank0
    BCF     ADCON0,3        ; --0 Seta AN0 para Leitura
    BCF     ADCON0,4        ; -00
READ_ADC:
    BCF     ADCON0,5        ; 0xx
    CALL    DELAY20         ; Espera 30us
    BSF     ADCON0,2        ; Inicia a Conversao
    CALL    FIM_CONVERSAO   ; Verifica o fim da conversao
    bank1
    MOVFW   ADRESL          ; Copia o valor convertido para W
    bank0
    MOVWF   T0              ; Envia o valor convertido para T0
    BCF     STATUS,C
    RRF     T0,1            ; Divide por 2
    MOVFW   T0
    CALL    TABELA_TEMP
    MOVWF   T0
    RETURN

READ_AN1:
    bank0
    BSF     ADCON0,3        ; --1 Seta AN1 para Leitura
    BCF     ADCON0,4        ; -01
    goto    READ_ADC
   
READ_AN2:
    bank0
    BCF     ADCON0,3        ; --0 Seta AN2 para Leitura
    BSF     ADCON0,4        ; -10
    goto    READ_ADC
   
READ_AN3:
    bank0
    BSF     ADCON0,3        ; --1 Seta AN3 para Leitura
    BSF     ADCON0,4        ; -11
    goto    READ_ADC
   
FIM_CONVERSAO
    BTFSC   ADCON0,2
    GOTO    FIM_CONVERSAO
    RETURN
Thanks for the LCD warning, I think at the time of practice it's better to be more careful hehe.

Where you clear all the channel select bits to select channel 0 you set the pattern for the channel that you want to select. Correct?

Variables D0 and D1 are responsible for the ten and unit of sensor 1. Will I have to create a READ_ADC for the others AN1,AN2 and AN3, with the respective variable of each sensor?
Or is there a smarter way to do this?

I already have more or less an idea of how I'm going to compare if all the sensors are above 30, using an OR.
 

I'm almost done with the code, I have to make some adjustments. I wanted the sensor to activate the transito_fechado when one of the LM35 values passed 30, but the condition was reversed. I wanted the message on the lcd of transito_aberto to only appear when all sensors are below 30.
This was the condition I tried to apply:

IF ELSE CONDITION
Code:
;-----------------------------------------------------------
;        CONDIÇÃO IF ELSE
;-----------------------------------------------------------
   
    MOVLW d'3' ; WREG = 3
    MOVWF z ; Z = WREG
    MOVF z, W ; WREG = Z
    SUBWF D1, W ; WREG = D3 - WREG
    BTFSS STATUS, C ; SE D1<Z, ENTÃO C NÃO DEVE SER DEFINIDO
    GOTO TRANSITO_ABERTO
; SE CHEGOU AQUI SIGNIFICA QUE D1>=3
; TESTE SE D3>=3
    MOVLW d'3' ; WREG = 3
    MOVWF z ; Z = WREG
    MOVF z, W ; WREG = Z
    SUBWF D3, W ; WREG = D3 - WREG
    BTFSS STATUS, C ; SE D3<Z, ENTÃO C NÃO DEVE SER DEFINIDO
    GOTO TRANSITO_ABERTO
; SE CHEGOU AQUI SIGNIFICA QUE D3>=3
; TESTE SE D5 >= 5
    MOVLW d'3' ; WREG = 3
    MOVWF z ; Z = WREG
    MOVF z, W ; WREG = Z
    SUBWF D5, W ; WREG = D5 - WREG
    BTFSS STATUS, C ; SE D5<Z, ENTÃO C NÃO DEVE SER DEFINIDO
    GOTO TRANSITO_ABERTO
; SE CHEGOU AQUI SIGNIFICA QUE D5>=3
; TESTE SE D7>=3
    MOVLW d'3' ; WREG = 3
    MOVWF z ; Z = WREG
    MOVF z, W ; WREG = Z
    SUBWF D7, W ; WREG = D7 - WREG
    BTFSS STATUS, C ; SE D7<Z, ENTÃO C NÃO DEVE SER DEFINIDO
    GOTO TRANSITO_ABERTO
; SE CHEGOU AQUI SIGNIFICA QUE D7>=3
; E VAI ABRIR O TRANSITO
TRANSITO_FECHADO
    bank0  
    BCF RS ;SELECIONA O DISPLAY PARA COMANDOS
    MOVLW B'00000001'
    CALL ESCREVE ;ESCREVE COMANDO PARA LIMPAR TODO O DISPLAY
    BSF RS ;SELECIONA O DISPLAY PARA DADOS
    CALL DELAY1
    MOVLW 'T'    ;  
    CALL ESCREVE      
    MOVLW 'R'    ;  
    CALL ESCREVE      
    MOVLW 'A'    ;  
    CALL ESCREVE      
    MOVLW 'N'    ;  
    CALL ESCREVE
    MOVLW 'S'    ;  
    CALL ESCREVE
    MOVLW 'I'    ;  
    CALL ESCREVE
    MOVLW 'T'    ;  
    CALL ESCREVE
    MOVLW 'O'    ;
    CALL ESCREVE
    MOVLW 'G'    ;
    CALL ESCREVE
    MOVLW 'F'    ;
    CALL ESCREVE
    MOVLW 'E'    ;
    CALL ESCREVE
    MOVLW 'C'    ;  
    CALL ESCREVE
    MOVLW 'H'    ;  
    CALL ESCREVE
    MOVLW 'A'    ;
    CALL ESCREVE
    MOVLW 'D'    ;
    CALL ESCREVE
    MOVLW 'O'    ;

                 
   
    ; ACIONAMENTO E DESLIGAMENTO DOS LEDS
    BSF STATUS, 5; SELECIONA BANCO 1
    CLRF TRISC ; TRISC = 0
    BCF STATUS,5; SELECIONA BANCO 0
    BSF PORTC,1    ; PORTC RC1 = 1
    BCF PORTC,0    ; PORTC RCO = 0

    goto INICIO      ;loop

    continuacao
   
TRANSITO_ABERTO
   
    ; SE CHEGOU AQUI, SIGNIFICA QUE D1>Z
    bank0
    BCF RS; SELECIONA O DISPLAY PARA COMANDOS
    MOVLW B'00000001'
    CALL ESCREVE ;ESCREVE COMANDO PARA LIMPAR TODO O DISPLAY
    BSF RS ;SELECIONA O DISPLAY PARA DADOS
    CALL DELAY1
    MOVLW 'S'    ;  
    CALL ESCREVE      
    MOVLW 'I'    ;  
    CALL ESCREVE      
    MOVLW 'G'    ;  
    CALL ESCREVE      
    MOVLW 'A'    ;  
    CALL ESCREVE
    MOVLW 'D'    ;  
    CALL ESCREVE
    MOVLW 'E'    ;  
    CALL ESCREVE
    MOVLW 'V'    ;  
    CALL ESCREVE
    MOVLW 'A'    ;  
    CALL ESCREVE                          
    MOVLW 'G'    ;  
    CALL ESCREVE      
    MOVLW 'A'    ;  
    CALL ESCREVE
    MOVLW 'R'    ;        

    ; ACIONAMENTO E DESLIGAMENTO DOS LEDS
    BSF STATUS, 5; SELECIONA BANCO 1
    CLRF TRISC ; TRISC = 0
    BCF STATUS,5; SELECIONA BANCO 0

    BCF PORTC,1    ; PORTC RC1 = 0
    BSF PORTC, 0     ; PORTC RC0 = 1
    CALL DELAY20
    BCF PORTC,0    ; PORTC RCO = 0

    goto INICIO      ;loop

    GOTO continuacao
 

Attachments

  • complete_code.rar
    2.5 KB · Views: 103

It's working now, thank you very much for your help. You having sent me the way the channel change works helped me a lot.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top