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 support@ccsinfo.com

problem in 4*4 matrix keyboard interfacing with pic18f4550

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



Joined: 01 Oct 2013
Posts: 5

View user's profile Send private message

problem in 4*4 matrix keyboard interfacing with pic18f4550
PostPosted: Tue Oct 01, 2013 4:50 am     Reply with quote

Code:
#include "ex_usb_common.h"

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

///////////////////////////////////////////////////////////////////////////////
//#use delay(clock=20000000)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)//RS232
///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
#if __USB_PIC_PERIF__
 #if defined(__PCM__)
  #include <pic_usb.h>   //Microchip PIC16C765 hardware layer for usb.c
 #elif defined(__PCH__)
  #include <pic18_usb.h>   //Microchip PIC18Fxx5x hardware layer for usb.c
 #elif defined(__PCD__)
  #include <pic24_usb.h>   //Microchip PIC18Fxx5x hardware layer for usb.c
 #endif
#else
 #include <usbn960x.h>   //National 960x hardware layer for usb.c
#endif
#include "usb_desc_keyboard.h"    //USB Configuration and Device descriptors for this UBS device
#include "usb.c"        //handles usb setup tokens and get descriptor reports

///////////////////////////////////////////////////

#define row1_low output_low(PIN_B0)//ROWS
#define row1_high output_high(PIN_B0)//ROWS

#define row2_low output_low(PIN_B1)
#define row2_high output_high(PIN_B1)

#define row3_high  output_high(PIN_B2)
#define row3_low  output_low(PIN_B2)

#define row4_high  output_high(PIN_B3)
#define row4_low   output_low(PIN_B3)

#define read_col1 input(PIN_B4) // COLUMN
#define read_col2 input(PIN_B5)
#define read_col3 input(PIN_B6)
#define read_col4 input(PIN_B7)

int8 value;
int8 key;
char key_value;
void read_key1(void);
void read_key2(void);
void read_key3(void);
void read_key4(void);

void usb_keyboard_task(void);

void usb_debug_task(void)
{
   static int8 last_connected;
   static int8 last_enumerated;
   int8 new_connected;
   int8 new_enumerated;

   new_connected=usb_attached();
   new_enumerated=usb_enumerated();

   if (new_connected)
      LED_ON(LED2);
   else
      LED_OFF(LED2);

   if (new_enumerated)
      LED_ON(LED3);
   else
      LED_OFF(LED3);

   if (new_connected && !last_connected)
      printf("\r\n\nUSB connected, waiting for enumaration...");
   if (!new_connected && last_connected)
      printf("\r\n\nUSB disconnected, waiting for connection...");
   if (new_enumerated && !last_enumerated)
      printf("\r\n\nUSB enumerated by PC/HOST");
   if (!new_enumerated && last_enumerated)
      printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");

   last_connected=new_connected;
   last_enumerated=new_enumerated;
}

///////////////////////////////////////////////////

void main()
{
  key=0;
  key_value=0;
  printf("\r\n welcome");
 
   HW_INIT();

//   LED_ON(LED1);
 //  LED_OFF(LED2);
//   LED_OFF(LED3);

   printf("\r\n\nCCS HID Keyboard Demo");
   printf("\r\nCharacters received over serial get sent as a HID keyboard");
   
  #ifdef __PCH__
   printf("\r\nPCH: v");
   printf(__PCH__);
  #elif defined(__PCD__)
   printf("\r\nPCD: v");
   printf(__PCD__);
  #else
   printf("\r\nPCM: v");
   printf(__PCM__);
  #endif

   usb_init_cs();

  #if !(__USB_PIC_PERIF__)
   printf("\r\nUSBN: 0x%X", usbn_get_version());
  #endif
   printf("\r\n");
   
 
   while(1)
   {
     unsigned int8 tx_msg[7];
     usb_task();
     usb_debug_task();
     
     delay_ms(20);
   
     if (usb_enumerated())
        {
           if (usb_tbe(1))
           {
              read_key1();                 
              memset(tx_msg, 0x00, sizeof(tx_msg));
              tx_msg[2] = key_value;         
           }     
         
           usb_put_packet(1, tx_msg, sizeof(tx_msg), USB_DTS_TOGGLE);
        }
       
    key_value = 0;       
       
    }
}


