pic16f628a working with external oscillator

Discussion in 'Programmer's Corner' started by viva_unix, Jun 1, 2010.

  1. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    hello!
    i have an application that works. Looks like that:

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. #include <htc.h>
    4.  
    5. #define _XTAL_FREQ 4000000
    6. __CONFIG(MCLRDIS & BORDIS & INTIO & PWRTEN);
    7.  
    8. #define SRCLK   RB1
    9. #define SRDAT   RB2
    10. #define SRLAT   RB3
    11. static int iBinary;
    12. void OutPORT(unsigned char data);
    13. void main()
    14. {
    15.     CMCON = 0x07;
    16.     TRISB = 0x00;
    17.     PORTB = 0x00;
    18.  iBinary = 0x01;
    19.     while(1)
    20.     {
    21.         OutPORT( iBinary );
    22.         __delay_ms( 0x8F );
    23.         OutPORT( iBinary );
    24.         __delay_ms( 0x8F );
    25.   if( ++iBinary == 0x0100 )
    26.   {
    27.    iBinary = 0x01;
    28.   }
    29.     }
    30. }
    31. void OutPORT(unsigned char data)
    32. {
    33.    unsigned char bits;
    34.    for (bits = 0x80; bits != 0; bits >>= 1)
    35.    {
    36.       if ((bits & data) == bits)          
    37.       {
    38.          SRDAT = 1;
    39.       }
    40.       else
    41.       {
    42.          SRDAT = 0;
    43.       }
    44.       SRCLK = 1;                            
    45.       SRCLK = 0;
    46.    }
    47.     SRLAT = 1;                                
    48.     SRLAT = 0;
    49. }  
    50.  
    if I change top lines to:

    Code ( (Unknown Language)):
    1.  
    2. #define _XTAL_FREQ 8000000
    3. __CONFIG(MCLRDIS & BORDIS & INTIO & PWRTEN & HS);
    4.  
    it stops working.

    I do add an external oscillator for 8MHz. Is there a rule of how attach it? Should it use some resistor or capacitor? Am I missing something else?

    Thank you!
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    A circuit diagram would help us see just what you are doing.
    Are you adding an oscillator module or just a crystal? If its just a crystal do you have got capacitors going from each side of the crystal to earth (15pf-22pF is normal)?
    Have you read the PIC16F628A data sheet? If not do so as it has a section on how to use oscillators and crystals.
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Have you done everything according to section 14.2 of the manual?

    And no, it doesn't seem as if you need any additional components when using an external clock.
     
  4. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    I am trying to connect PIC16F628A to 74HC595 shift register that powers the lights.
    Here is the video of how it runs. Can someone explaine me why does it stops sometimes? This happens without external crystal, with crystall I do not see lights at all! :(

    http://www.youtube.com/watch?v=yLivQD4fmU4

    currently my application looks like that:
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #define _XTAL_FREQ 4000000
    4. __CONFIG(MCLRDIS & BORDIS & INTIO & PWRTEN & WDTEN);
    5.  
    6. #define SRCLK   RB1
    7. #define SRDAT   RB2
    8. #define SRLAT   RB3
    9. void OutPORT(unsigned char data);
    10. void main()
    11. {
    12.     CMCON = 0x07;
    13.     TRISB = 0x00;
    14.     PORTB = 0x00;
    15.     while(1)
    16.     {
    17.         OutPORT( 0x00 );
    18.         __delay_ms( 0x8F );
    19.         OutPORT( 0xFF );
    20.         __delay_ms( 0x8F );
    21.   CLRWDT();
    22.     }
    23. }
    24. void OutPORT(unsigned char data)
    25. {
    26.    unsigned char bits;
    27.    for (bits = 0x80; bits != 0; bits >>= 1)
    28.    {
    29.       if ((bits & data) == bits)          
    30.       {
    31.          SRDAT = 1;
    32.       }
    33.       else
    34.       {
    35.          SRDAT = 0;
    36.       }
    37.       SRCLK = 1;                            
    38.       SRCLK = 0;
    39.    }
    40.     SRLAT = 1;                                
    41.     SRLAT = 0;
    42. }  
    43.  
    interesting that it happened not constantly. It might run for 10 minutes fine than start doing some wired things... :(

    I will take a look at manual for 16f628a. I do have it in pdf.

    Thank you!

    P.S. I am trying to add 8 MHz Quartz Crystal HC-49/U4H

    P.S.S. I do also have SG531P, is it better?
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Like a said, post a circuit so we can see any glaring errors.
    Eg. Do you have your supply pins bypassed?, have you got the crystal capacitors connected correctly?

    Also when you connect the crystal do you change your code from
    "#define _XTAL_FREQ 4000000"
    to
    "#define _XTAL_FREQ 8000000"
    and recompile the code?

    If you don't then all your timing loops will be wrong and the circuit may be working but the LEDs would be flashing too fast to see them!
     
  6. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    Simply using the breadboard may be a problem due to the connecting wire lengths.

    Do you have a decoupling capacitor DIRECTLY across the power pins on the PIC and the other ICs?

    Any connections for grounds or high frequency signals should be as short as possible, eg. use bits of bent tinned copper wire or component lead to bridge a 0V/ground pin to the adjacent ground bus, not a loop of wire up in the air.

    The total length of the wires and leads for each crystal connection should not be more that about 20mm or so. Ideally plug the crystal leads directly in the rows from the PIC osc pins and add load caps (10pF should do) from the same two rows back to a PIC ground pin.
     
  7. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    question... I would like to draw the diagram... anyone knows what kind of software is being used here:
    http://www.doctronics.co.uk/555.htm

    I see a beautiful and well explained breadboard on this page. how do they draw it? what the software called?
     
  8. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    Here is my diagram in attachment. I have problem:

    1. the crystall does not work when I add it.
    2. the circuit works somewhat funny. Please take a look at youtube video I have uploaded.

    Open for any suggestions and recommendations.
    Thank you!
     
  9. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Add bypass capacitors (100nF) from PIC Vdd pin(14) to earth and from the 74XXX chip Vdd/Vcc pin(16) to earth.

    I also like to add a 100nF capacitor across each supply rails on the breadboard and a 220uF or 470uF electrolytic connected across where power comes into the breadboard.

    When you connect the crystal to the PIC (pins 15 and 16) make sure you also connect 22pF capacitors from each of those pins to earth.
     
  10. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    OK. I will try to do it right after I get home today.
    How important those numbers? I might not have the exact capacitors you are talking about... :(

    Thank you!
     
  11. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Bypass capacitor values are not all that critical, basically anything in the range 0.01uf to 1.0uF will do.

    The values of capacitors across the crystal must be in the range specified by the manufacturer.
    Too low a value and the crystal won't oscillate, too high a capacitor value and you over-drive the crystal giving poor stability and even possibly damaging it, 15pF to 22pF works well for the crystals I normally use but depending on the crystal you could need anything between 12pF and 47pF
     
  12. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    I do not have bypass capacitors at home. I have ordered them. They should arrive next week. I have tried with regular capacitors (25V 47uF) and it did not helped... :( So, i will wait for package to arrive.

    do you see anything wrong with the code? maybe any other prolems?

    thank you!
     
  13. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    Have you tried defining XT instead of HS (for one project I had to use XT for a slower than 20 crystal). Also, possibly try disabling INTIO? RB0 is an interrupt pin, it is floating, not sure whether this might cause a random issue and then an overflow/reset?
     
  14. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    I will try with XT. Possibly today...
    Before I start using RB0-RB1-RB2 I have used RA0-RA7-RA2 and got the same result.
    I have notices that when it stucks, as shown on youtube video, it does react on my movement (hand) close to these 3 wires. It start working when I egt my hand close to it (resistance?)
    That's why I am waiting for capacitors. Looks like I am missing some hardware...
    Thank you!
     
  15. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    Try locking RB0 to ground and turn off the INTIO. I had a similar issue when this value was floating (well actually my issue was I needed a 4.7K pullup, as I was trying to use the SQWE output of the DS1307 on RB0). I had similar results, the processing would pause, until I waved my hand near, this was enough for RB0 to trigger.
     
  16. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    This is interesting...
    I am actually using RB1-RB2-RB3. RB0 is empty and INT0 is not enabled in the code as you can see. So, I have added black wire from RB0 to ground and running test now... Let you know how it is going...

    Thank you!
     
  17. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    interesting staff happened. i have done it and looks like everything was running fine for about an hour or two, but than everything got back to crazy! but this is better than 20 minutes i got before.

    so, what the deal with my hand? why does it work this way? i still waiting for capacitors to arrive.

    thank you!
     
  18. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    Your hand will effect the system in two ways.
    1) It will cause eddies in the air which could, by friction, cause static charge to build up on the pins.

    2) Also, the hand will have a small electromagnetic field around it. For instance, all of the nerves in your body oscillate with roughly a 50Hz signal. This field can re-inforce or destroy other interference effects which might be in operation.
    For instance, if you have a bad signal on a radio, sometimes holding your hand near the arial (not necassarily touching it), can improve the signal.

    Why this has not fixed, but merely improved your problem I cannot be sure, is there any chance of a bad solder joint or connection or something?
     
Loading...