An Introduction to LTSpice

7 January, 2009 (20:25) | Tutorials | By: Chris

LTSpice

LTSpice (download from Linear Technology here) is our primary SPICE tool.  It’s the best free spice tool I’ve ever used, so I wanted to introduce it to any of you who haven’t used it and who aren’t familiar with SPICE in general.  I’m going to use the inverting amplifier as an example since I’ll be writing about it in another post.  For the same reason I made video tutorials for EAGLE, I recommend watching the video for this tutorial as it is much easier to follow as you learn to use LTSpice.

Getting Started

After installing LTSpice, run it and open a new schematic.  A description of most of the toolbar buttons is below.

New Schematic – Open Schematic – Save – Control Panel – Run Simulation – Halt Simulation

LTSpice Toolbar 2

Wire – Ground (Common) – Label Net – Resistor – Capacitor – Inductor – Diode – Place Component

Move – Drag – Undo – Redo – Rotate – Mirror – Text Label – Spice Directive

Placing Components

Placing components such as resistors, capacitors, inductors, diodes and ground nodes is very easy.  Click the appropriate symbol along the top toolbar, then place it on the schematic.  To rotate or mirror the component, click the corresponding button on the toolbar or press Ctrl+R to rotate or Ctrl+E to mirror.  To place anything besides those components on the toolbar, press the component button on the toolbar.  This will open the components dialog.  From here you can select voltage sources, specialty diodes, transistors and opamps among other things.

The inverting amplifier consists of two resistors and an opamp.  To be able to test it, we’ll also need to include a voltage source (and grounds of course).  To place the opamp, click on the component button on the toolbar, then double click on the Opamps folder.  Scroll all the way to the right and select “opamp” from the list.  In the description it will tell you that this is an ideal opamp and that you must invoke the spice directive .lib opamp.sub in order to use this opamp.  Double-click on the ideal opamp and place it on the schematic.  Then click on the spice directive button, enter in .lib opamp.sub, press OK and place it on the schematic.

To place the voltage supply, click on the component button again, click the the folder-up button, then go to the right and double-click “voltage” and place the supply on the schematic.  To set the voltage source to something besides a DC value, click advanced and you’ll see other ways to define your voltage source.  I’ll cover these in a future post, but you can probably figure them out yourself between the voltage source dialog and the LTSpice help.  For now, enter 1 in DC Value and click OK.

Place a resistor between the positive side of the voltage source and the inverting input of the opamp.  Place another resistor between the inverting input and the output of the opamp.  To change the resistor values, right-click the resistor symbol or the R (not R1 or R2) and change the value.  I made them 100k and 1MEG to have a gain of 10 (you must enter MEG, as m or M is interpreted as milli-).  Place a ground on the noninverting input and on the negative side of the voltage source.

To connect all of the components, use the wire tool.  Click on the Label Net button and label the output net as out.  Your schematic should look similar to the one below.

Simulating

With the schematic complete, the next step is to si

RS232 (or UART) Communication between your PIC and your PC

4 December, 2008 (06:00) | PIC, Tutorials | By: Joshua

In a previous post, we went through the different aspects of a UART and how to set it up and use it to communicate either between PICs or other peripherals. In this post, I’d like to address the specifics of communicating between your PIC and your PC through Hyperterminal or PuTTY. It is a simple process but if you miss one thing, you’ll run into problems.

Setting Up the Hardware

First you need to realize that your PIC and your PC run at different voltage levels and you’ll need an RS-232 transceiver. The industry standard seems to be Texas Instruments’ MAX232 but you’ll find different manufacturers make pin to pin compatible ICs if you don’t like Texas Instruments. With the transceiver, your 0V to 5V PIC signal will change to a -15V to +15V PC voltage. In actuality, the voltage will be closer to ±9V, so don’t get too worried when you’re testing it. It should be noted that the MAX232 requires several external capacitors. Refer to the data sheet for more information.

