![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Grkan13
Joined: 06 Apr 2011 Posts: 14
|
PIC18f4550 and USB interrupt |
Posted: Mon Apr 30, 2012 1:10 pm |
|
|
Hello,
I'm trying to control a level mechanism via USB and buttons. My system is working great with buttons. There are 10 buttons to go each level. And I want to control the system from computer too.
When you press a button, its firing an interrupt from B port. I want to use USB interrupt on same way. Is it possible?
My interrupt code for buttons is at below
Code: |
#int_RB
void RB_isr()
{
disable_interrupts(INT_RB);
delay_ms(10);
if(input_State(pin_b4)==0)
{
go=1;
button_pressed=1;
set_adc_channel(0);
delay_ms(50);
level_adc = read_adc();
if(level_adc<=50)
{
button_10_pressed=1;
level=10;
}
else if(level_adc<=200)
{
button_9_pressed=1;
level=9;
}
else if(level_adc<=300)
{
button_8_pressed=1;
level=8;
}
else if(level_adc<=400)
{
button_7_pressed=1;
level=7;
}
else if(level_adc<=500)
{
button_6_pressed=1;
level=6;
}
else if(level_adc<=600)
{
button_5_pressed=1;
level=5;;
}
else if(level_adc<=700)
{
button_4_pressed=1;
level=4;
}
else if(level_adc<=800)
{
button_3_pressed=1;
level=3;
}
else if(level_adc<=900)
{
button_2_pressed=1;
level=2;
}
else if(level_adc<=1200)
{
button_1_pressed=1;
level=1;
}
}
enable_interrupts(INT_RB);
} |
so what i want is, when i get the data from usb, an interrupt should be fired.
now i'm using below driver files.
Code: |
#include <pic18_usb.h>
#include <USB_configuration.h>
#include <usb.c>
|
It's my 1st USB try. I'm not so sure about these driver files. I've found these ones from one of the open-source project. Do I have to use all of these ones?
When I've checked those driver files. pic18_usb.h has those lines:
Code: | #int_usb
void usb_isr() {
if (usb_state==USB_STATE_DETACHED) return; //should never happen, though
if (UIR) {
debug_usb(debug_putc,"\r\n\n[%X] ",UIR);
if (UIR_ACTV && UIE_ACTV) {usb_isr_activity();} //activity detected. (only enable after sleep)
if (UCON_SUSPND) return;
if (UIR_UERR && UIE_UERR) {usb_isr_uerr();} //error has been detected
if (UIR_URST && UIE_URST) {usb_isr_rst();} //usb reset has been detected
if (UIR_IDLE && UIE_IDLE) {usb_isr_uidle();} //idle time, we can go to sleep
if (UIR_SOF && UIE_SOF) {usb_isr_sof();}
if (UIR_STALL && UIE_STALL) {usb_isr_stall();} //a stall handshake was sent
if (UIR_TRN && UIE_TRN) {
usb_isr_tok_dne();
UIR_TRN=0; // clear the token done interrupt., 0x190.3
} //a token has been detected (majority of isrs)
}
}
|
If I add the int_USB to the my main file. does it make any problem?
Thank you. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19365
|
|
Posted: Mon Apr 30, 2012 2:08 pm |
|
|
You are looking the wrong way at this.
USB, is transferring 'data' all the time when a device is connected. There are configuration packets, polling packets, etc. etc.. You cannot use the USB interrupt to find out if data you want has arrived.
You need to _poll_ for data being available, and let the usb code handle the interrupts for you. Simplest to just use the usb_cdc drivers, and just call usb_kbhit, which will return 'true' when data has arrived.
Also, your serial code is foul. First, there is no point in disabling the INT_RB in the interrupt handler, and second you are delaying in an interrupt, which potentially means missing characters. If you did this in anything associated with USB, you _would_ lose connection.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Grkan13
Joined: 06 Apr 2011 Posts: 14
|
|
Posted: Wed May 02, 2012 8:33 am |
|
|
Thank you Ttelmah, I understand what you mean about USB, now I'm checking it whenever I need but I have a problem at one section.
Code: |
while(input_state(s7)==1)
{
output_high(level5);
delay_ms(200);
output_low(level5);
delay_ms(200);
if(button_pressed==1)
{
adc_button();
output_low(level5);
optic_middle();
if(button_5_pressed==1)
{
optic_up();
go=0;
goto end;
}
else
goto start;
}
if (usb_kbhit(1))
{
usb_get_packet(1, received_packet, 4);
if(received_packet[1]==0b00100000)
{
optic_up();
go=0;
goto end;
}
}
} |
At above part, software is waiting for 3 response; sensor, button or usb. and while waiting it needs to blink LED. When I try to get data from USB, its not working. But when I spam the button from PC or when I remove the blinking delays, its working fine. How can i solve this?
By the way thanks for INT_RB warnings. I've removed the a-d conversion out of interrupt, So there is no delay inside of isr now. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|