pic24hj256gp210 course 2015-09-23

67
PIC24HJ256GP210 Prajin Palangsantikul presentation v0.01 Date 2015/09/23

Upload: prajin-palangsantikul

Post on 10-Dec-2015

3 views

Category:

Documents


0 download

DESCRIPTION

สไลต์คอร์สอบรม PIC24H จากบริษัทแอพซอฟต์เทคจำกัด

TRANSCRIPT

Page 1: PIC24HJ256GP210 course 2015-09-23

PIC24HJ256GP210

Prajin Palangsantikulpresentation v0.01

Date 2015/09/23

Page 2: PIC24HJ256GP210 course 2015-09-23

PIC24HJ256GP210

• ไมม่ี USB

9/23/2015 2Prajin Palangsantikul (www.appsofttech.com)

Page 3: PIC24HJ256GP210 course 2015-09-23

PIC24 Programming

• Include Head file (.h)

– #include <p24Hxxxx.h>

• Configuration Bits• Configuration Bits

– C:\Program Files(x86)\Microchip\xc16\v1.24\docs\ config_docs\24HJ256GP210.htm

• Configure Oscillator Clock

– oscConfig()

9/23/2015 3Prajin Palangsantikul (www.appsofttech.com)

Page 4: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: What?

9/23/2015 4Prajin Palangsantikul (www.appsofttech.com)

Page 5: PIC24HJ256GP210 course 2015-09-23

9/23/2015 5Prajin Palangsantikul (www.appsofttech.com)

Page 6: PIC24HJ256GP210 course 2015-09-23

9/23/2015 6Prajin Palangsantikul (www.appsofttech.com)

Page 7: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: What?

• Parallel I/O (PIO) Ports

– The voltage on a digital input pin can be between -

0.3V to 5.6V

• Open-Drain Configuration• Open-Drain Configuration

– This is controlled by the Open-Drain Control

register, ODCx, associated with each port.

– The open-drain feature allows the generation of

outputs higher than VDD(e.g., 5V) on any desired

digital only pins by using external pull-up resistors

9/23/2015 7Prajin Palangsantikul (www.appsofttech.com)

Page 8: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: What?

• Configuring Analog Port Pins

– The use of the ADxPCFGH, ADxPCFGL and TRIS registers control the operation of the Analog-to-Digital port pins.

• I/O Port Write/Read Timing• I/O Port Write/Read Timing

– One instruction cycle is required between a port direction change or port write operation and a read operation of the same port. Typically, this instruction would be a NOP.

• Input Change Notification

9/23/2015 8Prajin Palangsantikul (www.appsofttech.com)

Page 9: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: What?

• Input Change Notification

– Depending on the device pin count, there are up

to 24 external signals (CN0 through CN23) that

can be selected (enabled) for generating an can be selected (enabled) for generating an

interrupt request on a change-of-state.

– There are four control registers associated with

the CN module.

• The CNEN1 and CNEN2

• The CNPU1 and CNPU2

9/23/2015 9Prajin Palangsantikul (www.appsofttech.com)

Page 10: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: Control Register

• TRISx: Data Direction register

• PORTx: I/O Port register

• LATx: I/O Latch register

• ODCx: Open-Drain control register• ODCx: Open-Drain control register

– The open-drain feature allows the generation of

outputs higher than VDD(e.g., 5V) on any desired

digital only pins by using external pull-up resistors.

9/23/2015 10Prajin Palangsantikul (www.appsofttech.com)

Page 11: PIC24HJ256GP210 course 2015-09-23

9/23/2015 11Prajin Palangsantikul (www.appsofttech.com)

Page 12: PIC24HJ256GP210 course 2015-09-23

9/23/2015 12Prajin Palangsantikul (www.appsofttech.com)

Page 13: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: The Change Notification (CN) pins

9/23/2015 13Prajin Palangsantikul (www.appsofttech.com)

Page 14: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: CN Configuration and Operation

1. Set TRISx, digital input

2. Set CEN1/CEN2, Enable Interrupt

3. Set CNPU1/CNPU2, On the weak pull-up

4. Clear CNIF (IFSx register), Clear interrupt flag4. Clear CNIF (IFSx register), Clear interrupt flag

5. Select CNIP<2:0> (IPCx register), Interrupt

priority

6. Set CNIE (IECx register), Enable Interrupt

9/23/2015 14Prajin Palangsantikul (www.appsofttech.com)

Page 15: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: Peripheral Libraries

• C:\Program Files (x86)\Microchip\xc16\v1.24\

docs\periph_libs\16-bit Peripheral

Libraries.htm

– เลือก PIC24H– เลือก PIC24H

9/23/2015 15Prajin Palangsantikul (www.appsofttech.com)

Page 16: PIC24HJ256GP210 course 2015-09-23

