Design of communication function realized by PIC microcontroller

Publisher:数据小巨人Latest update time:2019-12-17 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. PIC1 microcontroller programming (sending part)

LIST P=16F877


#INCLUDE P16F876.INC

CBLOCK 0X24; Reserve three bytes for display

COUNT; used as a counter or register

ENDC

ORG 0X0000 ; program reset entry

NOP

START GOTO MAIN

ORG 0X20

MAIN MOVLW 0X30; The following will initialize the RAM content

MOVWF FSR; Start from unit 30H

MOVLW 0X30 ; assign value 30H to unit 30H

MOVWF COUNT

INTRAM MOVF COUNT, 0; assign 30H to 7FH to unit 30H to 7FH

Design of communication function realized by PIC microcontroller

MOVWF INDF

INCF COUNT, 1

INCF FSR, 1

BTFSS COUNT, 7

GOTO INTRAM

BSF STATUS, RP0; Initialize the SCI component

MOVLW 0X19; Set the transmission baud rate to about 9600 bps

MOVWF SPBRG

MOVLW 0X04; Select asynchronous high-speed mode to transmit 8-bit data

MOVWF TXSTA

BCF STATUS, RP0

MOVLW 0X80; Enable synchronous serial port to work

MOVWF RCSTA

BSF STATUS, RP0

BSF T RISC , 7; Set RC6 and RC7 to input mode and disconnect the external circuit

BSF TRISC, 6

BCF STATUS, RP0

MOVLW 0X30; 30H is sent as a synchronization character

MOVWF FSR

MOVF INDF,0

MOVWF TXREG; write the data to be sent into the transmit buffer TXREG

BSF STATUS, RP0

BSF TXSTA, TXEN; Send permission

BCF STATUS, RP0

BSF RCSTA, CREN; receive data enable

LOOPTX BTFSS PIR1, RCIF ; Wait for the response byte from PIC2

GOTO LOOPTX

MOVF RCREG, 0; read response byte, clear RCIF

LOOPTX1 BTFSS PIR1, TXIF; send next byte

GOTO LOOPTX1

INCF FSR

MOVF INDF,0

MOVWF TXREG

BTFSS FSR, 7; Have the contents of the 30H~7FH units been sent?

GOTO LOOPTX ; No, continue to send the next byte

BSF STATUS, RP0; if yes, stop sending

BCF TXSTA, TXEN

BCF STATUS, RP0; Data transmission completed

CALLLED; call the display subroutine to display the sent data

END; program ends


2. PIC2 microcontroller programming (receiving part)

LIST P=16F876

#INCLUDE P16F876.INC

CBLOCK 0X24

COUNT

ENDC

ORG 0X0000

NOP

START GOTO MAIN

MAIN BSF STATUS, RP0; Initialization procedure is the same as the sending subroutine

MOVLW 0X19; baud rate setting is the same as PIC1

MOVWF SPBRG

MOVLW 0X04 ; asynchronous high-speed transmission

MOVWF TXSTA

BCF STATUS, RP0

MOVLW 0X80; serial port operation enable

MOVWF RCSTA

BSF STATUS, RP0

BSF TRISC, 7; isolated from external circuits

BSF TRISC, 6

BCF STATUS, RP0

MOVLW 0X30; store the sent data starting from unit 30H

MOVWF FSR

BSF RCSTA, CREN; receive enable

BSF STATUS, RP0

BSF TXSTA, TXEN; Send permission

BCF STATUS, RP0

WAIT BTFSS PIR1, RCIF; Waiting to receive data

GOTO WAIT

MOVF RCREG, 0; read data

MOVWF INDF ;Store the received response byte into the RAM of PIC2

INCF FSR

MOVWF TXREG ; send response byte

LOOPTX BTFSS PIR1, TXIF; Wait for writing to complete

GOTO LOOPTX

BTFSS FSR, 7; Are all data received?

GOTO WAIT1; No, continue to receive other data

BCF RCSTA, RCEN; After receiving, turn off the receiving and sending data enable

BSF STATUS, RP0

BCF PIE1, TXEN

BCF STATUS, RP0

CALL LED; call the display subroutine to display the received data

END; program ends

Reference address:Design of communication function realized by PIC microcontroller

Previous article:PIC microcontroller common instructions introduction and instruction function classification table
Next article:You should know these advantages of PIC microcontrollers

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号