Code check (sorry)


Closed Thread
Results 1 to 6 of 6
  1. #1
    barkerben's Avatar
    barkerben Guest

    Default Code check (sorry)

    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

  2. #2
    barkerben's Avatar
    barkerben Guest


    Did you find this post helpful? Yes | No

    Default Desparation...

    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.

  3. #3
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    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 .......

    Code:
    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

  4. #4
    barkerben's Avatar
    barkerben Guest


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Lightbulb

    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

  6. #6
    barkerben's Avatar
    barkerben Guest


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. Nokia COLOR LCD PicBasicPro 2.50a example code
    By skimask in forum Code Examples
    Replies: 49
    Last Post: - 28th September 2011, 01:43
  2. can't find include file!
    By jimbostlawrence in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 10th November 2009, 08:44
  3. Manchester coding/decoding Code help
    By financecatalyst in forum Code Examples
    Replies: 0
    Last Post: - 25th August 2009, 19:05
  4. Code to control 4 LEDs and 2 servos
    By The Master in forum Off Topic
    Replies: 2
    Last Post: - 26th November 2008, 13:33
  5. changing code from 16F to 18F microprocessor
    By SCC_699 in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 14th May 2008, 13:27

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts