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

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

  3. #3
    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
    '*******************************************************************************

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

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

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

    Thanks for the assistance

    I've just sat down with a large mug of coffee and some toast and run through the datasheet... I think I've covered most things (I still find these datasheets a struggle, but hopefully picking things up). I've set the registers as follows (with comments), and the code compiles

    Code:
    ASM
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF  
    endasm
    
    DEFINE  OSC 4
    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
    
    ANSEL  = %00000000                      'Set all pins digital
    ANSELH = %00000000                      'Set all pins digital
    ADCON0 = %00000000                      'AD converter module disabled
    OSCCON = %01100101                      'bit7 not used, bits 6-4 4mhz default, bit3 int osc, bit2 HS stable (ignored - at 4Mhz), bit1 not stable, bit0 int osc used for system clk 
    CCP1CON= %00000000                      'Turn off CCP module
    SSPCON = %00001011                      'Ic2 master
    
    TRISA=%00000011                         'set PORTA as all output apart from RA0 and RA1( bits 7&8 unimplimented read as 0)
    TRISB=%11110000                         'set PORTB as all input ( bits 0-3 unimplimented read as 0)
    TRISC=%11111111                         'set PORTC as all input (default for POR)
    Having compiled and loaded the code, pleased to say it all works now !!

    OK I might of gone overboard on configuring registers, but at least it's helped me get some understanding with the datasheets.

  6. #6
    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,
    Glad you've got it going! Just a quick note: The I2CREAD/I2CWRITE commands does not use the (M)SSP module in the PIC. You're enabling the (M)SSP module for I2C mode so I'm actually a bit surprised that it works because I thought that when enabled it (the (M)SSP module) takes control over the I/O-pins - I'm obviously mistaken there.

    However, the PBP I2C commands are bitbanged, they don't use the I2C hardware in the PIC so there's no need to enable the hardware for it. It might lead to problems.

    /Henrik.

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Thanks Henrik,

    So I can remove the SSPCON line ?

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