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

usb keyboard

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



Joined: 05 May 2013
Posts: 3
Location: Turkey

View user's profile Send private message

usb keyboard
PostPosted: Sun May 05, 2013 4:01 pm     Reply with quote

Hi.
I'm working on usb keyboard with 18f2550.
I can send computer any character. And I want to control 'caps lock' buton on or off. But I dont know how I can..

Code:

   #include <18F2550.h>
   #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
   #use delay(clock=48000000)
   
   //leds ordered from bottom to top
   #DEFINE LED1 PIN_A5  //green
   #define LED2 PIN_B4  //yellow
   #define LED3 PIN_B5  //red
   #define LED_ON(x) output_high(x)
   #define LED_OFF(x) output_low(x)

   //see section below labeled USB_CABLE_IS_ATTACHED
   //#define PIN_USB_SENSE   PIN_B2

   //#define HW_ADC_CONFIG   ADC_CLOCK_INTERNAL
   //#define HW_ADC_CHANNEL  0
   //#define HW_ADC_PORTS    AN0


#include <pic18_usb.h>   //Microchip PIC18Fxx5x hardware layer for usb.c
 
#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

/////////////////////////////////////////////////////////////////////////////
//
// usb_debug_task()
//
// When called periodically, displays debugging information over serial
// to display enumeration and connection states.  Also lights LED2 and LED3
// based upon enumeration and connection status.
//
/////////////////////////////////////////////////////////////////////////////
 

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();

 

   last_connected=new_connected;
   last_enumerated=new_enumerated;
}


void dongu(void)
   {
         static int8 tx_msg[7]={0,0,0,0,0,0,0};
         static int8 leds;
   
         tx_msg[2]=0;
         usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
         delay_ms(5);
         
         tx_msg[2]=30;
         usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
         delay_ms(5);
         
         
   }





void usb_keyboard_task(void)
{
   static int8 tx_msg[7]={0,0,0,0,0,0,0};
   static int8 leds;
   int a=0;

   if (input(PIN_A4))
   {
   
     
     
      int i;
      for (i=0;i<10;i++)
     
      {
         if(input(PIN_A4))
      {
     
      tx_msg[2]=26;
      delay_ms(20);
     
      usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);  //w   
           
      tx_msg[2]=21;
      delay_ms(60);
     
      usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE); //r
     
      tx_msg[2]=0;
      delay_ms(60);
           
      usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);//boş
     
       tx_msg[2]=21;
      delay_ms(20);
     
      usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE); //r
           
     
     tx_msg[2]=31;
      delay_ms(20);
     
      usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);//2
     
      }

         }

       dongu();       
         
     
   }
   
 
   
   
   
   
   if (!input(PIN_A4))
   {
   
   tx_msg[2]=0;
   delay_ms(10);
     usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
      delay_ms(100);
   
   }
   
   
   //receive NUM LOCK, CAPS LOCK, etc LED status from PC.
   //we won't do anything with it.
   if (usb_kbhit(1))
   {
      usb_get_packet(1, &leds, 1);
   }
}

void main(void)
{
   //HW_INIT();

       
 

   usb_init_cs();

 

   while (TRUE)
   {
      usb_task();
      usb_debug_task();

      if (usb_enumerated())
      {
         usb_keyboard_task();
         delay_ms(10);
      }
   }
}



Anybody can help me ? Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Mon May 06, 2013 12:15 am     Reply with quote

Look at ex_usb_keyboard.c, which has the full list of values for this byte. 0x39, is caps lock on.

Best Wishes
aztech



Joined: 05 May 2013
Posts: 3
Location: Turkey

View user's profile Send private message

PostPosted: Mon May 06, 2013 2:11 am     Reply with quote

Ttelmah wrote:
Look at ex_usb_keyboard.c, which has the full list of values for this byte. 0x39, is caps lock on.

Best Wishes



Telmah thanks your answers. I studied ex_usb_keyboard.c but I couldn't .

I think I have tou use this code;

Code:
 //receive NUM LOCK, CAPS LOCK, etc LED status from PC.
   //we won't do anything with it.
   if (usb_kbhit(1))
   {
      usb_get_packet(1, &leds, 1);
   }


But I'm beginner usb and ccs protocol.

I want to 'if caps lock on' my pic sent only " b" character . Can you write this code ?

thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Mon May 06, 2013 4:07 am     Reply with quote

The caps lock key, is a key, just like any other.
It's scancode, is 0x39. When it is pressed, you just send this scancode to the PC. Nothing more is needed.

The LED's are a separate thing. When the caps lock key is pressed, the PC sets the bit in the LED field, corresponding to 'CAPS LOCK ON'. So you can read the LED value sent back by the PC if you require, and set a suitable LED on your keyboard. However you don't have to do anything if your unit does not want to. This is the approach used by the demo code. The bits in the LED field are:
BIT0 = NUM LOCK
BIT1 = CAPS LOCK
BIT2 = SCROLL LOCK

So if you want an LED on your keyboard to reflect that 'caps lock is on', you then need to test bit1 in the leds value returned.

Best Wishes
aztech



Joined: 05 May 2013
Posts: 3
Location: Turkey

View user's profile Send private message

PostPosted: Mon May 06, 2013 5:37 am     Reply with quote

Ttelmah wrote:
The caps lock key, is a key, just like any other.
It's scancode, is 0x39. When it is pressed, you just send this scancode to the PC. Nothing more is needed.

The LED's are a separate thing. When the caps lock key is pressed, the PC sets the bit in the LED field, corresponding to 'CAPS LOCK ON'. So you can read the LED value sent back by the PC if you require, and set a suitable LED on your keyboard. However you don't have to do anything if your unit does not want to. This is the approach used by the demo code. The bits in the LED field are:
BIT0 = NUM LOCK
BIT1 = CAPS LOCK
BIT2 = SCROLL LOCK

So if you want an LED on your keyboard to reflect that 'caps lock is on', you then need to test bit1 in the leds value returned.

Best Wishes




thank you very much.
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