A/D converter to LED matrix (Potentiometer)

Thread Starter

pStrafe

Joined Jan 24, 2018
10
So I want to read data from the A/D potentiometer and then display the status of the voltage on a 8*8 matris led. The idea is to use a column of the 8*8 and display the value there (0-7)

So, the A/D goes from 0-255 but I want to have 0-7 from that, I need to left justify the value?

Nothing works for me and I don't even know if I am doing anything correctly. Any help would be nice
Code:
MAIN    CLRF    TRISD
    CLRF    PORTD
    CLRF    TRISA,RA1
    CLRF    TRISA,RA1

    MOVLW B'000000001';ADCON0 setup: Use AN0, activate ADC-Module
    MOVWF ADCON0
  
    MOVLW B'00001110';ADCON1 setup: VSS,VDD ref. AN0 analog only
    MOVWF ADCON1
  
    MOVLW B'00010010' ;ADCON2 setup: Left justified
    MOVWF ADCON2
  

MAINL
    BSF ADCON0,1
SAMPLE    BTFSC ADCON0,1
    GOTO SAMPLE
    MOVLW    ADRESH
    MOVWF    ADRESH
    MOVWF    PORTD
    BSF    PORTA,RA1; Turn on LED
    CALL    DELAY
    BCF    PORTA,RA1 ; Turn off LED
    GOTO MAINL ;Restart
  
DELAY    MOVLW    0xFF
    MOVWF    VBA
LOOP2    MOVLW    0xFF
    MOVWF   VBB
LOOP1    DECFSZ    VBB,1
  
    GOTO LOOP1
    DECFSZ    VBA,1

    GOTO    LOOP2
    RETURN
 

LesJones

Joined Jan 8, 2017
4,191
It would be a LOT better if you told us what microcontroller you are using. My guess would be thet it is a PIC of some kind. If not my reply is probably wrong or does not make any sense. Also it would be helpful if you put comments to say which I/O ports controlled the LEDs A schematic would also be a great help to understand what you are doing.
In line 20 you load the "W" register with the address of ADRESH. In line 21 you load the address of ADRESH (From the "W" register.) into ADRESH which overwrites the value read by the A to D converter. In line 22 you write the address of ADRESH (From the "W" register.) into port D. This does not make any sense to me.

Les.
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
It would be a LOT better if you told us what microcontroller you are using. My guess would be thet it is a PIC of some kind. If not my reply is probably wrong or does not make any sense. Also it would be helpful if you put comments to say which I/O ports controlled the LEDs A schematic would also be a great help to understand what you are doing.
In line 20 you load the "W" register with the address of ADRESH. In line 21 you load the address of ADRESH (From the "W" register.) into ADRESH which overwrites the value read by the A to D converter. In line 22 you write the address of ADRESH (From the "W" register.) into port D. This does not make any sense to me.

Les.
Hey,
Sorry for not being clear! It's a PIC18F4520 in a HL-K18 board
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
Okay so this is my current code:
It doesn't read the value from the potentiometer, it only display the value B'11000100' to the LED column, doesn't change.
I think my ADCON0-2 is configured wrong
Code:
MAIN    CLRF    TRISD
    CLRF    PORTD
    BCF    TRISE,RE0
    BCF     PORTE,RE0
    SETF    TRISA

    MOVLW B'00000001';ADCON0 setup: Use AN0, activate ADC-Module
    MOVWF ADCON0
   
    MOVLW B'10011010';ADCON1 setup: VSS,VDD ref. AN0 analog only
    MOVWF ADCON1
   
    MOVLW B'00010010' ;ADCON2 setup: Left justified
    MOVWF ADCON2
   

MAINL
    BSF ADCON0,1        ; Starts the AD Conversion
SAMPLE    BTFSC ADCON0,1        ; Has it fnished?
    GOTO SAMPLE        ; No
    MOVLW    ADRESH       
    MOVWF    PORTD        ; Move to PORTD to display the value to the LED
    BSF    PORTE,RE0   ; Turn on LED Column
    CALL    DELAY        ; Delay
    BCF    PORTE,RE0   ; Turn off LED Column
    GOTO MAINL ;Restart ; Restart
   
DELAY    MOVLW    0xFF
    MOVWF    VARIABELA
LOOP2    MOVLW    0xFF
    MOVWF    VARIABELB
LOOP1    DECFSZ    VARIABELB,1
   
    GOTO LOOP1
    DECFSZ    VARIABELA,1

    GOTO    LOOP2
    RETURN
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
Okay here is my current code
To get the value of AN0 to AN7 we need to do Right justified and use ADRESL correct?

Code:
MAIN    CLRF    TRISD
    CLRF    PORTD
    BCF    TRISE,RE0
    BCF     PORTE,RE0
    SETF    TRISA

    MOVLW B'00011101' ;ADCON0 setup: Use AN0, activate ADC-Module
    MOVWF ADCON0
   
    MOVLW B'00000111' ;ADCON1 setup: VSS,VDD ref. AN0 analog only
    MOVWF ADCON1
   
    MOVLW B'10100111' ;ADCON2 setup: Right justified. We want 0-7 bits which are stored in ADRESL (RIGHT JUSTIFIED)?
    MOVWF ADCON2
  

