Code and Life

Programming, electronics and other cool tech stuff

Supported by

Supported by Picotech

Simple FAT and SD Tutorial Part 2

In the last week’s part 1 of my FAT and SD tutorial, we got as far as reading the file entries in root directory, and peeking into a file with hex editor. Now we’ll cover the file allocation table itself to enable reading longer files, and adapt the code into a small footprint FAT16 library!

File allocation table in FAT16

In the previous part, we learned that the data on a FAT-formatted disk is stored in clusters. In our test image, the cluster size was 32 sectors, i.e. 16 kiB (16 384 bytes). Let’s imagine a newly formatted disk with 100 free clusters, with the clusters numbered from 2 (the first cluster at the beginning of data area) to 101 (the very last cluster). Now let’s copy some files there:

File operation File allocation
Copy HAMLET.TXT (193 082 bytes) to disk Clusters 2-13 allocated for the file (196 608 bytes)
Copy README.TXT (353 bytes) to disk Cluster 14 allocated for the file
Create SUBDIR on disk Cluster 15 allocated for the directory file entries
Create 1.TXT in SUBDIR Cluster 16 allocated for the file, a new file entry written to SUBDIR (beginning of cluster 15)

Read post

Level Shifting 101

While writing my FAT+SD tutorial, I realized that my projects may soon contain both 3.3 V components like an SD card, and 5.0 V components like LCD displays or a MAX232 UART chip. Considering I only knew how to use resistors as voltage dividers, I decided to learn a bit more about voltage level shifting.

Having netted already two generous donations for this blog, I decided to spend the first one on $10 of components that could do the job, and employ my trusty, although not old, Picoscope 2205 to investage how they perform!

Simple low to high conversion: Flexible logic levels

When interfacing a 3.3V device with an AVR chip running at 5V, it is possible that no shifting is needed when communicating to the AVR chip. For example, the ATtiny13 datasheet specifies input low voltage as anything less than 0.3*VCC (1.5V when VCC=5V) and high as anything more than 0.7*VCC (3.5V when VCC=5V) – the 3.3V might be just enough to be reliably interpreted as “high” in your particular part combo. However, you might still want to consider the alternatives below, just to be sure.

Simple high to low conversion: Voltage dividers

The basic trick for lowering a voltage from, say 5V to 3.3V is to use two resistors in series, connected between the MCU pin and the ground. Because the voltage drop in each is proportional to the ohm value of each resistor, it’s easy to get any voltage between MCU VCC and ground from such a setup.

Basically, V0 = R1 / (R1+R2) * VCC. For example, if your MCU is providing 5V when an output pin is set high, you could have a 2k resistor (R2) and 3k resistor (R1) in series, resulting in 3V between the two resistors – that is because the 2k resistor “consumes” 2/5 of the overall voltage (5V->3V) and 3k resistor 3/5 of the voltage (3V->0V).

To see how it works in reality, I made a voltage divider from two 10k resistors, and measured both the voltage on MCU pin itself (the blue trace) and from between the two resistors (the red trace), when the MCU pin is set to alternate between low and high state 200 000 times a second (a 100 kHz square wave):

Read post

Simple FAT and SD Tutorial Part 1

Are you limited by 128 bytes of EEPROM on your MCU or even the few kilobytes of flash in your project? Instead of just downloading a library like Petit FAT File System Module and following blindly a tutorial on how to customize it to your microcontroller and SD card, would you like to really understand what you are doing, and maybe learn a bit about filesystems and SPI in the process?

In this first part of my FAT and SD tutorial, we’ll take a SD card image, and create a simple C program to interpret its contents. For this part, you don’t need any hardware at all, just a computer with gcc (GNU C Compiler) or any other ANSI C compatible compiler installed.

Getting ready: Hex editor and disk image

To make the coding easier, I recommend a good hex editor. The one I’m using is the free and excellent HxD by Maël Hörz. You can also use it to create a 1:1 disk image from a physical SD card. To have a filesystem to read, I purchased a 1 GB micro-SD card with SD adapter for 5€, plugged it into my computer and formatted it as FAT16 (over 2 GB cards will likely get formatted as FAT32), and copied Hamlet from Project Gutenberg and some other dummy test files to it (also created a subdirectory with a few text files in it):

Read post

Handy Pinout Reference Sheet

Just thought to share a quick tip today. I’ve noticed that when doing work with AVR MCU’s, I’m using ATtiny2313, ATmega88 and ATtiny45/85 for 99 % of the time. I got tired of opening the Atmel data sheets every time when swapping a new chip into a breadboard, so I constructed a real cut’n’paste reference sheet by gluing the relevant portions on a piece of rigid cardboard:

I even used a piece of paper to mark the 6-pin header pinout, it’s a real time-saver! And thanks to the cardboard back, it’s easy to keep below my LCD monitor and pull out every time I need it. Additional idea would be to leave some white space around the pinouts and laminate it with clear contact paper, so I could doodle project-specific notes with a water-based marker and clean it after I don’t need them anymore!

Oh, and as you can see, I’m using a Mac this time to do some flashing. I installed the excellent Crosspack for Mac from Obdev guys. It’s a streamlined way to install avrdude and avr-gcc on a Mac, and now I can take my electronics hobby with me when I’m traveling. :)

Read post

Using Arduino Uno as ISP

One exciting piece of hardware I received with my Digikey order was an Arduino Uno board (R3). There was conflicting information whether or not it could be used as an ISP (in-system programmer), so I decided to see for myself. It turned out that with just one tweak, I could use the $26 device to program my AVR chips, essentially eliminating the need for a separate ISP such as $22 USBtiny!

This is obviously good news for any beginner with a budget, so I decided to write a short tutorial on how to do it. I used my USB password generator as a guinea pig for this project, so if you have wanted to try that out, this post also doubles as tutorial on how to build it on breadboard (good idea in any case before soldering it anywhere). Read on for details!

Read post

Digi-Key Order and Review

Until now, I’ve been ordering all my electronics supplies through the excellent Finnish firm Partco. However, after running out of ATtiny85 chips I just couldn’t make myself pay 5.90€ ($7.80) when the same part was available for third the price from Digi-Key. I decided to try the fabled distributor myself. In case you haven’t tried them yet, read on for my experiences.

Website and Shopping Process

It is surprising how such a successful electronics component retailer is able to operate with such a poorly laid out and difficult to use website. From a usability standpoint, it’s a prime example of how not do design a web store. There’s so much to critize it’s hard to know where to begin, but here my top annoyances for the front page alone:

Read post