ECE 3740 SEP1: MPLAB Introduction and LED Blinking An Introduction to Microchip's MPLAB IDE and an Example Application: LED Blinking 1
Objective Rotate LEDs 2
Port Functions Library Help >Help Contents >Search: Port Functions PORTClearBits Function PORTRead Function PORTReadBits Function PORTResetPins Function PORTSetBits Function PORTSetPinsAnalogIn Function PORTSetPinsAnalogOut Function PORTSetPinsDigitalIn Function PORTSetPinsDigitalOut Function PORTToggleBits Function PORTWrite Function 3
PORTSetPinsDigitalOut FUNCTION Description Configures selected pins as digital outputs Input Parameters Description portid Enumerated PORT Identifier see ports.h Example MASK: Identifies which bits are to be configured as digital outputs: 1100 0000 PORTSetPinsDigitalOut(IOPORT_C, BIT_7 BIT_6); IO Port ID OR Operator 4
ports.h C:\Program Files (x86)\microchip\xc32\v1.33\pic32mx\include\peripheral typedef enum { #ifdef _PORTA IOPORT_A, #endif #ifdef _PORTB IOPORT_B,... #endif #ifdef _PORTG IOPORT_G, #endif IOPORT_NUM }IoPortId; #define BIT_31 (1 << 31) #define BIT_30 (1 << 30) #define BIT_29 (1 << 29) #define BIT_28 (1 << 28) #define BIT_27 (1 << 27) #define BIT_26 (1 << 26) #define BIT_25 (1 << 25) #define BIT_24 (1 << 24)... #define BIT_4 (1 << 4) #define BIT_3 (1 << 3) #define BIT_2 (1 << 2) #define BIT_1 (1 << 1) #define BIT_0 (1 << 0) 5
Complete MPLAB Project Led Blinking V1 main.c #include <plib.h> // include file required by all projects // Entire file is included at this point // Configuration Bits #pragma config FNOSC = PRIPLL // Oscillator Selection #pragma config FPLLIDIV = DIV_2 // PLL Input Divider #pragma config FPLLMUL = MUL_20 // PLL Multiplier #pragma config FPLLODIV = DIV_1 // PLL Output Divider #pragma config FPBDIV = DIV_1 // Peripheral Clock divisor #pragma config FWDTEN = OFF // Watchdog Timer #pragma config WDTPS = PS1 // Watchdog Timer Postscale #pragma config FCKSM = CSDCMD // Clock Switching & Fail Safe Monitor #pragma config OSCIOFNC = OFF // CLKO Enable #pragma config POSCMOD = XT // Primary Oscillator #pragma config IESO = OFF // Internal/External Switch-over #pragma config FSOSCEN = OFF // Secondary Oscillator Enable #pragma config CP = OFF // Code Protect #pragma config BWP = OFF // Boot Flash Write Protect #pragma config PWP = OFF // Program Flash Write Protect #pragma config DEBUG = ON // Debugger Enabled #pragma config ICESEL = ICS_PGx1 // Page 3 of 34: Cerebot_MX7cK_rm.pdf // CPU clock #define SYS_FREQ (80000000) 6
Complete MPLAB Project Led Blinking V1 main.c (Continued) int main(void) { // Start of main() function SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); //See <system.h> PORTSetPinsDigitalOut(IOPORT_G, BIT_12); //Set pin connected to LED1 as output PORTSetPinsDigitalOut(IOPORT_G, BIT_13); //Set pin connected to LED2 as output PORTSetPinsDigitalOut(IOPORT_G, BIT_14); //Set pin connected to LED3 as output PORTSetPinsDigitalOut(IOPORT_G, BIT_15); //Set pin connected to LED4 as output PORTClearBits(IOPORT_G, BIT_12); PORTClearBits(IOPORT_G, BIT_13); PORTClearBits(IOPORT_G, BIT_14); PORTClearBits(IOPORT_G, BIT_15); // LED1 off // LED2 off // LED3 off // LED4 off // Continued on next slide 7
Complete MPLAB Project Led Blinking V1 while(1) { unsigned int i; PORTSetBits(IOPORT_G, BIT_12); for(i = 0; i < 1000000; i++); PORTClearBits(IOPORT_G, BIT_12); PORTSetBits(IOPORT_G, BIT_13); for(i = 0; i < 1000000; i++); PORTClearBits(IOPORT_G, BIT_13); PORTSetBits(IOPORT_G, BIT_14); for(i = 0; i < 1000000; i++); PORTClearBits(IOPORT_G, BIT_14); PORTSetBits(IOPORT_G, BIT_15); for(i = 0; i < 1000000; i++); PORTClearBits(IOPORT_G, BIT_15); } } //End of main() function() // LED1 on // Delay some time // LED1 off // LED2 on // Delay some time // LED1 off // LED3 on // Delay some time // LED1 off // LED4 on // Delay some time // LED4 off 8
Project Modularization A project can be made more modular Object Oriented manner Easier to maintain Easier to read and debug Easier to manage Encapsulate similar items into their own file C files that do certain function and that function only Include files (*.h) files that contain certain code of definitions 9
Modular MPLAB Project Project was split into 3 C files main.c PortIO.c SysInit.c And 3 h files (include files) PICConfig.h PortConfig.h SysConfig.h Project 1.1: Figure out how I did the modularization, and implement it. 10
Configuration Management Project 1 has three parts, therefore: P1: P1.1, P1.1, and P1.3 P1.1 has two parts: therefore, P1.1.1 and P1.1.2 11
Configuration Management P1.1.1 has two versions: therefore, P1.1.1\v1 and P1.1.1\v2 12
Creating Folder for v2 For each major change, create a new MPLAB project and leave the old one untouched. Example: you want to change LED Blinking v1 to create modularized version v2 Using the OS: Create a new folder: v2 Using MPLAB: Create a new MPLAB project in that folder: v2, and name the project LEDBlinkingMod Using the OS: Copy the main.c file from the v1 folder to the v2 folder Using MPLAB: Include this v2\main.c file in your new project, and start making your edits. 13
Configuration Management Note: For project P1.1.2 The initial copy of the stack should not be changed. The first step is to save the original Stack, as shown in folder TCPIPStackOrg Using the OS: make a folder v1 under P1.1.2. Then, copy and paste TCPIPStack from TCPIPStackOrg to v1, as shown. TCPIPStackOrg should never be modified. The idea is to make a copy of the original stack, and work on that copy. If, for any reason, the application of the updates does not go as intended, you can always revert to the original stack distribution and start from there. Last working copy 14
What Should Not Be Changed Never change the Compiler s files!!! 15