96
11/12/53 11/12/53 1 1 Prajin Prajin Palangsantikul Palangsantikul APPSOFTTECH CO.,LTD APPSOFTTECH CO.,LTD

PIC32 Training

Embed Size (px)

DESCRIPTION

พรีเซนเตชัน "การอบรม PIC32 ที่คณะวิทยาศาสตร์ คอมพิวเตอร์ ม.ศิลปากร ปี 53/54"

Citation preview

Page 1: PIC32 Training

11/12/5311/12/53 11

PrajinPrajin PalangsantikulPalangsantikul

APPSOFTTECH CO.,LTDAPPSOFTTECH CO.,LTD

Page 2: PIC32 Training

11/12/53 2

Agenda..Agenda..

►►WhatWhat’’s a Microcontroller?s a Microcontroller?►►MicrocontrollerMicrocontroller►►PIC/dsPIC/PIC32PIC/dsPIC/PIC32►►PIC32 Block diagramPIC32 Block diagram►►PIC32MX460F512LPIC32MX460F512L►►PIC32 Start kitPIC32 Start kit►►Install : MPLAB, MPLAB C32Install : MPLAB, MPLAB C32

Page 3: PIC32 Training

11/12/53 3

Agenda..Agenda..

►►3232--Bit Language Tools LibrariesBit Language Tools Libraries►►How to PIC32 ProgrammingHow to PIC32 Programming►►OscillatorOscillator

Lab 1 : Osc Basic

►►I/O PORTI/O PORTLab 2 : Blink an LEDLab 3 : Port Basic

Page 4: PIC32 Training

11/12/53 4

Agenda..Agenda..

►► InterruptInterruptLab 4 : CN InterruptLab 5 : External Interrupt

►► Timer/CounterTimer/CounterLab 6 : TimerLab 7 : Counter

►► UARTUARTLAB 8 : UART Polling LAB 8 : UART Polling LAB 9 : UART InterruptLAB 9 : UART Interrupt

►► PIC32 Robot Application.PIC32 Robot Application.►► QuestionQuestion

Page 5: PIC32 Training

11/12/53 5

WhatWhat’’s a Microcontroller?s a Microcontroller?

►►A A microcontrollermicrocontroller((sometimes abbreviated sometimes abbreviated µµCC, , uCuC or or MCUMCU)) is a is a small computer on a single small computer on a single integrated circuitintegrated circuitcontaining a processor corecontaining a processor core,, memorymemory,, and and programmable programmable inputinput//outputoutput peripherals peripherals

Page 6: PIC32 Training

11/12/53 6

Microcontroller. Microcontroller.

►►AVRAVR►►ARM7ARM7►►Basic StampBasic Stamp►►MCSMCS--5151►►MSPMSP--430430►►PIC/dsPIC/PIC32PIC/dsPIC/PIC32►►PSoCPSoC►►STM32STM32

Page 7: PIC32 Training

11/12/53 7

PIC/dsPIC/PIC32PIC/dsPIC/PIC32

Page 8: PIC32 Training

11/12/53 8

PIC32 Block diagram.PIC32 Block diagram.

Page 9: PIC32 Training

11/12/53 9

PIC32MX460F512LPIC32MX460F512L

►► Operating FrequencyOperating FrequencyDC DC –– 80 MHz80 MHz

►► Program Memory (Bytes)Program Memory (Bytes)512K512K

►► Interrupt Sources / Vectors Interrupt Sources / Vectors 95 / 6395 / 63

►► I/O PortsI/O PortsPorts A, B, C, D, E, F, GPorts A, B, C, D, E, F, G

►► Total I/O PinsTotal I/O Pins8383

►► DMA ChannelsDMA Channels4 + 2 USB4 + 2 USB

Page 10: PIC32 Training

11/12/53 10

PIC32MX460F512LPIC32MX460F512L►► Timers:Timers:

Total number (16Total number (16--bit) = 5bit) = 53232--bit (from paired 16bit (from paired 16--bit timers) = 2bit timers) = 23232--bit Core Timer = 1bit Core Timer = 1

►► Input Capture ChannelsInput Capture Channels55

►► Output Compare/PWM ChannelsOutput Compare/PWM Channels55

►► Input Change Interrupt NotificationInput Change Interrupt Notification2222

►► Serial Communications:Serial Communications:Enhanced UART = 2Enhanced UART = 2SPI (3SPI (3--wire/4wire/4--wire) = 2wire) = 2I2C = 2I2C = 2

Page 11: PIC32 Training

11/12/53 11

PIC32MX460F512LPIC32MX460F512L

►► Parallel Communications (PMP/PSP)Parallel Communications (PMP/PSP)Yes, 8Yes, 8--bit/16bit/16--bitbit

►► JTAG Boundary Scan JTAG Boundary Scan YesYes

►► JTAG Debug and ProgramJTAG Debug and ProgramYesYes

►► ICSP 2ICSP 2--wire Debug and Programwire Debug and ProgramYesYes

►► Instruction TraceInstruction TraceYesYes

Page 12: PIC32 Training

11/12/53 12

PIC32MX460F512LPIC32MX460F512L

►►Hardware Break Points:Hardware Break Points:6 Instruction, 2 Data6 Instruction, 2 Data

►►1010--bit Analogbit Analog--toto--Digital Module (input Digital Module (input channels)channels)

1616►►Analog ComparatorsAnalog Comparators

22►►Internal LDOInternal LDO

YesYes

Page 13: PIC32 Training

11/12/53 13

PIC32MX460F512LPIC32MX460F512L

►► Resets (and Delays)Resets (and Delays)POR, BOR, /MCLR, WDT, SWR (Software Reset), POR, BOR, /MCLR, WDT, SWR (Software Reset), CM (Configuration Bit Mismatch)CM (Configuration Bit Mismatch)(PWRT, OST, PLL Lock)(PWRT, OST, PLL Lock)

