# Noob project needs some critique. EagleCADsoft file attached

#### jameschristian

Joined Nov 24, 2011
58
Hey all,

I am working on a project that tells a fan to turn on for different periods of time according to how a rotary switch is set. Attached is the Eagle CADsoft file for it. It's a first pass at the schematic, and you'll probably see something I need to change to get it to work. I am using a pic16f84 chip to run the thing, and the resistor you see on the schematic represents the fan. I hope I got it right, but if not, your feedback would be greatly appreciated. Thanks in advance!

p.s. I just realized I left off a switch to turn the thing on and off, so I'll be including that for my next go around.

#### Attachments

• 65.3 KB Views: 24

#### nerdegutta

Joined Dec 15, 2009
2,676
Hi,

My Eagle don't display anything...

You could:

-> click FILE
-> click EXPORT
-> click IMAGE

and saveit as a .PNG-file, and upload that...

#### jameschristian

Joined Nov 24, 2011
58
Sry about that, don't know why it won't show, attached is a .png file for you.

#### Attachments

• 6.6 KB Views: 57

#### MrChips

Joined Oct 2, 2009
21,843
Your Xtal is connected incorrectly and you should pullup MCLR to Vcc via a 10K resistor.

#### jameschristian

Joined Nov 24, 2011
58
How should I have connected the xtal? And I had a feeling that the MCLR needed connecting, so thanks a ton for the tip.

#### SgtWookie

Joined Jul 17, 2007
22,220
Here, have a look at the attached.

You can't feed 9v directly to a PIC; they need ~2.5v-5.5v to run.

I threw in a power switch, a 5v regulator, and caps for them

I reoriented your crystal, renamed it, and gave it a couple of caps.

You had both power and ground going to the rotary switch. That wasn't good - there was no connection for power.

Instead, use the PIC's internal weak pull-ups, and use the switch to connect the inputs to ground.

I got rid of the unnecessary wires running everywhere and used Vss/Vdd from supply2.lbr.

I don't know where you got R1 from. Since you didn't really need it anymore, I added R1 from rcl.lbr for the MCLR input.

#### Attachments

• 120.5 KB Views: 30

#### jameschristian

Joined Nov 24, 2011
58
Thanks Sgt. Wookie, et al. I saw what you did with the xtal, the voltage regulator and the filtering capacitors, and that all makes sense. The R1 was initially supposed to represent the fan, with one side connected to the R0 out from the uC so as to control when it comes on and for how long. I am still tinkering with the programming but you've helped me along the way. Thanks again for helping me with this project. Regards to you and yours.

#### jameschristian

Joined Nov 24, 2011
58
I'd like to add a 'solved' to the title of the post, but can't find a link to do that. Is that possible on this forum?

#### SgtWookie

Joined Jul 17, 2007
22,220
You don't have to change the topic title.

However, if you must - Click "Edit" on your original post, and then click the "Go Advanced" button on the bottom. You should be able to change the title. Please leave the original subject, as it's a good one - just add the "solved" thing on the end if you would.

#### jameschristian

Joined Nov 24, 2011
58
I don't see an edit button on the original post anymore. I read a forum post that they had taken away the ability to edit posts after a certain amount of time, so is it something that junior members can't do? Also, do you know somewhere i can send my schematic to actually get this thing made? I don't mind doing the soldering, programming, and placement into an enclosure myself on a small scale, but if I can sell a few of these things I'd like to be able to not have to worry about doing all the work if the numbers get high enough. Thanks again

#### SgtWookie

Joined Jul 17, 2007
22,220
I think that members with less than 10 posts have a number of restrictions on them; this is part of the effort to combat spammers. Once you get to 100 posts, you will have less restrictions than that.

If you want to do it "on the cheap", you might try Batchpcb. you may have to wait a month before your board(s) are paneled with other people's projects - but that's one of the reasons it's cheap.

better get a few prototypes made before you buy a lot of them - or you will likely wind up with a lot of "coasters".

#### nerdegutta

Joined Dec 15, 2009
2,676
How does the board look?

Where in the world are you?

#### jameschristian

Joined Nov 24, 2011
58
Attached is my preliminary board layout. As this is my first project, and something that I would like to actually make, any thoughts on inductive problems or something of that ilk would be great. Also, I noticed the footprint of the crystal is rather large. Is that normal and will it affect the build? Also, I am having trouble with the programming of the PIC itself as I cannot find any good and thorough resource on C programming for pics online. I've included some ode below that I typed out this afternoon which I think is close to being right, but there are some parts that obviously aren't. Anyone out there program PIC's?
Rich (BB code):
void main ()
TRISA=0
PORTA=1      //    this command syntax is wrong, but I need to have the Vcc pin turned to input (if that's even necessary to do).
// I also need to get the RA0 to output to power the fan

