PIC18F2550 communication over USB with PC

Microcontroller Topics
Post Reply
Ibanitescu
Corporal
Corporal
Posts: 7
Joined: Mon Jun 28, 2010 10:25 pm

PIC18F2550 communication over USB with PC

Post by Ibanitescu » Wed Aug 04, 2010 1:06 pm

Hi,

I am working at one project that commands two current channels (coupling and uncoupling a channel) both from computer (via USB) and from board using two pushbuttons that change the status of each channel.
I want to use 3 interrupts: on data received from USB, on pressing button1 INT0 and on pressing the button2 INT1.
I have read the article http://www.beyondlogic.org/interrupts/interupt.htm about interrupts.
What I am asking is that I will have three interrupt service requests that will be put in Interrupt Vector table at some certain addresses and I don''t know how to make it.
I have worked with Intel80552 --- PCmon and there I declared a funcion with no parameters and after then the word interrupt i (range of i= 1:16, because Intel 80552 can handle interrupts from 16 sources: UART, ADC,TIMER, etc).
I saw some codes with the macro: #pragma that puts an interrupt function at some address, but I don't know how to deal with. I want to write the code for MCU in MikroC (not assembly language, but C), if you know other integrated development environment please tell me. So please tell me how should be the look of the functions.


The second question will be: I want to use USB module of PIC18F2550. I have read in the datasheet:
When the PIC18F4550 is used for USB connectivity, it must have either a 6 MHz or 48 MHz clock for USB operation, depending on whether Low-Speed or Full Speed mode is being used.
. Well this microcontroller has a 48MHz cuartz crystal integrated, inside. I want to ask something: I know that CLK resource is a critical one (only one task can access this resource at a certain time). Do I need an external crystal?

The final question will be:
I have done the initialization of ports INPUTS/OUTPUTS and Interrupts, if I have to add some instruction please tell me: the code is below:

Code: Select all

void port_init(void)
	{
		//0-out,1-in
		TRISA&= ~31;  //00011111=31
		TRISB|=  3;   //00000011= 3
		PORTA=   0;   //00010000= 0		
		//000LLLMM;     L=LED,M=MOS		
		//LED3/LED2/LED1/MOS2/MOS1
	}


void INT0_init(void)
	{
		INTCONbits.INT0E=1;
		INTCONbits.INT0F=0;   //1 = The INT0 external interrupt occurred (must be cleared in software)
	}


void INT1_init(void)
	{	
		INTCON3bits.INT1E=1;
		INTCON3bits.INT1F=0;  //1 = The INT1 external interrupt occurred (must be cleared in software)
	}
	

void USB_init(void)
	{
	PIE2bits.USBIE = 1; //USB Interrupt Enable bit
	UCFGbits.FSEN  = 1; //Full Speed Enable bit
	UCFGbits.UPUEN = 1; //USB On-Chip Pull-up Enable
	UCFGbits.UTRDIS= 0; // On-Chip Transceiver Disable bit
	UCONbits.USBEN = 1; //USB Module Enable bit
	}

void init(void)
	{
		port_init();
		INT0_init();
		INT1_init();
		USB_init();	
	}
   
   
void main(void)
	{
		bit ch1=0;
		bit ch2=0;
		init();
.............................................
                }

User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

Re: PIC18F2550 communication over USB with PC

Post by Shane » Wed Aug 11, 2010 11:23 am

First question was about having multiple interrupts.
Interrupts can be easily handled by means of reserved word interrupt. mikroC PRO for PIC implicitly declares function interrupt which cannot be redeclared. Its prototype is:

Code: Select all

void interrupt(void);
For P18 low priority interrupts reserved word is interrupt_low:

Code: Select all

void interrupt_low(void);

Code: Select all

void interrupt() {
  if (INTCON.TMR0IF) {
    counter++;
    TMR0 = 96;
    INTCON.TMR0F = 0;

    // interrupts service routine's for TIMER0 interrupt functions go here
  }
  else if (INTCON.RBIF) {
    counter++;
    TMR0 = 96;
    INTCON.RBIF = 0;

    // interrupts service routine's for TIMER0 interrupt functions go here
  }
}
See more at http://www.mikroe.com/esupport/index.ph ... 4&nav=0,11

Good example of interrupts using MikroC:

Code: Select all

