usb problem using 18f4550

General discussion on mikroC.
Author
Message
pronk
Posts: 18
Joined: 16 Jul 2009 01:02

usb problem using 18f4550

#1 Post by pronk » 19 Jul 2009 17:30

simply I need my computer to detect a usb connection in proteus

my sch as shown
Image

the crystal is 20Mhz and i setup usb drivers


my simple code is the example at the mikroc projects usb read and write

Code: Select all

unsigned char k;
unsigned char userWR_buffer[64], userRD_buffer[64];

const char *text = "MIKROElektronika Compilers ER \r\n";

//**************************************************************************************************
// Main Interrupt Routine
//**************************************************************************************************
void interrupt()
{
  HID_InterruptProc();

}
//**************************************************************************************************

//**************************************************************************************************
// Initialization Routine
//**************************************************************************************************

void Init_Main()
{
        //--------------------------------------
        // Disable all interrupts
        //--------------------------------------
        INTCON = 0;                             // Disable GIE, PEIE, TMR0IE,INT0IE,RBIE
        INTCON2 = 0xF5;
        INTCON3 = 0xC0;
        RCON.IPEN = 0;                          // Disable Priority Levels on interrupts
        PIE1 = 0;
        PIE2 = 0;
        PIR1 = 0;
        PIR2 = 0;

        ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
        CMCON  |= 7;                            // Disable comparators
        //--------------------------------------
        // Ports Configuration
        //--------------------------------------
        TRISA = 0xFF;
        TRISB = 0xFF;
        TRISC = 0xFF;
        TRISD = 0;
        TRISE = 0x07;

        LATA = 0;
        LATB = 0;
        LATC = 0;
        LATD = 0;
        LATE = 0;
        //--------------------------------------
        // Clear user RAM
        // Banks [00 .. 07] ( 8 x 256 = 2048 Bytes )
        //--------------------------------------

        
}
//**************************************************************************************************





//**************************************************************************************************
// Main Program Routine
//**************************************************************************************************

void main() {
  unsigned char i, ch;
  
  Init_Main();

  HID_Enable(&userRD_buffer, &userWR_buffer);
  Delay_ms(1000); Delay_ms(1000);

  while (1) {
    k = HID_Read();
    i = 0;
    while (i < k) {
      ch = userRD_buffer[i];
      userWR_buffer[0] = ch;
      while (!HID_Write(&userWR_buffer, 1)) ;
      i++;
      }

    }
  HID_Disable();
}

and i added the USBdsc.c file while creating hex file


Image



why proteus cannot detect a new hardware with this examples although it detects at any samples project in the proteus which have only their hex file ?

if there is a problem plz tell me :?:

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#2 Post by yt7pwr » 20 Jul 2009 07:31

Where is RESET circuit on pin 1?

pronk
Posts: 18
Joined: 16 Jul 2009 01:02

#3 Post by pronk » 20 Jul 2009 13:45

yt7pwr wrote:Where is RESET circuit on pin 1?
Thanks for your reply .The proteus program donnot need for reset circuit but i connect it to the pin no1 but it still no response . what do you think about that problem ?

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#4 Post by yt7pwr » 20 Jul 2009 13:57

Can you post simple *.hex file from Proteus example folder.Maybe format is diferent?

pronk
Posts: 18
Joined: 16 Jul 2009 01:02

#5 Post by pronk » 20 Jul 2009 14:07

yt7pwr wrote:Can you post simple *.hex file from Proteus example folder.Maybe format is diferent?
Of corse .hex file here
http://www.filefactory.com/file/ahe13f8 ... estP18_hex


yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#7 Post by yt7pwr » 21 Jul 2009 07:50

I load example files in PICFLASH from MicroE:
1:file: HIDtestP18_hex is for 20MHz crystal,96MHz/2 for CPU,no ICD....
2:file: MCHPUSB: 48MHz in divide by 12(?),96/2 for CPU(ok), postscale /4(?)...
3:file same as above..
There is big difference between 1 and 2,3 configuration.Is this any help to you? :oops:

