Вы находитесь на странице: 1из 96

Prajin Palangsantikul

APPSOFTTECH CO.,LTD
11/12/53 1

Agenda..
Whats

a Microcontroller? Microcontroller PIC/dsPIC/PIC32 PIC32 Block diagram PIC32MX460F512L PIC32 Start kit Install : MPLAB, MPLAB C32
11/12/53 2

Agenda..
32-Bit

Language Tools Libraries How to PIC32 Programming Oscillator


Lab 1 : Osc Basic
I/O

PORT

Lab 2 : Blink an LED Lab 3 : Port Basic


11/12/53

Agenda..
Interrupt

Lab 4 : CN Interrupt Lab 5 : External Interrupt Lab 6 : Timer Lab 7 : Counter

Timer/Counter

UART

PIC32 Robot Question


11/12/53

LAB 8 : UART Polling LAB 9 : UART Interrupt

Application.
4

Whats a Microcontroller?
A

microcontroller
(sometimes abbreviated C, uC or MCU) is a small computer on a single integrated circuit containing a processor core, memory, and programmable input/output peripherals

11/12/53

Microcontroller.
AVR ARM7 Basic

Stamp MCS-51 MSP-430 PIC/dsPIC/PIC32 PSoC STM32


11/12/53 6

PIC/dsPIC/PIC32

11/12/53

PIC32 Block diagram.

11/12/53

PIC32MX460F512L

Operating Frequency
DC 80 MHz

Program Memory (Bytes)


512K

Interrupt Sources / Vectors


95 / 63

I/O Ports
Ports A, B, C, D, E, F, G

Total I/O Pins


83

DMA Channels
4 + 2 USB 9

11/12/53

PIC32MX460F512L

Timers:

Input Capture Channels


5 5

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

Output Compare/PWM Channels Input Change Interrupt Notification Serial Communications:


Enhanced UART = 2 SPI (3-wire/4-wire) = 2 I2C = 2

22

11/12/53

10

PIC32MX460F512L
Parallel JTAG

Communications (PMP/PSP)

Yes, 8-bit/16-bit

Boundary Scan Debug and Program 2-wire Debug and Program Trace
11

Yes
JTAG

Yes
ICSP

Yes
Instruction

Yes
11/12/53

PIC32MX460F512L
Hardware 10-bit

Break Points:

6 Instruction, 2 Data

Analog-to-Digital Module (input channels)


16

Analog

Comparators LDO
12

2
Internal

Yes
11/12/53

PIC32MX460F512L
Resets

(and Delays)

POR, BOR, /MCLR, WDT, SWR (Software Reset), CM (Configuration Bit Mismatch) (PWRT, OST, PLL Lock)
Instruction

Support

MIPS32 Enhanced Architecture (Release 2) MIPS16e Code Compression


Packages

100-pin TQFP
11/12/53 13

PIC32 Start Kit.

11/12/53

14

PIC32 Start Kit.

11/12/53

15

PIC32 Start Kit.


MCU 16Bit 32Bit Microchips 100Pin MCU PIC24F/PIC24H/dsPIC33 PIC32 MCU PIC24FJ128GB110, PIC24HJ256GP210, dsPIC33FJ256MC710, PIC32MX360F512L PIC32MX460F512L XTAL 8.00 MHz System Clock Source XTAL 32.768 KHz RTCC Clock Source
11/12/53 16

PIC32 Start Kit.


Ethernet LAN Driver (ENC28J60) RJ45 Ethernet LAN Port 1 SPI EEPROM (25LC256) Web Page Code Ethernet LAN Application Note Microchips TCP/IP Stack SD Card Interface Card Detect Write Protect 1
17

11/12/53

PIC32 Start Kit.


UART Driver RS232 CPA-4 2 USB Interface USB Device/OTG Host PIC32MX460F512 PIC24FJ128GB110 14Pin Header (Character Dot-Matrix LCD Interface) 1 LED Indicator Output Logic 4
11/12/53 18

PIC32 Start Kit.


