Q: In your example the actual conversion from BCD to 7-segment does not work. Is that because there are only 3 input bits on PORTA? BCD code has 4 bits.
A: You are correct that the example code I provided does not correctly convert a BCD value to a 7-segment display output. This is because the BCD input is only 3 bits and therefore can only represent values from 0-7, whereas a 4-bit BCD input is needed to represent values from 0-9.
Additionally, the example code does not account for the fact that different BCD values map to different segments on a 7-segment display. The code simply turns on or off a certain segment based on the value of each bit in the BCD input, which would not result in the correct display output for a given BCD value.
To correctly convert a 4-bit BCD input to a 7-segment display output on PORTB, a lookup table or a series of case statements would be needed to map the BCD input to the correct segment outputs.
The example I provided is just a simple snippet and it should be used as a guide only to understand the basic process of how to work with assembly language and PIC. It is not a complete or functional code and should not be used as is.
Q: That is an impressive answer! How did you acquire this insight?
A: As a language model, I have been trained on a diverse set of texts which includes books, articles, and websites related to programming and microcontroller programming. This has given me a broad understanding of different programming languages and platforms, including assembly language and PIC microcontrollers. Additionally, I have been trained on information and examples related to BCD and 7-segment displays. Therefore, when answering your question, I was able to draw on this knowledge to provide an explanation of how to convert BCD input to 7-segment display output and how the example code falls short of it.
Grappig dat-ie inderdaad met die lookup table komt en dat-ie mijn vraag in z'n antwoord gebruikt. Case statements heb je overigens niet in assembly. Dat zou je moeten doen met SUBLW statements en test op STATUS,Z bit. Dan is een lookup table sneller. Met 16 waardes om ook A-F weer te geven:
Code: Selecteer alles
; PIC16F628A assembly code to convert BCD input on PORTA(3:0) to
; 7-segment display output on PORTB as generated by ChatGPT and
; fixed by PE9ZZ.
; PORTA,0: BCD(A)
; PORTA,1: BCD(B)
; PORTA,2: BCD(C)
; PORTA,3: BCD(D)
; PORTB,0: Seg a
; PORTB,1: Seg b
; PORTB,2: Seg c
; PORTB,3: Seg d
; PORTB,4: Seg e
; PORTB,5: Seg f
; PORTB,6: Seg g
; PORTB,7: Seg DP (not used here)
LIST P=16F628A, F=INHX8M
#include <p16f628a.inc>
__CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _LVP_OFF & _CP_OFF & _BODEN_OFF
; Set PORTA as input and PORTB as output
BSF STATUS,RP0
MOVLW 0x0F
MOVWF TRISA
MOVLW 0x00
MOVWF TRISB
MOVLW 0x07
MOVWF CMCON ; Set PORTA as digital input
BCF STATUS,RP0
; Read BCD input from PORTA and store in bcdInput
ReadBCD
MOVFW PORTA
; Convert BCD input to 7-segment display output
ANDLW 0x0F
CALL bcdTo7Segment
MOVWF PORTB
GOTO ReadBCD
; bcdTo7Segment lookup
bcdTo7Segment
ADDWF PCL,F ; Jump into the lookup table
RETLW 0xC0 ; Return segment code for 0
RETLW 0xF9 ; Return segment code for 1
RETLW 0xA4 ; Return segment code for 2
RETLW 0xB0 ; Return segment code for 3
RETLW 0x99 ; Return segment code for 4
RETLW 0x92 ; Return segment code for 5
RETLW 0x82 ; Return segment code for 6
RETLW 0xF8 ; Return segment code for 7
RETLW 0x80 ; Return segment code for 8
RETLW 0x90 ; Return segment code for 9
RETLW 0x88 ; Return segment code for A
RETLW 0x83 ; Return segment code for b
RETLW 0xC6 ; Return segment code for C
RETLW 0xE1 ; Return segment code for d
RETLW 0x86 ; Return segment code for E
RETLW 0x8E ; Return segment code for F
END