+ All Categories
Home > Documents > PIC: architecture, programming and...

PIC: architecture, programming and...

Date post: 16-Apr-2018
Category:
Upload: phamdieu
View: 229 times
Download: 4 times
Share this document with a friend
33
PIC: architecture, programming and interfacing
Transcript
Page 1: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

PIC: architecture, programming and interfacing

Page 2: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

PIC18Fxx Microcontroller Architecture

Page 3: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Harvard architecture, separate memory banks for code and data

8 Bit CPU with hardware multiplier Internal address + data + control bus Internal RAM + ROM memory Peripheral integration, peripherals such as:

Timers, digital & analog IO ports Segmented LCDs – capable of driving up to 192 segments Charge Time Measurement Unit (CTMU) – precise time measurement and capacitive touch sensing Full-speed USB v2.0 – up to 12 Mbps USART interface Peripheral Pin Select – flexible pin mapping saves board space Etc.

Page 4: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Reset Reset condition causes the microcontroller to immediately stop operation and puts the PIC in a well-defined initial state so that the processor starts executing code from the first instruction. A reset signal may be generated externally at any moment (low logic level on the MCLR pin). If needed, it can also be generated by internal control logic. Power-on always causes reset. Resets can result from:

o external reset by MCLR pulled low o reset on power-up o reset by watchdog timer overflow o reset on power supply brown-out

Reset can be used as a last resort for recovering from some catastrophic software event but all current data will be lost

Page 5: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

SIMPLIFIED BLOCK DIAGRAM OF ON-CHIP RESET CIRCUIT

Page 6: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Power-ON Reset

When the power supply voltage reaches 1.2 - 1.7V, a circuit called Power-up timer resets the microcontroller within approximately 72mS. As soon as this time expires, another timer called Oscillator start-up timer generates another reset signal within 1024 quartz oscillator periods. When this delay expires (marked as T reset in figure) and the MCLR pin is set high, all conditions are met and the microcontroller starts to execute the first instruction in the program.

Page 7: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

BLACK-OUT RESET

Black-out reset takes place when the power supply normally goes off.

The microcontroller then has no time to do anything unpredictable simply because the voltage drops very fast beneath its minimum value.

Page 8: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

BROWN-OUT RESET

When the power supply voltage drops slowly, the internal electronics gradually stops to operate and the so called Brown-out reset occurs.

Before the microcontroller completely stops the operation there is a real danger that circuits which operate at higher voltages start to perform unpredictably.

Brown-out reset is saved in on-chip flash memory.

Page 9: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

MCLR PIN

A logic zero (0) on the MCLR pin causes an immediate and regular reset. The function of additional components is to sustain 'pure' logic one (1) during normal operation. If their values are selected so as to provide high logic level on the pin after T reset is over, the microcontroller will immediately start the operation.

Page 10: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

The CPU fetches instructions from memory, decodes them, and passes them to the ALU for execution The arithmetic logic unit (ALU) is responsible for adding, subtracting, shifting and performing logical operations The ALU operates in conjunction with:

o a general-purpose register called the W register o an F register that can be any location in data memory

Central Processing Unit

Page 11: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

PIC18 Program Memory Map

Page 12: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

PIC18 Data Memory Map

Data memory contains general purpose registers (GPRs) and special function registers (SFRs)

The PIC18F452 has 1536 (0x600) locations of GPR data memory

Page 13: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Port A 0xF80

Port B 0xF81

Port C 0xF82

Port D 0xF83

Port E 0xF84

Tris A 0xF92

Tris B 0xF93

Tris C 0xF94

Tris D 0xF95

Tris E 0xF96

The memory block 0xF80 to 0xFFF (128 locations) references special function registers (SFRs).Some of the SFRs :

SPBRG 0xFAF

Timer1L 0xFCE

Timer1H 0xFCF

Timer0L 0xFD6

Timer0H 0xFD7

Wreg 0xFE8

StkPtr 0xFFC

Memory Organization – SFRs

Page 14: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

All PIC SFR registers and their bits are implicitly declared as global variables.

These identifiers have an external linkage, and are visible in the entire project.

When creating a project, the mikroC PRO for PIC will include an appropriate (*) file from defs folder, containing declarations of available SFR registers and constants.

For a complete set of predefined globals and constants, look for

“Defs” in the mikroC PRO for PIC installation folder,

SFR Access in C (mikroC PRO for PIC)

Page 15: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

PIC Microcontroller Peripherals

The 18F452 PIC has the following peripherals:

Data ports A (6-bit), B (8-bit), C (8-bit), D (8-bit), E (3-bit)

Timer/counter modules 0 (8-bit), 1 (16-bit), 2 (8-bit), 3 (16-bit)

2 CP/PWM modules (for DA implementation)

I2 C / SPI serial port

USART (RS-232, RS-485)

Analogue-to-digital converter (10-bit) with 10 way input multiplexer

EEPROM (256 byte)

Page 16: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Data Ports (Digital I/O Ports)

Microcontroller has one or more registers (called ports) connected to the microcontroller pins. input/output - pin function (input or output) can be changed according to particular need. pin function change is performed by software pin function can be changed during operation

Page 17: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

• Maximum output current per pin For most microcontrollers, current obtained from one pin is sufficient to activate an LED or some other low-current device (10-20 mA).

• Pull-up resistors configurable by software These resistors connect pins to the positive power supply voltage and come into effect when the pin is configured as an input connected to a mechanical switch or a push button.

• SFR mappedEach I/O port is under control of the specialized SFR, which means that each bit of that register determines the state of the corresponding microcontroller pin. For example, by writing logic one (1) to a bit of the control register (SFR), the appropriate port pin is automatically configured as an input and voltage brought to it can be read as logic 0 or 1. Otherwise, by writing zero to the SFR, the appropriate port pin is configured as an output. Its voltage (0V or 5V) corresponds to the state of appropriate port register bit.

