New to programming and am having a few issues with some code


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Location
    Leicester UK
    Posts
    21

    Default New to programming and am having a few issues with some code

    Hi

    I'm having a few issues with some code I have written, it compiles with no errors but I am not getting the response that I expect.

    All of the outputs are being simulated with LEDs and are wired in with 470ohm resistors. I also have 5k ohm potentiometors for the target and current position inputs.

    When I first turn the ciruit on the direction LEDs come on but the power LED never turns on. However much I turn the current position potentiometer I can not seem to get the LEDs to turn on or off as expected.

    If anybody can help me with any errors with my code or can point me in the right direction to solve the issues I am getting.

    Many thanks in advance.
    My current code is below:

    DEFINE OSC 8

    'GP2 (AN2) IS FOR TARGET POSITION (INPUT)
    'GP4 DIRECTION SIGNAL CW (OUTPUT)
    'GP5 DIRECTION SIGNAL CCW (OUTPUT)
    'GP0 POWER (OUTPUT)
    'GP1 (AN1) CURRENT POSITION (INPUT)

    'REGISTERS
    OPTION_REG = %11000000
    INTCON = %11000000
    PIE1 = %01000000
    PIR1 = %00000000
    OSCCON = %01110001
    TRISIO = %00000111
    ANSEL = %01010110
    CCP1CON = %00000000

    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

    ;---------------------------------------------------------------------------
    wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
    ;wsave VAR BYTE $70 SYSTEM ' alternate save location for W
    ' if using $70, comment wsave1-3

    ' --- IF any of these three lines cause an error ?? ------------------------
    ' Comment them out to fix the problem ----
    ' -- Which variables are needed, depends on the Chip you are using --
    wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
    'wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
    'wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3
    ' --------------------------------------------------------------------------

    'OUTPUT PINS
    DIRCW VAR GPIO.4
    DIRCCW VAR GPIO.5
    PWMOUT VAR GPIO.0

    'VARIABLES
    TARGET VAR WORD bank0
    TARGET_L VAR Target.BYTE0
    TARGET_H VAR TARGET.byte1
    CURRENT VAR WORD bank0
    CURRENT_L VAR current.byte0
    CURRENT_H VAR current.byte1

    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler AD_INT, _CurrentPos, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM


    'READ TARGET POSITION FROM POT
    ADCON0 = %00001011 'TARGET POSITION READ
    REPEAT
    PAUSE 100
    UNTIL ADCON0.1 = 0

    asm
    MOVF ADRESL,W
    MOVWF _TARGET_L
    MOVF ADRESH,W
    MOVWF _TARGET_H
    endasm

    ADCON0 = %00000111 'CURRENT POSITION ADC SETUP

    'MAIN PROG
    MAIN:
    IF CURRENT=TARGET THEN
    goto motorbreak
    else
    if current > target then
    goto motorcw
    else
    goto motorccw
    endif
    endif
    GOTO MAIN

    'SUB PROGRAM
    MotorBreak:
    GPIO = %00110001
    return

    MotorCW:
    GPIO = %00010001
    Return

    MotorCCW:
    GPIO = %00100001
    Return

    CurrentPos:
    ASM
    MOVF ADRESL,W ;COPY LOW BYTE OF ADC TO W
    MOVWF _CURRENT_L ;COPY LOW BYTE OF ADC TO CURRENT POS FROM W
    MOVF ADRESH,W ;COPY HIGH BYTE OF ADC TO W
    MOVWF _CURRENT_H ;COPY HIGH BYTE OF ADC TO CURRENT POS FROM W
    ENDASM
    @ INT_RETURN ;RETURN

    END



    I am using a PIC12F683 microchip.

    Many thanks

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: New to programming and am having a few issues with some code

    those variant of
    Code:
    asm    
        MOVF    ADRESL,W
        MOVWF   _TARGET_L
        MOVF    ADRESH,W
        MOVWF   _TARGET_H
    endasm
    shoud be replaced with
    current.lowbyte = ADRESL
    Current.HighByte = ADRESH

    That's the first thing I see here on a lazy sunday while watching TV
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: New to programming and am having a few issues with some code

    Use GOSUB to go to subroutines.

    Robert

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: New to programming and am having a few issues with some code

    motion seconded... back to basics... and pillow
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: New to programming and am having a few issues with some code

    I don't see where you are re-reading the commanded position within the main loop. I only see where you are reading the feedback position in the interrupt routine... Maybe my eyes are going bad...
    Dave Purola,
    N8NTA
    EN82fn

  6. #6
    Join Date
    Apr 2012
    Location
    Leicester UK
    Posts
    21


    Did you find this post helpful? Yes | No

    Default Re: New to programming and am having a few issues with some code

    Thank you for the feedback so far.

    I am work at the moment so I can not impliment the suggestions until later today,I will give feedback on any results in due course.

    Dave - at the moment it only should read the target position at the start of program and only update the target position if the PIC is reset, I intend to write in a routine later in the programs evolution that will allow the target position to be updated while the program is running.

    Once again thank you for the help.

Members who have read this thread : 0

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