|
|
View previous topic :: View next topic |
Author |
Message |
KT315
Joined: 27 Oct 2009 Posts: 19
|
[pic18f2550] Setting timer interrupts & USB HID |
Posted: Tue Oct 27, 2009 5:46 am |
|
|
Hi!
I'm new to CCS and I cant sort out how to use timer interrupt.
I'm using pic18f2550 chip with 20MHz crystal resonator to create an usb hid device.
Now I want something to happen each second. So, the way to do this is using Timer0 Interrupt.
I've chosen divider 128 that should give about 152 timer events per second.
I'm using this code:
Code: |
#include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock = 20000000)
#define SET_TICKS_PER_ACTION 152
unsigned int8 tmrTmr=0;
#int_timer0
void clock_isr()
{
tmrTmr++;
if (tmrTmr >= SET_TICKS_PER_ACTION)
{
tmrTmr=0;
// One second passed, do something
}
}
void main (void)
{
setup_timer_0(RTCC_EXT_H_TO_L | RTCC_DIV_128 | RTCC_8_BIT);
set_timer0(0);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
usb_init_cs();
while (1)
{
usb_task();
if (usb_enumerated())
{
// some hid events
}
}
}
|
But there's no action, that should be every second. Where am I mistaken?
Thanks!
Or maybe there's a good way to work with timer by polling method?
Last edited by KT315 on Tue Oct 27, 2009 8:26 am; edited 1 time in total |
|
|
Ttelmah Guest
|
|
Posted: Tue Oct 27, 2009 6:27 am |
|
|
The problem is your first selection in the RTCC setup.
RTCC_EXT_H_TO_L
This says to clock the RTCC, from it's external clock input pin T0CKI (Timer 0 clock input). Unless you have a clock on this pin, the RTCC will never count....
Change this for 'RTCC_INTERNAL'.
Best Wishes |
|
|
KT315
Joined: 27 Oct 2009 Posts: 19
|
|
Posted: Tue Oct 27, 2009 8:46 am |
|
|
oh, thanks for timers!
Now I've got another lil problem. I'm trying to reproduce keyboard&mouse2 example.
I've got a composite hid descriptor, its first report is used for emulating keyboard. (This descriptor works with other language usage.)
It is composed with modifiers byte and buttons byte. To emulate this kbd I need to send 3 bytes - report id, modifier and button code.
To send messages I use my own polling function. When I want to send a single character, I set kbdSend flag on and define kbdBuffer[1] and kbdBuffer[2], and then call the function.
Code: | unsigned int8 kbdBuffer[3] = {1, 0, 0};
unsigned int8 kbdSend=0;
void pollHid()
{
if (!usb_enumerated()) return;
if (kbdSend == 1)
{
// keyboard array is already defined
usb_put_packet(1,kbdBuffer,3,USB_DTS_TOGGLE);
kbdSend = 15;
}
else if (kbdSend == 15)
{
kbdBuffer[1] = 0;
kbdBuffer[2] = 0;
usb_put_packet(1,kbdBuffer,3,USB_DTS_TOGGLE);
kbdSend = 0;
}
}
|
There's current usage:
Code: | while (1)
{
usb_task();
kbdBuffer[0] = 1;
kbdBuffer[1] = 0;
kbdBuffer[2] = 4;
kbdSend=1;
pollHid();
delay_ms(10);
usb_task();
pollHid();
delay_ms(10);
usb_task();
delay_ms(1000);
} |
I think that this code sould send little 'a' character (code 4 in hid tables), send ternination nil and then wait for a second.
But I get repeating 'a' character. My program can't send data {1,0,0}?? |
|
|
Ttelmah Guest
|
|
Posted: Tue Oct 27, 2009 9:26 am |
|
|
To 'send' a single key, you need to do two things.
Send the three bytes you have already identified (saying that the key is pressed), and then send the 'key released' sequence.
You are sending the 'key pressed' sequence, but never sending the key released sequence, so you will see the character repeating at the programmed keyboard repeat rate.....
Best Wishes |
|
|
KT315
Joined: 27 Oct 2009 Posts: 19
|
|
Posted: Tue Oct 27, 2009 9:31 am |
|
|
I'm calling pollHid, it sends defined key and sets kbdSend flag into 15. The next call of pollHid should send {1,0,0} and set kbdSend flag into 0. |
|
|
|
|
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
|