#include <16f877A.h>
#fuses XT, NOWDT
#use delay(clock=4M)




#include <lcd.c>
#include <kbd2.c>
const char num1[]={"1=#"};
const char num2[]={"ABC2"};
const char num3[]={"DEF3"};
const char num4[]={"GHI4"};
const char num5[]={"JKL5"};
const char num6[]={"MNÑO6"};
const char num7[]={"PQRS7"};
const char num8[]={"TUV8"};
const char num9[]={"WXYZ9"};
const char num0[]={" +.¿?"};



int8 i;
char c;
int8 posicion;
char tecla;
int8 scan;
void main()



{
   lcd_init();



 
lcd_putc("lcd ok");
delay_ms (1000);
lcd_putc("\f");
posicion=0;

inicio:

posicion++;
i=0;
captura:
do{
         c=kbd_getc();
        
        }       
     
   while (c==0);{
   tecla=c;
   
  numero:
 if (tecla =='1'){
      if (i>=3){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num1[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car1;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car1: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }      
  
  
  if (tecla =='2'){
      if (i>=4){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num2[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car2;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car2: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     
      
 
  if (tecla =='3'){
      if (i>=3){
      i=0;
      }
      lcd_gotoxy(posicion,1);
      lcd_putc(num3[i]);
      delay_ms (500);
      scan=0;
      for (scan=0;scan<=200;scan++)
      {
      
       delay_ms (5);
      c=kbd_getc();
     
      if (c!=0){
      goto car3;
      }
       
      
      }
        
      
      if (c==0){
      goto inicio;
      }
      
 car3:
      if (c==tecla){
      i++;
      goto numero;
      }    
      if (c!=tecla){
      i=0;
      tecla=c;
     posicion++;
     goto numero;
      }
      
      
      
}  

if (tecla =='4'){
      if (i>=4){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num4[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car4;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car4: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     
 if (tecla =='5'){
      if (i>=4){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num5[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car5;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car5: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     
if (tecla =='6'){
      if (i>=5){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num6[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car6;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car6: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     
if (tecla =='7'){
      if (i>=5){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num7[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car7;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car7: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     

if (tecla =='8'){
      if (i>=4){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num8[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car8;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car8: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     

if (tecla =='9'){
      if (i>=5){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num9[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car9;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car9: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     


if (tecla =='0'){
      if (i>=5){
      i=0;
      }
      
      lcd_gotoxy(posicion,1);
      lcd_putc(num0[i]);
      delay_ms (500);
     scan=0;
    
     for (scan=0;scan<=200;scan++)
      {
       delay_ms (5);
      c=kbd_getc();
     
       if (c!=0){
      goto car0;
      } 
     
      }  
      
      if (c==0){
      goto inicio;
      }
car0: 
      if (c==tecla){
      i++;
      goto numero;
      }
       if (c!=tecla){
      i=0;
       tecla=c;
      posicion++;
      goto numero;
      }
 }     
if (tecla=='*'){
      if (posicion==1){
      goto inicio;
      }
posicion--;
goto captura;
}









}







}
