• AVR Freaks

Hot!pic16f877a micro controller circuit problem

Author
slhulk
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/10/11 08:45:41
  • Location: 0
  • Status: offline
2019/10/11 09:31:11 (permalink)
0

pic16f877a micro controller circuit problem

I'm new to the world of pic and today I wanted to implement an ADC in my pic16f877a. But I was unable to figure out the pin connections exactly so I tried to implement a simple LED flash program instead. I programmed it using assembly and everything was fine. The program was built successfully and my proteus simulation ran smoothly. I uploaded the code into my pic16f877a using pickit3. 
 
I connected everything as they are shown in the below diagram. Proteus do not show the 2 Vdd pins and the 2 Vss pins. But I connected them im my breadboard implementation. Note that I have used a 9v battery as my power source and I have used a resistor voltage divider to get 5v.
 
I used the external RC oscillator (I did not change the config as the default is the RC oscillator). First I did not connect a capacitor to the RC circuit because according to an article I read somewhere, this capacitance is not necessary. But later I tried with 1uC and 100nF caps too.
 
The LEDs did not turn on. I checked all the connections. But couldn't find the bug. 
 
When I disconnected both the Vss pins from GND the LEDs lit up without flashing. It remained lit up until I connected the Vss to the GND again. 
 
Note that the pic is not dead. The MCU can be erased, programmed and verified using pickit3.
 
Any insight into how to solve this would be highly appreciated.
 
 
Here is my code:
 
PROCESSOR 16F877A
INCLUDE <p16F877A.INC>


CNT0 EQU 21h ;COUNTER IN BANK 00
CNT1 EQU 22h ;COUNTER IN BANK 01
CNT2 EQU 23h ;COUNTER 2

ORG 0x00 ;WE MAKE USE OF INTERUPTS

INIT:

BSF STATUS, 5 ;SELECT BANK 01
;PORTB IS SET TO OUTPUT MODE
MOVLW B'00000000'
MOVWF TRISB
BCF STATUS, 5 ;SELECT BANK 00


MAIN:

MOVLW B'11111111'
MOVWF PORTB
CALL DELAY
MOVLW B'00000000'
MOVWF PORTB
CALL DELAY
GOTO MAIN

DELAY:

BCF STATUS, 5
MOVLW B'11111111'
MOVWF CNT0
MOVLW B'11111111'
MOVWF CNT1

LOOP1
DECFSZ CNT0,1
GOTO LOOP2
CLRWDT
LOOP2
DECFSZ CNT1,1
GOTO LOOP2
RETURN

END
 
 

Attached Image(s)

#1

