View
1.666
Download
3
Category
Preview:
DESCRIPTION
Programação dos Periféricos, Acesso às portas do microcontrolador, Configuração dos periféricos e o Barramento de Led's
Citation preview
Programacao dos Perifericos
Prof.MSc.Rodrigo Maximiano Antunes de Almeida
Universidade Federal de Itajuba
rodrigomax@unifei.edu.br
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 1 / 27
Arquitetura de MicrocontroladoresEsquema eletrico e circuitos importantes
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 2 / 27
Arquitetura de MicrocontroladoresMultiplexacao nos terminais do microcontrolador
Ex: o oitavo terminal do PIC18F4550.
Pode ser configurado como terminal:
de entrada ou saıda referente ao primeiro bit da porta E (RE0)de leitura analogica pertencente ao quinto conversor analogico - digital(AN5)utilizado para enviar um clock externo de comunicacao paralela(CK1SPP)
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 3 / 27
Arquitetura de MicrocontroladoresRegistros de configuracao do microcontrolador
Registros de configuracao domicrocontrolador
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 4 / 27
Arquitetura de MicrocontroladoresEsquema eletrico e circuitos importantes
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 5 / 27
Arquitetura de MicrocontroladoresRegistros de configuracao do microcontrolador
1 // c o n f i g . h2 #ifndef CONFIG_H
3 #define CONFIG_H
4 // P l l d e s l i g a d o5 code char at 0x300000 CONFIG1L = 0x01 ;6 // O s c i l a d o r c / c r i s t a l e x t e r n o HS7 code char at 0x300001 CONFIG1H = 0x0C ;8 // Watchdog c o n t r o l a d o por s o f t w a r e9 code char at 0x300003 CONFIG2H = 0x00 ;
10 // Sem programac ao em b a i x a t e n s a o11 code char at 0x300006 CONFIG4L = 0x00 ;12 #endif //CONFIG H
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 6 / 27
Programacao dos Perifericos
Programacao dos Perifericos...
In theory, there is no difference between theory and practice; In practice, there is.Chuck Reid
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 7 / 27
Programacao dos PerifericosPerifericos disponıveis
Saıda
Barramento de Led’sDisplay de 7 segmentosDisplay LCD 2x16Saıdas PWM
Entrada
Teclado 4*4Conversor AD
Outros
Serial RS232Tratador de InterrupcoesWatchdog
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 8 / 27
Programacao dos PerifericosBarramento de Leds
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 9 / 27
Programacao dos Perifericos
Acesso as portas do microcontrolador
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 10 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
O microcontrolador PIC 18F4550 possui 5 portas
PORTA: bidirecional com 7 bits
PORTB: bidirecional com 8 bits
PORTC: bidirecional com 7 bits
PORTD: bidirecional com 8 bits
PORTE: 3 bits bidirecionais e 1 bit apenas entrada
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 11 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
Bit númeroPORTB 0
TerminalFísico 33
1
34
2
35
3
36
4
37
5
38
6
39
7
40
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 12 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
37 Trisb:4
Leitor
Gravador
4Trisb:4
= 1
= 0 = 0
= 1< 2volts = 0> 3volts = 1
PORTB
Abrir datasheet do PIC18f4550 na pagina 110.
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 13 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
Porta Endereco dos dados Endereco de configuracao (TRIS)
PORTA 0xF80 0xF92
PORTB 0xF81 0xF93
PORTC 0xF82 0xF94
PORTD 0xF83 0xF95
PORTE 0xF84 0xF96
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 14 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
1 // i n i c i o do programa2 v o i d main ( v o i d )3 {4 // d e f i n i m o s como :5 // ( u n s i g n e d c h a r ) p o i s os 8 b i t s r e p r e s e n t a m v a l o r e s6 // ( v o l a t i l e ) as v a r i a v e i s podem mudar a q u a l q u e r momento7 // ( n e a r ) i n d i c a que o r e g i s t r o e s t a na memoria de dados8 v o l a t i l e near uns igned char ∗PORTD = 0xF83 ;9 v o l a t i l e near uns igned char ∗TRISD = 0xF95 ;
10 // c o n f i g u r a n d o t o d o s os p i n o s como s a ı d a s11 // 0 = s a ı d a ( Output )12 // 1 = e n t r a d a ( I n p u t )13 ∗TRISD = 0b00000000 ;14 // l i g a apenas os q u a t r o u l t i m o s l e d s15 ∗PORTD = 0b11110000 ;16 //mantem o s i s t e m a l i g a d o i n d e f i n i d a m e n t e17 f o r ( ; ; ) ;18 }
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 15 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
1 // d e f i n e s para p o r t a s de e n t r a d a e s a ı d a2 #define PORTD (∗ ( v o l a t i l e near uns igned char ∗) 0xF83 )3 #define TRISD (∗ ( v o l a t i l e near uns igned char ∗) 0xF95 )4 // i n i c i o do programa5 v o i d main ( v o i d )6 {7 // c o n f i g u r a n d o t o d o s os p i n o s como s a ı d a s8 TRISD = 0b00000000 ;9 // l i g a apenas os q u a t r o u l t i m o s l e d s
10 PORTD = 0b11110000 ;11 //mantem o s i s t e m a l i g a d o i n d e f i n i d a m e n t e12 f o r ( ; ; ) ;13 }
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 16 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
Vantagens no uso dos #defines
Utilizacao nao requer uso do *, pois ja esta incluso no defineEconomiza memoria ram. Criando uma variavel e necessario reservarum espaco para armazenar o valor do ponteiro.A definicao pode ser feita em apenas um arquivo do tipo header. Osoutros arquivos que precisem utilizar estas definicoes devem apenasincluir este header.A mudanca de arquitetura e mais simples. Criando apenas um arquivocom os defines e mais simples migrar o codigo de uma arquitetura paraoutra.
Atencao
O tamanho do programa final esta intimamente ligado ao compilador utilizado.Dependendo das otimizacoes que sao (ou nao) realizadas, o programa pode ficar maiorcom o uso dos defines.
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 17 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
1 // b a s i c o . h2 // f u n c o e s de b i t3 #define BitSet ( arg , bit ) ( ( arg ) |= (1<<bit ) )4 #define BitClr ( arg , bit ) ( ( arg ) &= ˜(1<<bit ) )5 #define BitFlp ( arg , bit ) ( ( arg ) ˆ= (1<<bit ) )6 #define BitTst ( arg , bit ) ( ( arg ) & (1<<bit ) )78 // d e f i n e s para r e g i s t r o s e s p e c i a i s9 #define PORTA (∗ ( v o l a t i l e near uns igned char ∗) 0xF80 )
10 #define PORTB (∗ ( v o l a t i l e near uns igned char ∗) 0xF81 )11 #define PORTC (∗ ( v o l a t i l e near uns igned char ∗) 0xF82 )12 #define PORTD (∗ ( v o l a t i l e near uns igned char ∗) 0xF83 )13 #define PORTE (∗ ( v o l a t i l e near uns igned char ∗) 0xF84 )1415 #define TRISA (∗ ( v o l a t i l e near uns igned char ∗) 0xF92 )16 #define TRISB (∗ ( v o l a t i l e near uns igned char ∗) 0xF93 )17 #define TRISC (∗ ( v o l a t i l e near uns igned char ∗) 0xF94 )18 #define TRISD (∗ ( v o l a t i l e near uns igned char ∗) 0xF95 )19 #define TRISE (∗ ( v o l a t i l e near uns igned char ∗) 0xF96 )
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 18 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
1 // b a s i c o . h2 #define INTCON (∗ ( v o l a t i l e near uns igned char ∗) 0xFF2 )3 #define INTCON2 (∗ ( v o l a t i l e near uns igned char ∗) 0xFF1 )4 #define PIE1 (∗ ( v o l a t i l e near uns igned char ∗) 0xF9D )5 #define PIR1 (∗ ( v o l a t i l e near uns igned char ∗) 0xF9E )67 #define TMR0L (∗ ( v o l a t i l e near uns igned char ∗) 0xFD6 )8 #define TMR0H (∗ ( v o l a t i l e near uns igned char ∗) 0xFD7 )9 #define T0CON (∗ ( v o l a t i l e near uns igned char ∗) 0xFD5 )
1011 #define SPPCON (∗ ( v o l a t i l e near uns igned char ∗) 0xF65 )12 #define SPPCFG (∗ ( v o l a t i l e near uns igned char ∗) 0xF63 )1314 #define ADCON2 (∗ ( v o l a t i l e near uns igned char ∗) 0xFC0 )15 #define ADCON1 (∗ ( v o l a t i l e near uns igned char ∗) 0xFC1 )16 #define ADCON0 (∗ ( v o l a t i l e near uns igned char ∗) 0xFC2 )17 #define ADRESL (∗ ( v o l a t i l e near uns igned char ∗) 0xFC3 )18 #define ADRESH (∗ ( v o l a t i l e near uns igned char ∗) 0xFC4 )
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 19 / 27
Programacao dos PerifericosAcesso as portas do microcontrolador
1 // b a s i c o . h2 #define RCSTA (∗ ( v o l a t i l e near uns igned char ∗) 0xFAB )3 #define TXSTA (∗ ( v o l a t i l e near uns igned char ∗) 0xFAC )4 #define TXREG (∗ ( v o l a t i l e near uns igned char ∗) 0xFAD )5 #define RCREG (∗ ( v o l a t i l e near uns igned char ∗) 0xFAE )6 #define SPBRG (∗ ( v o l a t i l e near uns igned char ∗) 0xFAF )7 #define SPBRGH (∗ ( v o l a t i l e near uns igned char ∗) 0xFB0 )8 #define BAUDCON (∗ ( v o l a t i l e near uns igned char ∗) 0xFB8 )9 #define RCON (∗ ( v o l a t i l e near uns igned char ∗) 0xFD0 )
1011 #define WDTCON (∗ ( v o l a t i l e near uns igned char ∗) 0xFD1 )1213 #define T2CON (∗ ( v o l a t i l e near uns igned char ∗) 0xFCA )14 #define PR2 (∗ ( v o l a t i l e near uns igned char ∗) 0xFCB )15 #define CCP2CON (∗ ( v o l a t i l e near uns igned char ∗) 0xFBA )16 #define CCPR2L (∗ ( v o l a t i l e near uns igned char ∗) 0xFBB )17 #define CCP1CON (∗ ( v o l a t i l e near uns igned char ∗) 0xFBD )18 #define CCPR1L (∗ ( v o l a t i l e near uns igned char ∗) 0xFBE )1920 // f u n c a o para l i m p a r o watchdog21 #define CLRWTD ( ) _asm CLRWDT _endasm
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 20 / 27
Programacao dos Perifericos
Configuracao dos perifericos
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 21 / 27
Programacao dos PerifericosConfiguracao dos perifericos
Term. DescricaoConfigurarcomo
Funcao
2 RA0/AN0 AN0Potenciometro / Sen-sor de Temperatura
4 RA2/AN2/VREF-/CVREF RA2 Display 2
7 RA5/AN4/SS/C2OUT RA5 Display 1
8 RE0/AN5/CK1SPP RE0 RS-LCD / Display 3
9 RE1/AN6/CK2SPP RE1 EN-LCD
10 RE2/AN7/OESPP RE2 RW-LCD / Display 4
13 OSC1/CLKI OSC1Cristal
14 OSC2/CLKO/RA6 OSC2
16 RC1/T1OSI/CCP2/UOE CCP2 Aquecedor
17 RC2/CCP1/P1A CCP1 Ventilador / Buzzer
19 RD0/SPP0 RD0Barramento de dadospara o LCD/7seg/Led
20 RD1/SPP1 RD1
21 RD2/SPP2 RD2
22 RD3/SPP3 RD3
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 22 / 27
Programacao dos PerifericosConfiguracao dos perifericos
Term. DescricaoConfigurarcomo
Funcao
25 RC6/TX/CK TX/RC6 RS232/I2C
26 RC7/RX/DT/SDO RX RS232
27 RD4/SPP4 RD4Barramento de dadospara o LCD / 7seg /Led
28 RD5/SPP5/P1B RD5
29 RD6/SPP6/P1C RD6
30 RD7/SPP7/P1D RD7
33 RB0/AN12/INT0/SDI/SDA RB0/SDA Saıdas paraalimentacao doteclado, 0 e 1 paraI2C
34 RB1/AN10/INT1/SCK/SCL RB1/SCL
35 RB2/AN8/INT2/VMO RB2
36 RB3/AN9/CCP2/VPO RB3
37 RB4/AN11/KBI0/CSSPP RB4Entradas para leiturado teclado
38 RB5/KBI1/PGM RB5
39 RB6/KBI2/PGC RB6
40 RB7/KBI3/PGD RB7
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 23 / 27
Programacao dos PerifericosConfiguracao dos perifericos
1 TRISA = 0b00000010 ; // c o n f i g . os t e r m i n a i s como e n t r a d a e s a ı d a2 ADCON1 = 0b00001110 ; // apenas o p r i m e i r o t e r m i n a l e a n a l o g i c o34 TRISB = 0b11110000 ; // c o n f i g . os t e r m i n a i s como e n t r a d a e s a ı d a5 INTCON = 0b11000101 ; // c o n f i g . com i n t e r r u p c a o h a b i l i t a d a6 INTCON = 0b00000000 ; // c o n f i g . sem i n t e r r u p c a o7 INTCON2 = 0b00000001 ;8 SPPCFG = 0b00000000 ; //RB0−4 c o n t r o l a d o s p e l a p o r t a B9
10 TRISC = 0b10000000 ; // t e r m i n a i s s ao s a ı d a s , s o RC7 e e n t r a d a11 CCP1CON = 0b00001100 ; // c o n f i g . o 2o t e r m i n a l como PWM12 CCP2CON = 0b00001100 ; // c o n f i g . o 3o t e r m i n a l como PWM13 TXTA = 0b00101100 ; // c o n f i g . a t r a n s m i s s a o de dados da s e r i a l14 RCSTA = 0b10010000 ; // c o n f i g . a r e c e p c a o de dados da s e r i a l15 BAUDCON = 0b00001000 ; // c o n f i g . s i s t e m a de v e l o c i d a d e da s e r i a l16 SPBRGH = 0b00000000 ; // c o n f i g . para 56 k17 SPBRG = 0b00100010 ; // c o n f i g . para 56 k1819 TRISD = 0b00000000 ; // c o n f i g . os t e r m i n a i s como s a ı d a2021 TRISE = 0b00000000 ; // c o n f i g . os t e r m i n a i s como s a ı d a
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 24 / 27
Programacao dos Perifericos
Barramento de Leds
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 25 / 27
Programacao dos PerifericosBarramento de Leds
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 26 / 27
Programacao dos PerifericosBarramento de Leds
Biblioteca de acesso aos leds:
Inicializacao do dispositivoArmazenamento de variaveis internas (enderecos)Escrita/Leitura do estado dos leds
ELT024 Programacao dos Perifericos rodrigomax@unifei.edu.br 27 / 27
Recommended