►► Instruction SupportInstruction SupportMIPS32 Enhanced Architecture (Release 2)MIPS32 Enhanced Architecture (Release 2)MIPS16eMIPS16e™™ Code CompressionCode Compression

►► PackagesPackages100100--pin TQFPpin TQFP

Page 14: PIC32 Training

11/12/53 14

PIC32 Start Kit.PIC32 Start Kit.

Page 15: PIC32 Training

11/12/53 15

PIC32 Start Kit.PIC32 Start Kit.

Page 16: PIC32 Training

11/12/53 16

PIC32 Start Kit.PIC32 Start Kit.

►► คุณสมบัติของบอรดคุณสมบัติของบอรด

รองรับการใชงานรองรับการใชงาน MCU 16Bit MCU 16Bit และและ 32Bit 32Bit ของของ Microchips Microchips ขนาดขนาด

100Pin100Pin สามารถใชไดกับสามารถใชไดกับ MCU MCU ตระกูลตระกูล PIC24FPIC24F//PIC24HPIC24H//dsPIC33dsPIC33 และและ

PIC32PIC32 ไดได ตามการติดตั้งตามการติดตั้ง MCU MCU โมดูลใหกับบอรดโมดูลใหกับบอรด อาทิอาทิ เชนเชน

PIC24FJ128GB110, PIC24HJ256GP210, dsPIC33FJ256MC710, PIC24FJ128GB110, PIC24HJ256GP210, dsPIC33FJ256MC710,

PIC32MX360F512L PIC32MX360F512L และและ PIC32MX460F512LPIC32MX460F512L เปนตนเปนตน

XTAL XTAL ความถี่ความถี่ 8.00 MHz8.00 MHz สําหรับสําหรับ System Clock SourceSystem Clock Source

XTAL XTAL ความถี่ความถี่ 32.768 KHz 32.768 KHz สําหรับสําหรับ RTCC Clock SourceRTCC Clock Source

Page 17: PIC32 Training

11/12/53 17

PIC32 Start Kit.PIC32 Start Kit.

►►คุณสมบตัขิองบอรดคุณสมบตัขิองบอรด

Ethernet LAN Driver (ENC28J60)Ethernet LAN Driver (ENC28J60) พรอมพรอม RJ45 Ethernet LAN RJ45 Ethernet LAN Port Port จํานวนจํานวน 11 พอรตพอรต

SPI EEPROM (25LC256)SPI EEPROM (25LC256) ใชงานและเกบ็ใชงานและเกบ็ Web Page Code Web Page Code ในในการทดลองใชงานการทดลองใชงาน Ethernet LAN Ethernet LAN ตามตวัอยางตามตวัอยาง Application Application Note Note จากจาก Microchips TCPMicrochips TCP//IP StackIP Stack

SD Card Interface SD Card Interface มาตรฐานมาตรฐาน พรอมสัญญาณพรอมสัญญาณ Card Detect Card Detect และและ Write Protect Write Protect จํานวนจํานวน 11 ชุดชุด

Page 18: PIC32 Training

11/12/53 18

PIC32 Start Kit.PIC32 Start Kit.

►► คุณสมบัติของบอรดคุณสมบัติของบอรด

UART DriverUART Driver แบบแบบ RS232RS232 พรอมขั้วตอแบบพรอมขั้วตอแบบ CPACPA--44 มาตรฐานอีทีทีมาตรฐานอีทีที

จํานวนจาํนวน 22 ชองชอง

USB Interface USB Interface รองรับการใชงานรองรับการใชงาน USB USB ทั้งแบบทั้งแบบ DeviceDevice//OTG OTG และและ Host Host

สามารถใชงานไดกับสามารถใชงานไดกับ PIC32MX460F512PIC32MX460F512 และและ PIC24FJ128GB110PIC24FJ128GB110

14Pin Header (Character Dot14Pin Header (Character Dot--Matrix LCD Interface) Matrix LCD Interface) จํานวนจํานวน 11 ชุดชุด

LED IndicatorLED Indicator สําหรับทดสอบการทํางานของสําหรับทดสอบการทํางานของ Output Logic Output Logic จํานวนจํานวน

44 ชุดชุด

Page 19: PIC32 Training

11/12/53 19

PIC32 Start Kit.PIC32 Start Kit.

►► คุณสมบัติของบอรดคุณสมบัติของบอรด

SW Push ButtonSW Push Button สําหรับทดสอบการทํางานของสําหรับทดสอบการทํางานของ Input Logic Input Logic จํานวนจํานวน

44 ชุดชุด

Volume Volume ((VRVR) ) ปรับคาปรับคา ใชปรับแรงดันใชปรับแรงดัน 00--3.3V 3.3V สําหรับทดสอบการสําหรับทดสอบการ

ทํางานของทํางานของ ADC ADC จํานวนจํานวน 11 ชองชอง

ICD2 Port ICD2 Port แบบแบบ RJ11RJ11 มาตรฐานมาตรฐาน ICD2 Microchips ICD2 Microchips รองรับการรองรับการ InIn--

Circuit Program Circuit Program และและ DebugDebug

พรอมสวิตชพรอมสวิตช ตัดตอสัญญาณสําหรับตัดตอสัญญาณสําหรับ ProgramProgram//Debug Debug และและ ใชงานปรกติใชงานปรกติ

พรอมพรอม LED LED แสดงสถานะแสดงสถานะ

Page 20: PIC32 Training

11/12/53 20

PIC32 Start Kit.PIC32 Start Kit.

►► คุณสมบัติของบอรดคุณสมบัติของบอรด

ขั้วตอขั้วตอ JTAG Port JTAG Port แบบแบบ IDE Header 14 Pin IDE Header 14 Pin สําหรับใชสําหรับใช ProgramProgram//Debug Debug