It is important when wiring the MAX232 to make sure there is a common ground between the PIC, the MAX232, and the PC. You will need a female DB9 connector (a D-Sub) unless you want to randomly jab wires into the back of your PC (not recommended). You will need a minimum of three wires connected: Receive, Transmit, and Ground. The other pins have a function also but can be ignored (with the proper setup) until your projects become more complicated. You can learn the pinout by either going here or simply Googling it.

Setting Up the Software

Setting up Hyperterminal or PuTTY is almost the exact same. In Hyperterminal, you can choose your options as you set up a connection whereas PuTTY is setup under Connection->Serial. In your setup, you need to choose your COM port, most likely COM1 but depends on your computer. Set your speed or baud rate to whatever your PIC is running at, and leave your Data bits, Stop bits, and Parity at their default settings (8, 1, and none, respectively). However, the Flow control needs to be disabled, so set that to “None”. If you don’t disable that, the terminal will receive information from the PIC but won’t be able to send.

You’ll note that your terminals communicate in ASCII. If you tell your PIC to send a number, you’ll most likely get an odd symbol. If you’re using HI-TECH C, it has the stdio.h header file that can automatically change your number into its ASCII equivalent so you can actually understand it on the computer without a lookup table. I’ll give a brief description of how it works for an integer here.

Include the stdio.h file in your file. Create a char array with as many elements as digits you expect to display (if you think it’ll be <<999, create a 3 element array). We’ll call this char array my_number_in_ASCII. The number, be it a measurement or whatnot, we’ll call my_number. You then use sprintf like this:

sprintf(my_number_in_ASCII,”%d”,my_number);

Now, to transmit the number, my code would be:

for(i = 0; i < 3; i++)
transmit(my_number_in_ASCII[i]);

And it should show up perfectly in your terminal.

Now, this tutorial isn’t as exhaustive as it could be, so if there are any questions, feel free to post a comment or send me an e-mail and I’ll hopefully be able to help you out.

Using Eagle3D to Generate Virtual Images

28 November, 2008 (02:35) | Eagle, Tutorials | By: Chris

To visualize your boards, you’ll need to download Eagle3D and POVRay. Install those and you’ll be ready to go.

Eagle3D

A very useful tool written some time ago by Matthias Weißer, Eagle3D allows you to create a virtual three-dimensional image of your board.  It does this by using EAGLE’s built in scripting language, or User Language Programs (ULP for short).  It is a useful tool, and is pretty cool if nothing else.  I wish I had come across this when I was working on one of my first boards.  I had unknowingly reversed a power jack, and because the plug was so large, it took some creative soldering to be able to plug the power in without having my board remade.  It is also very handy in presentations, and can give people an idea of what the board should look like when it’s finished.  This is especially useful if you aren’t the one who will be soldering the board.  You can generate a virtual image of your board so whoever is soldering your board will know exactly what to expect.

To use Eagle3D, open up a completed board that you would like to visualize.  Click File, Run (or click the “ULP” button on the top menu bar), and navigate to the directory where you installed Eagle3D.  I used the default directory, and for me it was C:\Program Files\Eagle\ULP\Eagle3d.  From here, select the file 3d41.ulp (unless you’re using a pre 4.1 version of EAGLE, then select 3d40.ulp).  The first time this runs, you’ll see a menu prompting you to select a language.  After selecting your language, you’ll see another window a few different tabbed pages.  You can play with the options here to change what parts of your board are rendered and how, as well as change your virtual light sources and camera angles.  I just select my output path and click the button on the bottom to generate the pov file and exit Eagle3D.  With the pov file generated, we can now render the image in POVRay.

POVRay