1.I/O Ports: Examples

• Input/Output

– LAB_02_Blink.C Output

– LAB_02_SW.C Input

• Input Change Notification (Interrupt)• Input Change Notification (Interrupt)

– LAB_04_CN5_6.C CNx

• External Interrupt

– LAB_04_INT3.C INTx

9/23/2015 16Prajin Palangsantikul (www.appsofttech.com)

Page 17: PIC24HJ256GP210 course 2015-09-23

LAB_04_CN5_6

9/23/2015

LAB_04_CN5_6

(RB3/CN5, RB4/CN6)

17Prajin Palangsantikul (www.appsofttech.com)

Page 18: PIC24HJ256GP210 course 2015-09-23

LAB_04_INT3

9/23/2015

LAB_04_INT3

(RA14/INT3)

18Prajin Palangsantikul (www.appsofttech.com)

Page 19: PIC24HJ256GP210 course 2015-09-23

2.Timers

• Type A timer

– Timer1

• Type B timer

– Timer2, Timer4, Timer6 and Timer8– Timer2, Timer4, Timer6 and Timer8

• Type C timer

– Timer3, Timer5, Timer7 and Timer9

9/23/2015 19Prajin Palangsantikul (www.appsofttech.com)

Page 20: PIC24HJ256GP210 course 2015-09-23

2.Timers: Control Register

• TMRx

– 16-bit Timer Count register

• PRx

– 16-bit Timer Period register associated with the – 16-bit Timer Period register associated with the

timer

• TxCON

– 16-bit Timer Control register associated with the

timer

9/23/2015 20Prajin Palangsantikul (www.appsofttech.com)

Page 21: PIC24HJ256GP210 course 2015-09-23

2.Timers: Interrupt Control Register

• Interrupt Enable Control bit (TxIE)

• Interrupt Flag Status bit (TxIF)

• Interrupt Priority Control bits (TxIP<2:0>)

9/23/2015 21Prajin Palangsantikul (www.appsofttech.com)

Page 22: PIC24HJ256GP210 course 2015-09-23

2.Timers: Type A Timer

9/23/2015 22Prajin Palangsantikul (www.appsofttech.com)

Page 23: PIC24HJ256GP210 course 2015-09-23

2.Timers: Type B Timer

9/23/2015 23Prajin Palangsantikul (www.appsofttech.com)

Page 24: PIC24HJ256GP210 course 2015-09-23

2.Timers: Type C Timer

9/23/2015 24Prajin Palangsantikul (www.appsofttech.com)

Page 25: PIC24HJ256GP210 course 2015-09-23

2.Timers: Timer1 (Type A Timer)

• Timer1 operate in three modes

– 16-bit Timer

– 16-bit Synchronous Counter

– 16-bit Asynchronous Counter– 16-bit Asynchronous Counter

9/23/2015 25Prajin Palangsantikul (www.appsofttech.com)

Page 26: PIC24HJ256GP210 course 2015-09-23

2.Timers: Type A Timer Control Register

• T1CON register

9/23/2015 26Prajin Palangsantikul (www.appsofttech.com)

Page 27: PIC24HJ256GP210 course 2015-09-23

2.Timers: Tick

• T = 1/f

= 1/(FOSC/2)/prescale value

= 1/(40MHz/2)/256

= 0.013 ms= 0.013 ms

• 1ms?

0.013ms = 1 Tick

1ms = 1/0.013

= 76

9/23/2015 27Prajin Palangsantikul (www.appsofttech.com)

Page 28: PIC24HJ256GP210 course 2015-09-23

2.Timers: To configure Timer1 for operation

1. Set the TON bit (= 1) in the T1CON register.

2. Select the timer prescaler ratio using the TCKPS<1:0> bits in

the T1CON register.

3. Set the Clock and Gating modes using the TCS and TGATE

bits in the T1CON register.bits in the T1CON register.

4. Set or clear the TSYNC bit in T1CON to select synchronous or

asynchronous operation.

5. Load the timer period value into the PR1 register.

6. If interrupts are required, set the interrupt enable bit, T1IE.

Use the priority bits, T1IP<2:0>, to set the interrupt priority.

9/23/2015 28Prajin Palangsantikul (www.appsofttech.com)

Page 29: PIC24HJ256GP210 course 2015-09-23

2.Timers: 16-Bit Timer1

• Timer Mode

– LAB_05_Timer.c

• Counter Mode

– LAB_05_Counter.c– LAB_05_Counter.c

9/23/2015 29Prajin Palangsantikul (www.appsofttech.com)

Page 30: PIC24HJ256GP210 course 2015-09-23

2.Timers: Type B/Type C

9/23/2015 30Prajin Palangsantikul (www.appsofttech.com)

Page 31: PIC24HJ256GP210 course 2015-09-23