รวมกับรวมกับ JTAG Tools JTAG Tools ของของ MicrochipsMicrochips

ขั้วตอขั้วตอ DEBUG PortDEBUG Port((Debug Trace PortDebug Trace Port) ) แบบแบบ IDE Header 10 Pin IDE Header 10 Pin

สําหรับใชสําหรับใช Debug Debug รวมกับรวมกับ Debug ToolsDebug Tools((MPLAB REAL ICE InMPLAB REAL ICE In--Circuit Circuit

EmulatorEmulator) ) ของของ MicrochipsMicrochips

Regulate 3.3VRegulate 3.3V//3A 3A แบบแบบ Switching Switching ลดปญหาความรอนจากลดปญหาความรอนจาก Regulate Regulate

พรอมพรอม LED LED แสดงสถานะแสดงสถานะ ใชกับแหลงจายไฟใชกับแหลงจายไฟ ++5V 5V แบบแบบ TypeType--BB

Page 21: PIC32 Training

11/12/53 21

PIC32 Start Kit.PIC32 Start Kit.

I/O PortI/O PortPINPIN

Page 22: PIC32 Training

11/12/53 22

PIC32 Start Kit.PIC32 Start Kit.

►►การใชงานวงจรการใชงานวงจร LED LED แสดงผลแสดงผล

Page 23: PIC32 Training

11/12/53 23

PIC32 Start Kit.PIC32 Start Kit.

►►การใชงานวงจรการใชงานวงจร Push Button SwitchPush Button Switch

Page 24: PIC32 Training

11/12/53 24

PIC32 Start Kit.PIC32 Start Kit.

►►การใชงานการใชงาน RS232RS232

Page 25: PIC32 Training

11/12/53 25

PIC32 Start Kit.PIC32 Start Kit.

Page 26: PIC32 Training

11/12/53 26

MPLAB, MPLAB C32MPLAB, MPLAB C32

►►Install/SetupInstall/SetupMPLAB IDEMPLAB IDEMPLAB C32MPLAB C32

►►DemoDemo

Page 27: PIC32 Training

11/12/53 27

3232--Bit Language Tools Libraries.Bit Language Tools Libraries.

►► Standard C Libraries (with Math Functions)Standard C Libraries (with Math Functions)Standard ANSI C library functions are contained in the Standard ANSI C library functions are contained in the libraries libraries libc.alibc.a and and libgcc.alibgcc.a..

►► PIC32 Peripheral LibrariesPIC32 Peripheral Librariesfunctions and macros contained in the 32functions and macros contained in the 32--bit peripheral bit peripheral libraries (use peripheral libraries need to include libraries (use peripheral libraries need to include <<plib.hplib.h> file in their source file.)> file in their source file.)

►► PIC32 DSP LibraryPIC32 DSP LibraryVector Math FunctionsVector Math FunctionsFiltersFiltersTransformsTransformsVideoVideo

Page 28: PIC32 Training

11/12/53 28

PIC32 Peripheral Libraries.PIC32 Peripheral Libraries.

►► System FunctionsSystem FunctionsThe PIC32MX system library consists of functions and macros to perform system level operations.

►► Oscillator FunctionsOscillator FunctionsThe PIC32MX has multiple clock sources, with varying degrees of adjustability. The oscillator library functions are available to allow high-level control of the clock source and scaling of the frequency at runtime.

►► II//O PORT FunctionsO PORT FunctionsThe 32The 32--bit I/O PORT Peripheral Library consists of bit I/O PORT Peripheral Library consists of simple, code efficient macros and functions supporting simple, code efficient macros and functions supporting the configuration and use of this peripheral.the configuration and use of this peripheral.

Page 29: PIC32 Training

11/12/53 29

PIC32 Peripheral LibrariesPIC32 Peripheral Libraries..

►► Interrupt FunctionsInterrupt FunctionsFunctions for Peripheral Interrupt and Software Interrupt.

►► Timer FunctionsTimer FunctionsThe 32-bit TIMER Peripheral Library consists of simple, code efficient macros and functions supporting the configuration and use of this peripheral.

►► UART FunctionsUART FunctionsFunctions for UART module and an example of use of the functions. Functions may be implemented as macros.

Page 30: PIC32 Training

11/12/53 30

How to PIC32 Programming.How to PIC32 Programming.

►►Development BoardDevelopment Board►►MPLAB IDEMPLAB IDE►►PIC32 LIBRARYPIC32 LIBRARY►►C Language for PIC32C Language for PIC32►►DatasheetDatasheet►►GoGo…………..

Page 31: PIC32 Training

11/12/53 31

C Language for PIC32C Language for PIC32

►►File naming conventionsFile naming conventions

Page 32: PIC32 Training

11/12/53 32

C Language for PIC32C Language for PIC32

►►Integer RepresentationInteger Representation

Page 33: PIC32 Training

11/12/53 33

C Language for PIC32C Language for PIC32

►►FloatingFloating--Point RepresentationPoint Representation

Page 34: PIC32 Training

11/12/53 34

C Language for PIC32C Language for PIC32

►►Single FileSingle File

Page 35: PIC32 Training

11/12/53 35

C Language for PIC32C Language for PIC32

►►Multiple FilesMultiple Files

Page 36: PIC32 Training

11/12/53 36

C Language for PIC32C Language for PIC32

► Interrupt Handler Function

Ref.. (sys/attribs.h)(proc\p32mx460f512l.h)

// configure the Timer 1 interrupt handler// configure the Timer 1 interrupt handlervoid __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void)void __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void){{

// clear the interrupt flag// clear the interrupt flagmT1ClearIntFlag();mT1ClearIntFlag();// .. in this case, toggle the LED// .. in this case, toggle the LEDmPORTDToggleBits(BIT_0);mPORTDToggleBits(BIT_0);

}}

