My MicroC program to PIC16F877A does not work in hardware, please help

Discussion in 'Embedded Systems and Microcontrollers' started by Vincent Ng, Dec 7, 2015.

  1. Vincent Ng

    Thread Starter New Member

    Dec 7, 2015
    dsa.png This is my program to a Stepper motor using ULN2003A(using STP-ULN2003 which used in arduino) , sensor ( replace as rain sensor module in hardware) , and led ( water pump in hardware) , but when after I write the program into the PIC16F877A it does not work , here is my coding :

    Code (C):
    2. #define STEP1 0b00010010
    3. #define STEP2 0b00000010
    4. #define STEP3 0b00000110
    5. #define STEP4 0b00000100
    6. #define STEP5 0b00001100
    7. #define STEP6 0b00001000
    8. #define STEP7 0b00011000
    9. #define STEP8 0b00010000
    10. #define SENSOR1 PORTD.F0
    11. #define SENSOR2 PORTD.F4
    12. #define PUMP1 PORTC.F0
    13. #define PUMP2 PORTC.F5
    16. void main() {
    18.   unsigned int days,t1,t2;
    19. TRISD = 0x11;
    20. TRISB =   0x00;
    21. PORTB = 0;
    22. TRISC = 0;
    23. PORTC = 0;
    25. while(1)
    26. {
    27. for(days=1; days<=2; days++){
    28.   PORTB = STEP1;
    29.   Delay_ms(500);
    30.   PORTB = STEP2;
    31.   PORTB = STEP3;
    32.   Delay_ms(500);
    33.   PORTB = STEP4;
    34.   Delay_ms(500);
    35.   PORTB = STEP5;
    36.   Delay_ms(500);
    37.   PORTB = STEP6;
    38.   PORTB = STEP7;
    39.   Delay_ms(500);
    40.   PORTB = STEP8;
    41.   Delay_ms(500);
    42.   PORTB = STEP1;
    43.   }
    44.   Delay_ms(1000);
    45.   PUMP2 = 1;
    46.   t2=0;
    47.   do{
    48.       Delay_ms(1000);
    49.       t2++;}while(SENSOR2 == 0 && t2<=5);
    50.       PUMP2 = 0;
    51.   PUMP1 = 1;
    52.   t1=0;
    53.   do{
    54.       Delay_ms(1000);
    55.       t1++;}while(SENSOR1 == 1 && t1<=5);
    56.       PUMP1 = 0;
    59. }
    60. }
    so is there any problem that i should check for microC setting or Proteus or PIC setting ? The simulation works perfectly
    The voltage I supply to PIC is about 3.5V-4V with both Vdd and Vss supplied, i also had crystall oscillator 4MHz ready but it still does not work even I try with a simple LED light to replace the relay

    Moderators note : Used code tags for C
    Last edited by a moderator: Dec 7, 2015
  2. JohnInTX


    Jun 26, 2012
    When things simulate but don't run in hardware its frequently due to:

    1)Not setting the chip's configuration bits correctly - MikroC is bad in this regard because it does not show what the config bits are in the source. What are the config bits set to?
    2)Be sure that the programmer is actually programming the config bits. If you use a non-MikroC programmer or don't use MikroC's IDE, the config bits can be left out.
    3)Power not applied to all of the Vdd/Vss pins.
    4)You mention an oscillator but don't show one on the schematic. What kind of oscillator? If you are using a crystal or resonator, be sure that the configuration bits are set accordingly (use XT or HS). If you are using an external oscillator, be sure that its enabled and feeding OSC1 on the PIC. See section 14.2 in the databook. You can see if the oscillator is running by measuring OSC2 with a digital volt meter. If you have roughly 1/2 Vdd, its running. A scope is much better for this.
    5)Be sure that the watchdog timer is turned OFF since you don't clear it in code.
    6)Be sure that the brown-out detector is turned OFF since you are running a lower voltage. BOR will hold the PIC in reset below about 4Vdd unless its turned off. See sections 14.1 and 14.8.

    You should know that Proteus is notorious for simulating OK on a circuit that could never run in hardware. A good thing to do first is to write a very simple program that flashes an LED on an output port. If you get that working, you at least know that the PIC's configuration is OK to run.

    Good luck and welcome to AAC!
  3. Sensacell

    Well-Known Member

    Jun 19, 2012
    Just looking at your circuit-

    Driving a relay coil directly from an IO pin is probably not going to work, the pins can only source a few mA.

    Your SENSOR inputs are left floating when the sensor is open- add pull-down resistors.

    The LED's are shown without current limiting resistors? They will blow if directly powered from +5V

    Make the bypass capacitor bigger - 0.1 uF

    Check all your CONFIG bit settings- sometimes the hardest part of a programming project is just getting the damn hardware to RUN at all the first time. Check all the oscillator settings first.
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    What does "does not work"??????? Does it not program? Not run at all? What have you done to troubleshoot? We can point out flaws in the design and help with a specific section but you need to tell us where to look. This is a fairly complicated project. The best thing to do is to break it into smaller sections. Get your most simple as tion working first then start adding to the project.