eng-1450 microcontroladores e sistemas embarcadosabranco/eng1450/lab03/lab03.pdf · pic: 8.9 -...

20
ENG-1450 Microcontroladores e Sistemas Embarcados – Lab03 – Execução Experiência 01, Keypad, 7-seg, Exercícios

Upload: others

Post on 12-Mar-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

ENG-1450Microcontroladores e Sistemas Embarcados

– Lab03 – Execução Experiência 01,

Keypad, 7-seg,Exercícios

Page 2: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Agenda

● Execução da Experiência 01 no Proteus● Teclado por interrupção● Display 7-segmentos

OBS:Tem que praticar consultando

o manual do PIC18F452.

Page 3: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Execução Experiência 01

● Cada bancada deverá apresentar a execução da Experiência 1 no Proteus.

● A execução no Kit PicGenius é opcional a critério do aluno e do tempo disponível.

Page 4: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Teclado

Montar no protoboard e ligar no kit PicGenius.

Cód. no Proteus: KEYPAD-SMALLCALC

Obs: Teclas invertidas

Page 5: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Exemplo: Keypad 3x3

VCC

R=10K

Escrevenas Llinhas.(PIC output)

Lê dascolunas.(PIC input)

Pull-up

Page 6: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Exemplo

VCC

R

X

Escreve

0

1

1

1 10

X

Aberto

Fechado

Page 7: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Keypad 3x3

VCC

R

Escreve

1 1 0

1 0 1

0 1 1

Wr:

Wr:

Wr:

Rd: x x x

Rd: x x x

Rd: x x x

Varredura

Page 8: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Rotina do Teclado

● Usar interrupção para disparar uma varredura. Isto é, quando uma tecla for pressionada deve interromper o sistema.

● Varrer o teclado e identificar a tecla pressionada.

● Executar função de tratamento da tecla.

a

b

c

Page 9: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Procedimento Interrupção

● Fixar as linhas em “0”.● Monitorar se alguma coluna foi de “1” para “0”.● Pode usar interrupção de “edge” ou de “nível”

(INTn ou RB).● Prevenir Key-bouncing: Ignorar interrupções

com intervalos pequenos (20ms).

a

Page 10: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Identificação da tecla - Varredura● O procedimento de varredura seleciona um bit por vez

para ser “escrito”, por exemplo, nas linhas do teclado.● Para cada bit selecionado, deve-se verificar nas

colunas se existe alguma tecla pressionada. ● A primeira tecla pressionada deve parar o

procedimento de varredura.● A combinação da linha e coluna no momento que uma

tecla foi identificada é a base para gerar o código da tecla.

● Pode-se usar um switch/case para “mapear” o valor combinado para o código da tecla (1..16)

b

Page 11: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Dicas (caso de pull-up)

1. “Varrer” os bits com '0'

for (bit=0; bit < 4; bit++)

rowByte = ~(1 << bit); // para varrer '0'

2. Identificar tecla● Na hora que a leitura das colunas for diferente “1111”,

combinar os bits escritos nas linhas com os bits lidos das colunas. (llllcccc)

● Fazer um decodificador de “llllcccc” para um código de tecla. Pode ser um switch/case.

● Colocar as linhas e colunas na mesma porta pode simplificar o programa.

b

b

Page 12: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

void interrupt(){….. if (key interupt) { keyboardHandler(); }}

void keyboardHandler(){ ….. keyHandler(key);}void keyHandler(char key){…..}

void main(){…...

while(1)…...}

Loop principal

Inicialização - Portas & interrupções

Tratador de interrupções

- Trata interrupção teclado - Key bouncing

Tratador de tecla - Específico da aplicação

Tratador do teclado - Faz varredura, identifica tecla

c

b

a

Page 13: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Variações de circuitos

● Interrupção de “edge” – ex.: INTn, 0->1 edge– Linhas → 74LS20 (NAND 4 inputs) na INTn

– Linhas e colunas só não podem estar no mesmo pino da interrupção.

● Interrupção de nível - RB– Linhas na porta RB4..7 – Interrupção RB

– Colunas, se colocar na RB0..3, vai facilitar mas não poderá usar a INT0..2 no resto da aplicação.

– Obs: Interrompe de 1->0 e de 0->1

INTnLinhas 0123

a

Page 14: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Problema de mudança de contexto na interrupção

MikroCO pograma pode

chamar funções dedentro da rotinade interrupção.

PIC: 8.9 - Context Saving During InterruptsDuring an interrupt, the return PC value is saved on the stack. Additionally, the WREG, STATUS and BSR registers are saved on the fast return stack. If a fast return from interrupt is not used (See Section 4.3), the user may need to save the WREG, STATUS and BSR registers in software. Depending on the user’s application, other registers may also need to be saved.

MikroC: Function Calls from InterruptCalling functions from within the interrupt() routine is now possible. The compiler takes care about the registers being used, both in "interrupt" and in "main" thread, and performs "smart" context-switching between the two, saving only the registers that have been used in both threads.

Page 15: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Display 7-segmentos

Page 16: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Display 7-SegmentosCatodo Comum & Anodo Comum

Vcc

GND

1011 11100100 00011 0

Catodo ComumProteus: 7SEG-MPX1-CC

Anodo ComumProteus: 7SEG-MPX1-CA

BC337

BC327

330 Ω

Page 17: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

GND

RA2RA3RA4RA5

Page 18: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Decimal x 7-Seg

● Disponível no menu do MikroC

Digito Catodo Comum Anodo Comum

0 0x3F 0xC0

1 0x06 0xF9

2 0x5B 0xA4

3 0x4F 0xB0

4 0x66 0x99

5 0x6D 0x92

6 0x7D 0x82

7 0x07 0xF8

8 0x7F 0x80

9 0x6F 0x90

Page 19: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Rotina do display 7-Seg

● Loop de escrita usando interrupção de Timer.– Testar com o timer de 100ms.– Ajustar o tempo do timer para não piscar.

● Cada interrupção escreve num display.– Manter uma variável global para indicar o display da vez.

● Variável global para armazenar o valor do display.– A aplicação deve escrever nessa variável

– A rotina do display exibe o valor.

● Função para retornar valor de um digito específico– Função que recebe o valor global (x) e o display da vez (n) e

retorna o valor do display. => f(x,n) = (x/10n)%10

– Se quiser, já pode aproveitar e converter para os 7-segmentos.

Page 20: ENG-1450 Microcontroladores e Sistemas Embarcadosabranco/eng1450/Lab03/Lab03.pdf · PIC: 8.9 - Context Saving During Interrupts During an interrupt, the return PC value is saved on

Exercícios básicos(Proteus + Kit PicGenius)

1) Fazer a parte do código para tratamento do:● Teclado genérico● Display 7-segmentos

2) Exibir contator no display 7-segmentos● Loop principal com count++ e delay 500ms

3) Exibir valor digitado no LCD● Usar keypad externo