PIC microcontroller programming basic format typical program structure framework

Publisher:数字奇迹Latest update time:2020-02-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When creating a source program, first use the pseudo-instruction TITLE to provide the program title, then give a general description of the entire program, and use the list pseudo-instruction LIST to specify the microcontroller model and file output format used, and then use the INCLUDE pseudo-instruction to read the definition file provided in MPASM, such as "P16F84 INC", and then define the commonly used resources in the chip, and then give the basic structure framework of the general program. An example is as follows.


TITLE "This is..."; program title


;Procedure Description


LIST P="16F84",F=1NHX8M



include


-config_RC_Qsc&_WDT_0FF…


; Resource definition and variable definition


STATE OF THE HORSE 03


FSR EQU 04


PORTA EQUIPMENT 05


PORTB EQU 06


J EQU 01F


K EQU 01E


;…………………


ORG 0000 ;


goto MAIN ; skip the interrupt vector


ORG 0004


goto INTSRV; subroutine entry address


;……………………………………


MAIN


; Start putting the main program from 0005H


call Initports; port initialization


call InitTimers; timer initialization



INTSRV ... ;Interrupt service routine area


SVBRTH… ; Subroutine area


END ; program end character


Of course, you can adjust it according to the actual situation when writing the program. The following is an actual program list, which requires writing data 88H into the 20H unit of the internal EEPROM of the PIC16F84, and then reading it out from the 20H unit.


LIST P="16F84",F=INHX8M


;……………………………


STATUSEQU 03; define register


EEDATA EQU 08


EEADR EQU 09


INTCON EQU 0BH


EECON1 EQU 88H


EECON2 EQU 89H


;…………………………


RD EQU 0 ;Define bit


WR EQU 1


RP0 EQU 5


GIE EQU 7


;…………………………


ORG 0


GOTO WRSTART


;……………………………


ORG 10H


WRSTART ; write operation starts


CLRW; Clear W, make W=0


BCF STATUS, RP0; select BANK0


MOVLW 20H


MOVWF EEADR ; address → EEADR


MOVLW 88H


MOVWF EEDATA; write data →


; AUNT


BSF STATUS,RP0 ;选BANK1


BSF EECON1, 2; Write operation enable


BCF INTCON, GIE ; turn off all interrupts


MOVLW 0X55


MOVWF EECON2;55H→EECON2


MOVLW 0XAA


MOVWF EECON2 ;AAH→EECON2


BSF EECON1, WR; Start write operation


BSF INTCON, GIE; Restore interrupt


RDSTART ;Read operation starts


BCF STATUS,RP0


MOVLW 20H


MOVWF EEADR ; address → EEADR


BSF STATUS,RP0


BSF EECON1, RD; start read operation


BCF STATUS,RP0


MOVF EEDATA, W ; EEPROM


; Data read into W


END

Reference address:PIC microcontroller programming basic format typical program structure framework

Previous article:PIC microcontroller development issues
Next article:Motor control based on Microchip PIC32MM development board

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
Guess you like

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号