pronk
Posts: 18
Joined: 16 Jul 2009 01:02

#8 Post by pronk » 22 Jul 2009 19:26

:lol: thank you yt7pwr the proteus now detect a usb and then i made the circuit on a board and now in practical my computer detects a new hardware but now I cannot sending or recieving a data
Image

what do you think i must do now?

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#9 Post by yt7pwr » 23 Jul 2009 07:53

Try HIDTerminal from MicroE for testing and HID echo example.

pronk
Posts: 18
Joined: 16 Jul 2009 01:02

#10 Post by pronk » 29 Aug 2009 02:52

yt7pwr wrote:Try HIDTerminal from MicroE for testing and HID echo example.
I tried hid terminal but there is nothing happen and now when i chang any thing in the code my pc say

Image

i need a help , i realy donot know my error :cry:


i tried to write my program in basic but this message is still appearing

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#11 Post by yt7pwr » 31 Aug 2009 07:48

Try example "Hid ReadWrite test" in example folder.
If that dont help try MicroC Pro compiler version.

pronk
Posts: 18
Joined: 16 Jul 2009 01:02

#12 Post by pronk » 01 Sep 2009 00:40

yt7pwr wrote:Try example "Hid ReadWrite test" in example folder.
If that dont help try MicroC Pro compiler version.
thank you alot for your reply
I have some questions
what exactly this function do ?

Code: Select all

HID_Write(&userWR_buffer, 1)

if it takes one byte from the buffer as random? or the first byte only userWR_buffer[0] ?

if i want to empty( userWR_buffer) what i should do for that ?

if my configuration above is true ?

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#13 Post by yt7pwr » 03 Sep 2009 08:59

Function

Code: Select all

HID_Write(&userWR_buffer, 1)
write 1 byte(first byte in buffer).There is two potential problem:
- if your Send/Receive buffer size are diferent than usbdsc.c file:

Code: Select all

unsigned char const HID_INPUT_REPORT_BYTES      = 1;
unsigned char const HID_OUTPUT_REPORT_BYTES     = 1;
- calling HID_Write(&userWR_buffer) without blocking call could hang PIC if you try to send more data before previous USB packet is send.

Try this code:
- in USBdsc.c

Code: Select all

unsigned char const HID_INPUT_REPORT_BYTES      = 64;
unsigned char const HID_OUTPUT_REPORT_BYTES     = 64;
-in main *.c

Code: Select all

unsigned char userWR_buffer[64], userRD_buffer[64];
and in while/do loop:

Code: Select all

while (!HID_Write(&userWR_buffer, 64)) ;
This way you should send 64byte in one packet with enough procesor power for more jobs(A/D,flash Read/Write......).
Hope this will help you :)

pronk
Posts: 18
Joined: 16 Jul 2009 01:02

#14 Post by pronk » 03 Sep 2009 16:36

Thank you alot yt7pwr.
I can now send and recieve data using hid terminal of mikroc .
now i tried to use a bootloader , i installed MCHPFSUSB
then
C:\Microchip Solutions\USB Device - Bootloaders\HID - Bootloader\
then i programed my pic to USB Device - HID - HID Bootloader - C18 - PIC18F4450.hex file and i runned HIDBootLoader.exe file but i still cannot program my pic from usb can you help me ?

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#15 Post by yt7pwr » 04 Sep 2009 07:52

This is problem for Microchip support forum,but I try to help:
- study code for bootloader and modify it to meet your hardware;
- this bootloader dont use interrupt routine so your program must have org directive.First add org 0x1000,write,compile and debug without bootloader(also modify 18F4550.mlk file).Then prepare *.hex file for download.
This is hard job(and require licence for Microchip C compiler) so I write my one bootloader in MikroC Pro(with shared interrupt).For "firmware" I'm using MikroC 8.2.
In near future I release code and will write some instruction for it but for the moment I'm very busy :(

Post Reply

Return to “mikroC General”