Important Data Ports Specifications

Page 18: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Data Ports A single data I/O pin of one of the ports A-E:

Data I/O port functions: WR LAT- this latches the data written to the pin (configured as an output) WR TRIS - this latches the data direction for the pin (0 = output, 1 = input) RD Port- this reads the current value of the pin (configured as an input) Each data port (A-E) consists of a number of pins, each of which can individually be configured as an input or output

Page 19: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Data Ports Access in C

Data ports are controlled via SFR-mapped registers, and can be accessed as implicitly declared variables:

Examples:

PORTC=0xFF; // all bits of port A are set to 1PORTB.B5 =0; // bit 5 of PortB is reset to 0PORTD=PORTC & 0x0F; // PortD=PortC and 0b00001111PORTC=PORTC | 0b11110000; // PortC=PortC or 0b11110000

Exercise: LABxx.xx led diode toglling, …port combinations

Page 20: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Clock Generator

Pulses generated by the oscillator enable harmonic and synchronous operation of all circuits within the microcontroller. instructions are not executed at the rate imposed by the oscillator itself, but several times slower. It happens because each instruction is executed in several steps.

Page 21: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Clock Generator PICs use a fully static design so that any clock frequency up to the specified maximum can be used. There are 4 possible clock configurations.In practice the choice will normally be a compromise between cost and clock speed or clock stability.

a. external clock (e.g.. crystal oscillator module)

b. self-oscillating with external crystal or ceramic resonator

c. external or self-oscillating with phase-locked loop

d. self-oscillating with external RC

Page 22: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with
Page 23: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

f = 1/T :f = frequency [Hz];T = R * C = time constant [s];R = resistor resistance [Ω]; C = capacitor capacity [F].

Page 24: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

EasyPIC: using crystal oscillators

Page 25: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

The microcontroller oscillator uses quartz crystal for its operation The frequency of such oscillator is precisely defined and very stable, Pulses it generates are always of the same width => ideal for time measurement! Time measurement between two events = counting pulses generated by this oscillator. This is exactly what the timer performs.

Timers/Counters

Page 26: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

If the timer receives pulses from the microcontroller input pin, then it turns into a counter. It is the same electronic circuit capable to operate in two different modes. The only difference is that in this case pulses to be counted come over the microcontroller input pin and their duration (width) is mostly undefined. This is why they cannot be used for time measurement, but for other purposes such as counting products on an assembly line, number of axis rotation, passengers etc. (depending on sensor in use)

Timers/Counters

Page 27: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Timers/Counters

The PIC 18F452 has 4 timer/counters:

Timer 0: 8 or 16-bit (selectable)Timer 1: 16-bitTimer 2: 8-bitTimer 3: 16-bit

The timer/counters can be used to:

generate timed interrupts count incoming logic transitions capture timer/counter on an input event generate variable PWM outputs

Page 28: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Timer/Counter 0 operation (8 mode)

T0SE determines whether 0→1 or 1→0 transitions are active T0CS determines the source (I/O pin or internal clock)

PSA determine whether the input is pre-scaled or not

Page 29: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Timer0 control register T0CON controls the configuration for timer/counter 0:

TMR0ON Enable: off (0) or on(1) T08BIT Mode: 16-bit (0) or 8-bit (1) T0CS Time source: internal clock (0) or external (1) T0SE Edge select: 0→1 (0) or 1→0 (1) PSA Prescaler: on (0) or off (1) T0PS0-2 Prescaler ratio: 1/2 (000) .. 1/256 (111)

For example for 8-bit mode, external source, 0→1 edge, no pre-scaler: T0CON = 0b11100000 = 0xE0

Timer/Counter Control Register

Page 30: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Timer/Counter 0 operation (16 mode)

Page 31: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Pulse-Width Modulation

Pulse-width modulation (PWM) can be used to create an n-bit digital-to-analogue converter (DAC) A rectangular wave with a given mark-space ratio (duty cycle) is generated and this is applied to a 1-bit DAC DACs of this type have only a limited bandwidth because of the need to filter out the rectangular wave. Typical applications are in dc motor control, brightness control

of lights and in dc-dc converters The PIC18F452 has a PWM generator that make use of counter/timer 2

Page 32: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Pulse-Width ModulationSimplified diagram of PWM generator:

1. Disable the CCPx pin output driver by setting the associated TRIS bit.2. Select the 8-bit TimerX resource, (Timer2, Timer4 or Timer6) to be used for PWM generation by setting the CxTSEL<1:0> bits in the CCPTMRSx register.(1)3. Load the PRx register for the selected TimerX with the PWM period value.4. Configure the CCP module for the PWM mode by loading the CCPxCON register with the appropriate values.5. Load the CCPRxL register and the DCxB<1:0> bits of the CCPxCON register, with the PWM duty cycle value

Page 33: PIC: architecture, programming and interfacingtnt.etf.rs/~ir3ode/pdf/vezbe/PIC_Reset_OSC_TImers.pdf · “Defs” in the mikroC PRO for PIC installation folder, ... (10-bit) with

Pulse-Width ModulationSimplified diagram of PWM generator:

6. Configure and start the 8-bit TimerX resource:

• Clear the TMRxIF interrupt flag bit of the PIR2 or PIR4 register. • Configure the TxCKPS bits of the TxCON register with the Timer prescale value.• Enable the Timer by setting the TMRxON bit of the TxCON register.

7. Enable PWM output pin:• Wait until the Timer overflows and the TMRxIF bit of the PIR2 or PIR4 register is set. • Enable the CCPx pin output driver by clearing the associated TRIS bit.


Recommended