You are on page 1of 45

organiza ELECTRONICA ELEMON S.A.

Csar Angel Fuoco FAE Electrnica Elemon S.A.

cesarfuoco@elemon.com.ar
organiza ELECTRONICA ELEMON S.A. 2

Agenda
Introduccin a PIC18 Entorno de trabajo Lab 1 Toggle LED Conociendo el MCU Perifricos: GPIOs, ADC , COMPARADOR, TIMER Lab 2 ZCD: zero cross detection

organiza ELECTRONICA ELEMON S.A.

organiza ELECTRONICA ELEMON S.A.

Familia MCU PIC & DSC

organiza ELECTRONICA ELEMON S.A.

PIC18 Standard + Serie J y K


Traditional PIC18 PIC18 J-series PIC18 K-series 4KB 32KB Program Flash
Typically products with higher memory also have higher pin-counts and higher levels of integrated peripherals

128KB

Traditional PIC18
40 MHz, 10 MIPS, 5V Flash endurance 100k EEPROM Premium Features

PIC18 J-series
40-48 MHz, 10-12 MIPS, 3V Flash endurance 1k 10k Emulate EEPROM Most cost effective >32KB Flash

PIC18 K-series
64MHz, 16 MIPS, 3V Flash endurance 10k EEPROM Most cost effective <32KB Flash

organiza ELECTRONICA ELEMON S.A.

Arquitectura
Arquitectura de 8-bit PIC Diagrama en bloques simplificado

8-bit

Table Access

8-bit

Data RAM

Program Memory Flash

Program Bus
16-bit

Data Bus 8-bit CPU


8-bit

Peripherals

I/O Ports

PROGRAMA
organiza ELECTRONICA ELEMON S.A.

DATOS
7

Mapeo memoria de datos


Data Memory up to 4k bytes Divided into 256 byte banks Half of bank 0 and half of bank 15* form a virtual bank that is accessible no matter which bank is selected
ACCESS RAM Bank 0 GPR Bank 1 GPR

PIC18F2520/4520 Register File Map

Bank 2 GPR

ACCESS RAM
ACCESS SFR

Bank 13 GPR

Bank 14 GPR Bank 15 GPR ACCESS SFR

* The division isn't 50/50 on some devices with a large number of Special Function Registers

organiza ELECTRONICA ELEMON S.A.

Mapeo memoria de programa


One, contiguous linear program memory space up to 2MB (1M Words = Instrucciones! )
21-bit Program Counter
Reset Vector
High Priority Interrupt Vector Low Priority Interrupt Vector 000000h 000008h 000018h

On-chip Program Memory

007FFEh Stack Level 1 Stack Level 2 008000h

Stack Level 30 Stack Level 31

Unimplemented Program Memory (Read as 0)

31 Level Stack
1FFFFEh

organiza ELECTRONICA ELEMON S.A.

Clock
Primary
Fixed Selection LP, XT, HS, RC, EC, Int RC Osc

Secondary
Timer1 Oscillator - fixed frequency Required for Real Time Clock time base

Internal RC Oscillator
INTOSC (8 MHz) source 4, 2, 1 MHz, 500, 250, 125 and 31 kHz INTRC (31 kHz) source
organiza ELECTRONICA ELEMON S.A. 10

Clock

organiza ELECTRONICA ELEMON S.A. 11

Bits de Configuracin
Used to setup device features:
Code Protect Watchdog Timer Oscillator Options Debug Options More
CONFIG registers located in program memory space, outside range of executable code space (starts @ 0x300000) 16-bit Program Memory
Reset Vector High Interrupt Vector Low Interrupt Vector User Memory
12

User Flash

Configuration Registers Device ID

organiza ELECTRONICA ELEMON S.A.

Configuration Memory

Bits de Configuracin
CONFIG1H Register
R/W-0 IESO bit 7 R/W-0 FCMEN R/W-0 R/W-0 R/W-0 FOSC3 R/W-0 FOSC2 R/W-0 FOSC1 R/W-0 FOSC0 bit 0

CONFIG2L Register
R/W-0 bit 7 R/W-0 R/W-0 R/W-0 BORV1 R/W-0 BORV0 R/W-0 BOREN1 R/W-0 BOREN0 R/W-0 PWRTEN bit 0

CONFIG2H Register
R/W-0 bit 7 R/W-0 R/W-0 R/W-0 WDTPS3 R/W-0 WDTPS2 R/W-0 WDTPS1 R/W-0 WDTPS0 R/W-0 WDTEN bit 0

