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

Help Interrupt from PC

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



Joined: 24 Jul 2009
Posts: 21

View user's profile Send private message

Help Interrupt from PC
PostPosted: Sun Aug 02, 2009 11:39 pm     Reply with quote

Hi, I am Using a PIC18f2550 microcontroller and it is working fine. My problem is I want it to run only when an interrupt is received from the pc. That is, when pc wants to send something it should interrupt the currently executing portion of the code, and go into a function where pic code can accept the data from the pc. This is my code.
Code:

#include<18F2550.h>             //Microchip 18F2550 hardware layer

#define __USB_PIC_PERIF__ 1
#define LEDR PIN_B4            //Defining which all Pins U are Using             
#define LEDG PIN_B5
#define BUTTON PIN_C0

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

#build(reset=0x1, interrupt=0x8)          // Necessary for Bootloader
//#ORG 0x0F00,0x0FFF {}                   // Necessary for Bootloader
#use rs232(stream=PC, baud=9600, xmit=PIN_C6, rcv=PIN_C7)

//Tells the CCS PIC USB firmware to include HID handling code. 
#define USB_HID_DEVICE  TRUE              //Previously it was "DEFINE"

//the following defines needed for the CCS USB PIC driver to enable the TX endpoint 1   
// and allocate buffer space on the peripheral   
#define USB_EP1_TX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 for IN bulk/interrupt transfers   
#define USB_EP1_TX_SIZE    8                      //allocate 8 bytes in the hardware for transmission   
   
//the following defines needed for the CCS USB PIC driver to enable the RX endpoint 1   
// and allocate buffer space on the peripheral   
#define USB_EP1_RX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 for OUT bulk/interrupt transfers   
#define USB_EP1_RX_SIZE    8  //allocate 8 bytes in the hardware for reception   
 
// CCS USB Libraries
#include <pic18_usb.h>          //Microchip 18Fxx5x hardware layer for usb.c
#include <usb_desc_hid.h>       //USB Configuration and Device descriptors for this UBS device
#include <usb.c>                //handles usb setup tokens and get descriptor reports


#define LED_ON output_high 
#define LED_OFF output_low
//#define BUTTON_ON output_low
#define DELAY 1000

void main() {
   int8 out_data[2];   
   int8 in_data[2];   
//   int8 send_timer=0;
   usb_init();
   LED_OFF(LEDR);

    usb_task();                //Will call usb_attach().To attach USB Device to the bus.
   usb_wait_for_enumeration();
   if(usb_enumerated()){       //Checks if device Enumeration Successful or Not
     LED_ON(LEDR);
     delay_ms(DELAY);
     LED_OFF(LEDR);
    delay_ms(DELAY);   
    LED_ON(LEDR);   
     }
   else {LED_OFF(LEDR);}

  #if defined(AN0)                  //Enabling Port for Communication
   setup_adc_ports(AN0);
  #elif defined(AN0_AN1_AN3)
   setup_adc_ports(AN0_AN1_AN3);
  #else
   #error CONFIGURE ADC PORTS SO WE CAN READ CHANNEL 0
  #endif

   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);
   while(1)                //Consider this as executing code on chip wat modification i should do to make it to go to if(USB_GET_PACKET) .........
   {
      LED_ON(LEDR);
      delay_ms(100);
      LED_OFF(LEDR);
      delay_ms(100);
   }

   while (TRUE)
         {
       if(usb_kbhit(1))
         {
         if(usb_get_packet(1,in_data,2))
         {
         LED_OFF(LEDR);
         LED_ON(LEDG);
         delay_ms(500);
         }
         else {LED_OFF(LEDR);}
         out_data[0]=in_data[0];     //Output Buffer now contains values that are received
         out_data[1]=in_data[1];
         
       if(usb_put_packet(1,out_data,2, USB_DTS_TOGGLE))
         {
         LED_OFF(LEDG);
         LED_ON(LEDR);
         }
         else {LED_OFF(LEDG);}
        } 
      }
Ttelmah
Guest







PostPosted: Mon Aug 03, 2009 3:06 am     Reply with quote

Er.
Code:

