Having troubles with READ / WRITE confirming it works...
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    116

    Default Having troubles with READ / WRITE confirming it works...

    Hi all!

    I am using a 12f675 to control speed, direction, acceleration and deceleration of a dc motor. Speed and motor direction are both controlled by reading the position of a potentiometer. The potentiometer has a detent midway through it's range. This position is speed zero. Clockwise of speed zero increases motor speed clockwise. Counterclockwise of speed zero energizes a relay reversing polarity to the motor and increases speed in a counterclockwise direction.

    I am in the beginning of programming. The code below is meant to allow capturing the pot center position and storing it in a word variable in two eeprom locations. This happens by holding gpio.3 LOW for two seconds while power is applied to the circuit board. After two seconds, an A/D conversion captures the center position into a word variable "POT_CNTR". The word is stored in two eeprom locations, low byte and high byte.

    Just before the program passes to MAIN:, it reads the two eeprom locations back into word variable "POT_CNTR".

    Being the newbie programmer that I am, and having no LCD connected to the processor, I need a way to insure that I have, in fact, captured a good number into "POT_CNTR" before I move on. The programming below is meant to do that, audibly, by buzzing the directional relay and by using an LED on the board to provide visual feedback as I turn the pot left and right.

    But alas... it doesn't work! Ha!

    I provided some hysteresis around zero position (plus or minus 10 of "POT_CNTR") to provide some room around position pot zero. Counter clockwise of pot zero and the LED should turn on steady. Clockwise of pot zero and the LED should blink repeatedly. When the pot is located near center position, the LED should turn off.

    That's what I wanted it to do. Instead it just flashes repeatedly like it is always CW of pot center position (pot zero).

    It you were bored enough to have read all of this, I would appreciate any help you may have for me!

    Thank you!!!

    Ross


    Code:
        Define OSCCAL_1K 1  ' Calibrate internal oscillator (4mhz)
      
        TRISIO = %00111100  ' GPIO.2,3,4 and 5 INPUTS, GPIO.0 and 1 OUTPUTS
        ADCON0 = %10001001  ' Turn on A/D module, Enable AN2, R Justify Result
        ANSEL  = %00100100  ' Fosc /32, Set AN2 analog, the rest digital
        CMCON = 7           ' Analog comparators off
        WPU = %00110000     ' Turn on weak pullups on GPIO.4 and 5
       
        DEFINE ADC_BITS 10          ' Set number of bits in result
        DEFINE ADC_CLOCK 2          ' Set clock source (32 Tosc)
        DEFINE ADC_SAMPLEUS 50      ' Set sampling time in uS
        
    '*******************************************************************************
    'VARIABLES:
        
        MOTOR VAR GPIO.0        'MOTOR ON PIN 2, START WITH IT OFF
        LOW MOTOR
        LED VAR MOTOR
        
        RELAY var GPIO.1        'RELAY DETERMINES MOTOR DIRECTION, PIN 3
        HIGH RELAY              'START WITH RELAY DE-ENERGIZED
        
        BUTN VAR GPIO.3          'START/STOP SWITCH (ALSO REMOTE FOOT SWITCH), PIN 5
                                'BUTTON IS ACTIVE LOW (WHEN PUSHED)
                                
        POT_CNTR VAR WORD       'THIS VALUE TO BE STORED IN EEPROM DURING 
                                'INITILIZATION ROUTINE.
                                    
        I var byte              'VARIABLES USED FOR GENERAL COUNTERS. INIT AS ZERO.
        I = 0
        J VAR BYTE
        J = 0                   
    
        SPEED_NEW VAR WORD      'VARIABLE TO STORE A/D VALUE
        SPEED_OLD VAR WORD      'VARIABLE TO STORE PREVIOUS A/D VALUE
        
        DIR VAR BIT             'VARIABLE TO DETERMINE CW OR CCW REVOLUTIONS
        
        RUNN VAR bit            'RUN / STOP BIT. MACHINE COMES UP IN STOP MODE
        runn = 0
       
        PAUSE 500               '1/2 SECOND PAUSE BEFORE CONTINUING
    
    '******************************************************************************
    
    'INITIALIZATION ROUTINE... POT IS TURNED HALF-WAY TO MIDDLE POSITION AND 
    'START/STOP SWITCH (BUTTON SW) IS PUSHED AND HELD WHILE MACHINE POWER IS TURNED
    'ON. AFTER TWO SECONDS, MACHINE WILL BUZZ TO INDICATE IT CAPTURED POT CENTER  
    'POSITION AND LOADED IT INTO EEPROM LOCATIONS 0 AND 1 (LOW AND HIGH BYTES) OF
    'WORD VARIABLE.   
        
    INITIALIZE:
        WHILE BUTN = 0          'CHECK FOR BUTTON PRESS
        IF I = 100 THEN
        ADCIN GPIO.2, POT_CNTR
        WRITE 0, POT_CNTR.BYTE0
        WRITE 1, POT_CNTR.BYTE1
        GOSUB BUZZ
        I = 0
        WHILE BUTN = 0
        PAUSE 20
        WEND
        ENDIF
        PAUSE 20
        I = I + 1
        WEND
    
    '******************************************************************************
    'THE POT ONLY NEEDS TO BE INITIATED ONCE AS THE EEPROM SHOULD HOLD THAT VALUE
    'AFTER POWER IS REMOVED. THE FOLLOWING LINES READ THE LOWER AND UPPER BYTES
    'FROM THE EEPROM BACK INTO THE VARIABLE POT_CNTR (IN CASE INITIALIZATION WAS
    'NOT PERFORMED AT POWER-UP).    
    
        READ 0, POT_CNTR.BYTE0
        READ 1, POT_CNTR.BYTE1
        
    '******************************************************************************    
    
    MAIN:
        GOSUB GET_POT
        GOSUB CHECK_POT
        GOTO MAIN
        END
    
    BUZZ:
        for J = 1 to 10
        toggle relay         'This put in to buzz relay. Programming feedback
        pause 20
        next J
        return
    
    GET_POT:                    'READ POT POSITION AND PUT IT IN VARIABLE SPEED_NEW
        ADCIN GPIO.2, SPEED_NEW
        RETURN
       
    'TURNING THE SPEED POT CCW FROM CENTER POSITION CAUSES LED (ON MOTOR CIRCUIT) TO
    'COME ON. TURNING THE SPEED POT CW FROM CENTER POSITION CAUSES LED TO BLINK.
    'LED IS OFF WHEN THE POT IS NEAR CENTER POSITION
    
    CHECK_POT:
        IF (POT_CNTR - 10) <= SPEED_NEW <= (pOT_CNTR + 10) THEN
        LOW LED
        ENDIF
        IF SPEED_NEW < (POT_CNTR - 10) THEN
        HIGH LED
        ENDIF
        IF SPEED_NEW > (POT_CNTR + 10) THEN
        TOGGLE LED
        PAUSE 50
        ENDIF
        RETURN
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,686

    Default Re: Having troubles with READ / WRITE confirming it works...

    IF (POT_CNTR - 10) <= SPEED_NEW <= (pOT_CNTR + 10) THEN
    did you test out what this does ? i can't predict the result with any certainty

    the led may never be low its always on or toggling

    my take
    Code:
    CHECK_POT:
        if   abs( SPEED_NEW - POT_CNTR )< 10  then ;new speed is less than 10 either side of centre
            LOW LED      
        else       ;new speed is more than 10 either side of centre
            IF SPEED_NEW > POT_CNTR  then  ;new speed is  clockwise of centre
                HIGH LED
            else          ;new speed is  anticlockwise of centre
                TOGGLE LED
            ENDIF
        ENDIF
        PAUSE 50
    RETURN
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    116

    Default Re: Having troubles with READ / WRITE confirming it works...

    Thanks for your reply Richard,

    So...I can't make the PWM output no matter what I try. I'm not an electronics engineer. I'm mechanical. I fake it with electronics. Without a scope, I can't see what the input to the FET looks like but I bet it's garbage. I can turn the FET on solid with "HIGH GPIO.0" but I can't output PWM GPIO.0 125,200 and get it to work. So I need to play with this before I get back to programming.

    Thank you again!

    Ross
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,231

    Default Re: Having troubles with READ / WRITE confirming it works...

    Quote Originally Posted by rossfree View Post
    Thanks for your reply Richard,

    So...I can't make the PWM output no matter what I try. I'm not an electronics engineer. I'm mechanical. I fake it with electronics. Without a scope, I can't see what the input to the FET looks like but I bet it's garbage. I can turn the FET on solid with "HIGH GPIO.0" but I can't output PWM GPIO.0 125,200 and get it to work. So I need to play with this before I get back to programming.

    Thank you again!

    Ross
    Your "PAUSE 50" may be breaking your PWM.
    Just have a test routine like:
    Code:
    Main:
        PWM GPIO.0,125,200
    Goto Main
    nothing else in between...

    And see if it works.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. 16F690 read/write
    By louislouis in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 17th December 2016, 09:21
  2. PLEASE HELP...read write issues
    By nomad77 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 17th March 2011, 14:39
  3. read/write eeprom
    By mel4853 in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 1st March 2010, 01:17
  4. write -read problem?
    By turkuaz in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th August 2009, 13:06
  5. Write/Read DS1996
    By Bosse in forum Forum Requests
    Replies: 2
    Last Post: - 9th September 2008, 00:25

Members who have read this thread : 20

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