CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

kbd.c and lcd.c at the same time on port B pic16F877

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gardiatech



Joined: 20 Sep 2011
Posts: 1

View user's profile Send private message

kbd.c and lcd.c at the same time on port B pic16F877
PostPosted: Tue Sep 20, 2011 8:30 pm     Reply with quote

hi

I'm new using ccs, it's great
but i have a problem and i need some expert help.

I'm trying to use port_b to drive lcd (4 bits data bus) and 4x3 keypad on rb interrupt, actually lcd it is working, but when a key is pressed, i use k=kbd_getc() inside rb_isr and i try to show on lcd and nothing happens.....

Searching i found a library to handle 4x4 keypad and i tried with this keypad library and works, it shows the key pressed on Proteus, but a logic collision happen......

circuit


This is the code using kbd.c
Code:

static char c;

unsigned char k;   //Variable global tecla

//#include <keypad.c>
#include <kbd.c>
#define LCD_ENABLE_PIN PIN_A0
#define LCD_RS_PIN PIN_A1
#define LCD_RW_PIN PIN_A2
#define LCD_DATA4       PIN_B0                                   
#define LCD_DATA5       PIN_B1                                   
#define LCD_DATA6       PIN_B2                                    ////
#define LCD_DATA7       PIN_B3
#define LCD_DATA_PORT 0x06
#define LCD_TYPE 2
#define LCD_TRIS_LOCATION 0x86
#include <lcd.c>
#define use_portb_kbd TRUE
#byte PORTB=0x06

void main()
{
   enable_interrupts(GLOBAL);
   
   port_b_pullups(TRUE);
   SET_TRIS_B( 0xF0 );
   #asm movf 0x06,0 #endasm //evita interrupcion a la entrada.
   clear_interrupt(int_RB);
   enable_interrupts(INT_RB);
   lcd_init();
   kbd_init();

   // TODO: USER CODE!!

   lcd_putc("Ready...");
   

 while(1)
 {

 }

}



//RUTINA DE INTERRUPCION DEL PUERTO B
#int_RB
void RB_isr()
{
   disable_interrupts(INT_RB);
   delay_ms (100);
   #asm movf 0x06,0 #endasm   //Sin esta instruccion no se pone a cero bandera de interrupcion
// Put your code here.
 
   k=kbd_getc();
   lcd_putc(k);

   output_bit( PIN_C0, 1);//this it's just for see in proteus counter
   output_bit( PIN_C0, 0);

   clear_interrupt(int_RB);
   enable_interrupts(INT_RB);
}


This is the lib that works on proteus but showing logic collision
Code:

///////////////////////////////////////////////////////////////////////////
////                             keypad.c                              ////
////                  Driver para teclado 4x4 generico                 ////
////                                                                   ////
////  init_keypad()   Debe llamarse desde el programa principal        ////
////                  antes de cualquier funcion                       ////
////                                                   ////
////  scan_keypad()   FunciĆ³n de escaneo de teclado segun             ////
////              la matriz de teclado                         ////
////                                                                   ////
////  read_keypad()     Encuentra la tecla por el metodo de inversion    ////
////              de puerto, espera a que se deje de pulsar y      ////
////              retorna.                                 ////
////                                                   ////
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
////                                                   ////
////         RB4 RB5 RB6 RB7                                 ////
////             ^   ^   ^   ^                                 ////
////             |   |   |   |                                 ////
////           |---|---|---|---|      El teclado se conecta sin      ////
////  RB0 ---> | 1 | 2 | 3 | A |      necesedad de resistencias ya   ////
////           |---|---|---|---|      que tiene las resistencias de  ////
////  RB1 ---> | 4 | 5 | 6 | B |      pull-up activadas             ////
////           |---|---|---|---|                              ////
////  RB1 ---> | 7 | 8 | 9 | C |                              ////
////           |---|---|---|---|                              ////
////  RB3 ---> | * | 0 | # | D |                              ////
////           |---|---|---|---|                              ////
////                                                   ////
///////////////////////////////////////////////////////////////////////////



//Direcciones de los registros segun PDF del pic 16fXXX

#byte PORTB=0x06   //Pic 16f877a
#byte TRISB=0x86   //

void init_keypad()   
{
   port_b_pullups (TRUE);    //Habilita pull-up internos del puerto B
   TRISB=0xF0;
   PORTB=0;   
   enable_interrupts(INT_RB);      // Habilita la interrupcion del puerto B ( RB4 - RB7).
   enable_interrupts(GLOBAL);      // Habilita las interrupciones globales.
}

unsigned char scan_keypad()   
{
   unsigned char i=0,j=0,PORT,PORTH,PORTL,PH,PL,key;

   //Matriz de teclado
   char keypad[4][4] =   { {'1','2','3','A'},
                       {'4','5','6','B'},
                      {'7','8','9','C'},
                      {'E','0','F','D'} };
   PORT=PORTB;
   PORTH=PORT&0xF0;
   PORTL=PORT&0x0F;
   
   for(j=0;j<4;j++)
   {
      PH=~(0b00010000<<j)&0xF0;
      if(PORTH == PH)
      {
         for(i=0;i<4;i++)
         {
            PL=~(0b00000001<<i)&0x0F;
            if(PORTL == PL)
            {
               key=keypad[i][j];
               while(PORT==PORTB);
            }
         }
      }     
   }
   if(key)
      return(key);   //Si la tecla es encontrada retorna valor segun la matriz de teclado
   else   
      return('/');   //Si la tecla no es encontrada retorna un caracter nulo
}


unsigned char read_keypad()
{   
   char read,key;

   read=PORTB;
 //  TRISB=0x0F;
   PORTB=read;
 //  TRISB=0xF0;
   key=scan_keypad();
   
   return(key);
}



Could somebody help me?

Thanks

Crying or Very sad Crying or Very sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 20, 2011 8:52 pm     Reply with quote

See this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=26229&start=2
It explains how to put both the lcd and keypad on PortB, by using the
CCS driver files, lcd.c and kbd.c.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group