Before attempting to generate your image, POVRay needs to know where to look for the parts.  Open the directory where you installed POVRay (probably “C:\Program Files\POVRay for Windows” or something similar), then open the Renderer folder.  Open the povray.ini file.  Now find the path to your Eagle3D povray libraries (for me, it was “C:\Program Files\Eagle3D\ulp\Eagle3D\povray”).  You’ll need to add this to the paths that POVRay looks in for rendering information.  In the povray.ini file, add the line Library_Path=”C:\Program Files\Eagle3D\ulp\Eagle3D\povray” (or whatever your povray path was).  Next, open the povray file of your board in POVRay and click run.  You should see the image begin to render.  It will save the image in the same directory as your povray file, and with the same name (but with a bmp extension).

Now you can visualize your board before you submit it to your manufacturer.  You’d be surprised at how many mistakes you can catch with such a simple step.

I hope you’ve enjoyed these tutorials so far.  I’ll be leaving EAGLE tutorials for a while to work on writing a few posts about common ICs such as the 555 timer, as well as op-amp topologies, filters, and other useful circuits.  That isn’t to say that I’m done with EAGLE, but I believe what is available is sufficient for now.  As always, feel free to ask me any questions by emailing me at chris@ece101.com.  If you have questions about EAGLE or anything else, I’ll do my best to answer them.

Setting up and Using the UART for the PIC

15 November, 2008 (21:37) | PIC, Tutorials | By: Joshua

In a previous post I explained many of the registers and bits used in the UART. This time we’re going to put those pieces together to make a functional UART.

First thing you need to set is the baud rate. The baud rate is dependent on the clock speed. It’s important to know what you want your baud rate to be before you start setting everything up. If you’re just testing it out between two PICs, it isn’t as important. However, if you’re trying to communicate with a peripheral, read the data sheet and make sure you know what speed it communicates at. An example is that the XBee only communicates at 9600 baud, so you need to work with that.

Let’s assume that we want that 9600 baud. We’ll want to look at the data sheet of the PIC we’re using, where we’ll find a couple pages of tables. These tables help us decide what settings we need to use at different clock frequencies.  It also shows the error of the communication speed, or the deviation of actual rate versus desired rate.  You’ll note that some clock frequencies yield better error rates than others.  On the 16F690, I’ll actually vary the clock signal according what gives me the best rate (this, of course, affects time sensitive operations and power consumption, but in some projects, those don’t concern me).  On the 18F877A, where I have a set frequency of 4 MHz in one of my current projects, I set BRGH even though I only want 9600 baud.  This is because I get a much, much better error rate with BRGH set and SPBRG = 25 instead of clearing BRGH and putting SPBRG = 6.  

Those are just some considerations while setting up your UART.  The actual code I use to transmit with a PIC16F690 setup for 9600 baud using an 8MHz clock is as follows:

 

    SPBRGH = 0; // Most significant byte, not available on 16F877A.
    SPBRG = 12; // Least significant byte
    BRG16 = 0; // No 16 bit communication, also not available on 16F877A
    BRGH = 0;  // 8 MHz isn't that fast, so we don't need to set this.
    SYNC = 0;  // Asynchronous
    SPEN = 1;  // Serial Port Enabled
    TXEN = 1;  // Transmit enabled
    TXREG = a; // Transmitting the requested character.

and the receive code:

    	SYNC = 0;
    	SPEN = 1;
    	BRG16 = 0; // 8-bit communication
    	BRGH = 0; // Setting up the transmit speed
    	SPBRGH = 0;
    	SPBRG = 12;
   	CREN = 1;
        while (RCIF == 0) // Wait loop while waiting to receive data
	       CLRWDT();
   	a = RCREG; // Read from the receive register

For transmission, you’ll most likely want some sort of indication that the UART is done transmitting, because if you try and transmit again before it is done, you will get an error. I would suggest using the TRMT bit to verify that a transmission has completed. The TRMT bit is set when it is empty, i.e. when it’s done transmitting.

I split it up into a “setup” type function and a “use” type function and have a little bit more tacked on in there, but those bare bones should work for you. You should also note that much of the order of those instructions aren’t that important, but others are. Once you get an intuitive feeling of what is going on, it’ll be easy to discern which one is which.