if (TRISA=1)      // didd this to say 'if the power switch is on, proceed'
{
if (PORTA1=1)        //   this is supposed to be the port that the first pin of the rotary switch is connected to
{
delay_ms(6*10e3)    //   one minute of fan time,
and I know the delay cmd is wrong,
//    but is there something similar that will     turn the port on for a given time without having to compute a bunch of NOPS?
else if (PORTA2=1)
{
delay_ms(12*10e3)
}                                //     here the 'on' time would be for two minutes
else if (PORTA3=1)         //     this syntax may be wrong to designate the RA1-7 ports,
//      but it's the best I can do for the moment
{
delay_ms(30*10e3)    //five minutes this time
}
else if (PORTA4=1)        // you get the idea....
}
return(0)
By the by, I live in the southern U.S.
And I can't attach another file yet apparently. Oh well. I'll try to upload a screenshot and post a link.

Last edited:

#### jameschristian

Joined Nov 24, 2011
58

#### jameschristian

Joined Nov 24, 2011
58
Did some research and got this far....

Rich (BB code):
#include <pic.h>

void main()
{
TRIS A==0       //sets output to fan
PORTB==0       //CLEARS PORT B

if (TRISB0==1)
{
TRISA==1
delay_ms(6*10E3);     //ONE MINUTE ON
}
else if (TRISB1==1)
{
TRISA==1
delay_ms(12*10E3);      //2 MINUTES ON
}
ELSE IF (TRISB2==1)
{
TRISA==1
delay_ms(30*10e3)       //5 MINUTES ON
}
else if (TRISB3==1)
{
TRISA==1
delay_ms(60*10e3)         //10 MINUTES ON
}
else if (TRISB4==1)
{
TRISA==1
delay_ms(120*10e3);      //20 MINUTES ON
}
else if (TRISB5==1)
{
TRISA==1
delay_ms(180*10e3)       //30 MINUTES
}
else if (TRISB6==1)
{
TRISA==1
delay_ms(360*10e3);      //ONE HOUR ON
}
else if (TRISB7==1)
{
TRISA==1
delay_ms(720*10e3)     //2 HOURS ON
}
}
return ()
Do you think this will run correctly? I will compile it tonight or tomorrow and let you know if I get any errors. Here's hoping...

Last edited:

#### SgtWookie

Joined Jul 17, 2007
22,220
You were missing a bunch of semicolons.
It's much easier to read C code if it is indented properly.
There is no loop for the code; it'll run through once and crash into the end.

I don't know what the prototype is for the delay_ms(numbertype) function; if it is an integer, the max ms will be 32767; unsigned int will be 65535. 60*10E3 will overflow the 1st, 120*10e3 will overflow the 2nd.
You need to create a function called delay_sec() that accepts an integer, and executes a for loop that calls delay_ms(1000); something like:

Rich (BB code):
void delay_sec(int secs)
{
for(int ms=0;ms<secs;ms++)
{
delay_ms(1000);      // delay 1 second
}
}
return ();
As a bonus, you will eliminate a lot of multiply operators, which otherwise would have caused "code bloat". Multiplication operations are replaced with lots of code that does multiple adds in a loop. Using a subroutine like this means there is only one loop that does the adds.

Rich (BB code):
#include <pic.h>

void main()
{
TRIS A==0;      //sets output to fan
PORTB==0;       //CLEARS PORT B

if (TRISB0==1)
{
TRISA==1;
delay_ms(6*10E3);     //ONE MINUTE ON
}
else if (TRISB1==1)
{
TRISA==1;
delay_ms(12*10E3);    //2 MINUTES ON
}
ELSE IF (TRISB2==1)
{
TRISA==1;
delay_ms(30*10e3);    //5 MINUTES ON
}
else if (TRISB3==1)
{
TRISA==1;
delay_ms(60*10e3);    //10 MINUTES ON
}
else if (TRISB4==1)
{
TRISA==1;
delay_ms(120*10e3);   //20 MINUTES ON
}
else if (TRISB5==1)
{
TRISA==1;
delay_ms(180*10e3);   //30 MINUTES
}
else if (TRISB6==1)
{
TRISA==1;
delay_ms(360*10e3);   //ONE HOUR ON
}
else if (TRISB7==1)
{
TRISA==1;
delay_ms(720*10e3);   //2 HOURS ON
}
}
return ();

Last edited:

#### SgtWookie

Joined Jul 17, 2007
22,220
Here's a link to a .png of the project as it looks now: http://www.flickr.com/photos/71727093@N06/6478975069/
I put the battery connector near the top so as to make it easier once the enclosure is built to attach a nine volt.
You need to upload your current schematic and board files. They need a little help.

The crystal library model you used is HUGE! Does your actual crystal look like that?

#### jameschristian

Joined Nov 24, 2011
58
Ok, first of all, I need to just say thanks. I have been a few boards (sorry old habit of calling a forum a bulletin board) and this one has been by far the most helpful. So just know that you guys are great. I wish you would teach a class online so I could just soak up the knowledge, Sgt. Wookie, because you're above and beyond my prof here. They are phasing out teaching about microcontrollers and he couldn't care less about my little project. Ok, nuff said on that.

As per the crystal, yes, it is huge, and no, that is not what my crystal looks like. I bought a bag of them on ebay a few months ago, they are 20Mhz crystals and look like little silver 'half cylinders' (I am sure you know what I am talking about) with two leads and a flattened top. I just didn't know the part number and couldn't look it up because it's been too long for ebay's servers to carry the info any more. I uploaded my board and schematic file for your perusal, and am wondering if there are going to be inductance issues somewhere. I am trying to limit the PCB in terms of size because of cost, but if I need to rearrange things to get them to work properly, then of course I will do so.

Again, thank you for being helpful when others were not. It is a great boon to guys and gals like me that guys and gals like you will take a few minutes to give gentle correction rather than the flame sessions I have encountered on other sites. I will work on the code in a few minutes and post my revisions there too. Regards.

#### Attachments

• 14.8 KB Views: 8
• 120.5 KB Views: 8

#### SgtWookie

Joined Jul 17, 2007
22,220
We try hard to remember that we were also beginners once upon a time.

I try to help people as I would have liked to have been helped back in the day.

OK, what is the actual distance between the two pins on your crystal?

Better yet, can you create a simple .sch called "MyCrystal" and simply draw the image in the schematic? Turn the grid on, and measure the crystal carefully, and replicate what it looks like in the schematic drawing area. Most important is the "footprint" - how much actual area it will cover on the board, the spacing of the two leads, and exactly where the leads are in the footprint.

Do the best you can. We can go back and fourth a few times until you are happy with it.

I have to leave for awhile, but I'll be back later.