//************************************************************************
//
//  PIC16F887
//  xtal = 20MHz
//
//  EasyPIC 5 board settings
//  J2 set to pull down
//  J17 set to VCC
//  SW2 pin1 RB0 switch on (up position)
//  SW6 pin4 PORTD LED switch on (to the right)
//
//  EasyPIC 6 board settings
//  J2 set to pull down
//  J17 set to VCC
//  SW2 RB0 switch on (up position)
//  SW9 PORTD LED switch on (to the right)
//
//  This short program is a demo of external interrupt usage
//  there is also debounce to control spurious switch bounce
//  from causing the LEDs to flicker too quickly due to switch bounce
//
//  Pressing button RB0 will result in a logic Hi at RB0/INT which will cause
//  an interrupt, then the condition of RB0/INT is tested to see that it is
//  being pressed for more than 100ms before being accepted as a real button
//  press.
//
//  option register is set to operate on rising edge.
//
//************************************************************************


void interrupt(void){

    if (INTCON.INTF == 1)
       {
        if (Button(&PORTB, 0, 100, 1)){  // introduce debounce
                                         // RB0 100ms active Hi
           PORTD = ~PORTD;               // invert PORTD
           }// end button if
        INTCON.INTF = 0;                 // Clear flag everytime inside INTF
       }//end intcon.intf if
}//end ISR

void main() {

  ANSEL  = 0;
  ANSELH = 0;
  C1ON_bit = 0;
  C2ON_bit = 0;

  OPTION_REG = 0xFF; // default value
  
    TRISD = 0;       // PORTD as output
    PORTD = 0x55;    // Set PORTD to 0101 0101 pattern
    TRISB = 0xFF;    // PORTB as input

    INTCON.INTF = 0;  // Clear interrupt flag prior to enable
    INTCON.INTE = 1;  // enable external interrupts
    INTCON.GIE  = 1;  // enable Global interrupts

do{

   Delay_ms(10); // just an empty loop
                 // to keep code running
   }while(1);
}// main
Preferred Development environment: MPLab wth HI-TECH C.

2. Would be better to use an external oscillator.
You can use a 20MHz crystal, to clock the chip at 48MHz. The USB 'clock source', always requires 4MHz. The 'PLL5' entry, says 'take the external oscillator, and divide this by 5, to get the required 4MHz'. Hence 20MHz must be used. This frequency is then multiplied internally by 24, to generate 96Mhz. This is then fed to the USB circuitry. Then the HSPLL setting says that the CPU should be fed from this source (rather than directly from the crystal), and the CPUDIV1 setting says to take this and divide it by 2 (totally confusing, but the same bit pattern is used to divide by 1, when feeding from the external clock, rather than the PLL).

So you can use a 20MHz crystal, and the timings set to run at 48MHz.

3. Code looks correct. However I have no way to test it here.
Ibanitescu
Corporal
Corporal
Posts: 7
Joined: Mon Jun 28, 2010 10:25 pm

Re: PIC18F2550 communication over USB with PC

Post by Ibanitescu » Wed Aug 11, 2010 3:42 pm

Thank you very much.

I have another question: have anyone of you done a project with a PIC with USB driver incorporated and if yes, I want to use a virtual com port driver and I wish if someone can tell me more about usb to rs232 emulation and drivers and where I can find this drivers.
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

Re: PIC18F2550 communication over USB with PC

Post by Shane » Thu Aug 12, 2010 4:19 am

Didn't use PIC+USB. We used a Virtual COM port driver for one of our USB <-> RS232 converters and you can find it here.

Have a look at this AVR USB project. These drivers will help you to get a good idea.

Another nice tutorial (again based on Atmel AVR).
Ibanitescu
Corporal
Corporal
Posts: 7
Joined: Mon Jun 28, 2010 10:25 pm

Re: PIC18F2550 communication over USB with PC

Post by Ibanitescu » Tue Aug 17, 2010 3:23 pm

Hi,

Thank you Shane for your response. I don't wan't a device mmore (FTDI -FT245, FT232). I have a PIC18F2550 with usb incccorporated and I want to see it as a COM in the PC Device Manager. I request some informations about this problem. Where I can find CDC drivers (virtual com port drivers) and some projects for examples.

Thanks in advance.
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

Re: PIC18F2550 communication over USB with PC

Post by Shane » Tue Aug 17, 2010 7:44 pm

In that case the first link I posted is the one for you. http://www.recursion.jp/avrcdc/
You can download the source code for CDC driver and change it as you need.

Hope that helps!
RAT16F88
Lieutenant
Lieutenant
Posts: 72
Joined: Sat Nov 20, 2010 12:36 pm
Location: Inside a PIC

Re: PIC18F2550 communication over USB with PC

Post by RAT16F88 » Sun Nov 21, 2010 12:22 am

THANKS every body :) :) :)
Post Reply

Return to “Microcontrollers”