Let me know if anything doesn’t make sense or you have a specific question that I haven’t answered or you’re having any difficulties.

Understanding the A/D Converter of your PIC

20 October, 2008 (06:00) | PIC, Tutorials | By: Joshua

A built in analog to digital converter (A/D Converter or ADC) makes life easy when you’re using your PIC. As you know, an ADC is a powerful and even necessary tool in many projects. There are a lot of options, registers, and bits that can get confusing so this post will be dedicated to outlining and describing them. In the next post, we’ll briefly go over how to put them together to get everything functioning.

It should be noted that PICs use a sample-and-hold method for getting readings. In other words, the pin where you put the analogue signal is filling up a very small capacitor, which, when the conversion takes place, is disconnected from the pin. The voltage on this capacitor is then measured (I don’t know which method they use with the PICs) and then it shoots the value to you. It is just important to note that you can’t connect high-impedance output devices to the input of the ADC or it won’t fill the capacitor fast enough. The maximum output impedance is 10k ohms for the 16F690 and 2.5K for the 16F877A, so if you are above this (such as with some pH sensors), you’ll probably want a voltage follower circuit to drop your output impedance.

As is the norm, the names and values shown below are based on the PIC16F690 but a great deal will be in common with ADC modules in other PICs. And once you understand how one works, it isn’t such a big deal to switch to another.

ADFM – A/D conversion result ForMat select bit: Since the output is 10-bits and is placed into two 8-bit registers, this allows you to decide whether the LSB is all the way to the right or the MSB is all the way to the left. Set it for right justified, clear it for left justified.

VCFG – Voltage reference pin: Some PICs have a pin that you can place a voltage on to act as a reference for your reading. Say you have 2^10 resolution but want finer resolution and won’t be wandering from 0 to 5 volts but only from 0 – 2.5 volts. Then you use the reference voltage. PICs typically allow the upper limit to only drop to around 2-3 volts and some PICs allow a lower limit and others don’t. The reference limit can be found in the electrical characteristics of your data sheet under the ADC Characteristics. Set the bit if you want to use the reference pin, clear it if you don’t.

CHS(3:0) – adc CHannelS: Typically, if your PIC has a built-in ADC, it will only have one ADC, but several different channels. This basically means that if you have multiple sources that you’d like to convert, you can attach them to different pins, and you can switch between channels to first read from one pin then another. On your pin diagram, those labeled AN0-AN7 (or whatever yours goes to), are the different channels of the ADC.

ADON – ADc ON: This controls whether or not the ADC is on and consuming power. Set the bit to turn on the ADC, clear it to turn off the ADC.

ADCS2(2:0) – ADC clock Source: Depending on the values you put in these three slots, your PIC clock is divided to be used as the ADC clock. Optimum time for an A/D conversion is between two and six microseconds. Refer to your data sheet to figure out which values best fit your situation.

ADIE – ADc Interrupt Enable: If this is enabled, then you’ll get an interrupt when the ADC interrupt flag goes high, assuming that the PEIE and GIE bits are set. Set to enable, clear to disable.

ADIF – ADc Interrupt Flag: This interrupt flag is set when an A/D conversion is done. It needs to be cleared in software.

ADRESL – ADC RESults Low: This is an 8-bit register that the results of the ADC conversion go into. If you have a greater than 8-bit resolution ADC, this holds only the lower eight bits. How the information is placed into these registers is based on your setting of the ADFM.

ADRESH – ADC RESults High: This the other 8-bit register for the upper bits of your results.

GODONE – GO/DONE: This bit marks the start of the actual conversion process. When you set the bit, the conversion starts. It is cleared in hardware when the conversion is done.

And that’s it! Don’t be surprised if your particular PIC has more or less registers and bits but this covers the basics. If something is unclear or you have any questions, let me know.