9/23/2015 31Prajin Palangsantikul (www.appsofttech.com)

Page 32: PIC24HJ256GP210 course 2015-09-23

2.Timers: 32-Bit Timer Access

• the most significant word (msw)

• the least significant word (lsw)

9/23/2015 32Prajin Palangsantikul (www.appsofttech.com)

Page 33: PIC24HJ256GP210 course 2015-09-23

2.Timers: 32-Bit Timer

• Timer Mode

– LAB_05_Timer32.c

• Counter Mode

– LAB_05_Counter32.c– LAB_05_Counter32.c

9/23/2015 33Prajin Palangsantikul (www.appsofttech.com)

Page 34: PIC24HJ256GP210 course 2015-09-23

3. 10-Bit Analog-to-Digital Converter (ADC)

• The PIC24H family devices have up to 32 A/D

input channels.

• These devices also have up to two ADC

modules (ADCx, where x = 1 or 2), each with modules (ADCx, where x = 1 or 2), each with

its own set of Special Function Registers

(SFRs).

9/23/2015 34Prajin Palangsantikul (www.appsofttech.com)

Page 35: PIC24HJ256GP210 course 2015-09-23

3. 10-Bit Analog-to-Digital Converter (ADC)

• The 10-bit or 12-bit Operation Mode (AD12B)

bit in the ADCx Control 1(ADxCON1) register

allows each of the ADC modules to be

configured by the user application as either a configured by the user application as either a

10-bit, 4 Sample/Hold (S/H) ADC (default

configuration) or a 12-bit, 1 Sample/Hold ADC.

9/23/2015 35Prajin Palangsantikul (www.appsofttech.com)

Page 36: PIC24HJ256GP210 course 2015-09-23

9/23/2015 36Prajin Palangsantikul (www.appsofttech.com)

Page 37: PIC24HJ256GP210 course 2015-09-23

9/23/2015 37Prajin Palangsantikul (www.appsofttech.com)

Page 38: PIC24HJ256GP210 course 2015-09-23

9/23/2015 38Prajin Palangsantikul (www.appsofttech.com)

Page 39: PIC24HJ256GP210 course 2015-09-23

9/23/2015 39Prajin Palangsantikul (www.appsofttech.com)

Page 40: PIC24HJ256GP210 course 2015-09-23

• LAB_07_ADC_AN2.c

9/23/2015 40Prajin Palangsantikul (www.appsofttech.com)

Page 41: PIC24HJ256GP210 course 2015-09-23

4.Input Capture

9/23/2015 41Prajin Palangsantikul (www.appsofttech.com)

Page 42: PIC24HJ256GP210 course 2015-09-23

4.Input Capture: Input Capture Event Generation

9/23/2015 42Prajin Palangsantikul (www.appsofttech.com)

Page 43: PIC24HJ256GP210 course 2015-09-23

4.Input Capture: Timer Value Capture

9/23/2015 43Prajin Palangsantikul (www.appsofttech.com)

Page 44: PIC24HJ256GP210 course 2015-09-23

9/23/2015 44Prajin Palangsantikul (www.appsofttech.com)

Page 45: PIC24HJ256GP210 course 2015-09-23

4.Input Capture: Example

• Input Capture

– LAB_08_InputCapture.C (See: Figure12-5)

9/23/2015 45Prajin Palangsantikul (www.appsofttech.com)

Page 46: PIC24HJ256GP210 course 2015-09-23

5.Output Compare

9/23/2015 46Prajin Palangsantikul (www.appsofttech.com)

Page 47: PIC24HJ256GP210 course 2015-09-23

5.Output Compare: operating modes

• Active Low One-Shot mode

• Active High One-Shot mode

• Toggle mode

• Delayed One-Shot mode• Delayed One-Shot mode

• Continuous Pulse mode

• PWM mode

– without fault protection

– with fault protection

9/23/2015 47Prajin Palangsantikul (www.appsofttech.com)

Page 48: PIC24HJ256GP210 course 2015-09-23

5.Output Compare:

9/23/2015 48Prajin Palangsantikul (www.appsofttech.com)

Page 49: PIC24HJ256GP210 course 2015-09-23

5.Output Compare: Output Compare Registers

• OCxCON

– Output Compare Control register

• OCxR

– Output Compare register– Output Compare register

• OCxRS

– Secondary Output Compare register

9/23/2015 49Prajin Palangsantikul (www.appsofttech.com)

Page 50: PIC24HJ256GP210 course 2015-09-23

9/23/2015 50Prajin Palangsantikul (www.appsofttech.com)

Page 51: PIC24HJ256GP210 course 2015-09-23

9/23/2015 51Prajin Palangsantikul (www.appsofttech.com)

Page 52: PIC24HJ256GP210 course 2015-09-23

5.Output Compare: Examples

