CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

What's wrong with my code?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
theredkid



Joined: 27 Aug 2014
Posts: 14

View user's profile Send private message

What's wrong with my code?
PostPosted: Wed Aug 27, 2014 6:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 27, 2014 7:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 27, 2014 8:39 am     Reply with quote

Yes, it does compile but no led flashes.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 27, 2014 12:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 27, 2014 11:57 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 12:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 12:27 am     Reply with quote

Is there a way in this forum to post schematic?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 12:32 am     Reply with quote

Post your schematic on a free image hosting website. Examples:
http://postimage.org
http://tinypic.com
https://imageshack.com
Then post a link to it here.
theredkid



Joined: 27 Aug 2014
Posts: 14

View user's profile Send private message

Schematics
PostPosted: Thu Aug 28, 2014 12:50 am     Reply with quote

Here comes schematics. The PIC and RELAY are connected to each ther via big black arrows shown in the images. I think I didn't write any code for the register CD 4094. Might this be the reason why my relay doesn't blink?

http://tinypic.com/view.php?pic=2mxfm7r&s=8#.U_7QqsV_st0

http://tinypic.com/view.php?pic=ddc0tt&s=8#.U_7Q2sV_st0
Ttelmah



Joined: 11 Mar 2010
Posts: 19448

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 2:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 11:42 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 2:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 2:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 7:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 10:33 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group