Today , I begin a series of posts that will involve the things I hold dear – Sanskrit and Science.
Presenting , xkcd संस्कृतभाषायाम् !
पश्यन्तु , आनन्दम् अनुभवन्तु !
Pre – IISc :
ε> – Love
Post- IISc :
ε> – Epsilon greater than
They say love means different things at different ages.
I’m disappointed to know this what they meant.
Programming AVR MCUs in C
I am starting to write a series of tutorials on programming AVR uCs in C. I plan to write articles covering Interrupts , ADC , timers , internal EEPROM, PWM and USART. This is the first in those series and it covers basic I/O operations. Here is the link to the page.
I finally dug out that LCD from the mess in my room and got it to work :). Turns out , its really simple. You dont even need a microcontroller to do it.
Ok. Lets get started :
That is the LCD that i bought from SP road (kwality electronics) for 100 bucks. It incorporates the widely popular HD44780 pin configuration. It has 16 pins.
This is how it looks from the back :
There are 16 pins in all. They are numbered from left to right 1 to 16 (if you are reading from the backside) . My LCD came with a marking to indicate which was the 1st pin and which was the 16th. You can see the markings right next to 1st and 16th pins.
The LCD is
I took the back view pic after I soldered a 8 pin connector between the 7 to 14 pins to make it easier to work with. When you buy the LCD ,all the pins will be bare. ( like in the first pic).
Did you notice that there is a fine film covering the LCD display in the first pic? DON’T remove it until you finish soldering wires and connectors to the pins. Otherwise ,the flux and the alcohol that you use during the soldering process will ruin the LCD.
These are the functions of the 16 pins :
- VCC (+5V)
- Contrast adjustment
- Register Select (R/S)
- Read/Write (R/W)
- Clock (Enable)
- Bit 0
- Bit 1
- Bit 2
- Bit 3
- Bit 4
- Bit 5
- Bit 6
- Bit 7
- Backlight positive power rail
- Backlight negative power rail
Pin 1 and 2 are the power supply pins. They need to be connected to the negative rail and the postive rail of a +5v power supply respectively. To get a stable +5v Power supply , you can use a 7805 voltage regulator. It will regulate any voltage that you give it into +5v. If you are confused about using the 7805 , check out this tutorial.
Pin 3 is the contrast setting pin. It must be connected to ground in series with a 5K pot. The lower the resistance , the greater the contrast. I recommend setting it at around 1.5K – 2K. If you set the value too low or short the pin directly to ground , you will see only dark boxes on the screen. As far as i know , doing this does not have any adverse affect on the LCD.
Pins 4 , 5 and 6 are control pins of the LCD. I’ll explain about them later on in the post.
Pins 7 to 14 are the Data pins of the LCD. Pin 7 is the Least Significant Bit (LSB) and pin 14 is the Most Significant Bit (MSB) of the data inputs. If you want to display some number or letter on the display , you have to input the appropriate ‘codes’ for that character on these pins. These pins are also used for giving certain commands to the display like clearing the display or moving the cursor to a different location. Upon giving the correct signals to the 3 control pins , the character codes or the commands that you have given to the Data pins will be written to the display or executed by the LCD respectively. To make it easier to give the appropriate inputs to these pins , i recommend wiring up a DIP switch to these pins.
Most LCDs have a backlight. A backlight is a light within the LCD panel which makes seeing the chracters on screen easier. When you leave your cell phone or mp3 player untouched for sometime , the screen goes ‘dark’. That is the backlight turning off. It is possible to use the LCD without the backlight as well. Many LCDs come without a backlight. If your LCD has only 14 pins , then it has no backlight. However , the working of the LCD still remains the same even if your LCD doesnt have a backlight. The Backlight is nothing but an LED. So , a resistor must be connected in series with it to limit the current. I am not sure about what value of resistor must be used. This link mentions that the allowable current is 100ma. Then it is best to have a variable resistor (or a transistor) and adjust the current till it is around 90 ma.
Now for the important part : The 3 control pins : R/S , R/W and E
The LCD has basically two operating modes : Instruction mode and Data Mode
You have to activate the command mode if you want to give a Instruction to the LCD. Example – “Clear the display” , “Move cursor to home” etc.
You have to activate the Data mode if you want to tell the LCD to display some character.
To set the LCD in Instruction mode , you set the 4th pin of the LCD (R/S) to GND. To put it in data mode , you connect it to Vcc.
The enable pin has a very simple function. It is just the clock input for the LCD. The LCD will perform whatever function you want it to do on the falling edge of the enable pin.
Generally , we always use the LCD to show things on the screen. However , in some rare cases , we may need to read from the LCD what it is displaying. In such cases, the R/W pin is used. However , this function is beyond the scope of post and will not be explained. For all practical purposes , the R/W pin has to be permanently connected to GND.
So this the flowchart for operating the LCD
1) Set the LCD in either Instruction mode or Data mode depending on your requirement.
2) Give the appropriate codes at the Data pins for the function that you wish to perform.
3) Give a short pulse at the Enable Pin from positive to negative and back to positive again.
4) Voila , your instruction will have been executed/ Data shown on LCD
5) Go back to 1
Ok. Now that you have understood this , before starting , it would be more convenient if you soldered together a board with connectors and buttons for controlling all the pins.
Here’s the circuit :
Here’s a quick review :
1) Goes to GND as it is a power supply pin.
2) Goes to +5v as it is a power supply pin.
3)VEE – contrast adjustment. Connect this to GND along with a series resistor of 2K.
4) RS – Register select. – You can use this to select between the instruction mode and data mode of the LC. Depending on the status of this pin , the data on the 8 data pins (D0-D7) is treated as either an instruction or as character data.
There are 2 possibilites :
Switch Open : The pull up resistor holds the RS pin at Vcc. Data Mode is activated as RS = 1
Switch Closed : The RS pin becomes a part of the ground. Instruction Mode is activated as RS =0
5) RW – Permantently connect this to GND.
6) Enable – The instruction or the character data at the data pins (D0-D7) is processed by the LCD on the falling edge of this pin. The Enable pin is normally held at Vcc by the pull up resistor. When the momentary button switch is pressed , the Pin goes low and back to high again when you leave the switch.
Your instruction or data will be executed on the falling edge of the pulse. (ie. The moment the switch closes)
7-14) Data pins – This is where you give the LCD the ‘codes’ for any instruction or character.
Its not very convenient to have 8 wires and try to make them HIGH or LOW on breadboard. Hence , i recommend that you use a DIP switch along with 8 pin SIP connectors. This is the thing that i made for the job :
The circuit is very simple and you need only 8 pull down resistors (1K) to make the circuit. You can even use a SIP resistor pack to save space and soldering time.
I have mounted the DIP switch on quite a large PCB. You can make a small one if you want to conserve space.
Download the PCB files from here and make your own DIP switch board.
Join together two female connectors to make a female-female connector. Solder a male connector on the LCD data pins.
Now that you are ready with the supporting hardware, lets get started with the real stuff.
But first , before powering up the LCD , double check all these things.
1) No pins are shorting on the backside of the panel. (It can happen if you have not cleaned the board after soldering)
2) The contrast pin (3) (Vee) is connected to GND in series with a resistor
3) The backlight (pins 15 , 16) has a resistor in series to limit current.
4) The DIP switch board is working properly.
5) The R/W pin is tied to ground.
6) The circuit is being powered by +5v. This is very important. Dont zap your LCD by accidentally powering it with 12V.
7) The connector for connecting the 8 data pins from the DIP switch board to the LCD is proper. Double check for any loose wires and any mismatched wires on the connector.
If you are sure that nothing is wrong with your setup , then go ahead and power up you LCD.
See nothing at all on the LCD? Dont panic. Thats what is supposed to happen.
To make the LCD first show some signs of life , you need to issue an instruction to it. That instruction is 00001111. Since you are giving an instruction to the LCD , you have to put it in instruction mode by holding the RS pin to GND. In the board that you wired up, you can put RS at GND by pressing the RS pushbutton such that it remains in a depressed state.
Now , your LCD is in instruction mode and is ready to accept any instruction that we issue to it. Now , set the DIP switches as 00001111. This instruction tells it to turn on the display and show a blinking cursor. Once , you have set the DIP switch to 00001111 , press the Enable Button once and leave. You should be seeing a blinking cursor on the screen now. This means that your LCD is initialized and ready to accept characters to display.
Wait , you did not get a blinking cursor? Triple check if your connectors and switches are proper. Check if you have accidentally reversed the MSBs and LSBs on each side of the connector. Check your soldering to see if there are any cold solder joints.
Ok , once you have got the cursor blinking , the LCD will now accept character data to display. Now , we have to put the LCD in character mode to make it accept characters to display. Press the RS switch to make it ‘undepressed’. Now set the DIP switches to 01000001 and then press the Enable button. If everything has gone right , then you should be seeing a capital A on the screen. But what if you want to display some other letter , like “M”? The procedure is the same , you only replace the 01000001 with the code for M , which is 01001101. Try the same thing for “M” , M will appear next to A. You can totally write 16 characters in each row of the LCD. You would expect the 17th letter to appear at the 1st block of the 2nd row. However , this is not the case. For some silly reason , you have to do somemore things before that can happen. I’m pretty much bored of typing now , so i’m going to end this tutorial now.,
When the LCD starts , you have to initialize it first. That means that you have to give a certain instruction to it to make it work properly.
The initilizing instruction is 00001111.
Since it is an instruction , set the LCD in instruction mode by setting R/S to GND.
Then give the instruction code (00001111) at the data pins.
Then give a short pulse at the enable pin.
You should now be seeing a blinking cursor.
While taking a trip down the memory lane , I stumbled upon this picture of my “Cotton Candy Contraption” that I built two years ago. It was made from a motor that I extracted from my cousin’s toy car and a Tin can punched with holes. And it really did work!
The first question that comes to your mind :
What is an EEPROM?
To cut it short , EEPROM is a memory device , similar to the memory stick in your cell phone or camera.
The Pin layout of an EEPROM is very similar for most Static RAMS. So once you know how to program an EEPROM , you also know how to program RAM.
So lets get started.
Before knowing how to use an EEPROM , you have to know how it organizes its data.
The EEPROM is divided into 8912 “houses” with eight rooms in each house. You are allowed to store either a 1 or 0 in each room.
When you wish to write some data to the EEPROM , you have to specify the address of the “house” where you want to store your data.
(Did you ask yourself why the number of “houses” is something as arbitary as 8912? Its because 2^13 is 8912. And there are 13 Address choose pins on the IC.)
Then , using 8 input pins , you have to give 8 values which will be stored in the eight “rooms” of selected “house”.
The procedure is very similar when you want to read the data back.
Again , you will have to select the address of the “house” and the 8 values in the eight rooms will appear through the output pins.
If add up the total number of pins : 8 for input +8 for output + 2 for power + 13 for address + 3 control pins
= 34 pins!!!
So to reduce pin count , Manufacturers have combined the Input and Output pins and made them into a single set of 8 I/O pins. This makes it a bit tougher to use the IC as both the functions have to be done by the same set of pins.
The Procedure :
EEPROMs have 3 control pins : CE (Chip Enable) , WE (Write Enable) , OE (Output Enable)
This truth table should make it pretty clear how the 3 pins work :
|0||1||1||Outputs are Tristated|
|1||0||0||Chip Disabled – Outputs are Tristated|
|1||0||1||Chip Disabled – Outputs are Tristated|
|1||1||0||Chip Disabled – Outputs are Tristated|
|1||1||1||Chip Disabled – Outputs are Tristated|
Here’s the explanation :
The Chip can Read/Write only when CE is low. If CE is held high , then the chip is disabled and the I/O pins are tristated.
When CE is LOW, OE is low and WE is held high , the data in the memory location chosen by the address pins , appears at the I/O pins.
On a new EEPROM which has never been programmed before , all the memory locations are programmed with 1’s by default. So , while reading from a new EEPROM , you will see only 1’s in every memory location.
To write , input the 8 bits that you wish to write at the I/O pins while holding OE high and WE low. Then raise WE to high and then back to low again. Then , the data that you have given on the I/O pins will be written on the rising edge of the pulse to the address that is present on the address pins at the rising edge.
The waveform will be like this :
So , if you have not understood from the above waveform , let me make it very clear.
This is the way to manually write to an EEPROM :
1) Hold OE and CE low and WE high – Read Mode – Whatever is in the location determined by the address pins will appear at the I/O pins.
2)Hold CE Low, OE high and WE high – I/O pins are tristated – disconnect the output leds (or whatever) from the I/O ports
3)Hold CE low , OE high and WE low – Write Mode – Give the data to be written at the I/O port and the address where you want it to be written at the address pins.
4)Hold Ce low ,OE high and WE High – I/O pins are tristated AND data is written – Disconnect the Switches or wires that you used to give data to the I/O port.
5) Hold CE and OE Low and WE high – Read Mode – You are back to step 1. Whatever is in the location determined by the address pins will appear at the I/O pins.
The procedure is same for RAMs as well. The only difference between EEPROMs and RAMs is that RAMs lose whatever has been written when the power is turned off , but EEPROMs dont.
There are many EEPROM and RAM ICs available. The EEPROM that i generally use is AT28C64B from Atmel. It is available at around 100 INR (~2$) here in bangalore. The cheapest RAM IC is 6264 which is available for 35 INR (0.7$)
The writing procedure that i have described above is rather crude. There are better ways of writing to the same EEPROM. But , this is best way if you are using the EEPROM by giving CE ,WE, OE signals by hand.
I finally finished collecting the next 150 data sheets (74150-74300) yesterday. Here are the links to the data sheets :
Set 2 – 74150 to 74300
Set 1 7400 to 74150
Here “trimmed” means that I’ve edited the pdfs and have removed all the crap like ” IC Physical Dimensions “, which we never use.
I have the unedited pdfs with me. I couldnt upload them as 110mb.com (where i store my files) has a cap of 8mb per file.
If anyone wants the unedited collection , please email me at firstname.lastname@example.org or leave a comment. I will be more than happy to email it to you.