Page 37: PIC32 Training

11/12/53 37

C Language for PIC32C Language for PIC32

►► Low Level Processor ControlLow Level Processor ControlInclude either the generic processor header file (i.e., Include either the generic processor header file (i.e., p32xxxx.h) or the processorp32xxxx.h) or the processor--specific header file for the specific header file for the appropriate device (e.g., proc/p32mx360f512l.h).appropriate device (e.g., proc/p32mx360f512l.h).►►#include <p32xxxx.h>#include <p32xxxx.h>

Access Access SFRsSFRs like any other C variables.like any other C variables.►►Example, the following statement clears all the bits to zero in Example, the following statement clears all the bits to zero in

the special function register for Timer 1: the special function register for Timer 1: TMR1 = 0;TMR1 = 0;

►►The next statement enables the Watchdog Timer:The next statement enables the Watchdog Timer:WDTCONbits.ONWDTCONbits.ON = 1;= 1;

Page 38: PIC32 Training

11/12/53 38

C Language for PIC32C Language for PIC32►► Configuration Bit AccessConfiguration Bit Access

##pragmapragma configconfig►►The #The #pragmapragma configconfig directive specifies the processordirective specifies the processor--specific specific

configuration settings (i.e., configuration bits) to be used by configuration settings (i.e., configuration bits) to be used by the the application.application.

►► The example does the following:The example does the following:•• Enables the Watchdog Timer,Enables the Watchdog Timer,•• Sets the Watchdog Sets the Watchdog PostscalerPostscaler to 1:128, andto 1:128, and•• Selects the HS Oscillator for the Primary OscillatorSelects the HS Oscillator for the Primary Oscillator

##pragmapragma configconfig FWDTEN = ON, WDTPS = PS128FWDTEN = ON, WDTPS = PS128##pragmapragma configconfig POSCMOD = HSPOSCMOD = HSvoid main (void)void main (void){{

......}}

Page 39: PIC32 Training

11/12/53 39

C Language for PIC32C Language for PIC32// Include either the generic processor header file #include <p32xxxx.h>// Master header file for all peripheral library includes#include <plib.h>

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_4#pragma config FWDTEN = OFF

#define SYS_FREQ (80000000L)

int main(void){// Configure System ClockSYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);while (1)

;}

Page 40: PIC32 Training

11/12/53 40

Oscillator.Oscillator.

Page 41: PIC32 Training

11/12/53 41

Oscillator.Oscillator.

►►Primary Oscillator Operating ModesPrimary Oscillator Operating Modes

Page 42: PIC32 Training

11/12/53 42

Primary Oscillator (POSC) Configuration.Primary Oscillator (POSC) Configuration.

Page 43: PIC32 Training

11/12/53 43

Configuration Configuration Bits Bits register.register.

►► COSC<2:0>COSC<2:0>Current Oscillator Selection bitsCurrent Oscillator Selection bits

►► FPLLIDIV<2:0>FPLLIDIV<2:0>Default PLL Input Divider Value bitsDefault PLL Input Divider Value bits

►► PLLMULT<2:0>PLLMULT<2:0>PLL Multiplier bitsPLL Multiplier bits

►► PLLODIV<2:0>PLLODIV<2:0>Output Divider for PLLOutput Divider for PLL

►► HSPLL PrimaryHSPLL PrimaryOscillator with PLL moduleOscillator with PLL module

Page 44: PIC32 Training

11/12/53 44

System Functions.System Functions.

►►SYSTEMConfigSYSTEMConfig() function() functionIncludeInclude: : plibplib..hhPrototypePrototype: : void void SYSTEMConfigSYSTEMConfig((unsigned unsigned intint sys_clocksys_clock, , unsigned unsigned intint flagsflags));;Code ExampleCode Example: : SYSTEMConfigSYSTEMConfig((80000000, SYS_CFG_WAIT_STATES |80000000, SYS_CFG_WAIT_STATES |SYS_CFG_PCACHESYS_CFG_PCACHE)); ; // // Does no configure PBDIVDoes no configure PBDIV..

Page 45: PIC32 Training

11/12/53 45

Configuration Settings Configuration Settings "PIC32MX460F512L""PIC32MX460F512L"

Page 46: PIC32 Training

11/12/53 46

Setting Configuration Bits in Code.Setting Configuration Bits in Code.

►► // // Configuration Bit settingsConfiguration Bit settings// // SYSCLK SYSCLK = = 80 MHz 80 MHz ((8MHz Crystal8MHz Crystal/ / FPLLIDIV FPLLIDIV * * FPLLMUL FPLLMUL / / FPLLODIVFPLLODIV))// // PBCLK PBCLK = = 40 MHz40 MHz// // Primary Primary OscOsc ww//PLL PLL ((XTXT++,HS,HS++,EC,EC++PLLPLL))// // WDT OFFWDT OFF// // Other options are don't careOther options are don't care

##pragmapragma configconfig FPLLMUL FPLLMUL = = MUL_20, FPLLIDIV MUL_20, FPLLIDIV = = DIV_2,DIV_2,FPLLODIV FPLLODIV = = DIV_1, FWDTEN DIV_1, FWDTEN = = OFFOFF

##pragmapragma configconfig POSCMOD POSCMOD = = HS, FNOSC HS, FNOSC = = PRIPLL,PRIPLL,FPBDIV FPBDIV = = DIV_8DIV_8

Page 47: PIC32 Training

11/12/53 47

SYSCLK:80Mhz Calculation.SYSCLK:80Mhz Calculation.

►► SYSCLK SYSCLK = = 80 MHz 80 MHz ((8MHz Crystal8MHz Crystal/ / FPLLIDIV FPLLIDIV * * FPLLMUL FPLLMUL / / FPLLODIVFPLLODIV))