CONFIG3H Register
R/W-0 MCLRE bit 7 organiza ELECTRONICA ELEMON S.A. R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 LPT1OSC R/W-0 PBADEN R/W-0 CCP2MX bit 0 13

Bits de configuracin con CCS


Usar la directiva #FUSES
#FUSES NOWDT //No Watch Dog Timer

options vary depending on the device. A list of all valid options has been put at the top of each devices .h file in a comment for reference.

organiza ELECTRONICA ELEMON S.A.

14

organiza ELECTRONICA ELEMON S.A.

15

IDE

organiza ELECTRONICA ELEMON S.A.

16

Documentacin

www.ccsinfo.com/downloads/CReferenceManual.pdf

www.ccsinfo.com/devices.php?page=devices

organiza ELECTRONICA ELEMON S.A.

17

Para poder programar con CCS primero deben saber programar en C

organiza ELECTRONICA ELEMON S.A.

18

organiza ELECTRONICA ELEMON S.A.

19

RTOS: Sistema Operativo Real Time con CCS

http://wiki.pic24.ru/doku.php/en/osa/ref/intro

organiza ELECTRONICA ELEMON S.A.

20

ex_rtos_demo1_tasks.C
#include <18F458.h> #use delay(clock=20000000) #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7) #use rtos(timer=0,minor_cycle=100ms) #task(rate=1000ms,max=100ms) void The_first_rtos_task ( ) { printf("1\n\r"); } #task(rate=500ms,max=100ms) void The_second_rtos_task ( ){ printf("\t2!\n\r"); } #task(rate=100ms,max=100ms) void The_third_rtos_task ( ) { printf("\t\t3\n\r"); }
organiza ELECTRONICA ELEMON S.A. 21 This tells the compiler that the rtos functionality will be needed, that timer0 will be used as the timing device, and that the minor cycle for all tasks will be 100 miliseconds

Each function that is to be an operating system task must have the #task preprocessor directive located above it. in this case, the task will run every second, its maximum time to run must be less than or equal to the minor cycle, and there is no need for a queue at this point, so no memory will be reserved. The function can be called anything that a standard function can be called

organiza ELECTRONICA ELEMON S.A.

22

Conociendo el MCU

organiza ELECTRONICA ELEMON S.A.

23

Si vamos a usar un periferico del PIC No olviden leer ( y tratar de entender ) antes que nada la documentacin en la hoja de datos! y ADEMAS chequear si nuestro modelo de micro es soportado por CCS

organiza ELECTRONICA ELEMON S.A.

24

organiza ELECTRONICA ELEMON S.A.

25

Perifericos
Digital I/O Ports

Analog Comparator
Analog to Digital Converter Timers (0,1,2,3)

Capture, Compare, PWM (CCP)


Addressable USART (AUSART) Master Synchronous Serial Port (MSSP)

organiza ELECTRONICA ELEMON S.A.

26

GPIOs
Up to 70 bi-directional I/O pins
Some multiplexed with peripheral functions

High drive capability


25mA source/sink capability

Direct, single cycle bit manipulation 4kV ESD protection diodes


Based on human body model

After reset:
Digital I/O default to Input (Hi-Z) Analog capable pins default to analog

organiza ELECTRONICA ELEMON S.A.

27

GPIOs
I/O Pin Direction Control
TRISB PORTB
1 1 0 1 1 0 0 0

In

In

Out

In

In

Out Out Out

Bit n in TRISx controls the data direction of Bit n in PORTx 1 = Input, 0 = Output
organiza ELECTRONICA ELEMON S.A. 28

GPIOs
Internal Data Bus

Write PORTx or LATx

Read LATx

Read PORTx

LATx 0 1 0 1 0 0 1 1

Pins are all inputs by default (TRIS = 0xFF)

TRISx 0 0 1 1 0 1 0 0

Data Direction (1 = IN, 0 = OUT)

organiza ELECTRONICA ELEMON S.A.

29

Initialize Output Latches Write to: to Known State LAT Configure Data Direction of Pins Write to:

LATB = 0; LATBbits.LATB0 = 0; TRISB = 0x23; TRISBbits.TRISB0 = 1; LATB = 0xF0; LATBbits.LATB0 = 0;

TRIS
Write to:

3a Write to outputs

LAT
3b Read from inputs

Read from: myVar = PORTB; myVar = PORTBbits.RB0;

PORT

organiza ELECTRONICA ELEMON S.A.

30

Control de I/O con CCS


output_a (value) output_bit (pin, value) output_high (pin) output_low (pin) output_toggle(pin) variable = input (pin) variable = input_a()