• Toggle mode (See: Figure13-5)

– LAB_09_OutputCompare_Toggle.C

• PWM mode without fault protection • PWM mode without fault protection

(See: Figure13-12)

– LAB_09_OutputCompare_PWM.C

9/23/2015 52Prajin Palangsantikul (www.appsofttech.com)

Page 53: PIC24HJ256GP210 course 2015-09-23

6. Serial Peripheral Interface (SPI)

• The SPI module is a full duplex synchronous

serial interface useful for communicating with

other peripherals or microcontrollers in

master/slave relationship and it can transfer master/slave relationship and it can transfer

data over short distances at high speeds.

9/23/2015 53Prajin Palangsantikul (www.appsofttech.com)

Page 54: PIC24HJ256GP210 course 2015-09-23

6. Serial Peripheral Interface (SPI)

• The peripheral devices may be serial

EEPROMs, shift registers, display drivers,

analog-to-digital converters, etc.

• The SPI module is compatible with Motorola’s • The SPI module is compatible with Motorola’s

SPI and SIOP interfaces.

9/23/2015 54Prajin Palangsantikul (www.appsofttech.com)

Page 55: PIC24HJ256GP210 course 2015-09-23

6. Serial Peripheral Interface (SPI)

• The SPI serial interface consists of four pins,

which are further sub-divided into data and

control lines:

– Data lines:– Data lines:

• MOSI – Master Data Output, Slave Data Input

• MISO – Master Data Input, Slave Data Output

– Control lines:

• SCLK – Serial Clock

• /SS – Slave Select (no addressing)

9/23/2015 55Prajin Palangsantikul (www.appsofttech.com)

Page 56: PIC24HJ256GP210 course 2015-09-23

6. Serial Peripheral Interface (SPI)

9/23/2015 56Prajin Palangsantikul (www.appsofttech.com)

Page 57: PIC24HJ256GP210 course 2015-09-23

LAB_10_74HC595.c

• PIC24H 74HC595

• -------------------------------

• SDO1/RF8 -> DS pin

• SCK1/RF6 -> SH_CP pin• SCK1/RF6 -> SH_CP pin

• RF5 -> ST_CP pin

9/23/2015 57Prajin Palangsantikul (www.appsofttech.com)

Page 58: PIC24HJ256GP210 course 2015-09-23

LAB_10_74HC595.c

PIC24H

SDO1/RF8

9/23/2015 58Prajin Palangsantikul (www.appsofttech.com)

SDO1/RF8

RF5

SCK1/RF6

Page 59: PIC24HJ256GP210 course 2015-09-23

7. Inter-Integrated Circuit I2C

9/23/2015 59Prajin Palangsantikul (www.appsofttech.com)

Page 60: PIC24HJ256GP210 course 2015-09-23

7. Inter-Integrated Circuit I2C

9/23/2015 60Prajin Palangsantikul (www.appsofttech.com)

Page 61: PIC24HJ256GP210 course 2015-09-23

7. Inter-Integrated Circuit I2C

• 256K I2C™ Serial EEPROM

9/23/2015 61Prajin Palangsantikul (www.appsofttech.com)

Page 62: PIC24HJ256GP210 course 2015-09-23

7. Inter-Integrated Circuit I2C

EEPROM EEPROM 2424LCxxxxLCxxxx PCFPCF85748574A I/O expanderA I/O expander

9/23/2015

PCFPCF8591 88591 8bit A/D and D/Abit A/D and D/ADSDS1307 1307 RealReal--Time ClockTime Clock

62Prajin Palangsantikul (www.appsofttech.com)

Page 63: PIC24HJ256GP210 course 2015-09-23

LAB_11_PCF8574A.c

• PIC24 PCF8574A

• --------------------------

• xx -> SDA

• xx -> SCL• xx -> SCL

• P0-P7 ตอ่ LED 8 ดวง

9/23/2015 63Prajin Palangsantikul (www.appsofttech.com)

Page 64: PIC24HJ256GP210 course 2015-09-23

LAB_11_PCF8574A.c

xx

xx

PIC24H

9/23/2015

xx

64Prajin Palangsantikul (www.appsofttech.com)

Page 65: PIC24HJ256GP210 course 2015-09-23

SD card/FAT32 (เพิ�มเติม)

9/23/2015 65Prajin Palangsantikul (www.appsofttech.com)

Page 66: PIC24HJ256GP210 course 2015-09-23

9/23/2015 66Prajin Palangsantikul (www.appsofttech.com)

Page 67: PIC24HJ256GP210 course 2015-09-23

Ref.

• www.microchip.com

• PIC24HJXXXGP-X06-X08-X10 datasheet

• ETT manPIC32-START-KIT manual

9/23/2015 Prajin Palangsantikul (www.appsofttech.com) 67