FPLLIDIV = 2, FPLLMUL = 20,FPLLIDIV = 2, FPLLMUL = 20, FPLLODIVFPLLODIV = 1= 1

SYSCLK = 8MHz Crystal/2 = 4MHz SYSCLK = 8MHz Crystal/2 = 4MHz // Fin:4MHz// Fin:4MHz--5MHz5MHz= 4MHz x 20= 4MHz x 20= 80MHz/1= 80MHz/1= = 80MHz80MHz

Page 48: PIC32 Training

11/12/53 48

Oscillator FunctionsOscillator Functions..

►►OSCConfigOSCConfig() () functionfunctionIncludeInclude: : plibplib..hhPrototypePrototype: : void void OSCConfigOSCConfig((unsigned long unsigned long intint config1, unsigned config1, unsigned long long intint config2, unsigned long config2, unsigned long intint config3, unsigned config3, unsigned long long intint config4config4));;

Code ExampleCode Example: : OscConfigOscConfig((OSC_POSC_PLL, OSC_PLL_MULT_15, OSC_POSC_PLL, OSC_PLL_MULT_15, OSC_PLL_POST_1, 0OSC_PLL_POST_1, 0));;

Page 49: PIC32 Training

11/12/53 49

Oscillator Functions.Oscillator Functions.

►►mOSCSetPBDIVmOSCSetPBDIV() () functionfunctionIncludeInclude: : plibplib..hhPrototypePrototype: : mOSCSetPBDIVmOSCSetPBDIV((unsigned unsigned intint configconfig));;

Code ExampleCode Example: : mOscSetPBDIVmOscSetPBDIV((OSC_PB_DIV_8OSC_PB_DIV_8));;

Page 50: PIC32 Training

11/12/53 50

ConfigConfig Oscillator Examples.Oscillator Examples.

►►Examples OscillatorExamples OscillatorLab 1 : Lab 1 : OscOsc BasicBasic

►►Test.. Test.. Change 80MHz toChange 80MHz to40MHz40MHz

OscillatorOscillator

Page 51: PIC32 Training

11/12/53 51

SYSCLK:40Mhz Calculation.SYSCLK:40Mhz Calculation.

►► SYSCLK SYSCLK = = 40 MHz 40 MHz ((8MHz Crystal8MHz Crystal/ / FPLLIDIV FPLLIDIV * * FPLLMUL FPLLMUL / / FPLLODIVFPLLODIV))

FPLLIDIV = 2, FPLLMUL = 20,FPLLIDIV = 2, FPLLMUL = 20, FPLLODIVFPLLODIV = 2= 2

SYSCLK = 8MHz Crystal/2 = 4MHz SYSCLK = 8MHz Crystal/2 = 4MHz // Fin:4MHz// Fin:4MHz--5MHz5MHz= 4MHz x 20= 4MHz x 20= 80MHz/2= 80MHz/2= = 40MHz40MHz

Page 52: PIC32 Training

11/12/53 52

I/O PORT.I/O PORT.

Page 53: PIC32 Training

11/12/53 53

Pin Diagram Pin Diagram ((100100--Pin USBPin USB).).

Page 54: PIC32 Training

11/12/53 54

I/O PORT.I/O PORT.

►►The key features of this module:The key features of this module:Individual Individual output pin openoutput pin open--draindrain enable/disableenable/disableIndividual Individual input pin pullinput pin pull--upup enable/disableenable/disableMonitor select Monitor select inputs and generate interruptinputs and generate interrupt on on mismatch conditionmismatch conditionOperateOperate during CPU during CPU SLEEPSLEEP and and IDLEIDLE modesmodesFast bit manipulation using CLR, SET and INV Fast bit manipulation using CLR, SET and INV registersregisters

Page 55: PIC32 Training

11/12/53 55

Block diagram of a typical port structureBlock diagram of a typical port structure

Page 56: PIC32 Training

11/12/53 56

I/O PORT.I/O PORT.

►► The IThe I//O Ports module consists of the O Ports module consists of the following Special Function Registers following Special Function Registers ((SFRsSFRs):):

TRISxTRISx: : Data Direction register for the module Data Direction register for the module ‘‘xx’’PORTxPORTx: : PORT register for the module PORT register for the module ‘‘xx’’LATxLATx: : Latch register for the module Latch register for the module ‘‘xx’’ODCxODCx: : OpenOpen--Drain Control register for the module Drain Control register for the module ‘‘xx’’CNCONCNCON: : InterruptInterrupt--onon--Change Control registerChange Control registerCNENCNEN: : Input Change Notification Interrupt Enable Input Change Notification Interrupt Enable registerregisterCNPUECNPUE: : Input Change Notification PullInput Change Notification Pull--up Enable up Enable registerregister

Page 57: PIC32 Training

11/12/53 57

I/O PORT.I/O PORT.

►► The IThe I//O Ports module also has the following O Ports module also has the following associated bits for interrupt controlassociated bits for interrupt control::

Interrupt EnableInterrupt Enable Control bits for CN events Control bits for CN events ((CNIECNIE) ) in in INT register IEC1INT register IEC1: : Interrupt Enable Control Register 1Interrupt Enable Control Register 1..Interrupt Flag StatusInterrupt Flag Status bits for CN events bits for CN events ((CNIFCNIF) ) in INT in INT register IFS1register IFS1: : Interrupt Flag Status Register 1Interrupt Flag Status Register 1..Interrupt PriorityInterrupt Priority Control bits Control bits ((CNIP<2:0>CNIP<2:0>) ) in INT in INT register IPC6register IPC6: : Interrupt Priority Control Register 6Interrupt Priority Control Register 6..

Page 58: PIC32 Training

11/12/53 58

I/O PORT.I/O PORT.

