Showing posts with label SPI. Show all posts
Showing posts with label SPI. Show all posts

Friday, July 10, 2015

Intermediate: Getting Started with ATtiny13A-PU

Hi, everyone. It's been sooooo long since I last posted a new blog. Mostly because I'm busy and lazy. :D

Anyway, I'm in a good mood today - though pretty sleepy - and I'd like to share with you guys something that I'm currently playing with. It is called ATtiny13A-PU. It is an Integrated Circuit (IC) from Atmel, which also manufactured the lovable ATmega328, used by the Arduino UNO, Nano, etc.



I bought the chip from the one and only GI Electronic which you can find here (http://www.gie.com.my/products.php?action=ic/atmel/ATtiny13A-PU). It is pretty cheap compared to an ATmega328.

As you might have noticed, the different between ATtiny and ATmega is the word 'tiny' and 'mega' itself. ATmega has tons of I/O pins while ATtiny has 8 pins, minus VCC, GND, and RESET pins leaving just 5 GPIO pins left. However, it would still be great if your project doesn't require too many pins. (It even have 2 PWM pins, though.)

There are a few other ICs which resembles ATtiny13 like ATtiny45, ATtiny85, etc. They vary in some specs. However, there are quite a bunch of tutorials that already cover those ICs. I'm writing this blog because I found out that there isn't much about ATtiny13, specifically, or some of them aren't complete. I also stumbled upon quite a number of problems which they discussed in separate forums, blogs, instructables, etc.

So, I'm going to put all my findings here and I hope you would also able to follow them.

What do you need?
1. Arduino UNO (Or Mega or something else. We are just going to use it to program ATtiny through the ISP)
2. Some male-to-male jumper wires.
3. LED (Optional, for testing)
4. Resistor (Optional, for testing. 220R, 100R, 470R, or others within acceptable range for LED)
5. Breadboard

How to hook it up?

* You may also use 5V as well instead of 3.3V

Setting up Arduino IDE
At the moment that I'm writing this blog, the library that I've downloaded doesn't work with the latest Arduino IDE. I've tested with Arduino 1.6.5, and the IDE wouldn't even launch. So, I tried using Arduino 1.0.6. So, you might want to do that as well.

I suggest using the (.ZIP) and extract it somewhere, so you wouldn't need to install it. You can still keep the latest version of Arduino IDE installed. Do not launch the application yet.

Step 2: Download the ATtiny13 hardware file. (https://github.com/tekstop/attiny/tree/Arduino1)
There are some adjustments needed to be done later.

Step 3: Open the folder where you've extracted the Arduino 1.0.6 IDE. Navigate to the folder called hardware inside it.
Example: C:\Users\Shah\Desktop\arduino-1.0.6\hardware

Step 4: Extract the folder attiny from the ATtiny13 hardware file downloaded earlier into the folder.

Step 5: We are going to fix something now. Open the extracted folder and edit the file boards.txt
Example: C:\Users\Shah\Desktop\arduino-1.0.6\hardware\attiny\boards.txt

Step 6: Scroll to the bottom and find (or simply press Ctrl+F) something like:
"attiny13.build.f_cpu"
Change the value to 1000000L.

Step 7: We need to add something to the file wiring.c in the Arduino IDE. Navigate back to the Arduino hardware folder. Go to arduino/cores/arduino and edit the file wiring.c using your favourite editor or notepad.
Example: C:\Users\Shah\Desktop\arduino-1.0.6\hardware\arduino\cores\arduino\wiring.c

Step 8: Go to line no. 44, the one that has something like:

#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)

At the end of that line, add the following:

|| defined(__AVR_ATtiny13__)

So, now that line look something like this:

#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny13__)

Step 9: Now you can finally launch Arduino IDE 1.0.6. Open sketch ArduinoISP from File > Examples > ArduinoISP

Step 10: Hook up your Arduino UNO or Mega and select the port number as usual. Go to Tools > Programmers and select Arduino as ISP

Step 11: Upload the ArduinoISP sketch to the Arduino UNO or Mega as usual.

Step 12: Open Blink sketch from File > Examples Blink
Change all the pin number 13 to 4. There is no pin 13 on ATtiny. The LED that you've hooked up to if you follow the schematic accordingly should be on pin 4.

Step 13: To upload this program to ATtiny, File > Upload using programmer (Ctrl + Shift + U)

The LED should be blinking right now. You can play around with it and make something awesome out of it. I pray that you succeeded and have some fun with it. Just a heads up, the maximum sketch size is just around 1MB only, so, use it wisely.

Good luck and have a nice day or whatever~

Saturday, July 5, 2014

Intermediate: QC12864B (128x64 LCD using u8glib.h)

Hi, everyone. Today, I have some time to kill. So, I decided to play around with my 128x64 pixels LCD. This is also a product distributed by GI Electronic which can be found here.


There are some sample code written in C attached on the product page, but for me, it's not very helpful as I'm more familiar with Arduino. So, this tutorial will be about interfacing Arduino and the LCD.

Introduction

This LCD comes with build-in ST7920 controller chip which will handle the data sent to it. The attached user manual somewhat tells you how it should work and what command you should send, but it is still blurry, isn't it? It's just look too complicated. But, there's an easy solution for that. An external library makes our life so much easier. The library is called U8glib and it supports wide range of LCD.  However, this tutorial will focus on this LCD only. You can see the list of supported LCD here.

Usually, you need to send 8 bits of data to make the LCD display what you want it to display. So, basically, there'll be 8 wires going from Arduino to the LCD. Imagine how complicated that would look like. However, here's the sweetest part of all. The library allows you to program the LCD using 3 methods.

1. Using 8 bits of data as mentioned above. (Parallel Bus Mode)
2. Using hardware/built-in SPI port on Arduino. (Serial Bus Mode)
3. Using software SPI port.

Personally, I'd choose the second method in case where the SPI ports are available. It uses less port, making the connection far simpler and also leave the other ports available for other applications. For your information, SPI ports are on pin 10 (Slave Select), 11 (MOSI), 12 (MISO), 13 (SCK) on Arduino Nano and Arduino Uno. Using the software SPI port may slow down the refresh rate.

Connection

This product comes with 20x1 header, which really helpful to shove the LCD into a breadboard. I'll show you how to connect using the first and second methods as mentioned above. The third one is almost the same as the second one, but it is actually depends on what port you want to use.

Note that the pin in the following photo is equivalent as follows:
D/I = RS
CS1 = PSB
CS2 = NC

First method:

Second method: (Special thanks to this website for showing this method)
IMPORTANT! For this LCD to work using this method, you need to de-solder R9 and R11 at the back panel of the LCD.
The reason is, VDD is actually connected to PSB through the 0Ω resistors (R9 and R11), making PSB always HIGH, putting the LCD always in parallel bus mode. Connecting GND to this could end badly to your LCD and Arduino as you are basically connecting 5V to GND directly. So, just de-solder it. Don't worry, you can still connect it in parallel bus mode by connecting PSB to 5V.


Testing the LCD

To test the LCD, simply copy the U8glib library/folder into Documents > Arduino > libraries.
Start Arduino IDE. Goto File > Examples > U8glib > GraphicsTest.


The next step depends on the method that you choose.

If you choose the first method, un-comment the line:
U8GLIB_ST7920_128X64_4X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16

If you choose the second method, add the following line:
U8GLIB_ST7920_128X64_4X u8g(10);

If you choose the third method, modify the following line as you needed to:
U8GLIB_ST7920_128X64_4X u8g(18, 16, 17); // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17

If you choose the first method but using different pin numbers, make sure you also change the pin numbers in the declaration above.

If the LCD is showing various shapes and text, that means your LCD is working correctly. Now, you may go on and program your own graphic or anything that you want on the LCD. You may refer to more functions and explanation about the U8glib library on their official page and user reference page. Check out this page as well. There are links to the explanation of the examples on that page.

I've shown you the basics, so you should be able to explore more by yourselves. Just comment down here if you do not understand any step. Until next time, good bye. Have fun!