SW Push Button Input Logic 4 Volume (VR) 0-3.3V ADC 1 ICD2 Port RJ11 ICD2 Microchips InCircuit Program Debug Program/Debug LED
11/12/53 19

PIC32 Start Kit.


JTAG Port IDE Header 14 Pin Program/Debug JTAG Tools Microchips DEBUG Port(Debug Trace Port) IDE Header 10 Pin Debug Debug Tools(MPLAB REAL ICE In-Circuit Emulator) Microchips Regulate 3.3V/3A Switching Regulate LED +5V Type-B
11/12/53 20

PIC32 Start Kit.

I/O Port PIN

11/12/53

21

PIC32 Start Kit.


LED

11/12/53

22

PIC32 Start Kit.


Push Button Switch

11/12/53

23

PIC32 Start Kit.


RS232

11/12/53

24

PIC32 Start Kit.

11/12/53

25

MPLAB, MPLAB C32


Install/Setup

MPLAB IDE MPLAB C32


Demo

11/12/53

26

32-Bit Language Tools Libraries.


Standard PIC32

Standard ANSI C library functions are contained in the libraries libc.a and libgcc.a.

C Libraries (with Math Functions)

PIC32

functions and macros contained in the 32-bit peripheral libraries (use peripheral libraries need to include <plib.h> file in their source file.) Vector Math Functions Filters Transforms Video
27

Peripheral Libraries

DSP Library

11/12/53

PIC32 Peripheral Libraries.


System

The PIC32MX system library consists of functions and macros to perform system level operations.

Functions

Oscillator

The 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. The 32-bit I/O PORT Peripheral Library consists of simple, code efficient macros and functions supporting the configuration and use of this peripheral.
28

Functions

I/O

PORT Functions

11/12/53

PIC32 Peripheral Libraries.


Interrupt

Functions

Functions for Peripheral Interrupt and Software Interrupt.


Timer

Functions

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

Functions

Functions for UART module and an example of use of the functions. Functions may be implemented as macros.
11/12/53 29

How to PIC32 Programming.


Development MPLAB

Board

IDE PIC32 LIBRARY C Language for PIC32 Datasheet Go.

11/12/53

30

C Language for PIC32


File

naming conventions

11/12/53

31

C Language for PIC32


Integer

Representation

11/12/53

32

C Language for PIC32


Floating-Point

Representation

11/12/53

33

C Language for PIC32


Single

File

11/12/53

34

C Language for PIC32


Multiple

Files

11/12/53

35

C Language for PIC32


Interrupt

Handler Function

// configure the Timer 1 interrupt handler void __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void) { // clear the interrupt flag mT1ClearIntFlag(); // .. in this case, toggle the LED mPORTDToggleBits(BIT_0); } Ref.. (sys/attribs.h) (proc\p32mx460f512l.h) 11/12/53 36

C Language for PIC32


Low

Level Processor Control

Include either the generic processor header file (i.e., p32xxxx.h) or the processor-specific header file for the appropriate device (e.g., proc/p32mx360f512l.h).
#include Example,

<p32xxxx.h>

Access SFRs like any other C variables.


the following statement clears all the bits to zero in the special function register for Timer 1:
TMR1 = 0;
The

next statement enables the Watchdog Timer:


WDTCONbits.ON = 1;

11/12/53

37

C Language for PIC32

Configuration Bit Access #pragma config


The

The

example does the following: Enables the Watchdog Timer, Sets the Watchdog Postscaler to 1:128, and Selects the HS Oscillator for the Primary Oscillator

#pragma config directive specifies the processor-specific configuration settings (i.e., configuration bits) to be used by the application.

#pragma config FWDTEN = ON, WDTPS = PS128 #pragma config POSCMOD = HS void main (void) { ... } 11/12/53 38

C 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 Clock SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); while (1) ; }

11/12/53

39

Oscillator.

11/12/53

40

Oscillator.
Primary

Oscillator Operating Modes

11/12/53

41

Primary Oscillator (POSC) Configuration.

11/12/53

42

Configuration Bits register.