MAINL
    BSF ADCON0,1        ; Starts the AD Conversion
SAMPLE    BTFSC ADCON0,1        ; Has it fnished?
    GOTO SAMPLE        ; No
    MOVLW    ADRESL        ; We want 0-7 bits which are stored in ADRESL (RIGHT JUSTIFIED)?
    MOVWF    PORTD        ; Move to PORTD to display the value to the LED
    BSF    PORTE,RE0   ; Turn on LED Column
    CALL    DELAY        ; Delay
    BCF    PORTE,RE0   ; Turn off LED Column
    GOTO MAINL        ; Restart
  
DELAY    MOVLW    0xFF
    MOVWF    VARIABELA
LOOP2    MOVLW    0xFF
    MOVWF    VARIABELB
LOOP1    DECFSZ    VARIABELB,1
  
    GOTO LOOP1
    DECFSZ    VARIABELA,1

    GOTO    LOOP2
    RETURN
 
Last edited:

LesJones

Joined Jan 8, 2017
4,191
In line 21 you are still moving the address of the ADRESL register to W. As the address of ADRESL 0xFC3 which is a 12 bit value and you are treating it as a literal (an 8 bit value) I'm not sure weather it will set W to 0xFC or 0xC3. I think you should be moving the value that is ADRESL into W The instruction for this would be MOVFW ADRESL. I think you were correct in post 1 using ADRESH as the value was left justified. Using ADRESL you will just have the two least significant bits of the conversion in bits 6 and 7 of ADRESL.

Les.
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
In line 21 you are still moving the address of the ADRESL register to W. As the address of ADRESL 0xFC3 which is a 12 bit value and you are treating it as a literal (an 8 bit value) I'm not sure weather it will set W to 0xFC or 0xC3. I think you should be moving the value that is ADRESL into W The instruction for this would be MOVFW ADRESL. I think you were correct in post 1 using ADRESH as the value was left justified. Using ADRESL you will just have the two least significant bits of the conversion in bits 6 and 7 of ADRESL.

Les.
Alright so doing MOVF ADRESL,W gives me a blinking led (0x01) but when I turn around the potentiometer nothing changes.
Also using right justified and then adresl would give me 0-7 right? I checked this picture


Code:
org 0x000000
VARIABELA   equ    0x20
VARIABELB   equ    0x21
PORST    GOTO MAIN
    org 0x000020
MAIN    CLRF    TRISD
    CLRF    PORTD
    BCF    TRISE,RE0
    BCF     PORTE,RE0
    SETF    TRISA

    MOVLW B'00011101' ;ADCON0 setup: Use AN0, activate ADC-Module
    MOVWF ADCON0
  
    MOVLW B'00000111' ;ADCON1 setup: VSS,VDD ref. AN0 analog only
    MOVWF ADCON1
  
    MOVLW B'10100111' ;ADCON2 setup: Right justified. We want 0-7 bits which are stored in ADRESL (RIGHT JUSTIFIED)?
    MOVWF ADCON2
  

MAINL
    BSF ADCON0,1        ; Starts the AD Conversion
SAMPLE    BTFSC ADCON0,1        ; Has it fnished?
    GOTO SAMPLE        ; No
    MOVF    ADRESL,W    ; We want 0-7 bits which are stored in ADRESL (RIGHT JUSTIFIED)?
    MOVWF    PORTD        ; Move to PORTD to display the value to the LED
    BSF    PORTE,RE0   ; Turn on LED Column
    CALL    DELAY        ; Delay
    BCF    PORTE,RE0   ; Turn off LED Column
    GOTO MAINL        ; Restart
  
DELAY    MOVLW    0xFF
    MOVWF    VARIABELA
LOOP2    MOVLW    0xFF
    MOVWF    VARIABELB
LOOP1    DECFSZ    VARIABELB,1
  
    GOTO LOOP1
    DECFSZ    VARIABELA,1

    GOTO    LOOP2
    RETURN
If you go to the PIC manual http://ww1.microchip.com/downloads/en/devicedoc/39631a.pdf
and then search for adcon0, adcon1, adcon2 you can see the configuration.
Now I don't know if I've done mine correctly, the thins is the potentiometer has a value of 0-255 how would I set it up to give me a value of 0-7
three bits.
thanks


EDIT: So changing the ADCON0 settings to this:

Code:
MOVLW B'00000001' ;ADCON0 setup: Use AN0, activate ADC-Module
    MOVWF ADCON0
make the leds change when I turn the potentiometer around however, there are many leds blinking at the same time, I just want the values 0x01 0x02 0x04 0x08 0x10 0x20 0x40 0x80
 
Last edited:

LesJones

Joined Jan 8, 2017
4,191
I have not looked in detail at your code. I spotted straight away the MOVLW error. (I once spent many hours looking for a problem before I spotted that I had mistyped an instruction with just one letter wrong.) It is almost 10 years since I used the ADC on a PIC18 so I have copied the ADC initialisation and the ADC read code from the speed controller I was writing at the time. Here it is.
Code:
Using PIC18F2431

