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

Thread Starter

#### Vincent Ng

Joined Dec 7, 2015
6
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 :

C:
#define STEP1 0b00010010
#define STEP2 0b00000010
#define STEP3 0b00000110
#define STEP4 0b00000100
#define STEP5 0b00001100
#define STEP6 0b00001000
#define STEP7 0b00011000
#define STEP8 0b00010000
#define SENSOR1 PORTD.F0
#define SENSOR2 PORTD.F4
#define PUMP1 PORTC.F0
#define PUMP2 PORTC.F5

void main() {

unsigned int days,t1,t2;
TRISD = 0x11;
TRISB =   0x00;
PORTB = 0;
TRISC = 0;
PORTC = 0;

while(1)
{
for(days=1; days<=2; days++){
PORTB = STEP1;
Delay_ms(500);
PORTB = STEP2;
PORTB = STEP3;
Delay_ms(500);
PORTB = STEP4;
Delay_ms(500);
PORTB = STEP5;
Delay_ms(500);
PORTB = STEP6;
PORTB = STEP7;
Delay_ms(500);
PORTB = STEP8;
Delay_ms(500);
PORTB = STEP1;
}
Delay_ms(1000);
PUMP2 = 1;
t2=0;
do{
Delay_ms(1000);
t2++;}while(SENSOR2 == 0 && t2<=5);
PUMP2 = 0;
PUMP1 = 1;
t1=0;
do{
Delay_ms(1000);
t1++;}while(SENSOR1 == 1 && t1<=5);
PUMP1 = 0;

}
}
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:

#### JohnInTX

Joined Jun 26, 2012
4,379
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!

#### Sensacell

Joined Jun 19, 2012
2,758
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.

#### spinnaker

Joined Oct 29, 2009
7,835
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.

Similar threads