EAGLE Implicit Pins – Addlevel Explanation

18 October, 2008 (17:36) | Eagle, Tutorials | By: Chris

EAGLE Addlevel Parameter

 

Implicit Pins

Sometimes in EAGLE, you’ll see errors and warning based on implicit pins.  I touched on this before, but it’s something that deserves a little more attention.  For many ICs, the device in EAGLE includes the I/O pins in the symbol you place on the diagram, and the power and ground pins as a separate symbol that can only be accessed through the Invoke tool.  By default, these pins are connected to particular power and ground nets, such as VCC and DGND, or VDD and VSS.  If you don’t have these same power and ground names in your schematic, then these pins will be unconnected in your schematic.

There are different ways to remedy this situation.  One, is to rename your power and ground nets to the same name as the implicit pins.  This is fine if you have one pair of implicit pins, but what if you have multiple ICs with implicit pins, all using different name sets?  You could add and connect multiple supply symbols, each with a name corresponding to the names you need for your implicit pins, but this can cause problems later on during editing.  My preferred method is to simply use the Invoke tool, add these pins to the schematic, then connect whatever supplies you have on your schematic to these recently added implicit pins.

Creating Implicit Pins

This, and other available options, can be very useful in the devices you create in EAGLE.  To do this, you need to have more than one symbol in your device, and then set the addlevel parameter to the appropriate value for each symbol.  EAGLE refers to multiple symbols in one device as gates, and I will keep with this naming convention for consistency.  The addlevel values and outlined below.

Next – Any gate whose addlevel is set to next (the default value) will be added to the schematic one at a time in the order they were placed when you created the device.  These gates can be deleted from the schematic without deleting the entire device.  For a dual op-amp IC, you can set each to next so that you can add only one if you only plan on using one of the op-amps, but still allows you to add both gates if you’re going to use both op-amps.

Must – These gates will be added along with the first gate.  They cannot be deleted until all other gates are deleted, and all will be deleted at once.  Use Must for parts of an IC that, if unused, would prevent proper operation.

Always – Gates with addlevel set to Always, will always be added to the schematic when you use the device.  The difference between Always and Must, is that these gates can be deleted after the device is added.  Use always for parts of an IC that rarely go unused, but that aren’t required for proper operation.

Request – This is the setting for implicit pins.  They will not be an option like the gates marked as request, but can be added by using the Invoke tool and clicking on one of the gates you’ve already placed.

Can – Similar to request.  The only difference comes when all gates are set to Request or Can, in which case it seems that all gates are added simultaneously.  I had to create a test device for this, as I’ve never seen this addlevel used.

Usefulness

For ICs with high pin counts, using multiple gates within the device can help keep your schematic clean and easy to read by allowing you to place each gate in an area of your schematic that makes sense, rather than placing one big block of pins in one place.  It also helps keep it uncluttered by allowing you not to place a bunch of pins that you won’t use.  The video shows an example using multiple dip switches.

I hope this and my other EAGLE tutorials have helped.  I’ll continue posting things that I think are interesting, that helped me, or that took me some time to learn, but if you have any questions, shoot me an email at chris@ece101.com.

Reading from the built-in EEPROM of your PIC

13 October, 2008 (06:00) | PIC, Tutorials | By: Joshua

We’ve already looked at the process of writing to your EEPROM, but it isn’t much use if you can’t retrieve the information.  In this post we’ll figure out how to get that information back so you can use it again.  It is much simpler than writing because there’s not the concern of corrupting data.

Again, this is based on the PIC16f690 but its basic principles should apply to most all PICs. Check in your data sheet to make sure you have the right register and bit names.

The registers and bits used to read from the EEPROM are as follows:

EEADR – EEprom ADdress: The location of the data you are attempting to retrieve.

EEPGD – EEprom ProGram Data: Selects whether you are accessing program or data memory.  0 is for data memory, 1 is for program memory.