►►CONTROL REGISTERSCONTROL REGISTERSTRIS TRIS ((tritri--statestate) ) RegistersRegisters►configure the data direction flow through

port I/O pin(s).PORT RegistersPORT Registers►PORT registers allow I/O pins to be accessed (read).

LAT RegistersLAT Registers►►LAT registers (PORT data latch) hold data written to LAT registers (PORT data latch) hold data written to

port I/O port I/O pin(spin(s).).

Page 59: PIC32 Training

11/12/53 59

Control Registers.Control Registers.

►►TRIS (triTRIS (tri--state) Registersstate) RegistersTRIS registers configure the data direction flow TRIS registers configure the data direction flow through port I/O through port I/O pin(spin(s). The TRIS register bits ). The TRIS register bits determine whether a PORT I/O pin is an input or determine whether a PORT I/O pin is an input or an output.an output.

A TRIS bit set = 1A TRIS bit set = 1 configures the corresponding I/O port pin configures the corresponding I/O port pin as an as an input.input.A TRIS bit set = 0A TRIS bit set = 0 configures the corresponding I/O port pin configures the corresponding I/O port pin as an as an output.output.A read from a TRIS register reads the last value written to the A read from a TRIS register reads the last value written to the TRIS TRIS register.register.All I/O port pins are defined as inputs after a PowerAll I/O port pins are defined as inputs after a Power--on Reset.on Reset.

Page 60: PIC32 Training

11/12/53 60

Control Registers.Control Registers.

►►PORT RegistersPORT RegistersPORT registers allow I/O pins to be accessed (read).

A write to a PORT register writes to the corresponding LAT register (PORT data latch). Those I/O port pin(s) configured as outputs are updated.A write to a PORT register is the effectively the same as a write to a LAT register.A read from a PORT register reads the synchronized signal applied to the port I/O pins.

Page 61: PIC32 Training

11/12/53 61

Control Registers.Control Registers.

►►LAT RegistersLAT RegistersLAT registers (PORT data latch) hold data written to port I/O pin(s).

A write to a LAT register latches data to corresponding port I/O pins. Those I/O port pin(s)

configured as outputs are updated.

A read from LAT register reads the data held in the PORT data latch, not from the port I/O pins.

Page 62: PIC32 Training

11/12/53 62

Modes of Operation.Modes of Operation.

►► Digital InputsDigital InputsSetting the corresponding bit in the AD1PCFG register = Setting the corresponding bit in the AD1PCFG register = 1 enables the pin as a digital pin.1 enables the pin as a digital pin.

►► Analog InputsAnalog InputsSetting the corresponding bits in the AD1PCFG register Setting the corresponding bits in the AD1PCFG register = 0 enables the pin as an analog input pin.= 0 enables the pin as an analog input pin.

►► Digital OutputsDigital Outputssetting the corresponding TRIS register bits = 0. When setting the corresponding TRIS register bits = 0. When configured as digital outputs, these pins are CMOS configured as digital outputs, these pins are CMOS drivers or can be configured as opendrivers or can be configured as open--drain outputs by drain outputs by setting the corresponding bits in the ODC register.setting the corresponding bits in the ODC register.

Page 63: PIC32 Training

11/12/53 63

I/O Port I/O Port Code Example.Code Example.

Page 64: PIC32 Training

11/12/53 64

II//O PORT Functions & Macros.O PORT Functions & Macros.

►► Port Pin Digital Port Pin Digital Functions and MacrosFunctions and MacrosPORTSetPinsDigitalInPORTSetPinsDigitalInPORTSetPinsDigitalOutPORTSetPinsDigitalOutmPORTxSetPinsDigitalInmPORTxSetPinsDigitalInmPORTxSetPinsDigitalOutmPORTxSetPinsDigitalOut

►► Port Pin Direction Port Pin Direction Functions and MacrosFunctions and MacrosmPORTxDirectionmPORTxDirectionmPORTxGetDirectionmPORTxGetDirectionmPORTxReadDirectionmPORTxReadDirectionmPORTxReadDirectionBitsmPORTxReadDirectionBitsmPORTxCloseBitsmPORTxCloseBitsmPORTxCloseAllmPORTxCloseAllPORTResetPinsPORTResetPins

Page 65: PIC32 Training

11/12/53 65

Port Pin Digital ExamplesPort Pin Digital Examples

►►PORTSetPinsDigitalInPORTSetPinsDigitalInCode Example:Code Example:PORTSetPinsDigitalIn(PORTSetPinsDigitalIn(IOPORT_CIOPORT_C, BIT_1 | , BIT_1 | BIT_0);BIT_0);

►►mPORTmPORTxxSetPinsDigitalInSetPinsDigitalInCode Example:Code Example:mPORTmPORTCCSetPinsDigitalInSetPinsDigitalIn((BIT_1 | BIT_0BIT_1 | BIT_0));;

Page 66: PIC32 Training

11/12/53 66

II//O PORT Functions & Macros.O PORT Functions & Macros.

►► Port InputPort Input Functions and MacrosFunctions and MacrosmPORTxReadmPORTxReadmPORTxReadBitsmPORTxReadBitsmPORTxReadLatchmPORTxReadLatchmPORTxReadLatchBitsmPORTxReadLatchBitsPORTReadPORTReadPORTReadBitsPORTReadBits

►► Port OutputPort Output Functions and MacrosFunctions and MacrosmPORTxWritemPORTxWritemPORTxSetBitsmPORTxSetBitsmPORTxClearBitsmPORTxClearBitsmPORTxToggleBitsmPORTxToggleBitsPORTWritePORTWritePORTSetBitsPORTSetBitsPORTClearBitsPORTClearBitsPORTToggleBitsPORTToggleBits

Page 67: PIC32 Training

11/12/53 67

Port Input ExamplesPort Input Examples