COSC<2:0>

Current Oscillator Selection bits


FPLLIDIV<2:0>

Default PLL Input Divider Value bits


PLLMULT<2:0>

PLL Multiplier bits


PLLODIV<2:0>

Output Divider for PLL


HSPLL

Primary
43

Oscillator with PLL module


11/12/53

System Functions.
SYSTEMConfig()

function

Include: plib.h Prototype: void SYSTEMConfig(unsigned int sys_clock, unsigned int flags); Code Example:
SYSTEMConfig(80000000, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); // Does no configure PBDIV.

11/12/53

44

Configuration Settings "PIC32MX460F512L"

11/12/53

45

Setting Configuration Bits in Code.

// Configuration Bit settings


// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV) // PBCLK = 40 MHz // Primary Osc w/PLL (XT+,HS+,EC+PLL) // WDT OFF // Other options are don't care

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_8
11/12/53 46

SYSCLK:80Mhz Calculation.

SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV) FPLLIDIV = 2, FPLLMUL = 20, FPLLODIV = 1 SYSCLK = 8MHz Crystal/2 = 4MHz = 4MHz x 20 = 80MHz/1 = 80MHz // Fin:4MHz-5MHz

11/12/53

47

Oscillator Functions.
OSCConfig()

function

Include: plib.h Prototype:

void OSCConfig(unsigned long int config1, unsigned long int config2, unsigned long int config3, unsigned long int config4); OscConfig(OSC_POSC_PLL, OSC_PLL_MULT_15, OSC_PLL_POST_1, 0);

Code Example:

11/12/53

48

Oscillator Functions.
mOSCSetPBDIV()

function

Include: plib.h Prototype:

mOSCSetPBDIV(unsigned int config); mOscSetPBDIV(OSC_PB_DIV_8);

Code Example:

11/12/53

49

Config Oscillator Examples.


Examples Test..

Oscillator

Lab 1 : Osc Basic Change 80MHz to 40MHz Oscillator

11/12/53

50

SYSCLK:40Mhz Calculation.

SYSCLK = 40 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV) FPLLIDIV = 2, FPLLMUL = 20, FPLLODIV = 2 SYSCLK = 8MHz Crystal/2 = 4MHz = 4MHz x 20 = 80MHz/2 = 40MHz // Fin:4MHz-5MHz

11/12/53

51

I/O PORT.

11/12/53

52

Pin Diagram (100-Pin USB).

11/12/53

53

I/O PORT.
The

key features of this module:

Individual output pin open-drain enable/disable Individual input pin pull-up enable/disable Monitor select inputs and generate interrupt on mismatch condition Operate during CPU SLEEP and IDLE modes Fast bit manipulation using CLR, SET and INV registers
11/12/53 54

Block diagram of a typical port structure

11/12/53

55

I/O PORT.
The

I/O Ports module consists of the following Special Function Registers (SFRs):
TRISx: Data Direction register for the module x PORTx: PORT register for the module x LATx: Latch register for the module x ODCx: Open-Drain Control register for the module x CNCON: Interrupt-on-Change Control register CNEN: Input Change Notification Interrupt Enable register CNPUE: Input Change Notification Pull-up Enable register

11/12/53

56

I/O PORT.
The

I/O Ports module also has the following associated bits for interrupt control:
Interrupt Enable Control bits for CN events (CNIE) in INT register IEC1: Interrupt Enable Control Register 1. Interrupt Flag Status bits for CN events (CNIF) in INT register IFS1: Interrupt Flag Status Register 1. Interrupt Priority Control bits (CNIP<2:0>) in INT register IPC6: Interrupt Priority Control Register 6.

11/12/53

57

I/O PORT.
CONTROL

REGISTERS

TRIS (tri-state) Registers configure the data direction flow through port I/O pin(s). PORT Registers
PORT LAT

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

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

11/12/53

