PDA

View Full Version : Code check (sorry)



barkerben
- 29th November 2004, 22:18
Hi,
I'm trying to control a camera mount I've built as part of my final year project at Uni. However, I'm quite concerned about this code I've written - especially the section that deals with sending and receiving serial data.

It is used to control a stepper motor driver chip, and the idea is that it receives commands over the serial link, which calls an interrupt. To make sure the interrupt is serviced before a buffer overun, I'm using the hardware timer and timer interrupt rather than pause commands. The serial link can issue commands such as speed, direction, number of steps, and can request motor position. This is returned as the number of steps the motor has taken, which is tracked in a variable.

My code is at:

http://www.srcf.ucam.org/~dbrb2/code/

I realise it is unlikely anyone will be able to give any advice, but it would be very welcome!

Cheers,


Ben

barkerben
- 30th November 2004, 03:15
This was initially supposed to be a minor part of my project, but has become a major part! The serial section of the code is the most worrying - in theory the code I have written should work as follows:

Interrupt on serial arrival
Receive 6 bytes or timeout trying
If first byte is not equal to PIC ID, ignore
If first byte is equal to PIC ID, change behaviour of PIC accordingly


Does the code make ny sense at all? Speifically the serial reception section?

Yours desperately,

Ben
Meng Student
Cambridge Engineering Dept.

Ingvar
- 30th November 2004, 11:26
Hi Ben,

I only took a quick peek at your code, my findings were...

You should have all of your interruptcode between the DISABLE-ENABLE. The interrupthandler should end with RESUME. My suggestion is that you change it to .......



DISABLE 'Disable interrupts for duration of service routine
service: 'Interrupt service routine
IF PIR1.5=1 THEN 'If PIR1.5 is high, then we have
'a serial data interrupt
PIR1.5=0
HSERIN 10,timeout,[bin ID,bin MODE,bin dist,bin SPEED,bin DIR,bin REQUEST]
'We are expecting 6 bytes input.
ENDIF
timeout:
IF INTCON.1=1 THEN 'If INTCON.1 is high, then this
'is a timer0 interrupt
INTCON.1=0
timer_counter=timer_counter+1
ENDIF
RESUME
ENABLE


Perhaps this is enough to get you going.

/Ingvar

barkerben
- 30th November 2004, 11:58
Thank - I thought every IF statement had to be followed by a
lable to jumpt to, rather than just the commands to be executed. Is this incorrect?

Cheers,

Ben

Ingvar
- 30th November 2004, 13:40
The older PBC could only handle a label after the IF-THEN statement.

PBP can handle more situations.

1. A label, same as PBC.
IF x=y THEN Label

2. Multiple(or single) statements
IF x=y THEN
z=x+y
q=w-e
r=z+q
ENDIF

3. Multiple(or single) statements with else
IF x=y THEN
z=x+y
q=w-e
r=z+q
ELSE
z=x-y
q=w+e
r=z*q
ENDIF

For some reason the manual states that this .....

IF x=y THEN z=x+y

.... is illegal, but it works just fine. It compiles exactly the same as ....

IF x=y THEN
z=x+y
ENDIF

If you're a gambler you could use the first(i do), if you want to be safe you should use the latter.

/Ingvar

barkerben
- 30th November 2004, 16:54
Thanks - that will make the code far more readable than having to jump between labels!

Does anyone havge any experience of using serial interrupts?

I hope that using the timer interrupt rather than pause in my code should mean I can service the serial interrupt in time to avoid overflow, if I use a low baud rate. Does anyone have any experience with this?


Cheers,


Ben