►►PORTReadPORTReadCode Example:Code Example:value = value = PORTRead(IOPORT_CPORTRead(IOPORT_C););

►►mPORTmPORTxxReadReadCode Example:Code Example:value = value = mPORTmPORTCCReadRead();();

Page 68: PIC32 Training

11/12/53 68

I/O Port ExamplesI/O Port Examples..

►►ExamplesExamplesLab 2 : Blink an LEDLab 2 : Blink an LEDLab 3 : Port BasicLab 3 : Port Basic

►►Test.. Test.. INPUTINPUT

OUTPUTOUTPUT

Page 69: PIC32 Training

11/12/53 69

Change Notification Pins.Change Notification Pins.

►► The Change Notification (CN) pins provide The Change Notification (CN) pins provide PIC32MX devices the ability to generate interrupt PIC32MX devices the ability to generate interrupt requests to the processor in response to a change requests to the processor in response to a change of state on selected input pins (corresponding of state on selected input pins (corresponding TRISxTRISx bits must = 1).bits must = 1).

Note: Pull up on CN pins should always be disabled whenever the port pin is configured as a digital output.

Page 70: PIC32 Training

11/12/53 70

Input Change Notification Block Diagram.Input Change Notification Block Diagram.

Page 71: PIC32 Training

11/12/53 71

Change Notice Configuration Example.Change Notice Configuration Example.

Page 72: PIC32 Training

11/12/53 72

Change Notice ISR Code Example.Change Notice ISR Code Example.

Page 73: PIC32 Training

11/12/53 73

InterruptInterrupt..

InterruptInterrupt ISRISR

Page 74: PIC32 Training

11/12/53 74

InterruptInterrupt..

►►Internal InterruptInternal InterruptTimer/CounterTimer/CounterSerialSerialA/DA/DEtc.Etc.

►►External InterruptExternal InterruptIntIntCNCN

Page 75: PIC32 Training

11/12/53 75

External InterruptsExternal Interrupts..

►► The interrupt controller supports The interrupt controller supports five external five external interruptinterrupt--requestrequest signals (INT4signals (INT4--INT0). INT0).

►► These inputs are edge sensitive, they require a These inputs are edge sensitive, they require a lowlow--toto--high or a highhigh or a high--toto--low transitionlow transition to create an to create an interrupt request.interrupt request.

►► The INTCON register has five bits that select the The INTCON register has five bits that select the polarity of the edge detection circuitry: polarity of the edge detection circuitry:

INT4EP (INTCON<4>)INT3EP (INTCON<3>)INT2EP (INTCON<2>)INT1EP (INTCON<1>) INT0EP (INTCON<0>).

Page 76: PIC32 Training

11/12/53 76

Setting External Interrupt Polarity.Setting External Interrupt Polarity.

Page 77: PIC32 Training

11/12/53 77

Interrupt ExamplesInterrupt Examples..

►►ExamplesExamplesLab 4 : CN InterruptLab 5 : INT Interrupt

►►Test.. Test.. InterruptInterrupt

OutputOutput

Page 78: PIC32 Training

11/12/53 78

Timer/CounterTimer/Counter..

►► All timers are classified into All timers are classified into two typestwo types to account to account for their functional differences.for their functional differences.

Type A TimerType A Timer (16-bit synchronous/asynchronous timer/counter with gate)Type B TimerType B Timer (16-bit, 32-bit synchronous timer/counter with gate and Special Event Trigger)

►► All Timer modulesAll Timer modules includes the following common includes the following common features:features:

16-bit timer/counterSoftware-selectable internal or external clock sourceProgrammable interrupt generation and priorityGated external pulse counter

Page 79: PIC32 Training

11/12/53 79

Timer/CounterTimer/Counter..

►► Beyond the common features, each timer type Beyond the common features, each timer type offers these additional features:offers these additional features:

Type A:Type A:►Asynchronous timer/counter with a built-in oscillator►Operational during CPU SLEEP mode►Software selectable prescalers 1:1, 1:8, 1:64 and 1:256

Type B:Type B:►Ability to form a 32-bit timer/counter►Software prescalers 1:1, 1:2, 1:4, 1:8, 1:16, 1:32, 1:64 and

1:256►Event trigger capability

Page 80: PIC32 Training

11/12/53 80

Type A Timer Block DiagramType A Timer Block Diagram..

Page 81: PIC32 Training

11/12/53 81

Type B Timer Block Diagram (16Type B Timer Block Diagram (16--Bit)Bit)..

Page 82: PIC32 Training

11/12/53 82

Type B Timer Block Diagram (32Type B Timer Block Diagram (32--Bit)Bit)..

Page 83: PIC32 Training

11/12/53 83

Timer Functions.Timer Functions.

►The 32-bit TIMER Peripheral Library consists of simple, code efficient macros and functions supporting the configuration and use of this peripheral.

Page 84: PIC32 Training

11/12/53 84

Timer Functions.Timer Functions.

►►CPU Core Timer Functions and MacrosCPU Core Timer Functions and MacrosOpenCoreTimerOpenCoreTimerUpdateCoreTimerUpdateCoreTimermConfigIntCoreTimermConfigIntCoreTimermEnableIntCoreTimermEnableIntCoreTimermDisableIntCoreTimermDisableIntCoreTimermSetPriorityIntCoreTimermSetPriorityIntCoreTimerReadCoreTimerReadCoreTimerWriteCoreTimerWriteCoreTimer

Page 85: PIC32 Training

11/12/53 85

Timer Functions.Timer Functions.

►►General Purpose Timers Functions and General Purpose Timers Functions and MacrosMacros

OpenTImerOpenTImer, , CloseTimerCloseTimerConfigIntTimerConfigIntTimerSetPriorityIntTSetPriorityIntTDisableIntTDisableIntT, , EnableIntTEnableIntTReadTimerReadTimer, , WriteTimerWriteTimerReadPeriodReadPeriod, , WritePeriodWritePeriod