Control Registers.
TRIS (tri-state) Registers TRIS registers configure the data direction flow through port I/O pin(s). The TRIS register bits determine whether a PORT I/O pin is an input or an output.
A TRIS bit set = 1 configures the corresponding I/O port pin as an input. A TRIS bit set = 0 configures the corresponding I/O port pin as an output. A read from a TRIS register reads the last value written to the TRIS register. All I/O port pins are defined as inputs after a Power-on Reset. 11/12/53 59

Control Registers.
PORT

Registers

PORT 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.
11/12/53 60

Control Registers.
LAT

Registers LAT 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.

11/12/53

61

Modes of Operation.
Digital

Inputs Inputs Outputs

Setting the corresponding bit in the AD1PCFG register = 1 enables the pin as a digital pin.
Analog

Setting the corresponding bits in the AD1PCFG register = 0 enables the pin as an analog input pin.
Digital

setting the corresponding TRIS register bits = 0. When configured as digital outputs, these pins are CMOS drivers or can be configured as open-drain outputs by setting the corresponding bits in the ODC register.
11/12/53 62

I/O Port Code Example.

11/12/53

63

I/O PORT Functions & Macros.

Port Pin Digital Functions and Macros


PORTSetPinsDigitalIn PORTSetPinsDigitalOut mPORTxSetPinsDigitalIn mPORTxSetPinsDigitalOut

Port Pin Direction Functions and Macros


mPORTxDirection mPORTxGetDirection mPORTxReadDirection mPORTxReadDirectionBits mPORTxCloseBits mPORTxCloseAll PORTResetPins

11/12/53

64

Port Pin Digital Examples


PORTSetPinsDigitalIn

Code Example: PORTSetPinsDigitalIn(IOPORT_C, BIT_1 | BIT_0);


mPORTxSetPinsDigitalIn

Code Example: mPORTCSetPinsDigitalIn(BIT_1 | BIT_0);


11/12/53 65

I/O PORT Functions & Macros.

Port Input Functions and Macros


mPORTxRead mPORTxReadBits mPORTxReadLatch mPORTxReadLatchBits PORTRead PORTReadBits mPORTxWrite mPORTxSetBits mPORTxClearBits mPORTxToggleBits PORTWrite PORTSetBits PORTClearBits PORTToggleBits

Port Output Functions and Macros

11/12/53

66

Port Input Examples


PORTRead

Code Example: value = PORTRead(IOPORT_C);


mPORTxRead

Code Example: value = mPORTCRead();

11/12/53

67

I/O Port Examples.


Examples

Lab 2 : Blink an LED Lab 3 : Port Basic


Test..

OUTPUT

INPUT

11/12/53

68

Change Notification Pins.


The

Change Notification (CN) pins provide PIC32MX devices the ability to generate interrupt requests to the processor in response to a change of state on selected input pins (corresponding TRISx bits must = 1). Note: Pull up on CN pins should always be disabled whenever the port pin is configured as a digital output.

11/12/53

69

Input Change Notification Block Diagram.

11/12/53

70

Change Notice Configuration Example.

11/12/53

71

Change Notice ISR Code Example.

11/12/53

72

Interrupt.

Interrupt

ISR

11/12/53

73

Interrupt.
Internal

Interrupt

Timer/Counter Serial A/D Etc.


External

Interrupt

Int CN
11/12/53 74

External Interrupts.
The

interrupt controller supports five external interrupt-request signals (INT4-INT0). These inputs are edge sensitive, they require a low-to-high or a high-to-low transition to create an interrupt request. The INTCON register has five bits that select the polarity of the edge detection circuitry:
INT4EP INT3EP INT2EP INT1EP INT0EP (INTCON<4>) (INTCON<3>) (INTCON<2>) (INTCON<1>) (INTCON<0>).

11/12/53

75

Setting External Interrupt Polarity.

11/12/53

76

Interrupt Examples.
Examples

Lab 4 : CN Interrupt Lab 5 : INT Interrupt


Test..

Output

Interrupt

11/12/53

77

Timer/Counter.
All

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

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

All

Timer modules includes the following common features:


16-bit timer/counter Software-selectable internal or external clock source Programmable interrupt generation and priority Gated external pulse counter

