Code and Life

Programming, electronics and other cool tech stuff

Supported by

Supported by Picotech

Simple FAT and SD Tutorial Part 3

Finally back! Sorry for taking a while, but getting the SD cards to work required quite a bit of research! In this part of the tutorial, we’ll start talking with the SD card. Once you get that working, adding the FAT library developed in the previous part will be easy.

This tutorial will most likely be the most challenging covered in my blog so far, so beware: You may need to do some troubleshooting on this one! If you encounter problems, I recommend you to ask any questions at the AVR Freaks tutorial forum topic, so more people might be able to help you than just me!

Basic hardware setup: ATmega88 with UART console

The 3.3V UART I covered just a while ago will form the basis for this project. Only change we will be doing is to replace ATtiny2313 with ATmega88. This is because SPI, SD and FAT code will eat up almost 3 kB of program memory, and the ATtiny chips with that much program memory and separate RX/TX and SPI pins are not that common, while ATmega88 is readily available (48 and 168 work as well, of course). To accommodate the new chip, the following hardware changes are made to the ATtiny2313 version:

  • ATmega88 will require two ground connections
  • In addition to VCC, also AVCC for analog circuitry needs to be wired to VCC
  • Additional capacitor between AVCC and GND is recommended (I used 10 uF)
  • Programming header MOSI/MISO/SCK are also in different place

Read post

3.3V UART with MAX3232CPE

Before diving right into SPI communications for my SD tutorial, I wanted to have a 3.3V development platform that could output some meaningful status information, not just light a LED if something goes wrong. In this post, I will outline the basic testing platform that will be used in the upcoming part 3 of that tutorial, and discuss a little about UART on AVR in the progress. Here’s what we’ll build:

If you want to build it yourself, you’ll need:

  • ATtiny2313 or other AVR chip with UART pins (RX/TX) separate from SPI pins (MOSI/MISO/SCK)
  • 20 MHz crystal (other speeds will work, too) and ~27 pF capacitors
  • 4k7 pullup resistor for ATtiny2313 RESET pin
  • 3.3V regulator such as LD1086V33 or some other 3.3V voltage source
  • 2 filtering caps for the regulator input/output sides, 10 uF
  • MAX3232CPE or similar RS-232 transceiver that works on a 3.3V voltage
  • RS-232 port on your computer or a USB to RS-232 dongle
  • RS-232 to breadboard connector (home-soldered example seen above)

Read post

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