Você está na página 1de 15

Introduo a programao de micro controladores PIC

Ol mundo
Programao em C utilizando o Mplab C18 e PICs da famlia 18F
Luis Rafael 2010 http://www.lingtronic.com
1

Facilidade de Aquisio Baixo consumo Entradas e Sadas Conversor ADC Fcil Interligao com outros dispositivos Interrupes externas e internas

Microchip Technology
Luis Rafael 2010 http://www.lingtronic.com

8 bits , 16 bits, mais recentemente 32 Extensa variedade de modelos e perifricos internos Programao por Memria flash Podem ser programados em assembler, Pascal C Basic Compiladores geram um cdigo em formato hexadecimal Gravados na memria de programa desses microcontroladores. Para tal procedimento, utiliza-se um hardware especial (gravador)

acoplado a um PC. PICs com memria FLASH so altamente flexveis na fase de desenvolvimento pois permitem uma rpida alterao do cdigo de programa. Como ferramentas de desenvolvimento, encontram-se disponveis: gravadores, depuradores, emuladores, placas de prottipos, etc.
Luis Rafael 2010 http://www.lingtronic.com

Microcontrolador PIC
Programar em C
#include <p18cxxx.h>

void main(void) { /* Coloca os registos a O e define o port B como saidas */ LATB = 0x00; TRISB = 0x00;
/* Ligar um led B0 */ LATBbits.LATB0 = 1; /* PORTDbits.RB0 =1 teria funcionado mas no a maneira correcta*/

while(1); } Luis Rafael 2010


http://www.lingtronic.com 4

Microcontrolador PIC
Programar em C
. . . while(1) { /* Liga LED, B0 */ LATBbits.LATB0 = 1;

int i; ... LATBbits.LATB0 = 1; for ( i = 0; i < 32768; i++); nop; LATBbits.LATB0 = 1; for ( i = 0; i < 32768; i++); nop;

/* Desliga LED, B0 */ LATBbits.LATB0 = 0; }


Luis Rafael 2010 http://www.lingtronic.com

Microcontrolador PICc
Tempos e Delays
Supondo a utilizao de 10 MHz clock com 250 nanossegundos tempo de ciclo de instrucao.

So presisas 2,000,000 ciclos de intruses para ter um atraso de 500 milissegundos Pode ser conseguido com a utilizao da seguinte instruo Delay10KTCYx 200;
Luis Rafael 2010 http://www.lingtronic.com

#include <p18cxxx.h> #include <delays.h> /* Para utilizar as funcoes de delay */ void main(void) { /* Config PORT D como saidas digitais */ LATD = 0x00; TRISD = 0x00;

Luis Rafael 2010 http://www.lingtronic.com

while(1) { /* Turn on LED, D1 */ LATDbits.LATD0 = 1; /* Delay de 500 milissegundos */ Delay10KTCYx(200); /* Turn off LED, D1 */ LATDbits.LATD0 = 0; /* Delay de 500 milissegundos */ Delay10KTCYx(200); } }

Microcontrolador Delays.h
Function Functionality Provided Delay one instruction cycle. Same as Nop(). Parameters Example Usage
Delay1TCY();

Delay in Example 1 Instruction Cycle 100 Instruction Cycles 1,000 Instruction Cycles

Delay1TCY

None
unsigned char (8 bit value)

Delay10TC Delay in multiples of 10 Yx instruction cycles.

Delay10TCYx(10);

Delay100T CYx

Delay in multiples of 100 instruction cycles.

unsigned char (8 bit value)


unsigned char (8 bit value) unsigned char (8 bit value)

Delay100TCYx(10);

Delay1KTC Delay in multiples of Yx 1,000 instruction cycles. Delay10KT CYx Delay in multiples of 10,000 instruction cycles.

Delay1KTCYx(10);

10,000 Instruction Cycles


100,000 Instruction Cycles
8

Delay10TCYx(10);

Luis Rafael 2010 http://www.lingtronic.com

#define LED0 LATDbits.LAT0


#define ON 1

#define OFF 0
#include <p18cxxx.h> #include <delays.h> /* This is needed for the delay functions */

/* Define LED0 as LATDbits.LAT0 */ #define LED0 LATDbits.LAT0 void main(void) { /* Set PORT D as digital outputs */ LATD = 0x00; TRISD = 0x00;
/* Loop forever */ while(1) { /* Turn on LED, D1 */ LED0 = 1;
Luis Rafael 2010 http://www.lingtronic.com

MPLAB

Luis Rafael 2010 http://www.lingtronic.com

10

MPLAB

Luis Rafael 2010 http://www.lingtronic.com

11

MPLAB

Luis Rafael 2010 http://www.lingtronic.com

12

MPLAB

Luis Rafael 2010 http://www.lingtronic.com

Ficheiros a Adicionar Ficheiro com o cdigo fonte na pasta source files Ficheiro de configurao e outras bibliotecas na pasta header files Ficheiro .Lib do microcontrolador a utilizar na pasta Library Files Ficheiro *.lkr do microcontrolador a utilizar
13

Configuration Bits

#pragma config OSC = HS /* Sets the oscillator mode to HS */ #pragma config WDT = OFF /* Turns the watchdog timer off */ #pragma config LVP = OFF /* Turns low voltage programming off */ #pragma config DEBUG = OFF /* Compiles without extra debug code */
Luis Rafael 2010 http://www.lingtronic.com

14

Fim

Luis Rafael 2010

http://www.lingtronic.com
15

Você também pode gostar