14 Replies Related Threads

    bitdoctor
    Starting Member
    • Total Posts : 24
    • Reward points : 0
    • Joined: 2019/10/11 06:10:09
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/11 11:05:38 (permalink)
    +1 (1)
    You can not use a resistive divider to supply any circuit that draws significant current - use a voltage regulator.
    #2
    slhulk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/10/11 08:45:41
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/11 12:38:56 (permalink)
    0
    UPDATE:
     
    I connected two capacitors (each 1uF) across both Vdd Vss pin pairs. Now the LED bulb lit up. I didn't have to remove the Vss pin from GND. But the bulb remains steady without flashing.
    #3
    Mysil
    Super Member
    • Total Posts : 3421
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/11 12:42:25 (permalink)
    +2 (2)
    Hi,
    A 9V battery and resistor voltage divider is a somewhat awkward power supply for a microcontroller.
    As a mosfet digital circuit, the microcontroller will use current in short sharp spikes,
    so absolutely should have Capacitors connected between Vdd and Vss supply pins.
    Preferably one each side of the package, connected with short wiring, as close to the pins as possible.
    Use Ceramic capacitors with value between 100nF and 1uF.
     
    Slightly better than resistive voltage divider, would be to use a Zener diode 4.7V, instead of the R12 resistor. 
    Ceramic capacitors shall still be used between Vdd and Vss pins.
     
    What is the value of capacitor in the RC circuit connected to OSC1/CLKIN pin?
    1 microfarad is much too large, something between 100 pF and 1 nF would probably be better.
     
    The double loop in the Delay routine do not seem to function, only the inner loop, loop2 is executed, 
    and code return to the caller when the innermost  loop is completed.
     
    Since you have a PICkit 3 for programming and debugging,
    you may use the PICkit 3 to power the circuit during development, instead of battery.
     
        Mysil
     
     
    #4
    slhulk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/10/11 08:45:41
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/11 13:22:32 (permalink)
    0
    Mysil
    Hi,
    A 9V battery and resistor voltage divider is a somewhat awkward power supply for a microcontroller.
    As a mosfet digital circuit, the microcontroller will use current in short sharp spikes,
    so absolutely should have Capacitors connected between Vdd and Vss supply pins.
    Preferably one each side of the package, connected with short wiring, as close to the pins as possible.
    Use Ceramic capacitors with value between 100nF and 1uF.
     
    Slightly better than resistive voltage divider, would be to use a Zener diode 4.7V, instead of the R12 resistor. 
    Ceramic capacitors shall still be used between Vdd and Vss pins.
     
    What is the value of capacitor in the RC circuit connected to OSC1/CLKIN pin?
    1 microfarad is much too large, something between 100 pF and 1 nF would probably be better.
     
    The double loop in the Delay routine do not seem to function, only the inner loop, loop2 is executed, 
    and code return to the caller when the innermost  loop is completed.
     
    Since you have a PICkit 3 for programming and debugging,
    you may use the PICkit 3 to power the circuit during development, instead of battery.
     
        Mysil
     
     




     
    Hi Mysil, Thank you for the detailed review. Is it possible to run the program by connecting the MCU to the pickit3? If so is it still needed to connect an external oscillator? 
    #5
    Mysil
    Super Member
    • Total Posts : 3421
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/11 13:55:33 (permalink)
    +1 (1)
    Hi,
    Running PIC with power supply from the PICkit 3 is possible,
    but how it is done will depend somewhat upon what software is used for Programming or Debugging.
    MPLAB X, MPLAB 8, PICkit 3 Programmer application,
    all have different user interface to control power supply from PICkit 3 to the target microcontroller. 
    In MPLAB X, such settings are hidden inside "Project Properties" box, select "PICkit 3" properties "Power" settings.
     
    Anyway, trying to flash 8 leds at the same time, is likely to cause problems
    whether powered from a resistive voltage divider, or from PICkit 3.
    There is a 1200 Ohm resistor in the voltage divider, what will be the voltage drop when this shall provide current to 8 LEDs at the same time?
    For the PICkit 3, there is a limitation: max 30 mA current may be used by the target circuit.
     
    If the program actually run as intended, with the mistake in delay loop code,
    it is possible that the program may run so fast that it will not be possible to observe flashing visually.
     
        Mysil
    #6
    slhulk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/10/11 08:45:41
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/11 14:03:39 (permalink)
    0
    Hi Mysil,
     
    I gave up on using the 9v battery to power up. I'm using MPLAB X. The error in the code maybe because of clearing the WDT as soon as the PC is stepped out of the LOOP1. I fixed the subroutine as follows.
     
    DELAY:

    BCF STATUS, 5
    MOVLW B'11111111'
    MOVWF CNT0
    MOVLW B'11111111'
    MOVWF CNT1

    LOOP1
    CLRWDT
    DECFSZ CNT0,1
    GOTO LOOP2

    LOOP2
    DECFSZ CNT1,1
    GOTO LOOP2
    RETURN
     
    I have given the maximum possible voltage that my laptop can output, 4.75v to the pickit3. 
     
    Note that since failing to flash 8 LEDs I have tried to do it with only 1 LED. But all the pins of PORTB are set as outputs.
     
    I would like to point out that I can connect, erase, program and verify my code by using MPLABX IPE. But when I try to do it with MPLABX IDE it returns,
     
    'Target device was not found (could not detect target voltage VDD). You must connect to a target device to use PICkit 3.'
     
    Which is resulted by not setting the output power as you pointed out. But I have done it. Still no progress.
    #7
    ric
    Super Member
    • Total Posts : 24278
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: pic16f877a micro controller circuit problem 2019/10/11 14:31:25 (permalink)
    +2 (2)
    As pointed out, the voltage divider power supply was a bad idea.
    Powering from the PK3 should be possible.
    You must have some small capacitors close to the PIC on the power supply pins as already described. If you have too much, it will slow down the rise time of the power supply, causing the problem you describe.
     
    Do you HAVE to use a PIC16F877A?
    There are newer, cheaper PICs available in the same package which are much easier to get going and debug,
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    teenix
    Starting Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2017/12/21 13:47:21
    • Location: Australia, Melbourne
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/11 16:03:00 (permalink)
    +1 (1)
    I would suggest using a 5V regulator for powering the circuit. The supply to the PIC will be a bit unstable when circuit currents change. For example if all the LEDs come on, the PIC supply voltage will drop due to the current flowing through R1.
     
    The RC mode of operation is ok, but is dependant on many factors and will change frequency. There are charts of data in the AC Specs section of the data sheet which should give RC values to use.
     
    I think a 4MHz crystal with 2 x 22pF capacitors is a good choice to get started. The fuses will have to be set for HS mode but is easy to set up. This will give you an easy to work with 1MHz instruction rate.
     
     
    If you aren't going to use the watch dog timer, it would be best to disable it in the fuses. This may save you some problems with false triggering while you learn some of the art of PIC programming.
     
    This code to set the fuses can be placed under the #include statement.
     
    ; PIC16F877A Configuration Bit Settings
    ; CONFIG
    ; __config 0xFF7A
     __CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF
     
    or, for RC Mode
     __CONFIG _FOSC_EXTRC & _WDTE_OFF & _PWRTE_ON & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF

    You have floating inputs for most of the PIC pins. These would be better set as outputs set to low state, or left as inputs with weak pullups enabled, or inputs with external (say 10K) pull up or pull down resistors. Floating inputs can cause problems with the processor.
     
    You assumed that STATUS,6 was clear at the start of your code. It should have been on reset, but who knows??
    Better to have...
          bcf STATUS,RP0 ; RAM Page 0
          bcf STATUS,RP1 ; RP0/1 are defined in the p16F877a.inc file
     
    Better yet, let the compiler sort out RAM page setting.
    This will generate the same STATUS code as above

           BANKSEL PORTA  ; RAM Page 0
     
    Then initialise all ports
         clrf PORTA     ; all ports = low
          clrf PORTB
          clrf PORTB
          clrf PORTB
          clrf PORTB

    I would also put in...
          BANKSEL TRISA ; RAM page 1
          clrf TRISA  ; all ports = outputs
          clrf TRISB
          clrf TRISC
          clrf TRISD
          clrf TRISE
     
    After TRIS is set, for your simple code, all the rest of your registers are in RAM bank 0 so no need to change anymore after this.
          BANKSEL Cnt0  ; RAM page 0
     
    Your delay code needs changing to work as you expect.
    Delay
           movlw b'11111111'
           movwf cnt0
           movwf cnt1
    Loop1
           decfsz cnt0,f
           goto Loop1
           decfsz cnt1,f
           goto Loop1
           return
     
    With a 4MHz crystal this delay will be nearly 200mS. For a longer delay use Cnt2 in the delay subroutine.
     
    Delay
           movlw b'11111111'
           movwf cnt0
           movwf cnt1
           movlw 5h ; 5 x 200mS is about 1 second
           movwf cnt2
    Loop1
           decfsz cnt0,f
           goto Loop1
           decfsz cnt1,f
           goto Loop1
           decfsz cnt2,f
           goto Loop1
           return
     
    You can power the circuit from the PICkit3, but I would only flash a single LED otherwise the PICkit3 may complain of excessive current being drawn.
     
    In IPE, click the Power Button and check the [Power Target Circuit from tool] item.
     
    Example
    flashLoop
         movlw b'00000001' ; flash LED on PORTB 0 pin
         movwf PORTB
         call Delay
         clrf PORTB
         call Delay
         goto flashLoop
     
    cheers
     
    Tony
     
    post edited by teenix - 2019/10/11 20:21:56
    #9
    NorthGuy
    Super Member
    • Total Posts : 5754
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: online
    Re: pic16f877a micro controller circuit problem 2019/10/11 18:11:51 (permalink)
    +2 (2)
    When you turn LEDs on and off quickly, your eyes cannot follow the blinking. So LEDs look steady, as they were always on, but with less brightness. I guess this is what you see with your design. Due to a bug here:
     
    LOOP1
    CLRWDT
    DECFSZ CNT0,1
    GOTO LOOP2

     
    your delay becomes very short, and your LED blinks so quickly that you think it's steadily on.
     
    post edited by NorthGuy - 2019/10/11 18:13:08
    #10
    slhulk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/10/11 08:45:41
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/12 00:51:35 (permalink)
    0
    ric
    As pointed out, the voltage divider power supply was a bad idea.
    Powering from the PK3 should be possible.
    You must have some small capacitors close to the PIC on the power supply pins as already described. If you have too much, it will slow down the rise time of the power supply, causing the problem you describe.
     
    Do you HAVE to use a PIC16F877A?
    There are newer, cheaper PICs available in the same package which are much easier to get going and debug,
     
     


     
    I'm just starting with pic so I believed that I could get the system up and working with a 9v battery. However, I ordered a development board recently and once it comes 90% of my problems will be solved hopefully (power supply, oscillator). 
     
    In the meantime, can you suggest a good pic MCU for a beginner? I went for the pic16f877a because it had many I/O pins and due to the 8 Analog pins. Is there a chip that comes with the above pin specifications with an internal oscillator?
    #11
    slhulk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/10/11 08:45:41
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/12 00:53:42 (permalink)
    0
    teenix
    I would suggest using a 5V regulator for powering the circuit. The supply to the PIC will be a bit unstable when circuit currents change. For example if all the LEDs come on, the PIC supply voltage will drop due to the current flowing through R1.
     
    The RC mode of operation is ok, but is dependant on many factors and will change frequency. There are charts of data in the AC Specs section of the data sheet which should give RC values to use.
     
    I think a 4MHz crystal with 2 x 22pF capacitors is a good choice to get started. The fuses will have to be set for HS mode but is easy to set up. This will give you an easy to work with 1MHz instruction rate.
     
     
    If you aren't going to use the watch dog timer, it would be best to disable it in the fuses. This may save you some problems with false triggering while you learn some of the art of PIC programming.
     
    This code to set the fuses can be placed under the #include statement.
     
    ; PIC16F877A Configuration Bit Settings
    ; CONFIG
    ; __config 0xFF7A
    __CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF
     
    or, for RC Mode
     __CONFIG _FOSC_EXTRC & _WDTE_OFF & _PWRTE_ON & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF

    You have floating inputs for most of the PIC pins. These would be better set as outputs set to low state, or left as inputs with weak pullups enabled, or inputs with external (say 10K) pull up or pull down resistors. Floating inputs can cause problems with the processor.
     
    You assumed that STATUS,6 was clear at the start of your code. It should have been on reset, but who knows??
    Better to have...
         bcf STATUS,RP0 ; RAM Page 0
         bcf STATUS,RP1 ; RP0/1 are defined in the p16F877a.inc file
     
    Better yet, let the compiler sort out RAM page setting.
    This will generate the same STATUS code as above

          BANKSEL PORTA  ; RAM Page 0
    Then initialise all ports
         clrf PORTA     ; all ports = low
         clrf PORTB
         clrf PORTB
         clrf PORTB
         clrf PORTB

    I would also put in...
         BANKSEL TRISA ; RAM page 1
         clrf TRISA  ; all ports = outputs
         clrf TRISB
         clrf TRISC
         clrf TRISD
         clrf TRISE
     
    After TRIS is set, for your simple code, all the rest of your registers are in RAM bank 0 so no need to change anymore after this.
          BANKSEL Cnt0  ; RAM page 0
     
    Your delay code needs changing to work as you expect.
    Delay
          movlw b'11111111'
          movwf cnt0
          movwf cnt1
    Loop1
          decfsz cnt0,f
          goto Loop1
          decfsz cnt1,f
          goto Loop1
          return
     
    With a 4MHz crystal this delay will be nearly 200mS. For a longer delay use Cnt2 in the delay subroutine.
     
    Delay
          movlw b'11111111'
          movwf cnt0
          movwf cnt1
           movlw 5h ; 5 x 200mS is about 1 second
           movwf cnt2
    Loop1
          decfsz cnt0,f
          goto Loop1
          decfsz cnt1,f
          goto Loop1
          decfsz cnt2,f
          goto Loop1
          return
     
    You can power the circuit from the PICkit3, but I would only flash a single LED otherwise the PICkit3 may complain of excessive current being drawn.
     
    In IPE, click the Power Button and check the [Power Target Circuit from tool] item.
     
    Example
    flashLoop
         movlw b'00000001' ; flash LED on PORTB 0 pin
         movwf PORTB
         call Delay
         clrf PORTB
         call Delay
         goto flashLoop
     
    cheers
     
    Tony
     




    Hi, 
    Thank you for the lengthy explanation.
     

    'You have floating inputs for most of the PIC pins. These would be better set as outputs set to low state, or left as inputs with weak pullups enabled, or inputs with external (say 10K) pull up or pull down resistors. Floating inputs can cause problems with the processor.'
     
    can you please elaborate on this more, please. 
    I will try out all the tips you suggested.
    #12
    slhulk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/10/11 08:45:41
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/12 00:54:44 (permalink)
    0
    NorthGuy
    When you turn LEDs on and off quickly, your eyes cannot follow the blinking. So LEDs look steady, as they were always on, but with less brightness. I guess this is what you see with your design. Due to a bug here:
     
    LOOP1
    CLRWDT
    DECFSZ CNT0,1
    GOTO LOOP2
     

     
    your delay becomes very short, and your LED blinks so quickly that you think it's steadily on.
     




    wow. didn't notice this. Thanks you
    #13
    teenix
    Starting Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2017/12/21 13:47:21
    • Location: Australia, Melbourne
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/12 01:30:28 (permalink)
    +2 (2)
    Floating inputs are IO pins that are set as inputs but have nothing attached to them. In this state and because they are high impedance, they will "pick up" stray electrical fields and can toggle logic states, sometimes very fast. This can upset the processor and it may do weird things.
     
    Therefore, to stop this happening, tie those pin to a power rail via a resistor. It is not wise to directly tie them to a power rail because something might change a TRIS register and if it is suddenly sets to a LO output connected to the +ve supply, it will cause a short and may damage the chip.
     
    If you want to keep PORTB free for programming, change your single LED to operate from any other port. ie PORTA PORTC etc. Just replace PORTB in your code.
     
    You can then set PORTB as all inputs in your TRIS sections as
     
          BANKSEL TRISA
          clrf TRISA
          movlw b'11111111'
          movwf TRISB
          clrf TRISC
          clrf TRISD
          clrf TRISE
     
    Then you can activate the internal pullups on PORTB to keep them happy.
     
          BANKSEL OPTION_REG
          bcf OPTION_REG,RPBU ; check out the data sheet for explanation
     
          BANKSEL cnt0
          etc.
     
    A nice little PIC to play with and which has a programmable internal oscillator ready to go is the PIC16F18345. It only has 20 pins but with up to 18 IO pins, and has lots of ROM/RAM/EEPROM and has many peripherals like A2D RS232 etc to use and it only costs a few dollars. It has some differences like enabling the peripheral before using them through the PMDx and PPx registers but you should have plenty of help to sort that out as you go.
     
    If you get one, let me know and I'll put some simple LED flash code together for you to figure out with the help of a data sheet. It will run direct from PICkit3 so will be easy to experiment.
     
    cheers
     
    Tony
     
    post edited by teenix - 2019/10/12 04:14:35
    #14
    jean12
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2015/11/29 00:17:07
    • Location: 0
    • Status: offline
    Re: pic16f877a micro controller circuit problem 2019/10/14 07:58:07 (permalink)
    0
    With PIC Catalog you can identify easily the pin which gives you ADC functionalities.
     
    Kindly consider.
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5