11/12/53

78

Timer/Counter.
Beyond

the common features, each timer type offers these additional features:
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:
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 11/12/53 79

Type A Timer Block Diagram.

11/12/53

80

Type B Timer Block Diagram (16-Bit).

11/12/53

81

Type B Timer Block Diagram (32-Bit).

11/12/53

82

Timer Functions.
The

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

11/12/53

83

Timer Functions.
CPU

Core Timer Functions and Macros

OpenCoreTimer UpdateCoreTimer mConfigIntCoreTimer mEnableIntCoreTimer mDisableIntCoreTimer mSetPriorityIntCoreTimer ReadCoreTimer WriteCoreTimer


11/12/53 84

Timer Functions.
General

Macros OpenTImer, CloseTimer ConfigIntTimer SetPriorityIntT DisableIntT, EnableIntT ReadTimer, WriteTimer ReadPeriod, WritePeriod

Purpose Timers Functions and

11/12/53

85

Timer/Counter Examples.
Examples

Lab 6 : Timer Lab 7 : Counter


Test..

Timer

Counter

11/12/53

86

UART.
The

are:

primary features of the UART module

Full-duplex, 8-bit or 9-bit data transmission Even, odd or no parity options (for 8-bit data) One or two Stop bits Hardware auto-baud feature and flow control option Fully integrated Baud Rate Generator with 16-bit prescaler Baud rates ranging from 76 bps to 20 Mbps at 80 MHz Parity, framing and buffer overrun error detection Support for interrupt only on address detect (9th bit = 1) Separate transmit and receive interrupts
11/12/53 87

UART Baud Rate Generator.


UART

Baud Rate with BRGH = 0

11/12/53

88

UART Baud Rate Generator.


UART

Baud Rate with BRGH = 1

11/12/53

89

Calculation of the baud rate.


calculation

of the baud rate error for the following conditions:


FPB = 4 MHz, Desired Baud Rate = 9600

11/12/53

90

UART Function.

UART Functions

BusyUART1, BusyUART2 CloseUART1, CloseUART2 ConfigIntUART1 , ConfigIntUART2 DataRdyUART1 ,DataRdyUART2 OpenUART1, OpenUART2 ReadUART1, ReadUART2 WriteUART1, WriteUART2 getsUART1, getsUART2 putsUART1, putsUART2 getcUART1, getcUART2 putcUART1, putcUART2 UART1GetErrors, UART2GetErrors UART1ClearErrors, UART2ClearErrors 91

11/12/53

UART Function.
UART

Macros

EnableIntU1RX, EnableIntU2RX EnableIntU1TX, EnableIntU2TX DisableIntU1RX, DisableIntU2RX DisableIntU1TX, DisableIntU2TX SetPriorityIntU1, SetPriorityIntU2 SetSubPriorityIntU1, SetSubPriorityIntU2 UART1SendBreak, UART2SendBreak UART1EnableAutoAddr, UART2EnableAutoAddr
11/12/53 92

UART Examples.
Examples

Lab 8 : Polling Lab 9 : Interrupts


Test..

11/12/53

93

PIC32 Robot Application.


Main

Controller

PIC32 Start kit


Actuator

Servo Motor
Continuous

Rotation Servo

Sensor

GP2D120
Distance

measuring range: 4 to 30 cm.


11/12/53 94

Reference.

PDF Files

CHM files Web

MPLAB IDE MPLAB C32 C Compiler Users Guide PIC32MX3XX/4XX Family Data Sheet 32-Bit Language Tools Libraries PIC32 Peripheral Libraries for MPLAB C32 Compiler PIC32 START KIT hlpPIC32MXConfigSet.chm Microchip-PIC32MX-Peripheral-Library.chm

Development Board Development Tools


MPLAB IDE MPLAB C32

www.Microchip.com

ET-PIC16/32 Start Kit

11/12/53

95

Thank you. Questions?


Prajin palangsantikul APPSOFTTECH CO.,LTD. 11/12/53 96

Вам также может понравиться