Académique Documents
Professionnel Documents
Culture Documents
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
PORT
Agenda..
Interrupt
Timer/Counter
UART
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
PIC/dsPIC/PIC32
11/12/53
11/12/53
PIC32MX460F512L
Operating Frequency
DC 80 MHz
I/O Ports
Ports A, B, C, D, E, F, G
DMA Channels
4 + 2 USB 9
11/12/53
PIC32MX460F512L
Timers:
Total number (16-bit) = 5 32-bit (from paired 16-bit timers) = 2 32-bit Core Timer = 1
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
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
100-pin TQFP
11/12/53 13
11/12/53
14
11/12/53
15
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
11/12/53
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
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
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
11/12/53
21
11/12/53
22
11/12/53
23
11/12/53
24
11/12/53
25
11/12/53
26
Standard ANSI C library functions are contained in the libraries libc.a and libgcc.a.
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
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
Functions
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
Board
11/12/53
30
naming conventions
11/12/53
31
Representation
11/12/53
32
Representation
11/12/53
33
File
11/12/53
34
Files
11/12/53
35
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
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>
11/12/53
37
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
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
11/12/53
41
11/12/53
42
Primary
43
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
11/12/53
45
#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
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
Code Example:
11/12/53
49
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
11/12/53
53
I/O PORT.
The
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
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
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
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
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
11/12/53
63
11/12/53
64
11/12/53
66
11/12/53
67
OUTPUT
INPUT
11/12/53
68
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
11/12/53
70
11/12/53
71
11/12/53
72
Interrupt.
Interrupt
ISR
11/12/53
73
Interrupt.
Internal
Interrupt
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
11/12/53
76
Interrupt Examples.
Examples
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
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
11/12/53
80
11/12/53
81
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
Timer Functions.
General
Macros OpenTImer, CloseTimer ConfigIntTimer SetPriorityIntT DisableIntT, EnableIntT ReadTimer, WriteTimer ReadPeriod, WritePeriod
11/12/53
85
Timer/Counter Examples.
Examples
Timer
Counter
11/12/53
86
UART.
The
are:
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
11/12/53
88
11/12/53
89
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
11/12/53
93
Controller
Servo Motor
Continuous
Rotation Servo
Sensor
GP2D120
Distance
Reference.
PDF Files
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
www.Microchip.com
11/12/53
95