View previous topic :: View next topic |
Author |
Message |
theredkid
Joined: 27 Aug 2014 Posts: 14
|
What's wrong with my code? |
Posted: Wed Aug 27, 2014 6:18 am |
|
|
Hello,
Could you maybe check my following ccs c code used to control a relay driver for turning on/off leds. Unfortunately does not work.
Code: | #include <18F452.h>
#define Fosc 16000000 //Baudrate
#use delay(clock=Fosc,crystal=4000000)
#fuses H4,PROTECT,BROWNOUT,PUT,NOWDT,NODEBUG,NOWDT,NOCPD,CCP2C1,STVREN,NOLVP
void OneSC()
{
delay_ms(1000);
}
void main()
{
unsigned char a;
set_tris_b(0x00);
output_b(0x01);
while(1)
{
OneSC();
for(a=1;a<6;a++)
{
output_b(0x03); // Sadece RELAY-01 ve RELAY-02 etkin kılınıyor...
delay_ms(250); // 250ms bekle
output_b(0x01); // Sadece RELAY-01 etkin kılınıyor...
delay_ms(250); // 250ms bekle
}
OneSC();
output_b(0x05);
Delay_ms(6000);
output_b(0x01);
OneSC();
output_b(0x09);
Delay_ms(3000);
output_b(0x01);
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Aug 27, 2014 7:50 am |
|
|
What does not work?
What does work?
Does the code compile?
Can you do an LED flasher?
Give us some clues.
Mike
And learn to use the code button |
|
|
theredkid
Joined: 27 Aug 2014 Posts: 14
|
|
Posted: Wed Aug 27, 2014 8:39 am |
|
|
Yes, it does compile but no led flashes. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 27, 2014 12:38 pm |
|
|
I ran your program in hardware and it works. It works. It turns on, or
blinks a sequence of 3 Leds. I tested this on a PicDem2-Plus board (old
version) with compiler vs. 5.026.
Do any of your LEDs turn on ? If not, the first thing to do, is to get rid
of your flasher program and make a very short program to turn on
one LED only.
Each LED must have a series resistor (any value from 220 to 1K ohms).
The cathode of the LED (longest lead) should go to ground, if you want
a high level on the PIC pin to turn on the LED. Example:
Code: |
pin 470 ohms LED
B0 -----/\/\/\/------->|----
|
|
----- Ground
---
- |
Also, the 18F452 is an older PIC and it has the restriction that to make
the PLL mode work (after programming it), you must remove the power
from your board, and then plug power back in again. The PIC requires
a Power-on-reset cycle to occur before the PLL is activated. |
|
|
theredkid
Joined: 27 Aug 2014 Posts: 14
|
|
Posted: Wed Aug 27, 2014 11:57 pm |
|
|
The thing is that when I power the relay board, the leds turn on like a lamp, but no blinking meanwhile. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 28, 2014 12:15 am |
|
|
Possible reasons:
1. Your board's power supply has insufficient output current to drive relay
coils, and the PIC's Vdd voltage droops to a lower level. Improve the
power supply.
2. Your relay coils are connected directly to PIC pins - but the PIC pins
have insufficient ability to sink the relay coil current. Use a relay driver
chip.
3. You didn't install a catch diode across the coil of each relay, with
the cathode going to the relay's supply voltage and the anode to the
other side of the relay.
4. Poor board layout, with narrow power and ground tracks.
5. Lack of bypass capacitors on the PIC's Vdd pins to ground (100 nF). |
|
|
theredkid
Joined: 27 Aug 2014 Posts: 14
|
|
Posted: Thu Aug 28, 2014 12:27 am |
|
|
Is there a way in this forum to post schematic? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
theredkid
Joined: 27 Aug 2014 Posts: 14
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Thu Aug 28, 2014 2:39 am |
|
|
First thing, the 2803, has it's own clamp diodes built in, and these have a sensible speed and power rating for it's design load. You need to connect the relay supply to pin 10, and get rid of your own diodes across the relays.
You then have a crystal, with no load capacitors.
Then (the main problem....) your code bears no resemblance to your hardware...
Your hardware has a serial shift register that needs to be clocked. Your code is for the ULN being driven directly off port B on the PIC.
It looks as if you have taken one person's code, and a different set of hardware.
The shift register has the advantage of saving pins. If you need to do this, then you need to output the data serially. If not, then get rid of the 4094, and make the connections from the ULN driver inputs directly to port B on the PIC. |
|
|
theredkid
Joined: 27 Aug 2014 Posts: 14
|
|
Posted: Thu Aug 28, 2014 11:42 pm |
|
|
Will it be enough if I just clock shift register or do I have to control both shift register and ULN? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Aug 29, 2014 2:07 am |
|
|
theredkid wrote: | Will it be enough if I just clock shift register or do I have to control both shift register and ULN? | You do not have to ask this sort of question if you read and understand the the data sheets.
Driving the 4094 is more complex than simply clocking it.
Since you are having to ask this sort of question do as Mr T. suggested.
I.e. get rid of the 4094 and connect the PIC directly to the ULN part.
At your level it will be much easier to make progress.
When you have more expertise put the 4094 back in.
Mike |
|
|
theredkid
Joined: 27 Aug 2014 Posts: 14
|
|
Posted: Fri Aug 29, 2014 2:13 am |
|
|
Unfortunately my boss insists I do this way. You can understand my level from overseas but he can't even tho he is here. We'll see how it goes. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Aug 29, 2014 7:37 am |
|
|
Too bad your boss makes you work like this.
Besides the missing capacitors with the crystal there is another large problem I see in the schematic: the 4094's output enable is hard enabled (with an RC-delay on startup). This means that shifting new data into the 4094 will cause the relays to short change states during the shifting. When the shifting is performed at high speed the mechanical relays will have enough physical delay for this to be noticed, but I consider it poor design.
It seems like all pins of the PIC18F452 are used and this is a means of saving a few pins.
Still, considering the PIC18F452 is very old and not recommended for new designs, I would advice to choose one of the newer chips with more pins to spare. Those newer chips are cheaper and have lots more features. Nowadays SMD soldering is cheaper than through-hole so the PCB can be made smaller and perhaps more advantages are to be achieved by a redesign. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Aug 29, 2014 10:33 am |
|
|
OK. So we now know a bit more about where you are coming from.
Assuming you really are stuck with the design, tackle the problem in small, easy to manage stages.
Get each stage to work correctly before progressing to the next.
Start by wiring temporary LEDs to each control line you intend using for the 4094, (i.e. Clock, Data, Strobe, Output Enable)
First get each LED to flash at a well defined speed, (say once per second).
Do not proceed until you are satisfied it is perfect.
Then using the data sheet work out how to drive signals into the 4094.
Do this at slow speed so you can SEE the LEDs flash in the correct sequence.
At this stage you SHOULD be able to control the relays to order.
Then crank up the speed, (you may now need a 'scope to follow what is going on).
Continue until the job is done.
Mike
Like ckielstra says there are much better ways to do things.
I would also want to do something about the output enable being hard-wired on.
Otherwise you have NO control over what happens at start-up. |
|
|
|