PDA

View Full Version : Interesting delay problem



Kreston
- 1st September 2008, 13:22
i'm writing a code for control three leds from serial port. so i started with only one led at first.

here is the codes :

@ DEVICE PIC16F628A
@ DEVICE PIC16F628A, WDT_OFF
@ DEVICE pic16F628A, PWRT_ON
@ DEVICE PIC16F628A, PROTECT_OFF
@ DEVICE PIC16F628A, MCLR_OFF
@ DEVICE PIC16F628A, BOD_OFF
@ DEVICE PIC16F628A, INTRC_OSC_NOCLKOUT

OPTION_REG = %10000000
INTCON = %11000000
PIE1 = %00100000
TRISA=%00000000

ON INTERRUPT GoTO kestirici
CMCON=7

define HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
define HSER_BAUD 4800

led var byte[3]
par var byte[3]

par[0] = 128

basla

for led[0] = 0 to 255
if led[0] < par[0] then
high porta.0
else
low porta.0
endif
next

goto basla

disable
kestirici:
HSERIN [wait("t"),dec2 par[0]]
Resume
enable

When i started this project.I was using pauesus to create duty cycles.But that was making a phase difference problem.So i searched,searched... for another methods to drive leds. Then i found timer interrupt.That was using timer for delays so there wasnt any phase difference problem. So i decided to use timer interrupt.

But there was a problem again :) When i was trying to pwm one led with timer interrupt, that was fine. i got about 2000hertz. But if i try to pwm three leds. The frequency falls down about 60 hertz. (and i'm not even using hserin for all this codes yet).

Whatever. 60 Hertz is not bad. But how was i'm going to use hserin with that codes? i tried to insert the hserin command to main loop. Didn't worked.Frequency falls down.Tried with timeout 1ms and 0ms. Frequency falls down again.

After that, i decided to write timer interrupt codes to main loop and change the timer interrupt with serial interrupt.

So here is my codes i get about 100hertz right now with one led. WHY? That should be about 2000 hertz like in the timer interrupt. Is that about if-statements? if i use less if statement, frequency increases.

It is just one led. When i make it three. Frequency is about 30hz. That sucks.

If you have another idea for control three leds(brightness) from serial port with about 100hz. I'm ready for do it. I'm tired. Trying to do this thing for 2 weeks.

Jerson
- 1st September 2008, 14:14
Dont be disheartened. It sure looks like you have the general idea to make it work. Now, what you can try to do is to take interrupts on Receive and Timer.

In the Timer ISR, you can take care of the LEDs. In the Receive ISR, you grab the serial port character and either buffer it for the mainline code to see it (circular buffer) or signal it to the mainline if the data rate is very slow. This will help you decide on the Led states.

You will definitely get better results with this method. If possible, try to keep the ISR code as short as possible and if you can do it, use assember in there. You got to try; may seem difficult at first, but once done, you'll wonder why you didnt do it before.

Kreston
- 1st September 2008, 14:41
Thanks for quick answer. But how can use timer interrupt and serial interrupt together? Can you write a short sample code for that?
and what do you think about huge delaying when three leds is being driven by the timer interrupt or main loop. As i said, its 2000Hz with one led.But 60hz with three leds. That's too much i guess.

i have more questions :) I'm going to send one byte for each led, 30 times in a second.
do you think baud rate is ok for this?

one more :) i don't know so much about buffer. Can you explain it?