Write subroutines Bin2BCD and BCD2Bin to realize the conversion between binary numbers and compressed BCD codes. The binary number to be converted is stored in the w register, and the BCD code obtained after the subroutine call is completed is still stored in the w register and returned. For example:
movlw .45 ; w=45
call Bin2BCD ;
nop ; w=0x45
1. Convert binary numbers to compressed BCD codes
The method of converting binary numbers into compressed BCD code is to shift the binary code left 8 times. After each shift, check whether the lower four bits are greater than 4. If they are greater than 4, add 3 to the lower four bits, otherwise do not add; the same process is done for the upper 4 bits.
list p=16f877A ; Indicate the processor type used
#include __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF ;*****************Variable definitions******************************************************* BIN EQU 0x20 ; store binary numbers BCD EQU 0x21 ; store compressed BCD code BCDLO EQU 0x22 ; store the lower four bits of the BCD code BCDHI EQU 0x23 ; store the high four bits of the BCD code LOW3 EQU 0x24 ; used to add the lower four bits HIGH3 EQU 0x25 ; used to add the upper four bits COUNT EQU 0x26 ; Number of loops ;****************************************************** ****************************** ORG 0x0000 ; Reset entry address nop ; compatible with ICD debugging tools, nop must be added goto Main; Jump to Main function ;*****************************Main function code********************************** Main MOVLW b'00101101' ; Send binary number 00101101 to BIN MOVWF BIN CLRF BCD ; BCD clear MOVLW 0x03 ; 0x03 sends LOW3 MOVWF LOW3 MOVLW 0x30; 0x30 sends HIGH3 MOVWF HIGH3 MOVLW 0x07 ; Initialize loop count MOVWF COUNT CALL Bin2BCD ; Call the conversion program RLF BIN RLF BCD ; The eighth left shift should result in 0x45 nop goto $ ; stop ;**************************Binary to compressed BCD code subroutine******************** ORG 0X0100 Bin2BCD RLF BIN RLF BCD; Binary code is sent to BCD from high to low MOVLW b'00001111' ; Clear the upper four bits of W register ANDWF BCD,W ; Clear the high four bits of BCD and store the result in the W register MOVWF BCDLO ; Temporarily save here for judgment MOVLW 0x04 BCF STATUS,C ; Flag cleared SUBWF BCDLO,F ; Check if the lower four bits are greater than 4 BTFSC STATUS, C; If the C bit is 0, jump CALL ADDLO ; C=1, that is, the lower four bits are greater than 4, then add 3 to the lower four bits nop MOVLW b'11110000' ; Clear the lower four bits of the W register ANDWF BCD,W ; Clear the lower four bits of BCD and store the result in the W register MOVWF BCDHI; Temporarily save here for judgment MOVLW 0x40 BCF STATUS,C ; Flag cleared SUBWF BCDHI,F ; Check whether the upper four bits are greater than 4 BTFSC STATUS, C; If the C bit is 0, jump CALL ADDHI ; C=1, that is, the upper four bits are greater than 4, then add 3 to the upper four bits nop DECFSZ COUNT,F ; loop GOTO Bin2BCD RETURN ;****************************************************** ***************** ADDLO ; add 3 to the lower four bits MOVF LOW3,W ADDWF BCD,F RETURN ADDHI ; add 3 to the upper four digits MOVF HIGH3,W ADDWF BCD,F RETURN ;****************************************************** ****************************** END ; End of program 2. Realize the conversion of compressed BCD code to binary number Because the compressed BCD code is a decimal number, we only need to take the upper four bits and the lower four bits, multiply the upper four bits by 10D and add the lower four bits. For the convenience of programming, we can convert the multiplication by 10D into accumulation 10 times. list p=16f877A ; Indicate the processor type used #include __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF ;*****************Variable definitions******************************************************* BIN EQU 0x20 ; store binary numbers BCD EQU 0x21 ; store compressed BCD code BCDLO EQU 0x22 ; store the lower four bits of the BCD code BCDHI EQU 0x23 ; store the high four bits of the BCD code COUNT EQU 0x24 ; Multiply the high bit by 10 (add itself 9 times) loop variable ;****************************************************** ****************************** ORG 0x0000 ; Reset entry address nop ; compatible with ICD debugging tools, nop must be added goto Main; Jump to Main function ;*****************************Main function code********************************** Main MOVLW b'01000101' ; BCD code 01000101 sent to BCD MOVWF BCD MOVLW .10 ; Initialize the number of loops MOVWF COUNT CLRF BIN ; clear CALL BCD2Bin ; Call the conversion program nop goto $ ; stop ;**************************Compress BCD code to binary subroutine******************** ORG 0X0100 BCD2Bin MOVLW b'00001111' ANDWF BCD,W ; Clear the high four bits of BCD and store the result in the W register MOVWF BCDLO ; Get the lower four bits of BCD MOVLW b'11110000' ANDWF BCD,W ; Clear the lower four bits of BCD and store the result in the W register MOVWF BCDHI SWAPF BCDHI,F ; Get the high four bits of BCD MOVF BCDHI,W CALL MPY10 ; Call MPY10 and multiply the upper four bits by 10 nop MOVF BCDLO,W ADDWF BIN,F ; add the lower four bits to complete the conversion RETURN MPY10 ADDWF BIN,F DECFSZ COUNT,F ; The upper 4 bits are multiplied by 10 and stored in BIN, that is, add the upper 4 bits 10 times GOTO MPY10 RETURN ;****************************************************** ****************************** END ; End of program
Previous article:PIC microcontroller lights up LED in various ways
Next article:PIC microcontroller I2C communication (slave mode)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- TI CCS compiler download update
- Simple and efficient zero-point acquisition circuit
- BMS solutions for electric bicycles and electric motorcycles under the new national standard for electric vehicles
- IIC communication problem between MSP430 and SHT11
- A Problem with TTL Inverter Circuit
- EEWORLD University Hall ---- Sensor Technology and Applications Fan Shangchun, Beijing University of Aeronautics and Astronautics
- 【Case Study】 Rigol spectrum analyzer base station signal test and Bluetooth frequency hopping signal test
- Analog Discovery 2 Review (6) Waveform Generator
- Share: How to lay out a circuit board for an op amp
- Questions about the settings of BQ24610's termination charge current + pre-charge current + battery charge voltage