Página 1 de 1

Por que não funciona

MensagemEnviado: 08 Abr 2007 18:59
por KILB
Estou querendo fazer uma rotina que ao iniciar a mesma apareça em um Lcd de 2x16 "Teste de teclas" e ao apertar uma das tres teclas apareça "tecla 1"
ou "tecla 2" ou "tecla 3" dependendo obviamente da tecla que for pulsada.
Mas ao testa-lo no proteus, aparece "Teste de teclas" e em seguida o que deveria aparecer somente ao apertar uma das tres teclas, fica correndo no display feito loco.

Se puderem dar uma olhada para podermos discutir?

No aguardo

KILB

PS. Segue abaixo o fonte

#include "18f442.h"
#use delay(clock=4000000)
#include "lcd.c"
#use fast_io(A)
#use fast_io(B)
void tela_padrao(void);
void tela_1(void);
void tela_2(void);
void tela_3(void);

void main(void)
{
int estado_tecla;
int estado_tecla1;
int estado_tecla2;
//portb=0x00;
// portd=0x00;
lcd_init();
tela_padrao();
delay_ms(2000);

for(;;)
{
if (!input(pin_b0))estado_tecla = 1;
if (!input(pin_b0)&& (estado_tecla));
tela_1();
if (input(pin_b1))estado_tecla1 = 1;
if (!input(pin_b1)&& (estado_tecla1));
tela_2();
if (input(pin_b2)) estado_tecla2= 1;
if (!input(pin_b2)&& (estado_tecla2));
tela_3();
}
}

void tela_padrao()
{
lcd_putc("Teste de teclas");
}

void tela_1()
{
lcd_putc("Tecla 1");
}

void tela_2()
{
lcd_putc("Tecla 2");
}

void tela_3()
{
lcd_putc("Tecla 3");
}

:x :cry: :?:

MensagemEnviado: 08 Abr 2007 21:22
por KILB
Fiz algumas alterações e melhorou um pouquinho, mas mesmo assim o numero das teclas 1,2,e3 ainda ficam rolando e não atende às teclas.

Se tiverem alguma idéia do que é?


Valeu

KILB


#include "18f442.h"
#use delay(clock=4000000)
#include "lcd.c"
#use fast_io(B)
#use fast_io(D)
void tela_padrao(void);
void tela_1(void);
void tela_2(void);
void tela_3(void);

void main(void)
{
int estado_tecla;
int estado_tecla1;
int estado_tecla2;
//portb=0x00;
// portd=0x00;
lcd_init();
tela_padrao();
delay_ms(2000);

for(;;)
{
if (!input(pin_b0))estado_tecla = 1;
if (!input(pin_b0)&& (estado_tecla));
tela_1();
if (!input(pin_b1))estado_tecla1 = 1;
if (!input(pin_b1)&& (estado_tecla1));
tela_2();
if (!input(pin_b2)) estado_tecla2= 1;
if (!input(pin_b2)&& (estado_tecla2));
tela_3();
}
}

void tela_padrao()
{
lcd_putc("Teste de teclas\n");
}

void tela_1()
{
lcd_gotoxy(1,2);
lcd_putc("Tecla 1\n\b");
}

void tela_2()
{
lcd_gotoxy(1,2);
lcd_putc("Tecla 2\n\b");
}

void tela_3()
{
lcd_gotoxy(1,2);
lcd_putc("Tecla 3\n\b");
}

MensagemEnviado: 08 Abr 2007 22:41
por Renie
Olá Kilb,

Eu não uso C, mas, uma falha de programação é visível...

Vccê só define os valores para as variáveis estado_ teclaX e nunca muda!

Parece qye a idéia era um deboucing, sem alterar os valores após a apresentação das mensagens, não funciona!

Outra coisa é usar negação "!", os pinos estão ligados a pull-ups? pelo menos internos e aterrados ao pressionar as teclas?

MensagemEnviado: 08 Abr 2007 23:10
por lucaszampar
Tente:

Código: Selecionar todos
#include "18f442.h"
#use delay(clock=4000000)
#include "lcd.c"
#use fast_io(B)
#use fast_io(D)

void tela_padrao(void);
void tela_1(void);
void tela_2(void);
void tela_3(void);

void main(void)
{
int estado_tecla;
int estado_tecla1;
int estado_tecla2;

lcd_init();
tela_padrao();
delay_ms(2000);

for(;;)
{
if (!input(pin_b0)) estado_tecla = 1;
delay_ms(50);
if ((!input(pin_b0)) && (estado_tecla)) tela_1();

if (!input(pin_b1)) estado_tecla1 = 1;
delay_ms(50);
if ((!input(pin_b1)) && (estado_tecla1)) tela_2();

if (!input(pin_b2)) estado_tecla2 = 1;
delay_ms(50);
if ((!input(pin_b2)) && (estado_tecla2)) tela_2();

}
}

void tela_padrao()
{
lcd_putc("\fTeste de teclas\n");
}

void tela_1()
{
lcd_gotoxy(1,2);
lcd_putc("Tecla 1\n\b");
}

void tela_2()
{
lcd_gotoxy(1,2);
lcd_putc("Tecla 2\n\b");
}

void tela_3()
{
lcd_gotoxy(1,2);
lcd_putc("Tecla 3\n\b");
}


Existem melhores maneiras de otimizar a leitura de teclas... procure no fórum ou no google pela palavra-chava DEBOUNCE

MensagemEnviado: 09 Abr 2007 13:49
por KILB
Oooopa!!

Em primeiro lugar obrigado pelas respostas..., quebrando um pouco mais a cabeça consegui fazer funcionar do jeito que eu queria, agora vou dar continuidade ao brinquedo.

Valeu


KILB

PS: Segue abaixo o que eu consegui.também simulei no PROTEUS e funcionou legal.

#include "18f442.h"
#use delay(clock=4000000)
#include "lcd.c"
#use fast_io(B)
#use fast_io(D)
void tela_padrao(void);
void tela_1(void);
void tela_2(void);
void tela_3(void);

void main(void)
{

disable_interrupts(GLOBAL);


set_tris_b(0xff);
port_b_pullups(true);
lcd_init();
tela_padrao();
delay_ms(2000);

for(;;)
do {
if (!input(PIN_B0))
{
delay_ms(30);
tela_1();
}

if (!input(PIN_B1))
{
delay_ms(30);
tela_2();
}

if (!input(PIN_B2))
{
delay_ms(30);
tela_3();
}

}while(1);
}

void tela_padrao()
{
lcd_putc("Casa de Bombas\n");
}

void tela_1()
{
lcd_gotoxy(1,2);
lcd_putc("Bomba 1 - ON\n\b");
}

void tela_2()
{
lcd_gotoxy(1,2);
lcd_putc("Bomba 2 - ON\n\b");
}

void tela_3()
{
lcd_gotoxy(1,2);
lcd_putc("Bomba 3 - ON\n\b");
}