USB PIC18f2550 microcontroller with other interrupt problem

Thread Starter

Hassan Abu al rub

Joined Feb 10, 2019
11
pic18f2550

here in this code i have problem using USB with other timer interrupt

i need to test code with USB
i have internal interrupt with timer 1 every one one second toggle led

and external interrupt T0CK

just small problem every thing working fine but "" usb_interrupt_proc(); ""

make external interrupt fast
and timer not correct

code working without usb or usb disable -- disconnect usb -- timer will be correct

who to make two interrupt separate
my code timer include usb other thing mikroc give error
________________________________________________________________________________________________
void interrupt()
{
usb_interrupt_proc();

if (TMR1IF_bit){
TMR1IF_bit = 0;
onescound++;
if(onesnd==4){onesnd=0; LATB.F5=~LATB.F5 }


TMR1H = 0x0B; // 250 ms
TMR1L = 0xDC;

}
if (TMR0IF_bit){
TMR0IF_BIT=0;
counter++;
TMR0L=255;

}

}
 
Last edited by a moderator:

geekoftheweek

Joined Oct 6, 2013
607
The problem is your timer interrupt happens when the program is in the USB interrupt programming. You will either have to move the usb to the main loop and check for it there or set up interrupt priorities and have the timer be high priority and the USB low priority.
 

Thread Starter

Hassan Abu al rub

Joined Feb 10, 2019
11
The problem is your timer interrupt happens when the program is in the USB interrupt programming. You will either have to move the usb to the main loop and check for it there or set up interrupt priorities and have the timer be high priority and the USB low priority.
is any example
what about
usb_interrupt_proc();???
 

Thread Starter

Hassan Abu al rub

Joined Feb 10, 2019
11
The problem is your timer interrupt happens when the program is in the USB interrupt programming. You will either have to move the usb to the main loop and check for it there or set up interrupt priorities and have the timer be high priority and the USB low priority.
we try this but not working



void USBhigh() iv 0x0008 ics ICS_AUTO {

usb_interrupt_proc();
}

void timerlow () iv 0x0018 ics ICS_AUTO {


if (TMR1IF_bit){
TMR1IF_bit = 0;
onescound++;
if(onesnd==4){onesnd=0; LATB.F5=~LATB.F5 }


TMR1H = 0x0B; // 250 ms
TMR1L = 0xDC;

}
if (TMR0IF_bit){
TMR0IF_BIT=0;
counter++;
TMR0L=255;

}

}
 
Sort of...

The USB needs to be low priority and the timer needs to be high priority. The high priority will interrupt a low priority interrupt then return to finish the low priority one before returning to where it was interrupted from.

There are also interrupt priority bits (IPRx registers) to change. Usually the RCON register has an interrupt priority enable bit also.

The timer 0 interrupt will also affect your timer 1 interrupt. It will not be as noticeable as the USB interrupt, but over time it will make a difference. Make timer 1 the only high priority and everything else low priority.
 
Just found your 18F2550 in the original post and checked my datasheet.
RCON (section 4.1 in datasheet) - set IPEN to 1
INTCON2 (section 9.2) - set TMR0IP to 0.
IPR2 (section 9.5) - set USBIP to 0.
 

Thread Starter

Hassan Abu al rub

Joined Feb 10, 2019
11
Just found your 18F2550 in the original post and checked my datasheet.
RCON (section 4.1 in datasheet) - set IPEN to 1
INTCON2 (section 9.2) - set TMR0IP to 0.
IPR2 (section 9.5) - set USBIP to 0.

find attachment complete code with simillation
only timer 1 working usb and timer 0 no

IPEN to 1
TMR0IP to 0.
set USBIP to 0.


add it

Make timer 1 the only high priority and everything else low priority.

see code
____________________________________________________________________________


char read_buffer[64] absolute 0x500;
char write_buffer[64] absolute 0x540;

char cuenta=0;
char USB;
char onesecond=0;

void timer1High() iv 0x0008 ics ICS_AUTO {

if (TMR1IF_bit){
TMR1IF_bit = 0;
onesecond++;
if(onesecond==4){LATB.F5=~LATB.F5;onesecond=0;}

TMR1H = 0x0B;
TMR1L = 0xDC;

} }




void USBtim0low() iv 0x0018 ics ICS_AUTO {

usb_interrupt_proc();



if (TMR0IF_bit){
TMR0IF_BIT=0;
LATB.F4=~LATB.F4;
TMR0L=255;
}
}

void main() {
IPEN_bit=1;
USBIP_bit=0;

T1CON = 0x31;
TMR1IF_bit= 0;
TMR1H = 0x0B; // 250 ms
TMR1L = 0xDC;
TMR1IE_bit= 1;
INTCON = 0xC0;

TMR0IP_bit=0;
TMR0IE_bit=1;
t08bit_bit=1;
T0CS_bit=1;
T0SE_bit=1;
PSA_BIT=1;
GIE_BIT=1;

TMR0L=255;

LATB.f5 =0;
trisb.f5=0;

HID_Enable(&read_buffer,&write_buffer);
PWM1_Init(3000);
PWM1_Set_Duty(0);
PWM1_Start();

while(1)
{


USB = HID_Read();
if (USB != 0) {


if ( strstr(read_buffer,"start" )) {PWM1_Start(); PWM1_Set_Duty(250); }
else if(strstr(read_buffer,"stop")){ PWM1_Set_Duty(0); }

memset(read_buffer,0,30);

}

}
}
 

Attachments

@Hassan Abu al rub I normally use assembly with PICs so my C is a little weak in this area. One other thing I did notice is PEIE/GIEL will also need to be set in INTCON. I don't see it in your code above, but thought since you had USB interrupts already working it was set. Everything else looks right.

I can not open .rar files at this time and will have to update my system before I can install the software to decompress them. I will look at that later to see if anything else seems not right. It has been a few years since I did anything with the 18f2550... I'll keep trying to help.
 
Top