//-------------------------- READ MUX KEY PAD ----------------------------------

void read_key1(void)
{
   row1_high;
   row2_high;
   row3_high;
   row4_high;
   
  /////////////////////
 
   row1_low;
   row2_high;
   row3_high;
   row4_high;
   delay_ms(5);
   key=input_b();
   key=(key & 0xF0);
 
   if(key==0xE0)
     {
      key_value=0x09;
      output_low(PIN_C1);
      goto exit_key; 
     }
    if(key==0xD0)
     {
      key_value=0x05;
        goto exit_key; 
     }
   if(key==0xB0)
     {
      key_value=0x24;
         goto exit_key; 
     }
    if(key==0x70)
     {
      key_value=0x20;
         goto exit_key; 
     } [/b]

   row1_high;
   row2_low;
   row3_high;
   row4_high;
   delay_ms(5);
   key=input_b();
   key=(key & 0xF0);
 

   if(key==0xE0)
     {
      key_value=0x08;

         goto exit_key; 
     }
   if(key==0xD0)
     {
      key_value=0x04;
       goto exit_key;   
     }
   if(key==0xB0)
     {
      key_value=0x23;
         goto exit_key;
     }
  if(key==0x70)
     {
      key_value=0x1F;
         goto exit_key; 
 }
 
   row1_high;
   row2_high;
   row3_low;
   row4_high;
   delay_ms(5);
   key=input_b();
   key=(key & 0xF0);
 
   if(key==0xE0)
     {
      key_value=0x07;
         goto exit_key; 
     }
   if(key==0xD0)
     {
      key_value=0x26;
       goto exit_key;   
     }
   if(key==0xB0)
     {
      key_value=0x22;
         goto exit_key;
     }
  if(key==0x70)
     {
      key_value=0x1E;
         goto exit_key; 
     }
     
   row1_high;
   row2_high;
   row3_high;
   row4_low;
   delay_ms(5);
   key=input_b();
   key=(key & 0xF0);
 
   if(key==0xE0)
     {
      key_value=0x06;
         goto exit_key; 
     }
   if(key==0xD0)
     {
      key_value=0x25;
       goto exit_key;   
     }
   if(key==0xB0)
     {
      key_value=0x21;
         goto exit_key;
     }
  if(key==0x70)
     {
      key_value=0x27;
         goto exit_key; 
     }     
     
     exit_key:;
delay_ms(20);
}

This is my 4*4 matrix keypad code. I want USB output. This code is partially working. First row is not working.
If i run same code for rs232 output by changing only display function its working properly. But same code when i integrate for USB then its creating problem.

please help me...

thnx
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Oct 01, 2013 5:29 am     Reply with quote

debugging101...

Since you say the KPD code works using RS-232 then hardware and KPD driver should be OK.
Since row1 doesn't work, look for something 'common' or used by both KPD row1 code and the USB drivers.
I suspect the USB code test LEDs( LED2, LED3). One might be connected to B0. I can't check the code( on wrong computer), but that's the first place I'd look.

hth
jay
PRITEE NIKAM



Joined: 01 Oct 2013
Posts: 5

View user's profile Send private message

PostPosted: Tue Oct 01, 2013 9:55 pm     Reply with quote

I have checked all port pins but here I'm facing different problem..
when i write routine for row1 first in main program, row1 is not working. If i interchange other row routine (row2 or row3 or row4) with row1 then that particular row is not working...
I guess there is problem in my usb code but I'm not getting it exactly...
PRITEE NIKAM



Joined: 01 Oct 2013
Posts: 5

View user's profile Send private message

PostPosted: Wed Oct 02, 2013 1:44 am     Reply with quote

in this code i want to send 3 key_values(hid codes) at one keypress..
eg.- if i press one key it should send hid code of a,b,c (0x04,0x05,0x06)

how can i send???

please help me
i need it urgently.
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