PICkit2 2 second program

Discussion in 'Programmer's Corner' started by Robert Webb, Dec 20, 2014.

  1. Robert Webb

    Thread Starter New Member

    Dec 20, 2014
    18
    0
    Hi
    Am new to this forum and hope that I am in order.
    I am using a 12f683 IC incorporated in a switch built by a friend to control a clock I have made and am using the software he wrote drive it. The switch has a 32K clock crystal and the first line of his code turns the internal IC clock off to avoid conflict. His original program was to turn the chip on once a minute but I want to turn it on every two seconds.
    I have been less than successful in modifying his code and would be grateful if somebody can point me in the direction of some code that will allow me to program my PICkit2 so that the circuit executes 30 times a minute to a reasonable level of accuracy.
    regards
    Robert
     
  2. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,501
    380
    hi Robert,
    If possible can you post the PIC program, use the 5th symbol, counting from the right end of the Post Reply menu bar, Insert/Code.
    E
     
  3. Robert Webb

    Thread Starter New Member

    Dec 20, 2014
    18
    0
    Hi Eric
    Thanks you for the reply. Before I do that I will need to check with my friend who wrote the program I am playing with. I will get b
    I am not sure of the process used for these threads but will just reply to your email and see what happens.


    I have been given permission to provide the code I mentioned and it appears below. Bruce, my friend who wrote it, uses this one minute program for a clock he has built. What I need to be able to do is to alter the timing so that it provides a pulse every 2 seconds, or 30 times per minute to drive a clock I have just completed. I am not a programmer and feel rather like a man in a darkened room with a shotgun trying to shoot rats.


    #CONFIG

    _config _FOSC_LP&_WDT_OFF&_CP_OFF_FCMEN_OFF

    #ENDCONFIG

    This is a special code that overrides the regular configuration of your PIC time base so that a 32,768 MHz crystal becomes the time base.

    ANSEL = 0

    This makes all your pins except 1,2,3 and 8 outputs.

    CMCON0 =7

    This turns off the comparator in your chip otherwise it might interfere.



    main: Start point of program

    HIGH GPIO.0 Turns on pin 7 making it a positive pin

    PAUSE 2 Instructs pin 7 to stay on for a fraction of a second, in this case because of

    number 2.

    LOW GPIO.0 Instructs pin 7 to turn off following the last PAUSE 2 instruction.

    PAUSE 490 Instructs circuit to wait so many micro seconds, almost a minute including the

    First PAUSE 2

    PAUSEUS 460 A very fine adjustment in even small fractions of a second to get a one minute.

    GOTO main Instructs program to start all over by returning to main:





    This code gives you a fraction of a second pulse once every minute and has been written to drive a switch that has a 12f683 chip and a 32,768 MHz crystal.
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    This program is written in PBP, which I don't use, but it seems pretty simple.
    PBP uses a default frequency of 4MHz (4000000Hz) and your PIC is running at 32.768kHZ (32768Hz) and the pause function is supposed to pause for that number of milliseconds. If you do (4000000/32768)*492, you get very close to 60000 ms which is one minute.
    Really you can just recalculate so the total pause is 492/30 = about 16 to give about 2 seconds, you can adjust the pauseus amount to get it even more accurate.
    It seems that you can't tell PBP that the oscillator is 32.768Hz which is why this fudge has been used.
     
  5. Robert Webb

    Thread Starter New Member

    Dec 20, 2014
    18
    0
    Hi Markd77
    Thank you. Bruce and I are hobby clock makers (he is in the US and I am in Australia) and he taught himself some basic programming to see if we could develop a different sort of clock motor which, incidentally, we have done. I had intended to fiddle with the numbers in the pause and pauseus, but with only a vague understanding of what I was doing. That has been made more difficult because I had a connectivity issue with PIC programmer which I think I have solved. I will do what you suggest and let you know the outcome.
    Again, thanks for taking the time, I appreciate it.
    Robert
     
  6. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    So it needs to be pretty accurate. If you ignore the time the rest of the code takes, I think you want the pauses to total 16 and the pauseus to be 384. The actual code outside the pause instructions takes 4 instruction cycles and PBP may be doing some other things that we don't know about. It will probably need to be timed and then adjusted by trail and error.
     
  7. Robert Webb

    Thread Starter New Member

    Dec 20, 2014
    18
    0
    Thanks for the additional information. I am not looking for Atomic Clock accuracy but something that is reasonably close to the what your common or garden quartz clock can achieve. Your explanation of the way the program works has helped. If I have a better understanding of what the program is doing and what adjustments to the parameters we are talking about can achieve, I can use that should I decide to change the wheel work in future clocks.
    Thanks again
    Robert
     
  8. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,677
    900
    Which pins are you using for Input and Output. Ansel =0 sets the pins to digital I/O; it does not set the direction. See datasheet pp. 31/32. It is the Trisio (often just called Tris for bigger chips) register that sets the direction.

    upload_2014-12-21_18-33-11.png

    Sorry, but I can't help you with the program per se. I only do ASM.

    Edit: In Assembly, your code will look something like this:

    Code (ASM):
    1.  
    2. MAIN
    3.     bsf       STATUS,RP0    ;switch to bank 1                 |B1
    4.     clrf      ANSEL        ;digital I/O             |B1
    5.     movlw     b'00001000'    ;GP3 input, all others output     |B1
    6.     movwf     TRISIO        ;                 |B1
    7.  
    8.  
    John
     
    Last edited: Dec 21, 2014
  9. Robert Webb

    Thread Starter New Member

    Dec 20, 2014
    18
    0
    Hi John
    Thanks for the input. I will pass it on to Bruce. Hopefully the values Markd77 suggested will do the job.
    Robert
     
Loading...