The test function is to reset the microcontroller once, automatically read the data from 24C02 to the digital tube display, then add 1 to the value and write it into 24C02. Finally, the data in the digital tube is the number of times the machine is turned on, which has certain practical significance.
; The 24C02 used in this circuit is from ATMEL, or not from this manufacturer, so the programming time may be different
;The DELAY time of this program must be adjusted
The purpose of this practice is to make everyone more familiar with the timing of I2C communication, familiar with the reading and writing of 24CXX, and how to use software to simulate I2C communication.
;Hardware connection:
;1.24CXX's SDA connects to 877A's RB5 port, SCLK connects to 877A's RB4 port, WP connects to ground, A0, A1, A2 connect to ground
;2. Experiment: For this experiment, the 93CXX series chip on the MCD-DEMO experimental board must be removed first. During the experiment, do not press the buttons that are also connected to the RB port to avoid affecting the communication timing.
;3. The DIP switches S4 and S5 on the experimental board should be turned ON, and other DIP switches can be turned off.
;The program list is as follows:
;************************************
LIST P=16F877A, R=DEC
include ”P16F877A.inc“
;***********************************
__CONFIG _DEBUG_OFF&_CP_OFF&_WRT_HALF&_CPD_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC;
;**************************************** Define the lookup table offset
#define SDA PORTB,5
#define SCLK PORTB,4
;*********************
COUNT EQU 20H
ADDR EQU 21H
DAT EQU 23H
TEMP EQU 24H
;**********************
ORG 000H
NOP ; Place a no-operation instruction required by ICD
GOTO MAIN
ORG 004H
RETURN
ORG 0008H
;******************************************************
TABLE
ADDWF PCL, 1; address offset plus current PC value
RETLW 0C0H ;0
RETLW 0F9H ;1
RETLW 0A4H ;2
RETLW 0B0H ;3
RETLW 99H ;4
RETLW 92H ;5
RETLW 82H ;6
RETLW 0F8H ;7
RETLW 80H ;8
RETLW 98H ;9
RETLW 00H ;A
RETLW 00H ;B
RETLW 00H ;C
RETLW 00H ;D
RETLW 00H ;E
RETLW 00H ;F
;*******************************************************
MAIN
MOVLW 0FFH
MOVWF PORTC; All digital tubes are turned off first
MOVLW 0FFH
MOVWF PORTA
MOVLW 0FFH
MOVWF PORTB; SDT, SCLK are both high
BSF STATUS, RP0; define RA port, RC port, RB port are all output
MOVLW 07H
MOVWF ADC ON1; Set all RA ports to normal digital IO ports
CLRW
MOVWF TRISB ;
MOVWF TRISA
MOVWF TRISC
MOVWF OPTI ON_REG ; Enable the internal weak pull-up of RB port
BCF STATUS,RP0
CLRW ; Address 00H
CALL RD24 ; read address
MOVWF DAT ; the read value is sent to F1
SUBLW .9; If the read value is greater than 9, F1 is sent to 0, starting from 0 (because the 1-digit digital tube can only display 0-9)
BC TT2; C=0, transfer to TT2
TT1
CLRF DAT
TT2
MOVFW DAT
CALL TABLE ; Get the display segment code
MOVWF PORTC ;segment code sent to port C
BCF PORTA, 1; light up the first digital tube
INCF DAT, 1; Each time the power is turned on, the value stored in the 00H address of 24CXX is increased by 1
CLRW ; Address 00H
CALL WT24 ; write 24CXX
GOTO $
;****************************
RD24
MOVWF ADDR; The address is temporarily stored in F4
CALL START24 ; Start I2C
MOVLW 0A0H
CALL SUBS; write device address 1010000 + last bit 0 write operation
MOVFW ADDR ; load address
CALL SUBS ; write address
CALL START24 ; resend the start signal
MOVLW 0A1H; write device address 1010000 + last bit 1 read operation
CALL SUBS
BSF STATUS ,RP0
BSF TRISB, 5; Set SDA pin as input, ready to read
BCF STATUS ,RP0
MOVLW 08H; read 8 bits of data
MOVWF COUNT
RD000
NOP
NOP
NOP
BSF SCLK ; read data
NOP
BSF STATUS,C
BTFSS SDA
BCF STATUS,C
RLF TEMP ,1
BCF SCLK
DECFSZ COUNT, 1; loop to read 8 bits
GOTO RD000
BSF STATUS ,RP0
BCF TRISB, 5; restore SDA pin to output
BCF STATUS ,RP0
BSF SDA
CALL DELAY2
BSF SCLK
CALL DELAY2
BCF SCLK; Response completed, SDA set to 1
CALL STOP ; send stop signal
MOVFW TEMP ; send the read data to W
RETURN
;******************************Write 24C02 program
WT24 MOVWF ADDR; first store the address temporarily in F4
CALL START24 ;Start condition
MOVLW 0A0H
CALL SUBS; write device address 1010000 + last bit 0 write operation
MOVFW ADDR ; load address
CALL SUBS ; write address
MOVFW DAT ; load data
CALL SUBS ; write data
CALL STOP ;Stop signal
RETURN
START24
;Start condition
BSF SDA
BSF SCLK
CALL DELAY2
BCF SDA
CALL DELAY2
BCF SCLK
RETURN
STOP
BCF SDA ; Stop condition
NOP
NOP
BSF SCLK
CALL DELAY2
BSF SDA
RETURN
SUBS ; write data
MOVWF TEMP ; store the data to be written in F2
MOVLW 08H
MOVWF COUNT ; write 8-bit data
SH01
RLF TEMP ,1
BSF SDA
BTFSS STATUS ,C
BCF SDA
NOP
BSF SCLK
CALL DELAY2
BCF SCLK
DECFSZ COUNT, 1; loop to write 8 bits
GOTO SH01
BSF SDA
NOP
NOP
BSF SCLK
BSF STATUS,RP0
BSF TRISB ,5
BCF STATUS,RP0
REP
BTFSC SDA; judge whether the response has arrived, if not, wait
GOTO REP
BCF SCLK
BSF STATUS,RP0
BCF TRISB ,5
BCF STATUS,RP0
RETURN
DELAY2
NOP
NOP
NOP
NOP
RETURN
;********************************************
end ; source program ends
;*****************************************************
Previous article:The role of the prescaler in the PIC microcontroller
Next article:Application of Timer/Counter of PIC16F87X MCU
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Plant growth monitoring system source code based on Raspberry Pi 5
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- General Concept of Frequency Response
- (FPGA Experiment 5): Verilog HDL language digital tube clock (hours, minutes, seconds)
- How to choose the right AFE for BMS
- Design and production of LED electronic dot matrix screen control card (provide some ideas for question I)
- HC05 and HC06 Bluetooth configuration experience summary
- Some understanding of C language memory
- VGA display image
- Two watchdogs of STM32
- [NXP Rapid IoT Review] + Mobile Synchronizer 4
- The difference between bit rate, baud rate and data transmission rate