#include #include #include #include #include "config1.c" void D_7seg (char C); void delay (unsigned int ms); void teclado_init (); char tecla, tecla_antiga; void main (){ char teclapres, teclado='0'; teclado_init (); while (1){ teclapres = teclado_getec(); if (teclapres!=0xFF){ teclado = teclapres; } D_7seg(teclado); } } #define fila1por LATBbits.LATB0 // define portas a serem utilizadas pelo teclado matricial #define fila2por LATBbits.LATB1 #define fila3por LATBbits.LATB2 #define fila4por LATBbits.LATB3 #define colu1por PORTBbits.RB4 // define entrada #define colu2por PORTBbits.RB5 #define colu3por PORTBbits.RB6 #define _XTAL_FREQ 8000000 int x[10]={0b00111111, //0 0b00000110, //1 0b01011011, //2 0b01001111, //3 0b01100110, //4 0b01101101, //5 0b01111101, //6 0b00000111, //7 0b01111111, //8 0b01101111, //9 }; char const matrizteclado[]={ '1','2','3', '4','5','6', '7','8','9', '*','0','#', 0xFF }; void teclado_init (){ //Para 7 segmentos PORTB = 0x00; //Reinicializa as portas B,D e resistor pullup porta b. LATB = 0x00; TRISB = 0xF0; INTCON2bits.NOT_RBPU = 1; //0 ativo 1 negado para NOT ! Esta função é para fazer a leitura somente após soltar o botão pressionado TRISD = 0x00; } int teclado_getec(){ // Faz a busca da tecla selecionada char tecla = 0, linha; for (linha = 0b00000001; linha<0b00010000; linha <<= 1){ { //linhas ativas fila1por = (linha & 0x0001)>>0; fila2por = (linha & 0x0002)>>1; fila3por = (linha & 0x0003)>>2; fila4por = (linha & 0x0004)>>3; delay(100); } if (colu1por) break; tecla++; if (colu2por) break; tecla++; if (colu3por) break; tecla++; } fila1por = 0; fila2por = 0; fila3por = 0; fila4por = 0; if(tecla!=tecla_antiga){ tecla_antiga = tecla; return matrizteclado[tecla]; } else return matrizteclado[ 0x0C ]; } void delay (unsigned int ms) { unsigned int i,j; for (i=0; i<=120; i++); // gera 1ms de atraso for (j=0; j<=ms; j++); // gera atraso } void D_7seg (char c){ LATD = 0x00; if ((c>'9')||(c<'0')) if (c=='*') LATD=0x77; // A else if (c=='#') LATD=0x76; // H else LATD = (0x00); else LATD = (x[c-'0']); }