Page 86: PIC32 Training

11/12/53 86

Timer/Counter ExamplesTimer/Counter Examples..

►►ExamplesExamplesLab 6 : TimerLab 6 : TimerLab 7 : CounterLab 7 : Counter

►►Test.. Test.. CounterCounter

TimerTimer

Page 87: PIC32 Training

11/12/53 87

UART.UART.

►► The primary features of the UART module The primary features of the UART module are:are:

FullFull--duplex, 8duplex, 8--bit or 9bit or 9--bit data transmissionbit data transmissionEven, odd or no parity options (for 8Even, odd or no parity options (for 8--bit data)bit data)One or two Stop bitsOne or two Stop bitsHardware autoHardware auto--baud feature and flow control optionbaud feature and flow control optionFully integrated Baud Rate Generator with 16Fully integrated Baud Rate Generator with 16--bit bit prescalerprescalerBaud rates ranging from 76 bps to 20 Mbps at 80 MHzBaud rates ranging from 76 bps to 20 Mbps at 80 MHzParity, framing and buffer overrun error detectionParity, framing and buffer overrun error detectionSupport for interrupt only on address detect (9th bit = 1)Support for interrupt only on address detect (9th bit = 1)Separate transmit and receive interruptsSeparate transmit and receive interrupts

Page 88: PIC32 Training

11/12/53 88

UART Baud Rate Generator.UART Baud Rate Generator.

►►UART Baud Rate with BRGH = 0UART Baud Rate with BRGH = 0

Page 89: PIC32 Training

11/12/53 89

UART Baud Rate Generator.UART Baud Rate Generator.

►►UART Baud Rate with BRGH = 1UART Baud Rate with BRGH = 1

Page 90: PIC32 Training

11/12/53 90

Calculation of the baud rate.Calculation of the baud rate.

►► calculation of the baud rate error for the calculation of the baud rate error for the following conditions:following conditions:

FPB = 4 MHz, Desired Baud Rate = 9600FPB = 4 MHz, Desired Baud Rate = 9600

Page 91: PIC32 Training

11/12/53 91

UART Function.UART Function.►► UART FunctionsUART Functions

BusyUART1, BusyUART2BusyUART1, BusyUART2CloseUART1, CloseUART2CloseUART1, CloseUART2ConfigIntUART1 , ConfigIntUART2ConfigIntUART1 , ConfigIntUART2DataRdyUART1 ,DataRdyUART2DataRdyUART1 ,DataRdyUART2OpenUART1, OpenUART2OpenUART1, OpenUART2ReadUART1, ReadUART2ReadUART1, ReadUART2WriteUART1, WriteUART2WriteUART1, WriteUART2getsUART1, getsUART2getsUART1, getsUART2putsUART1, putsUART2putsUART1, putsUART2getcUART1, getcUART2getcUART1, getcUART2putcUART1, putcUART2putcUART1, putcUART2UART1GetErrors, UART2GetErrorsUART1GetErrors, UART2GetErrorsUART1ClearErrors, UART2ClearErrorsUART1ClearErrors, UART2ClearErrors

Page 92: PIC32 Training

11/12/53 92

UART Function.UART Function.

►►UART MacrosUART MacrosEnableIntU1RX, EnableIntU2RXEnableIntU1RX, EnableIntU2RXEnableIntU1TX, EnableIntU2TXEnableIntU1TX, EnableIntU2TXDisableIntU1RX, DisableIntU2RXDisableIntU1RX, DisableIntU2RXDisableIntU1TX, DisableIntU2TXDisableIntU1TX, DisableIntU2TXSetPriorityIntU1, SetPriorityIntU2SetPriorityIntU1, SetPriorityIntU2SetSubPriorityIntU1, SetSubPriorityIntU2SetSubPriorityIntU1, SetSubPriorityIntU2UART1SendBreak, UART2SendBreakUART1SendBreak, UART2SendBreakUART1EnableAutoAddr, UART2EnableAutoAddrUART1EnableAutoAddr, UART2EnableAutoAddr

Page 93: PIC32 Training

11/12/53 93

UART ExamplesUART Examples..

►►ExamplesExamplesLab 8 : PollingLab 8 : PollingLab 9 : InterruptsLab 9 : Interrupts

►►Test.. Test..

Page 94: PIC32 Training

11/12/53 94

PIC32 Robot Application.PIC32 Robot Application.

►►Main Controller Main Controller PIC32 Start kitPIC32 Start kit

►ActuatorServo MotorServo Motor►►Continuous Rotation ServoContinuous Rotation Servo

►►SensorSensorGP2D120 GP2D120 ►►Distance measuring range: Distance measuring range: ►►4 to 30 cm.4 to 30 cm.

Page 95: PIC32 Training

11/12/53 95

Reference.Reference.►► PDF FilesPDF Files

MPLAB IDEMPLAB IDEMPLAB C32 C Compiler UserMPLAB C32 C Compiler User’’s Guides GuidePIC32MX3XXPIC32MX3XX//4XX Family Data Sheet4XX Family Data Sheet3232--Bit Language Tools LibrariesBit Language Tools LibrariesPIC3232 Peripheral Libraries for MPLAB C3232 CompilerPIC32 START KIT

► CHM fileshlpPIC32MXConfigSet.chmMicrochip-PIC32MX-Peripheral-Library.chm

► Webwww.Microchip.com

► Development BoardET-PIC16/32 Start Kit

► Development ToolsMPLAB IDEMPLAB C32

Page 96: PIC32 Training

11/12/53 96

Thank you.Thank you.Questions?Questions?

PrajinPrajin palangsantikulpalangsantikulAPPSOFTTECH CO.,LTD.APPSOFTTECH CO.,LTD.