16F628 Start Up problem

Discussion in 'Embedded Systems and Microcontrollers' started by TCOP, Jul 4, 2013.

  1. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    Hi all,
    I have built a stepper motor contoller using 16F628. It worked fine for last few months until I decided to change from the internal oscilator to a 20Mhz external. My problem is that the pic is not properly initialized on startup. MCLR is pulled up using a 10k resistor and a momentary switch connects it to ground and allows me to perform a reset. If I press the button, then the pic is properly initialized and works as it is supposed to work. If I just power on my board and dont press the push button to reset, then there is a 50% chance that i will not work as expected. Basically I have tied some dip switches to the pic pins (properly pulled down to gnd) but unless I press the reset button, the μc half of the time reads wrong values (0 when it should be 1).
    This only happens on 20Mhz. If i switch to 4Mhz there is no problem at all.
    Any suggestions?
    These are my configuration bits : __config _HS_OSC & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BOREN_ON & _BODEN_OFF & _MCLRE_ON
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The problem could be the crystal decoupling caps. Have you done as described in the datasheet? Have you also used the correct #define _XTAL_FREQ. If not used the default value is 4MHz. So it will fit the standard internal osc setting. Just guessing here, not sure about anything
     
  3. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    I use 22pf caps so there should be no problem with them.
    I use this __config _HS_OSC to set the external osc. Note that I code in asm.
    I feel that i just have to add a startup delay but I am not sure how to do it.
    A normal delay routine does not work.
     
  4. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
  5. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    Yes. I use de-caps too. 0.1μF indeed. I am aware of this link :)
    22pf are the to caps next to the crystal.
     
  6. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Is it on a breadboard? If so too much stray capacitance for 20MHz.

    Might help to post your code.
     
  7. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    No it is not. Everything is soldered.
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Perhaps you should try clock switching.. start on the internal clock and switch to external when it is stable...
     
  9. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    Yes. This is a good idea. But is this possible with 16f628?
     
  10. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    Ok i checked it.....it is not! :(
     
  11. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Where's your code? Schematic?
     
  12. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    The Power-up Timer(PWRT, pg. 103) does just this...
     
  13. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    I don't thinnk it does this. It just offers an extra delay on start up.
    I have already enabled this in the configuration line: "_PWRTE_ON".

    Ok...I've tried everything last night. My last trial was to trigger the mclr pin using an other pin's output, from inside in my code, about 200 ms after starting up. I checked a bit in INTCON that stays unchanged on reset to make sure that the reset will be done only once after initial power up.
    The pic failed to stabilize even then which is quite strange since it does so, when i press the reset switch by hand. It is something else I am missing here. I'll post a picture of the schematic. There is no fancy thing there since this is only the brain board and not the H bridge board but perhaps you'll see something I miss.
     
    Last edited: Jul 5, 2013
  14. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    See attached image
     
  15. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    Problem solved!!!
    I really don't believe my eyes. See the code below.
    Code ( (Unknown Language)):
    1.  
    2.  
    3. ; ------------------------------------
    4. ; Intitialize according to Dip Switches
    5. ; Pins RB1 & RB2 adjust PWM duty cycle
    6. ; ------------------------------------
    7. clrf Tmp
    8. Movf PORTB,W
    9. andlw b'110'
    10. movwf Tmp
    11. rrf Tmp
    12. movlw b'11'
    13. xorwf Tmp,W
    14. btfss STATUS,Z
    15. goto Check1
    16. movlw 0xF9 * 1
    17. goto SetPWM
    18. Check1:
    19. movlw b'10'
    20. xorwf Tmp,W
    21. btfss STATUS,Z
    22. goto Check2
    23. movlw 0xF9 * 3/4
    24. goto SetPWM
    25. Check2:
    26. movlw b'01'
    27. xorwf Tmp,W
    28. btfss STATUS,Z
    29. goto Check3
    30. movlw 0xF9 * 1/2
    31. movwf PWM
    32. goto SetPWM
    33. Check3:
    34. movlw 0xF9 * 1/4
    35. SetPWM:
    36. movwf PWM
     
  16. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    If you don't see anything strange, I'll say neither did I. But then this is what i found in table 14-7 on the datasheet.

    INITIALIZATION CONDITION FOR REGISTERS

    Reg: STATUS 03h​
    Power-on Reset (bits state): 0001 1xxx
    ...
    ...
    Legend: u = unchanged, x = unknown, - = unimplemented bit, reads as ‘0’, q = value depends on condition.



    Did you notice the "x = unknown"???

    Thus for some damn reason, the state of STATUS reg is different on start up when i use the internal osc than the extrernal HS and when powering on 20Mhz, the Carry bit sometimes was set for no reason at all.
    I had to clear STATUS just before the rotate command (4th line on my code) even though I had not altered any STATUS bit previously.
     
  17. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    It's not the startup that is the issue. :)

    You should ALWAYS clear or set the status bit before doing a right or left shift on a PIC 16F. Unless the bit has been cleared/set deliberately by a previous operation. It is necessary to ensure the new bit is not garbage.
     
  18. TCOP

    Thread Starter Member

    Apr 27, 2011
    89
    0
    you are probably right :)
    ...I just realised that RB was Roman Black. I am building an alternative to Linistepper using Bibolar motors (based again in 16f628 but will move it to 16F1827), for my diy CNC. If you like, we can discuss more about it.
     
  19. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    If you're updating PICs I'd recommend moving to an 18F series, especially when using MPASM. Very easy to migrate from 16F, smaller code too and designed so you don't have to deal with bank switching & RMW.
     
  20. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Sure if you like. :)

    I considered making a bipolar Linistepper for a long time, but decided not to. Driving the motor bipolar requires two hbridges and high side FET driving etc and is very complex with discretes so hobby kit builders don't want to build something so complex.

    Then if you go for an IC dual hbridge capable of the currents and voltages required it bumps the price up and uses a specialty hard to get IC. AND if using a pricy hard to get IC then you can just use a specialty stepper driver IC that already has the microstep sequencer and power down features built in, so that is the same as all those toshiba IC stepper drivers, fully built, for sale on ebay.

    I wish you luck with the project, I'm just advising you might want to check out all the cheap 3-axis and 4-axis stepper drivers you can buy on ebay before committing to a lengthy open source project that few people will build and China has already flooded the market... :)
     
Loading...