;*** A/D converter initialization
  movlw  b'00000001'  ; ADON=ON , Single shot mode
  movwf  ADCON0  ;Set ADCON0 register
  movlw  b'00000000'  ;
  movwf  ADCON1  ;Set ADCON1 register
   MOVLW   b'10000110'   ;Right justified, no delay, Fosc/64
   MOVWF   ADCON2
   MOVLW   b'00001000'   ;
   MOVWF   ADCON3
   MOVLW   b'00000000'
   MOVWF   ADCHS
   MOVLW   b'000000001'   ;
   MOVWF   ANSEL0

; Read ADC
ad_check           ;  ******** Read required speed from A/D input value  ***********
   BSF   ADCON0,GO   ;Start conversion.

ad_check_1
  btfsc  ADCON0,GO  ;A/D convert end ?
  goto  ad_check_1  ;No. Again

   MOVFF   ADRESL, Set_Speed_L  ;Read ADRESL register
   MOVFF   ADRESH, Set_Speed_H    ;Read ADRESH register (Only bottom two bits used)
As this is a different pic to the one that you are using The bit positions in the registers is probably different.
For me to understand how your LED display works you will need to post the schematic.

Les.


Les.
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
I have not looked in detail at your code. I spotted straight away the MOVLW error. (I once spent many hours looking for a problem before I spotted that I had mistyped an instruction with just one letter wrong.) It is almost 10 years since I used the ADC on a PIC18 so I have copied the ADC initialisation and the ADC read code from the speed controller I was writing at the time. Here it is.
Code:
Using PIC18F2431

;*** A/D converter initialization
  movlw  b'00000001'  ; ADON=ON , Single shot mode
  movwf  ADCON0  ;Set ADCON0 register
  movlw  b'00000000'  ;
  movwf  ADCON1  ;Set ADCON1 register
   MOVLW   b'10000110'   ;Right justified, no delay, Fosc/64
   MOVWF   ADCON2
   MOVLW   b'00001000'   ;
   MOVWF   ADCON3
   MOVLW   b'00000000'
   MOVWF   ADCHS
   MOVLW   b'000000001'   ;
   MOVWF   ANSEL0

; Read ADC
ad_check           ;  ******** Read required speed from A/D input value  ***********
   BSF   ADCON0,GO   ;Start conversion.

ad_check_1
  btfsc  ADCON0,GO  ;A/D convert end ?
  goto  ad_check_1  ;No. Again

   MOVFF   ADRESL, Set_Speed_L  ;Read ADRESL register
   MOVFF   ADRESH, Set_Speed_H    ;Read ADRESH register (Only bottom two bits used)
As this is a different pic to the one that you are using The bit positions in the registers is probably different.
For me to understand how your LED display works you will need to post the schematic.

Les.


Les.

So I'm using PORTE since the Porta is used for the A/D conversion.
 

Attachments

LesJones

Joined Jan 8, 2017
4,191
Can you post the full schematic of the LED matrix including showing the current limiting resistors. What does the strange picture mean that looks like cross between a matrix display and a 7 segment display ? Can you confirm that you are using an LED matrix not a multiplexed 7 segment display ?

Les.
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
Can you post the full schem

Iatic of the LED matrix including showing the current limiting resistors. What does the strange picture mean that looks like cross between a matrix display and a 7 segment display ? Can you confirm that you are using an LED matrix not a multiplexed 7 segment display ?

Les.
The cross thingy is just illustration. It's a 8*8 matrix LED.
Also the schematic with resistors is in the attachment. HL-K18 check that (bottom righ)
 

LesJones

Joined Jan 8, 2017
4,191
Where are your current limiting resistors connected ?
It looks like you are just trying to select column 1 using RE0. If so you signal is inverted. You need all the NONE selected bits to be a logic high (1) and the SELECTED column bit to be a logic low (0)

Les.
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
Where are your current limiting resistors connected ?
It looks like you are just trying to select column 1 using RE0. If so you signal is inverted. You need all the NONE selected bits to be a logic high (1) and the SELECTED column bit to be a logic low (0)

Les.
Turning on the column using RE0 works perfectly fine, I have not issues with turning on LEDs etc... Problem is to do the A/D conversion correctly
 

LesJones

Joined Jan 8, 2017
4,191
verify that the matrix is working correctly by writing fixed valued to port D. Use values that that just switch one LED on at a time like 0x01, 0x02, 0x04, 0x08, 0x10 and so on and make sure the correct LED comes on each time. This is where you can use the MOVLW instruction.

Les.
 

Thread Starter

pStrafe

Joined Jan 24, 2018
10
verify that the matrix is working correctly by writing fixed valued to port D. Use values that that just switch one LED on at a time like 0x01, 0x02, 0x04, 0x08, 0x10 and so on and make sure the correct LED comes on each time. This is where you can use the MOVLW instruction.

Les.
So as I wrote earlier configured LEDs is working properly and every LED dot works, I just double checked.
Thing is when I have the A/D thingy, there are like 4 dots, one static, 2 blinking and moving 1-2 row up and down, it's all messed up.
 
Top