void main() {
   int8 out_data[2];   
   int8 in_data[2];   
//   int8 send_timer=0;
   usb_init();
   LED_OFF(LEDR);

    usb_task();                //Will call usb_attach().To attach USB Device to the bus.
   usb_wait_for_enumeration();
   if(usb_enumerated()){       //Checks if device Enumeration Successful or Not
     LED_ON(LEDR);
     delay_ms(DELAY);
     LED_OFF(LEDR);
    delay_ms(DELAY);   
    LED_ON(LEDR);   
     }
   else {LED_OFF(LEDR);}


Study carefully.
How can the code ever get 'beyond' this?. How many opening, and closing brackets are there...

Best Wishes
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Aug 03, 2009 5:08 am     Reply with quote

Ttelmah wrote:
Er.
Code:

void main() {
   int8 out_data[2];   
   int8 in_data[2];   
//   int8 send_timer=0;
   usb_init();
   LED_OFF(LEDR);

    usb_task();                //Will call usb_attach().To attach USB Device to the bus.
   usb_wait_for_enumeration();
   if(usb_enumerated()){       //Checks if device Enumeration Successful or Not
     LED_ON(LEDR);
     delay_ms(DELAY);
     LED_OFF(LEDR);
    delay_ms(DELAY);   
    LED_ON(LEDR);   
     }
   else {LED_OFF(LEDR);}


Study carefully.
How can the code ever get 'beyond' this?. How many opening, and closing brackets are there...

Best Wishes


I think you missed the one before the L Embarassed
else {LED_OFF(LEDR);}
Ttelmah
Guest







PostPosted: Mon Aug 03, 2009 9:58 am     Reply with quote

Yes.
However it makes the point. Messy styling, makes it hard to read code. The harder code is to read, the more likelyhood there is for errors.
Tidy it up, and the odds are the actual error will be found.

Best Wishes
Guest








PostPosted: Mon Aug 03, 2009 1:54 pm     Reply with quote

I like to wire up the DTR line to INT0 (or some int pin) on the PIC. That way I can toggle the DTR line on the PC and immediately get the attention of the PIC.

Just a thought.

HTH - Steve H.
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Aug 04, 2009 12:11 pm     Reply with quote

Anonymous wrote:
I like to wire up the DTR line to INT0 (or some int pin) on the PIC. That way I can toggle the DTR line on the PC and immediately get the attention of the PIC.

Just a thought.



I think that defeats the purpose of being a USB device.

Look carefully at the datasheet for the 18F2550 on page 37.
You'll see you can turn off/on various clocks.

This is what you'll need to save power. (I'm assuming this is what you mean by "RUN" when an interrupt is received. Otherwise, it could just spin in a loop from main() )

You can't "shut down" the whole device because then it would be lost from the USB bus. I'm guessing there's a heartbeat pulse that needs to be serviced every now and then.

So put the core to sleep using the PRI_IDLE setup.

This will keep the USB clock running while the CPU goes sleepy. When the PIC receives a packet for it's attention, it will generate and interrupt (don't forget to turn them on) and then wake the processor.

I did this on another project with the 18F46J11 where the RTC wakes the CPU from PRI_IDLE to PRI_RUN.

Fun.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
ibsumith



Joined: 24 Jul 2009
Posts: 21

View user's profile Send private message

thank you
PostPosted: Tue Aug 11, 2009 1:21 am     Reply with quote

Thank you for your reply....
ibsumith



Joined: 24 Jul 2009
Posts: 21

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 1:25 am     Reply with quote

My code is working properly. Consider this... My device is running some application. Now I connect my device to pc. Now I want an isr which can communicate with pc so I can send and receive data. Which interrupt will help me detect device is connected to pc? Or which interrupt will help me to find out if there are some data to receive? Thank you.
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 9:40 pm     Reply with quote

In terms of plug-detection, you could use EXT_INT and trigger on rising edge.

If your device is USB powered entirely, then powerup would occur and reset would take the device through the normal USB enumeration steps anyway.

As for data transfer, that's all part of USB. Read the spec closely.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
ibsumith



Joined: 24 Jul 2009
Posts: 21

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 10:41 pm     Reply with quote

thank you.....I wants to know hw can i impliment interrupt transfer using CCs c
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