organiza ELECTRONICA ELEMON S.A.

31

Timer 1
Timer 1 Oscillator
0 1

T1OSO/ T13CKI T1OSI T1OSCEN FOSC/4

Prescaler 1, 2, 4, 8

Sync Detect Sleep Input

T1SYNC TMR1ON

TMR1CS T1CKPS1: T1CKPS0 TMR1IF


Clear Timer 1 (CCP Special Event Trigger)
READ TMR1L

High Byte

TMR1L
WRITE TMR1L

RD16
Bit 7

T1RUN

TCKPS1:0

T1OSCEN

T1SYNC

TMR1CS

TMR1ON
Bit 0

TMR1H

T1CON Register
organiza ELECTRONICA ELEMON S.A.

32

Timer 1
16-bit Timer / Counter Timer, Synchronous Counter or Asynchronous Counter Can operate from separate external crystal Two read/write 8-bit registers 1, 2, 4, or 8 Prescaler

Interrupt on overflow from FFFFh to 0000h

organiza ELECTRONICA ELEMON S.A.

33

Configuro el timer en CCS


setup_timer_x(mode)Disables or sets the source and prescale for timer1 set_timerx(value). Initializes the timer1 clock/counter value=get_timerx. Returns the value of the timer1 clock/counter

organiza ELECTRONICA ELEMON S.A.

34

A/D
Channel Select
AN12

CHS3:CHS0

In ADCON0 Register

AN12
AN10 AN9 AN8 AN7*

13:1 Analog Mux

AN6*
AN5* AN4

VAIN

10-bit A/D Result Registers

ADC
VREF+ VREF-

ADRESH

ADRESL

VREF+ / AN3 VREF- / AN2


AN1 AN0

VDD
x0 x1 1x 0x

VSS VREF Select

VCFG1:VCFG0
35

In ADCON1 Register organiza ELECTRONICA ELEMON S.A.

Ejemplo 1
Example:
10-bits ADC => 210 = 1024 codes Full Scale values are 0 5.12 V What is the Bit Size?

Answer:
Bit Size = 5.12 / 1024 = 5 mV

organiza ELECTRONICA ELEMON S.A.

36

Ejemplo 2
Example:
8-bits ADC => 28 Full Scale values are 0 5.0 V
Bit Size = 19.53 mV

Calculate voltage if ADRESH = 0xCA

Result = VIN / Bit Size = Result * Bit Size VIN = 202 * 19.53 mV VIN = 3.9456 V
organiza ELECTRONICA ELEMON S.A. 37

Agenda
FS = full scale For N-bits, 2N codes
are possible

Bit Size or Step


Bit Size = VFS/2N

organiza ELECTRONICA ELEMON S.A.

38

Canales de conversin
ADCON0 Register
U-0 U-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0

bit 7

CHS3

CHS2

CHS1

CHS0

GO/DONE

ADON
bit 0

bit 5-2

CHS3:CHS0 Analog Channel Select Bits


0000 = 0001 = 0010 = 0011 = 0100 = 0101 = 0110 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = Channel 0 (AN0) Channel 1 (AN1) Channel 2 (AN2) Channel 3 (AN3) Channel 4 (AN4) Channel 5 (AN5) Channel 6 (AN6) Channel 7 (AN7) Channel 8 (AN8) Channel 9 (AN9) Channel 10 (AN10) Channel 11 (AN11) Channel 12 (AN12)

bit 1

GO/DONE: A/D Conversion Status bit


1 = A/D Conversion in progress 0 = A/D Idle

bit 0

ADON: A/D On bit


1 = A/D Converter module is enabled 0 = A/D Converter module is disabled

organiza ELECTRONICA ELEMON S.A.

39

ADC en CCS
setup_adc(mode)Sets up the a/d mode like off, the adc clock

etc. setup_adc_ports(value)Sets the available adc pins to be analog or digital. s (channel)Specifies the channel to be use for the a/d call. et_adc_channel read_adc(mode)Starts the conversion and reads the value. The mode can also control the functionality. adc_done()Returns 1 if the ADC module has finished its conversion.

#DEVICE ADC=xx Configures the read_adc return size.

organiza ELECTRONICA ELEMON S.A.

40

organiza ELECTRONICA ELEMON S.A.

41

Como disear para garantizar el Exito?

organiza ELECTRONICA ELEMON S.A.

42

Compilacin Con CCS Existosa !

organiza ELECTRONICA ELEMON S.A.

43

organiza ELECTRONICA ELEMON S.A.

44

organiza ELECTRONICA ELEMON S.A.

45

You might also like