View previous topic :: View next topic |
Author |
Message |
ibsumith
Joined: 24 Jul 2009 Posts: 21
|
Help Interrupt from PC |
Posted: Sun Aug 02, 2009 11:39 pm |
|
|
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
|
|
Posted: Mon Aug 03, 2009 3:06 am |
|
|
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
|
|
Posted: Mon Aug 03, 2009 5:08 am |
|
|
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
else {LED_OFF(LEDR);} |
|
|
Ttelmah Guest
|
|
Posted: Mon Aug 03, 2009 9:58 am |
|
|
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
|
|
Posted: Mon Aug 03, 2009 1:54 pm |
|
|
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: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Aug 04, 2009 12:11 pm |
|
|
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
|
thank you |
Posted: Tue Aug 11, 2009 1:21 am |
|
|
Thank you for your reply.... |
|
|
ibsumith
Joined: 24 Jul 2009 Posts: 21
|
|
Posted: Tue Aug 11, 2009 1:25 am |
|
|
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: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Aug 11, 2009 9:40 pm |
|
|
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
|
|
Posted: Tue Aug 11, 2009 10:41 pm |
|
|
thank you.....I wants to know hw can i impliment interrupt transfer using CCs c |
|
|
|