1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Problem with PIC 16F876A

Discussion in 'Oshonsoft' started by steve langner, Jan 20, 2017.

  1. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    Just tested some inexpensive PIC16F876A micros imported from China. So in each case I used the code below to put all the outputs high. I used a PIC Kit K150 programmer to program the chips, and in every case it confirmed "Program completed". And of course the Oshonsoft simulator showed all the outputs high (ON). However, in fact none of the output pins on the chips themselves went high, and a voltmeter reading on all showed about 0.9 volt. I then modified the code to switch all outputs low. Measured voltages descended to about 0.5.

    I'm quite inexperienced with coding and micros, by the way, so my question to you kind folks is : are the chips dud, or is my code wanting in some respect?
    Define CLOCK_FREQUENCY = 4 MHz
    TRISB = %00000000
    TRISC = %00000000
    RB = %00000000
    RC = %00000000
    AllDigital
    main:
    RB = %11111111
    RC = %11111111
    Goto main
    End

    Define CLOCK_FREQUENCY = 4 'MHz
    TRISB = %00000000
    TRISC = %00000000
    RB = %00000000
    RC = %00000000
    AllDigital
    main:
    RB = %11111111
    RC = %11111111
    Goto main
    End
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi steve,
    Are you sure the 4mHz xtal is working OK.?
    Eric
    I assume also that the /MCLR pin is connected correctly on the test board.?
     
  3. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    hi Eric
    Not using a crystal. Changed the fuse value to RC to use internal osc. Not sure if this is permissible for this chip, but as a dabbler I haven't much of a clue what I'm doing really. I think you're quite right in homing in on the MCLR connection which in this case was o/c after programming. I had been thinking that like some smaller chips, the MCLR pin was configurable as an output, which my code would have catered for. So I tried tying it to VDD via 820 ohms, and did some re-arranging of the board wiring. Now I'm getting intermittent results which I'm trying to sort out. Still inconclusive I'm afraid. Thanks very much for the sleuthing, and I'll come back when/if I get it working, or if I'm further baffled.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2

    The 16F876A does not have an internal oscillator, you must use a crystal or external oscillator circuit of some sort. See page 145 of the datasheet. It is the 16F88x series that have internal oscillators, not the 16F87x series.
    You can use a RC circuit if you have no crystal, but harder to set a fixed frequency (section 14.2.3 in datasheet)
    FYI, MCLR pin is input only, it cannot be used as output.
    When you define pins low, the internals are current sink, you may not see a voltage that is valid. ^The chip does not output 0 volts, it turns on a "grounding" transistor circuit to draw down an external load. Output high should show between 4.5 to 5V. To test any pin, you could put a high value resistor between test pin and ground, like 10k to 100k ohms.

    Unless you have RB and RC defined somewhere (are they compiler defaults?), it may not work. Use the keywords "PortB" and "PortC", easier to read and understand. (EDIT: I take that back, RA, RB, RC are compiler defined as ports...)
     
    Last edited: Jan 21, 2017
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi steve.
    As sagor1 states,,, physical crystal is required.
    Eric
     
  7. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    Thanks to sagor1 and Eric for info and advice. You can see I was truthful in intimating I didn't have a clue what I was doing.
    So far this morning I've managed to destroy two perfectly good 876's. Fortunately I have three left from the Chinese consignment of five. I also have some 16MHz crystals and 22 puff caps. So, now armed with fresh knowledge from you guys, there's some hope that by the end of the day I'll have a LED flashing. Will let you know. Thanks again.
     
  8. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    If you order more chips, just get the 16F886, instead of the 876A. They are about the same price from China. At least the 886 has an internal oscillator, easier to prototype that way.
    Word of advice, the 16F886 starts at default 4Mhz when powered on. To get 8Mhz internal oscillator, you have to set OSCCON register. The Oshonsoft compiler does not do it automatically, so if you define 8Mhz as frequency in the code, you still have to go set OSCCON bits to 8Mhz in initialization part of the code. (Section 4.2 of 16F886 datasheet)
    Of course, you can just leave it at the default 4Mhz if you wish, as long as you define the same in the code.
    Steve

    PS If flashing LEDs directly from the PIC, always safer to put a 220 Ohm resistor (or thereabouts) in series with the pin and the LED
     
    Last edited: Jan 21, 2017
    • Agree Agree x 1
  9. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    Hitting all kind of problems with the hardware. Giving up for the day, if not for ever. Thank you both (Eric and Sagor1) for all your help, I've learned a lot today.
     
  10. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    Never give up. It all does work, once you learn how. We all had to start from ground zero at some point. If in doubt, post the entire code you are working with and many here will review and correct (or suggest) how to fix things. However, how you actually wire things on a breadboard are up to you... ;)

    Steve (another Steve lol...)
     
  11. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    OK Sagor1, back again. Done some sorting out of hardware; got a 16Mhz crystal connected between pins 9 and 10, with two 22pF caps one on each leg of xtal, and with other ends of caps to earth (Vss). LED with 390 ohm series connected to Vdd. Using ICSP. MCLR tied to Vdd via 1k8 resistor (disconnected during programming just in case!). Vdd from programmer o/c permanently. Local 5 volt Vdd permanently connected.

    After programming, LED remains extinguished, no flashing LED. Programmer confirms programming complete.
    Here's the code: I'm wondering if the hardware Xtal install needs some additional initialising code? Some further help would be greatly appreciated.

    Define CLOCK_FREQUENCY = 16
    TRISB = %00000000
    TRISC = %00000000
    RB = %00000000
    AllDigital
    main:
    RB = %11111111
    WaitMs 100
    RB = %00000000
    WaitMs 100
    Goto main
    End
     
  12. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    Sorry, should have said LED with series resistor connected between pin 22 and earth (Vss)
     
  13. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    Ok, I think the 100ms is a bit short. a dim led may not flash all that much at that rate, it may seem to be steady on, but dim.
    Do 1000ms instead. I tested this code in the simulator, and it works just fine. I turned off all options in the config (brownout, watchdog, etc...), and set HS crystal as clock source.
    Make sure your configuration bits are set properly.

    Define CONFIG = 0x3f3a
    Define CLOCK_FREQUENCY = 16
    'Define SIMULATION_WAITMS_VALUE = 1 'Used for simulation only

    AllDigital

    ConfigPin PORTB = Output
    ConfigPin PORTC = Output

    PORTB = 0 'Set low to start
    PORTC = 0 'Set low to start


    Symbol testpin = PORTB.1 'Define RB.1 as test pin

    main:
    testpin = 1
    WaitMs 1000
    testpin = 0
    WaitMs 1000
    Goto main

    Always test one pin at a time. The PIC cannot, technically, drive all outputs at full 25mA at the same time, there is an overall current limit for each port (actually 200mA source or sink for all ports combined).

    PS: Make sure the LED is the right way around....
     
    Last edited: Jan 22, 2017
  14. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    upload_2017-1-22_13-17-48.png
     
    • Informative Informative x 1
  15. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    hi Steve
    Hardware works fine to your setup and code. I'm hugely relieved and grateful to you for your help. I was despairing that I'd have to abandon my project or search for some other way, but I can now resume with relative impunity.

    As a point of interest, may I ask where you obtained the configuration table. It appears as if it automatically generates the numeric code 3F3A corresponding with the bit setup chosen, and looks extremely useful.

    But if it isn't generally available, well c'est la vie, and many, many thanks again.
     
  16. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    I comes built into the Oshonsoft PIC Simulator IDE. Under "Options", Configuration bits... Once you set them, tell it to "generate Basic code" and it inserts it into the start of the Basic source code. (See below)
    Of course, one has to ensure you have the right PIC selected first. The bits are different for different PICs
     
    Last edited: Jan 22, 2017
  17. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
  18. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    Thanks again. I can get on with things now. This all came about from my interest in learning how to use I2C communication, and I needed a micro equipped with the "MSSP" module enabling this protocol to be used. Bound to be back with questions about it!
    Regards
     

Share This Page