12F683 - basic code not working


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Hi again,

    I'm stumped on what's happening when I run the following code, and having gone over it time and time again I might be overlooking the obvious.

    I have a motor connected to the H-bridge module that arrived yesterday - making pins 1 high and 2 low makes the motor run one way and reversing which pin is high and low makes the motor run the opposite way. I'm using a separate 9v supply for the motor.

    When first turned on, the threshold set means that the code is set to night mode. I then shine a light on the LDR and the motor runs until I connect GPIO.5 to 5v to simulate the door closing the micro-switch at the end of its travel. So far so good. However, if I remove the connection between GPIO.5 and +5v to simulate the door in the close state GPIO.5 remains latched high. Grounding the pin momentarily has no affect, and disconnecting the H-bridge has no affect. Only way to clear this is to turn off and on the EasyPic5 board.

    Code:
    ASM
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF 
    endasm
    
    DEFINE ADC_BITS 10              ' ADC resolution
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    number VAR word                 ' Variable to store ADC result
    day var byte                    ' Variable to indicate day or night
    
    CMCON0=7
    CCP1CON=0
    ANSEL = %00000001               ' GPIO.0 A/D in, rest digital
    ADCON0.7 = 1                    ' Right justify for 10-bit
    GPIO = %00000000                ' Initialize outputs
    TRISIO = %101011                ' GPIO 0,1,3,5 = input, 2 & 4 outputs
    
    
    Motor1          var gpio.2      ' H-Bridge pin 1
    Motor2          var gpio.4      ' H-Bridge pin 2
    SW1             VAR gpio.5      ' Door open limit switch
    
    
    low sw1                         ' Ensure limit switch is 0 (door is closed)
    low Motor1                      ' Turn off H-Bridge pin 1
    low Motor2                      ' Turn off H-Bridge pin 2
    day=0                           ' Initialise unit to check if day time
    
    main:
    if sw1=0 then day = 0           ' If limit switch is open, and mode is night then
    adcin 0,number                  ' Read AN0
    if day=0 then                   ' Day 0 = night, Day 1 = Day
      If number >660 then           ' If ACD result is above a threshold
      low SW1                       ' Set state of limit switch to off
      high Motor1                   ' Turn on H-Bridge pin 1
      Low Motor2                    ' Turn off H-Bridge pin 2
    endif
    endif
    if SW1 = 1 then                 ' Door closes limit switch and applies 5v to GPIO.5
      low Motor1                    ' Turn off H-Bridge pin 1 }  Stop motor
      low Motor2                    ' Turn off H-Bridge pin 2 }
      day = 1                       ' Set variable to day to indicate the door is open
    endif
    
    goto main
    The logic behind this project is that when the light level increases at dawn past the threshold it drives the motor which by use of a threaded rod slides open the door on the coop. When the door closes a micro-switch at the end of the travel the power to the motor is turned off and the unit placed in day mode. At the end of the day a switch will be pressed directly applying 5v to pin2 of the H-Bridge thus reversing the motor, opening the door and opening the switch, changing its state from 1 to 0.

    Any ideas ??

    Malcolm

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Hi,
    First you configure GPIO.5 as an input by setting the TRISIO.5=1 - OK, that's fine since you're going to be monitoring a switch connected to that pin.
    Then you set up an alias so that SW1 accesses GPIO.5 - perfectly fine.
    Then you go and do LOW SW1 - which turns the GPIO.5 into an output and pulls it low - not OK, the pin is no longer an input so it can't read the state of the switch.

    Keep GPIO.5 as an input and make sure it has a pullup resistor to Vcc if the switch pulls it low or a pulldown resistor do Vss if the switch pulls it high.

    /Henrik.

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Cheers Henrik,

    I've removed all reference to SW1 =1 or LOW SW1 and it now no longer latches. - Thank you.

    Now to actually turn this into a working door

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code now working

    I've been playing about with the code and have now tested it on the development board over and over again, simulating the process of the door actuator being manually closed, and then opened triggered by the increasing light levels at dawn. Next step will be to transfer the PIC to a bit of strip board and add the regulators etc and then test it for real and monitor the current drawn to close the door on the coop. I plan on using a motorcycle battery 12v /4Ah which should be plenty. If funds permit I may add solar charging as a project at a later date.

    Anyway, thanks guys for the assistance with this...

    Malc

  5. #5
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code now working

    I did a project similar to this and had to account for something blocking door travel. I ended up with .1 ohm, I think, in series with the motor to monitor load by measuring the voltage across the resistor to determine current flow.

    If motor is free running then current will be high initially but should be lower once friction and inertia are overcome. But if it's blocked/hampered/whatever the stall current will stay relatively high. I think I set it up to try to open and if blocked wait some time and then try again. Another ADC input if you've got a free port. Just a thought.

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code now / NOT / working

    Well that version of the project didn't work so well in the field...

    I'm now working on a second version which uses a RTC. Basically I already have half the code written for one of my other projects so most of this will be a cut and paste. But something is really puzzling me and would welcome your advice.

    I'm using a 16F690 (20 pin DIP) because it has both a basic UART and IC2 interface, and I happened to have a sample in my project box. I could be cleaver (yeah right) and use the full capabilities of the DS1307 and program the times for each month so it takes care of BST and GMT etc, but for now I thought the easiest way would be to use a serial utility to set the time and trigger value as required. Using PicMultiCalc to get the coms values I am able to connect and receive data, however the time is shown as 00:00 in the following code. Now like I mentioned , I've cut and pasted the section and same variables in a previous project so there should be no reason for it not to work. To prove the DS1307 is working on the breadboard (it hasn't been removed since I took a break from the previous project) I removed the 16F690 and replaced it with the 18F4580 and connected the jumper to the EasyPIC5 board - clock was displaying the time set by the code. Noted the correct clk and dta pins, powered down the EP5 board and mapped the connections to the pins used by the 16F690. Replaced the 16F690 and tested again - the serial monitor in Microcode Studio connected and displayed "Time : 00:00"

    I've looked at the data sheet, but not sure if I am missing anything (probably some register needs setting but I'll be dammed if I can see it) so hopefully someone here might be able to assist.

    Here's the code

    Code:
    '****************************************************************
    '*  Name    : Chicken Coop Door Control                         *
    '*  Author  : Malcolm Crabbe                                    *
    '*  Date    : 30th January 2014                                 *
    '*  Version : 1.0                                               *
    '*  Notes   : Uses PBP 2.60c                                    *
    '*          : 16F690, int osc 4MHz                              *
    '*          : Program uses RTC  to turn                         *
    '*          : on an electric motor and open a sliding door.     *
    '*          : Door travel controlled by limit switches          *
    '****************************************************************
    
    
    ASM
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF  
    endasm
    
    ANSEL=%00000000
    ADCON1=7
    ADCON0 = %00000000                      'AD converter module disabled
    ADCON1 = %00001111 
    
    TRISA=%00000011                         'set PORTA as all output 
    TRISC=%00000001                         'set PORTC as all output apart from RC0
    TRISB=%00000011
    
    DEFINE HSER_RCSTA 90h                   'Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h                   'Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 25                    '9600 Baud @ 0.16%
    DEFINE HSER_CLROERR 1                   'Clear overflow automatically
    
    day             var byte 
    RTCMin          var byte		        'RTC Minutes
    RTCHour         var byte	            'RTC Hours
    RTCsec          var byte                'RTC Seconds
    
    TimeH           var byte                'variable to store Hours
    TimeM           var byte                'variable to store Minutes
    SS              VAR Byte                'variable to store Seconds
    
    CounterA        var byte	            'General purpose Variable
    CounterB        var byte	            'General purpose Variable
    
    Counter1        var word                'used to store the current time as a digit.  Counts minutes from 0000 at midnight (to be added later)
    
    Relay           var PortA.5             'used to activate relay and thus reverse direction
    Motor           var PortA.4             'used to drive the motor
    SW1             VAR PortA.0             'Door open limit switch
    SW2             VAR PortA.1             'Door closed limit switch
    Tx              var PortB.7             'used for sending time value in testing
    Rx              Var PortB.5             'used for receiving time value in testing
    
    SetMN           var byte		        'Used to set time Minutes
    SetHR           var byte	            'Used to set time Hours
    SetSS           var byte                'Used to set time Seconds 
    
    RCIF            VAR PIR1.5              'USART receive flag
    GIE             VAR INTCON.7
    RCIF=1
    
    SCLpin          var PORTB.6             'RTC pin - clk
    SDApin          var PORTB.4             'RTC pin - data
    
    setHR = 08                              'initial time to 8am 
    setMN = 00
    
    'convert initial time to BDC and write it to the DS1307
    CounterA=SetMN
    Gosub ConvertBCD
    RTCMin=CounterB
    
    CounterA=SetHR
    Gosub ConvertBCD
    RTCHour=CounterB
    
    I2Cwrite SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour] 
    
    
    test:
    
    I2CREAD SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]  ; read DS1307 chip
    
    timeH=(RTCHour>>4)                        'convert the BCD format of the hours register and store in variable timeH
    timeH=(timeH &$03)*10
    timeH=timeH+(RTCHour&$0F)
    
    timeM=(RTCMin>>4)                         'convert the BCD format of the mins register and store in variable timeM
    timeM=(timeM &$07)*10
    timeM=timeM+(RTCMin&$0F)    
    
    ss=(RTCSec>>4)                            'convert the BCD format of the sec register and store in variable ss
    ss=(ss &$07)*10
    ss=ss+(RTCSec&$0F)                     
    
    Counter1 = (TimeH *60)+ TimeM             'take hours and multiply it by 60 then add odd minutes to get the total minutes into counter1
    
    Hserout["Time: ",#TimeH DIG 1,#TimeH DIG 0,":",#TimeM DIG 1,#TimeM DIG 0,10]
    pause 1000
    
    
    goto test
    
    
    
    '*******************************************************************************
    ' convert BDC format to decimal
     ConvertBCD:
    	CounterB=CounterA DIG 1                        ' CounterA is the entry variable
    	CounterB=CounterB<<4                           ' CounterB holds the converted BCD result on exit
    	CounterB=CounterB+CounterA DIG 0
    	Return
    '*******************************************************************************

  7. #7
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code now / NOT / working

    Well, just the first part of the post I'm not sure what state you want ADCON1 set to. Initially you have it set to 7 then 15. That one extra bit appears to significantly effect your ADC conversion cycle.

    I also think you need to set OSCCON and put a DEFINE OSC ? in your code. I know the default is 4MHz but isn't the RC oscillator in your config the low low frequency oscillator? Could be wrong about that without reading further.

    TRISA is only 6 ports and I think PBP starts with the LSB so you'll have bits 0 and 1 as input. TRISB doesn't look to have RB0 to RB3 so only 4 bits needed to set as output. TRISIC is indeed 8 bits but you've got TRISIC.0 set to input not as described in comments.

    I don't often do serial so you'll have to let someone else chime in. I didn't look at rest of code. Hopefully I'm correct on at least one of these items and it will solve your problem.

Similar Threads

  1. 12F683 - Pin1 not working
    By ruijc in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th February 2014, 17:38
  2. 12F683 Maths and code genius reqd.
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st January 2014, 08:44
  3. please help with 12f683 pwm code
    By haidar in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 22nd May 2013, 21:25
  4. Problem converting 12F683 code to 12F1840
    By RossWaddell in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 14th March 2013, 01:55
  5. Working code but my layman approach uses too much code space
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th December 2012, 20:44

Members who have read this thread : 2

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