Professional Documents
Culture Documents
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
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
Arquitectura
Arquitectura de 8-bit PIC Diagrama en bloques simplificado
8-bit
Table Access
8-bit
Data RAM
Program Bus
16-bit
Peripherals
I/O Ports
PROGRAMA
organiza ELECTRONICA ELEMON S.A.
DATOS
7
Bank 2 GPR
ACCESS RAM
ACCESS SFR
Bank 13 GPR
* The division isn't 50/50 on some devices with a large number of Special Function Registers
31 Level Stack
1FFFFEh
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
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 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
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.
14
15
IDE
16
Documentacin
www.ccsinfo.com/downloads/CReferenceManual.pdf
www.ccsinfo.com/devices.php?page=devices
17
18
19
http://wiki.pic24.ru/doku.php/en/osa/ref/intro
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
22
Conociendo el MCU
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
24
25
Perifericos
Digital I/O Ports
Analog Comparator
Analog to Digital Converter Timers (0,1,2,3)
26
GPIOs
Up to 70 bi-directional I/O pins
Some multiplexed with peripheral functions
After reset:
Digital I/O default to Input (Hi-Z) Analog capable pins default to analog
27
GPIOs
I/O Pin Direction Control
TRISB PORTB
1 1 0 1 1 0 0 0
In
In
Out
In
In
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
Read LATx
Read PORTx
LATx 0 1 0 1 0 0 1 1
TRISx 0 0 1 1 0 1 0 0
29
Initialize Output Latches Write to: to Known State LAT Configure Data Direction of Pins Write to:
TRIS
Write to:
3a Write to outputs
LAT
3b Read from inputs
PORT
30
31
Timer 1
Timer 1 Oscillator
0 1
Prescaler 1, 2, 4, 8
T1SYNC TMR1ON
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
33
34
A/D
Channel Select
AN12
CHS3:CHS0
In ADCON0 Register
AN12
AN10 AN9 AN8 AN7*
AN6*
AN5* AN4
VAIN
ADC
VREF+ VREF-
ADRESH
ADRESL
VDD
x0 x1 1x 0x
VCFG1:VCFG0
35
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
36
Ejemplo 2
Example:
8-bits ADC => 28 Full Scale values are 0 5.0 V
Bit Size = 19.53 mV
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
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
bit 1
bit 0
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.
40
41
42
43
44
45