Escolar Documentos
Profissional Documentos
Cultura Documentos
Curso Pic
Curso Pic
so
executadas
em
um
Exemplo:
fosc = 4MHz
ciclo de mquina = fosc /4 = 4Mhz / 4 = 1Mhz
tciclo = 1/1Mhz = 1s
ciclo
de
Tipos de osciladores:
RC - RC externo;
INTOSC - RC interno;
EC - fonte clock externa;
HS - cristal/ressonador de alta frequncia(acima de 4MHZ);
XT - cristal/ressonador de mdia frequncia;
LP - cristal/ressonador de baixa frequncia(32 KHz)
U1
1
REXT
CEXT
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
U1
1
C1
XTAL
C2
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
So divididas em 3 tipos:
Memria no voltil;
Usada para armazenar valores que no podem ser
perdidos;
256 bytes;
Reset externo;
Quando aplicado nvel baixo ocorre o reinicio do
programa;
Pino 1 RE3/MCLR
R1
10K
U1
1
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
Co de guarda;
Contador de 8 bits;
Caractersticas:
Estruturada;
Alto nvel (comparada ao Assembly);
Facilidade de acesso ao hardware;
Portabilidade;
Curva de aprendizagem reduzida;
Reutilizao de cdigo;
So Utilizados para:
Documentao do software;
Excluso de linhas durante a compilao.
Exemplo:
temperatura;
_tempo1;
velocidade_angular;
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
struct
switch
typedef
union
unsigned
void
volatile
while
Tipo
Tamanho em bits
Intervalo
char
0 a 255
int
0 a 255
float
32
void
signed
signed int variavel_x;
unsigned
unsigned int variavel_x
short
short int flag_x;
long
long int variavel_x;
Bases:
Binria;
Octal ;
Decimal;
Hexadecimal:
Exemplo:
Representao do numero 50 nas diferentes bases:
50 - Decimal
0x32- Hexadecimal
0b00110010 - Binrio
062- Octal
Declarao:
const
#define
valor1 100
Declarao:
tipo nome_da_varivel;
tipo nome_da_varivel = valor_da_varivel;
Exemplo:
void main()
{
int valor;
valor =0;
Exemplo:
int valor;
//varivel global
void verifica_valor(void) //funo para verificar o valor do ad lido
{
if(valor < 127)
// se valor for menor que 127
RB0 = 1;
//liga RB0
}
void main()
//funo principal
{
valor = read_adc();
//le valor do ad
verifica_valor();
//chama funo para verificar valor
}
Tipos:
Atribuio;
Aritmticos;
Relacionais;
Lgicos;
Lgicos bit a bit;
Exemplo:
x = 10;
y = x+3;
AO
Adio
Subtrao
Multiplicao
Diviso
++
Incremento
--
Decremento
operador % :
x = 5%2;
Operador ++;
x = 1;
x++;
Operador --;
x = 10;
x--;
AO
>
Maior que
>=
<
Menor que
<=
==
Igual a
!=
Diferente de
OPERADOR
AO
&&
AND (E)
||
OR (OU)
NOT (NO)
AO
&
AND(E)
OR(OU)
NOT (NO)
>>
Deslocamento direita
<<
Deslocamento esquerda
Forma reduzida
x = x + y
x += y
x = x - y
x -= y
x = x * y
x *= y
x = x / y
x /= y
x = x % y
x %= y
x = x & y
x &= y
x = x | y
x |= y
x = x ^ y
x ^= y
x = x << y
x <<= y
x = x >> y
x >>= y
Testes condicionais;
Estruturas de repeties;
Exemplo:
if(temperatura < setPoint) RELE = 1;
if (condio)
comandoA;
else
comandoB;
Exemplo:
if (temperatura < setpoint)
rele = 1;
else
rele = 0;
Exemplo
switch(caracter_recebido)
{
case 1:
LED1 = 1;
break;
case 2:
LED2 = 1;
break;
default:
LED1 = 0;
LED2 = 0;
}
Forma geral:
for( inicializao;condio;incremento) comando;
Ou
for( inicializao;condio;incremento)
{
Comando1;
Comando2;
. . .
ComandoN;
}
Exemplo
for(x = 0;x <11;x++)
{
printf( %d x 10 = %d,x, x*10);
}
Exemplos
x = 0;
while(x<10)
{
LED = 1;
delay_ms(500);
LED = 0;
delay_ms(500);
x++;
}
while(1);
while(!RA0);
Exemplo
do
{
led = 1;
delay_ms(500);
led = 0;
delay_ms(500);
x++;
}while(x<10);
Caractersticas de funes
programa fica mais legvel e melhor estruturado;
Reutilizao de cdigo;
Cada funo tem um nome nico;
Os nomes de funes seguem as mesmas regras de nomenclatura
para variveis;
Podem ter qualquer nome exceto main.
Exemplo
int calcula_media(int A, int B)
{
int media;
media = (A + B)/2;
return media;
}
Manipulao de I/O;
Configurao de registradores;
Leitura do conversor A/D;
Configurao e uso dos timers;
Delays;
PWM;
Matemticas;
.....
output_low()
output_low (PIN_B1); // coloca o pino RB1 em 0
output_high()
Output_high (PIN_A0); // coloca o pino RA0 em 1
output_bit()
Output_bit (pin_A0, 0);// coloca o pino RA0 em 0
output_x()
output_b (0xFF); // escreve o valor 0xFF no PORTB
input()
x = input(PIN_A0); // l o estado do pino RA0
input_x()
x = input_b(); // l o estado do PORTB
delay_cycles()
delay_us (1); // aguarda 1 us.
delay_us()
Delay_us (1); // aguarda 1 us.
delay_ms()
delay_ms (1); // aguarda 1 ms.
setup_adc_ports()
setup_adc_ports (AN0);//define RA0 como entrada analgica
set_adc_channel ()
set_adc_channel(2); //seleciona canal 2 para converso
read_adc ()
valor = read_adc(); //le o resultado da converso
adc_done ()
while (!adc_done());
#asm #endasm
Permite a incluso de cdigo assembly.
Exemplo:
#asm
bsf PORTB,3 // estas duas instrues geram um
pulso
nop
bcf PORTB,3 // no pino RB3.
#endasm
#case
Ativa a distino entre caracteres maisculos/minsculos.
#define
Utilizado para substituir valores por identificadores ou at
mesmo macro comandos.
Exemplo:
#define BT1 RB0
#define liga_led RC0 = 1
#include
Insere arquivos externos
Exemplo:
#include <16F883.h>
#include "PIC16F883_SFR.h
#ROM
Armazena valores na EEPROM durante a gravao
Exemplo:
#rom 0x2100 = { 0x00, 0x25};
#fuses
Configura os fusveis de funcionamento
Exemplo:
#fuses
XT,NOMCLR,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
1.00
XT
1.00
EC_IO
External clock
1.03
NOWDT
1.03
WDT
1.05
NOMCLR
1.05
MCLR
1.06
PROTECT
1.07
NOCPD
No EE protection
1.07
CPD
1.08
#ifdef
#endif
#INT_XXXX
Indica para o compilador que o trecho a seguir refere-se a
uma rotina de interrupo
#use delay
Informa o clock do sistema
Exemplo:
#use delay (clock=4000000)
#byte
Permite o acesso a posies de RAM (Registros e memria
geral) pela indicao de seu endereo real
Exemplo:
#byte PORTB = 0x06
#bit
Usado para definir um identificador para um bit de uma
varivel j definida.
Exemplo:
int flags;
#bit ST_BT1 = flag.0
PIC Bsico 28 P
Hardware:
+ 5V
R1
10K
U1
1
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
Esquema 1
U1
1
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
R1
330R
D1
LED-RED
Esquema 2
+5V
R1
330R
D1
U1
1
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
LED-RED
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
U1
1
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
R1
330R
R2
330R
R3
330R
R4
330R
R5
330R
R6
330R
R7
330R
+5V
U1
1
2
3
4
5
6
7
10
9
21
22
23
24
RE3/MCLR/VPP
RB4/AN11/P1D
RB5/AN13/T1G
RB6/ICSPCLK
RB7/ICSPDAT
RA0/AN0/ULPWU/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC0/T1OSO/T1CKI
RA4/T0CKI/C1OUT
RC1/T1OSI/CCP2
RA5/AN4/SS/C2OUT
RC2/CCP1/P1A
RA6/OSC2/CLKOUT
RC3/SCK/SCL
RA7/OSC1/CLKIN
RC4/SDI/SDA
RC5/SDO
RB0/AN12/INT
RC6/TX/CK
RB1/AN10/P1C/C12IN3RC7/RX/DT
RB2/AN8/P1B
RB3/AN9/PGM/C12IN2PIC16F883
25
26
27
28
11
12
13
14
15
16
17
18
R1
330R
R2
330R
R3
330R
R4
330R
R5
330R
R6
330R
R7
330R
So utilizados para:
Contagem de tempos;
Contagem de eventos;
Registradores associados:
Registradores associados:
Temporizador de 8 bits
Registradores associados:
Eventos assncronos;
Desvia o programa automaticamente para o vetor
de interrupo (0x04);
Convencionais ;
Perifricos;
Caractersticas
Conversor de 10 bits;
11 canais disponveis;
Referncia interna;
Referncia externa;
Conversor por aproximaes sucessivas;
Freqncia de converso ajustvel por software;
Interrupo associada ao termino da converso.
Exemplo:
TRISA = 0b00000001; //pino RA0 como entrada
ANSEL = 0b00000001; //pino RA0 funcionando como AN0
Clock
Resultado da converso
printf
Envia uma string(seqncia de caracteres) pela serial.
Exemplo:
printf(hello world); //imprime na serial a string: hello world
putc
Envia apenas um caractere na serial.
Exemplo:
putc(f);
gets
L uma string da serial
Exemplo:
char buffer[10]; //declara o vetor buffer para armazenamento da string
gets(buffer);
//aguarda a chegada de string na serial
Getc
L um caracter da serial
Exemplo:]
char c;
c = getc();
Kbhit
Retorna verdadeiro (1) se chegar um caractere pela
serial e falso(0) enquanto no houver caractere.
Exemplo:
if(kbhit())
{
c = getc();
}
10
// endereo na EEPROM
volume++;
//incrementa o valor
write_eeprom(END_VOLUME,volume); //salva na EEPROM endereo 10
read_eeprom (endereo);
Exemplo:
#define END_VOLUME
10
//endereo
Fbio B. de Souza
fabio_souza53@hotmail.com
Cel.: 1195322559