Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

what wrong with my keypad code

Status
Not open for further replies.

eleceyes

New Member
Uhm ... my question is in my code, red part, thanks in advance !
Code:
   // MNEMONICS FOR BUTTONS
   // MNEMONICS FOR LCD
   #define   lcd_rs   PIN_B0
   #define   lcd_rw   PIN_B1
   #define   lcd_e   PIN_B2
   #define   lcd_cmd_wri   0x00
   #define   lcd_data_wri   0x01
   #define   lcd_busy_rd   0x02
   #define   lcd_set_function   0x38   //8 bit interface, 2 line mode, 5x7 dot format
   #define   lcd_set_visible   0x0F   //Display on, cursor underline on, cursor blink on
   #define   lcd_set_shift   0x16   //Cursor move, Right shift
   #define   lcd_set_mode   0x06   //Increment, display shift off
   #define   lcd_set_cgaddr   0x40
   #define   lcd_set_ddaddr   0x80
   #define   lcd_clr   0x01
   #define   lcd_init   0x30
   #define   NULL   0
   #include "D:\Pic16F877\MyProject\Doit\MainDoit\Doit.h"


void LcdEnable();
void WrCmd2Lcd(int cmd);
void WrDat2Lcd(int data);
void InitLcd();
void Speech(int *words,int position);
void LcdWait();
int OnRelease();
void main()
{
   //INTRO OR GUIDED WORDS
   int hello[]="HELLO WORLD! ";
   int keyPressed,key;
   int array[16]={0x31,0x34,0x37,0x3C,0x32,0x35,0x38,0x30,0x33,0x36,0x39,0x3E,0x43,0x26,0x45,0x4D};
   //PIC SET-UP PARAMETERS
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   // TODO: USER CODE!!
   set_tris_b(0x00);
   set_tris_c(0x00);
   InitLcd();
   delay_ms(1000);
   WrDat2Lcd(array[1]);
   key=2;
   WrDat2Lcd(array[key]);
   WrDat2Lcd('A');
   WrDat2Lcd('B');
   Speech(hello,lcd_set_ddaddr+0x40);
   play:
   keyPressed=OnRelease();
   if (keyPressed == -1)
   goto play;
   WrDat2Lcd(array[keyPressed]); 
[COLOR="Red"]// I press '1' button, a square dark appear, 
// but if I add WrDat2Lcd('any one char here') after WrDat2Lcd(array[keyPressed]);  
// then when I press '1' button, 
// a square dark and the next number '1' appear on screen, could anyone explain me this phenomenon ? Thanks !!![/COLOR]
   goto play;
}
//----------------------
void LcdEnable()
{
   output_high(lcd_e);
   output_low(lcd_e);
}
//----------------------
void WrCmd2Lcd(int cmd)
{
   set_tris_d(0x00);
   output_high(lcd_rs);
   output_low(lcd_rs);
   output_d(cmd);
   LcdEnable();
}
//----------------------
void WrDat2Lcd(int data)
{
   set_tris_d(0x00);
   output_high(lcd_rs);
   output_low(lcd_rs);
   output_d(data);
   LcdEnable();
   output_high(lcd_e);
}
//----------------------
void InitLcd()
{
   set_tris_d(0x00);
   output_low(lcd_rs);   //added
   output_low(lcd_rw);   //added
   output_low(lcd_e);
   delay_ms(125);
   WrCmd2Lcd(lcd_init);
   delay_ms(20);
   LcdEnable();
   delay_us(200);
   LcdEnable();
   delay_us(100);
   WrCmd2Lcd(lcd_set_function);
   delay_ms(10);
   WrCmd2Lcd(lcd_set_visible);
   delay_ms(10);
   WrCmd2Lcd(lcd_clr);
   delay_us(100);
   WrCmd2Lcd(lcd_set_ddaddr);
   delay_us(100);
}
//-------------------------
void Speech(int *words,int position)
{
   int *textptr;
   textptr = words;
   WrCmd2Lcd(position);
   do
   {
   WrDat2Lcd(*textptr);
   *textptr++;
   }
   while(*textptr != '\0');
}
//----------------------
void LcdWait()
{
   int status;
   set_tris_d(0xFF);
   output_high(lcd_rw);
   do
   {
      status=input_d();
      LcdEnable();
   }
   while(status & 0x80);   // test busy flag.
}
//-------------------------
int OnRelease()
{
   int i,j,k;
   int value[4]={0x07,0x0B,0x0D,0x0E};
   set_tris_a(0x0f);
   set_tris_d(0xf0);
   k=0;
   for(i=0;i<4;i++)
   {
      output_d(value[i]);
      delay_us(100);
      for(j=0;j<4;j++)
      {
         if(!input(40+j))
         {
            do
            {
               delay_ms(400);
            }
            while(!input(40+j));
            return k;
         }
         else
            k++;
      }
   }
   return -1;
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top