|
|
View previous topic :: View next topic |
Author |
Message |
magestik
Joined: 16 Oct 2008 Posts: 59
|
PIC18F4550 USB + RS232 |
Posted: Mon Feb 02, 2009 8:49 am |
|
|
Hello,
I would like to interface a PIC18F4550 as a HID keyboard, which I did succesfully.
The fuses are :
Code: |
#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8)
|
I have another chip connected to the PIC, which I (would like to) read via rs232, then send the result as a keyboard keypress.
Now here is the difficult part.
I can successfully read a chip (getc, which is PIN_C7), an send it (printf, which is PIN_C6), using these fuses :
Code: |
#fuses HS, WDT, NOPUT, NOPROTECT, LVP, NOWRT, NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8)
|
Which mean, no use of the USB !!!!
Since my other chip works with these fuses, there must be a problem with the USB fuses.
But if I use the PIC as a HID keyboard (using the fuses for USB), I can printf, but I don't receive anything from the other chip
The problem of communication between the PIC and the chip seems to come from the fuses. Maybe a speed problem. I don't know.
Could it be the "HSPLL" ?
Any idea would be greatly appreciated. |
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 02, 2009 10:27 am |
|
|
Your external clock is 20MHz.
This is divided by 5 to give the clock to feed the USB PLL (HSPLL, PLL5).
This multiplies the clock by 24, to generate the USB master clock at 96MHz.
This is then divided by two.
You then have 'CPUDIV1' selected, which means that your CPU, is now clocked from this clock (48MHz).
So, you need to change your #use delay statement, in the first example, to have the clock specified as 48Mhz, not 20Mhz.
Alternatively, just change the first fuse to 'HS', _but leave the PLL5 fuse in place_. This tells the system, to clock the USB, from it's PLL, based on the master clock/5, _but clock the CPU, directly from the external clock, rather than from the PLL_.
Look carefully at the clock diaram in the data sheet.
Best Wishes |
|
|
magestik
Joined: 16 Oct 2008 Posts: 59
|
|
Posted: Mon Feb 02, 2009 10:40 am |
|
|
This is what i tried in the meantime.
Now i use the fuse HS.
Still, my code is like :
Code: |
void main(void)
{
int8 tx_msg[7]={0,0,0,0,0,0,0,};
while(1)
{
printf("hello"); // hello is for example, i communicate with another chip
tx_msg[2] = getc();
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
}
}
|
Now, instead of this, if i comment "tx_msg[2] = getc();" and write "tx_msg[2] = 4", 'a' char is send via USB.
With the getc active, nothing on the USB line.
I hope i am clear enough. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Feb 02, 2009 12:48 pm |
|
|
I don't see any USB initialization in your example. I don't think, that this can be a working USB application. |
|
|
magestik
Joined: 16 Oct 2008 Posts: 59
|
|
Posted: Mon Feb 02, 2009 1:33 pm |
|
|
I didn't post the full program, as the USB communication is perfectly working, and since i use the usb_kbmouse example. i'll post the full code tomorrow if that can help ;) |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Feb 02, 2009 5:00 pm |
|
|
Quote: | I didn't post the full program, as the USB communication is perfectly working, and since i use the usb_kbmouse example. |
A minimal test application showing the problem would be appropriate. As an important difference, the said usb_kbmouse example does not pause USB communication while waiting for a user action. I guess, you should correct your application in this point. |
|
|
magestik
Joined: 16 Oct 2008 Posts: 59
|
|
Posted: Tue Feb 03, 2009 2:30 am |
|
|
I'm just starting with USB on PICs .
Here is my program :
Code: | #include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000) // 48000000
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//Tells the CCS PIC USB firmware to include HID handling code.
#define USB_HID_DEVICE TRUE
//the following defines needed for the CCS USB PIC driver to enable the TX endpoint 1
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE 8
#define USB_EP2_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP2 for IN bulk/interrupt transfers
#define USB_EP2_TX_SIZE 8
#define USB_EP2_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP2 for IN bulk/interrupt transfers
#define USB_EP2_RX_SIZE 4
#include <pic18_usb.h>
#include <usb_desc_kbmouse.h> //USB Configuration and Device descriptors for this UBS device
#include <usb.c> //handles usb setup tokens and get descriptor reports
int8 i=4;
/////////////////////////////////////////////////////////////////////////////
//
// usb_keyboard_task()
//
// Sends a packet of keyboard data. The protocol was specified in the HID
// report descriptor (see usb_desc_kbmouse.h), and is:
// tx_msg[0] = modifier (an 8bit bitmap of shift, tab, alt keypress)
// tx_msg[1] = const 0
// tx_msg[2:6] = an array of held down keys. a=4, b=5, etc.
// if msg[2:7]={0} then no keys are held down
//
// rx_msg[0] = 5bit bitmap of led status
//
/////////////////////////////////////////////////////////////////////////////
void usb_keyboard_task(void) // this is for testing, send 'a' to 'f' correctly
{
static int8 tx_msg[7]={0,0,0,0,0,0,0};
tx_msg[2]=i;
i++;
if(i>9) i=4; // abcdef
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
}
void main(void)
{
int8 i;
int8 tx_msg[7]={0,0,0,0,0,0,0,};
output_low(LED1);
delay_ms(100);
output_high(LED1);
usb_init_cs();
while(TRUE)
{
usb_task();
printf("hello"); // hello is for example, i communicate with another chip
tx_msg[2] = getc(); // receive answer from the other chip
if(usb_enumerated())
{
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
//usb_keyboard_task();
delay_ms(10);
}
}
} |
So as i said, commenting the "getc()" and using "usb_keyboard_task()", i send the letters 'a' to 'f' correctly.
Using "getc()" and usb_put_packet, the USB line doesn't work anymore.
@FvM : i do not know yet how to pause USB communication, but it seems to be my kind of problem.
Last edited by magestik on Tue Feb 03, 2009 7:44 am; edited 3 times in total |
|
|
magestik
Joined: 16 Oct 2008 Posts: 59
|
|
Posted: Tue Feb 03, 2009 7:42 am |
|
|
An other illustration if i may :
Code: | while(1)
{
usb_task();
printf("hello"); // hello is for example, i communicate with another chip
tx_msg[2] = 5;
if(usb_enumerated())
{
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
delay_ms(10);
}
tx_msg[2] = 6;
if(usb_enumerated())
{
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
delay_ms(10);
}
} |
The result on the computer is : "bcbcbcbcbcbcbc..."
Code: | while(1)
{
usb_task();
printf("hello"); // hello is for example, i communicate with another chip
tx_msg[2] = getc(); // receive answer from the other chip
if(usb_enumerated())
{
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
delay_ms(10);
}
tx_msg[2] = 6;
if(usb_enumerated())
{
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
delay_ms(10);
}
} |
The result is : "c".
Note that the first send, sent nothing. (The RX line is constantly receiving, without USB i manage to receive everything)
EDIT : Seems to work now, for whatever magical reason .
I just used :
Code: | toto = getc();
if(toto==0x68) // 0x68 or anything else
tx_msg[2] = 0x06; // send c
|
Sends "chchchchchchch" for now... i'll see in the next few days . |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Feb 03, 2009 12:36 pm |
|
|
Apart from the said problem of delaying the USB response by getch(), are you sure, that the received character codes are an allowed value for HID communication? |
|
|
mpardinho
Joined: 16 Jul 2008 Posts: 8 Location: Mexico
|
|
Posted: Sun Aug 15, 2010 10:14 am |
|
|
USB Keyboard ok
tx_msg[2] = 0x04; // send a
tx_msg[2] = 0x05; // send b
tx_msg[2] = 0x06; // send c .....
But not work than i send character "/" = 0x38, pic send character "รง".
my pc keyboard is caracter set (portugues-br) |
|
|
|
|
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
|