The basic framework of C language programming for PIC microcontroller

Publisher:恬淡岁月Latest update time:2020-02-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: The basic framework of C language programming for PIC microcontrollers


The basic method of writing PIC microcontroller programs based on the PICC compilation environment is similar to that of standard C programs. The program generally consists of the following main parts:


1. Use the #include preprocessing directive at the beginning of the program to reference the included header file, which must include a "pic.h" file provided by the compiler to declare special registers and other special symbols in the microcontroller;


2. Use the "_CONFIG" preprocessor directive to define the chip's configuration bits;


3. Declare the types of all functions called in this module. PICC will perform strict type matching checks on the called functions.


4. Define global variables or symbol replacement;


5. Implement the function (subroutine), paying special attention that the main function must be an infinite loop with no return.


The following is an example of a C program for your reference.


#include //Includes the predefined internal resources of the MCU


#include "pc68.h" //Include custom header file


//Define the configuration bits when the chip is working


__CONFIG(HS&PROTECT&PWRTEN&BOREN&WDTDIS);


//Declare the function type called in this module


voidSetSFR(void);


voidClock(void);


voidKeyScan(void);


voidMeasure(void);


voidLCD_Test(void);


voidLCD_Disp(unsignedchar);


//Define variables


unsignedcharsecond,minute,hour;


bitflag1,flag2;


// Functions and subroutines


voidmain(void)


{


SetSFR();


PORTC=0x00;


TMR1H+=TMR1H_CONST;


LED1=LED_OFF;


LCD_Test();


//Main loop of program work


while(1){


asm("clrwdt"); // clear watchdog


Clock(); //Update the clock


KeyScan(); //Scan the keyboard


Measure(); //Data measurement


SetSFR(); //Refresh special function registers


}


}

Reference address:The basic framework of C language programming for PIC microcontroller

Previous article:Design of intelligent cleaning and nursing machine using PIC microcontroller
Next article:PIC C language programming_Variable definition in PICC

Latest Microcontroller Articles
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号