EEDAT – EEprom DATa: This register is where the data retrieved from the EEPROM is placed.

RD – ReaD: This bit is set to start the process of pulling the data from the EEPROM and putting it into the EEDAT register.  This bit is set in software but can only be cleared in hardware.  It is reset when the transfer is complete, which takes only one clock cycle.

And an example function:

int read_eeprom(unsigned int address) {
EEADR = address;
EEPGD = 0;
RD = 1;
return EEDAT;
}

And that’s it. Piece of cake, right? If you have any issues with this or any further questions, just let me know!

Writing to EEPROM with your PIC

6 October, 2008 (14:30) | PIC, Tutorials | By: Joshua

For me, the EEPROM is a powerful tool for debugging.  Sure, there are debugging tools out there and even better “home-made” variety debuggers, but for me, it is simple and easy to use.  With the PICkit 2 software, you are able to read what is currently on your PIC.  If your PIC has written something to its EEPROM, you can easily see it by reading the PIC.  There are a couple of issues with this; you have to physically have the PICkit 2 connected, reading the PIC resets it, the EEPROM only holds 8-bits per address space, and dealing with large amounts of information is difficult.  But if you’re at the beginning of your project, it’s a good way to figure out what values your PIC is reading without hooking up an output or to what point your program made it to before it crashed.

But don’t think that the EEPROM is just for debugging, it obviously wasn’t designed for that.  EEPROM allows you to store values and not have to worry about losing them when you lose power.  Maybe I’m incorrect in saying this, but my view of the EEPROM is that it is the hard drive for your microcontroller.  There’s typically much more of it than flash data space and it really can be used for whatever you think up.

So, how does it work?  You basically put the information you want written in one register, the address of where you want it written in another register, and then set the write bit.  Now!  Microchip seems quite paranoid of “spurious writes” so there are a couple precautionary steps you’re required to take before writing.  They seem nonsensical and that’s the point.  The chances of these steps occurring randomly are quite small.

Let’s go through the different registers and bits first.  These are from the PIC16F690 but other PICs should be extremely similar in their setup and usage.

    EEADR – EEprom ADRess: The address where you want to write the data.

    EEDAT  - EEprom DATa:  The data you want to write.

    EEPGD – EEprom ProGram Data: Selects whether you are accessing program or data memory.  0 is for data memory, 1 is for program memory.

    WREN – WRite ENable: Makes it so that setting the write bit actually does something.

    EECON2 – EEprom CONtrol register 2: A special register used to limit spurious writes.  When read, it will show “0″s.

    WR – WRite bit:  When this is bit is set, the data in EEDAT is written to the address at EEADR.  It cannot be cleared in software.

    EEIF – EEprom Interrupt Flag: Is set when the write is complete.  Must be cleared in software.

 

To actually write, you simply use these registers and bits in the correct order.  Steps six and seven have to be done exactly in that order and without any other commands between them. 

  1. Put the address in the EEADR register.
  2. Put the data in the EEDAT register.
  3. Clear EEPGD so you actually write to the data memory.
  4. Set the WREN bit.
  5. Clear the Global Interrupt Bit (GIE) so you don’t have an interrupt in the middle of the next two steps.
  6. Put the value 0×55 into EECON2.
  7. Put the value of 0xAA into EECON2.  
  8. Set the WR bit.
  9. Wait for EEIF to be set.
  10. Clear the EEIF bit and the WREN bit.
  11. Set the GIE bit if you want interrupts.
And that’s it.  In C, my code looks like this:
    EEADR = address;
    EEDAT = data;
    EEPGD = 0;
    WREN = 1;
    GIE = 0;
    EECON2 = 0×55;
    EECON2 = 0xAA;
    WR = 1;
    while(!EEIF) continue;
    EEIF = 0; 
    WREN = 0;
    GIE = 0;
Hopefully this helps you as you try and get your EEPROM working for you.  Let me know if there are any questions or issues.