Monday, July 25, 2022

Library for MCP4725 DAC for AVR microcontrollers

The MCP4725 is a low-power, high accuracy, single channel, 12-bit buffered voltage output Digital-to-Analog Convertor (DAC) with non-volatile memory (EEPROM). Its on-board precision output amplifier allows it to achieve rail-to-rail analog output swing. The advantage of this DAC is the A0 pin that can be used to control multiple DACs by changing their address (more on that later).

This DAC library also includes an I2C driver that is needed by the AVR to communicate with the MCP4725.

Library for MCP4725 DAC for AVR microcontrollers

Contents

  1. Pin Descriptions
  2. Wiring up one or multiple DACs
  3. MCP4725 Characteristics
  4. Code Example
  5. Using the MCP4725 library
  6. Download

 

1. Pin Descriptions

 

MCP4725 DAC pin description

MCP4725 pins

Wednesday, July 13, 2022

SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 2 of 2)

This is part 2 of the tutorial on SD card specifications. In part 1 of the tutorial we made functions necessary for SPI communication and card initialization in SPI mode. At the end of this second part you should be able to read and write an SD card.

Contents

5. Reading/Writing Data Blocks

5.1 Block Length

5.2 CMD17 – Reading a Single Block

5.2.1 Read Errors

5.3 CMD24 – Writing a Single Block

6. Conclusion or Confusion

7. Links 


SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 2 of 2)


5. Reading/Writing Data Blocks

5.1 Block Length

Block length can be set in Standard Capacity SD cards using CMD16 (SET_BLOCKLEN) however for SDHC and SDXC cards, the block length is always set to 512 bytes. Since nowadays most if not all cards are of high capacity type, we will only consider the latter in this tutorial.

SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 1 of 2)

Personally I learn better using practical examples instead of abstract data, and for this reason I have constructed this tutorial as a step by step with practical code examples written in C language, that can be followed by anyone with basic programming skills and knowledge on how to use a microcontroller. Although I am using an ATmega328P in this tutorial, the concepts extend to any microcontroller.

If you need an SD card library, you can find one here: https://www.programming-electronics-diy.xyz/2022/07/sd-memory-card-library-for-avr.html. This tutorial will help in understanding the low-level interactions between a microcontroller and a memory card but it will not be enough to use an SD card in a practical way. Apart from an SD card driver, that is the code described in this articles, you will also need a file system driver such as FAT16 or FAT32 in order to read or write files.

Contents

1. General Description

1.1 Bus Mode and Clock Speed

1.2 Read/Write Mode Selection

2. SD Card Hardware Interface

2.1 microSD Card Schematic SPI Interface

3. SPI Setup

4. Card Initialization

4.1 Power Up Sequence

4.2 Sending Commands

4.3 Initialization Flow

4.4 CMD0

4.5 Response R1

4.6 CMD8

4.7 Response R7

4.9 CMD58

4.10 Response R3

4.11 ACMD41 & CMD55

5. Reading/Writing Data Blocks

5.1 Block Length

5.2 CMD17 – Reading a Single Block

5.2.1 Read Errors

5.3 CMD24 – Writing a Single Block

6. Links


SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 1 of 2)

1. General Description

The Secure Digital (SD) Card was developed by the SD Association (SDA) as an improvement over MMCs. The SD Card specifications were originally defined by MEI (Matsushita Electric Company), Toshiba Corporation and SanDisk Corporation. Currently, the specifications are controlled by the Secure Digital Association (SDA).

In addition to the mass storage specific flash memory chip, the SD Card includes an on-card intelligent controller which manages interface protocols, security algorithms for copyright protection, data storage and retrieval, as well as Error Correction Code (ECC) algorithms, defect handling and diagnostics, power management and clock control.

Thursday, July 7, 2022

SD Memory Card Library for AVR Microcontrollers - SD and FAT Driver

This project facilitates the reading and writing of SD flash memory cards using FAT16 or FAT32 file systems, over the SPI protocol, and it is designed with embedded systems in mind. It includes an SD driver that uses the SPI interface and a FAT driver that is controlled by the host microcontroller.

If you whish to know about the low level communication between the microcontroller and the SD card, I have a two part series tutorial here https://www.programming-electronics-diy.xyz/2022/07/sd-card-tutorial-interfacing-sd-card.html.

There are two main standards for flash memory cards: MMC (MultiMediaCards) and SD (Secure Digital), SD being the most used today. SD was developed by the SD Association (SDA) as an improvement over MMCs. These cards have basically a flash memory array and a microcontroller inside that controls erasing, reading, writing, error controls, and wear leveling of the flash array. The data is transferred between the memory card and the host controller as data blocks in units of 512 bytes.

The recommended file systems for memory cards are FAT12/16/32 or exFAT. Maximum volume sizes for each one are: 256MB, 4GB, 16TB* for FAT32 and 128PB for exFAT.

*Windows will refuse to format cards over 32GB using FAT32, offering exFAT and NTFS as an option but there are workarounds and third-party software that can do this.


FAT16 FAT32

Flash RAM Flash RAM
Read 6.1k 605 6.6k 607
Write 7.8k 607 8.6k 609
Read/Write 7.9k 607 8.7k 609

Some rounded up values of the code footprint depending on the functions used: for reading, writing or both. RAM size is expressed in bytes and it includes 512 bytes for the read/write buffer.

Features

  • Communication protocol: SPI
  • Supported memory cards: SD cards (MMC's are not implemented)
  • Includes SD driver: Yes
  • Supported file systems: FAT16 and FAT32
  • Support for LFN (Long File Names): Yes (up to 255 characters)
  • Formatting utility: No
  • CRC: not yet
  • Multiple files and folders instances: Yes
  • Ability to create folders and directories
  • Delete files: No (only file truncation is implemented)
  • FSInfo (FAT32): not implemented in order to reduce the code size. The only situations when this could be a downside is when querying for free space, creating files/folders or expand them since this is when it is necessary to search for a free cluster. If the card is mostly empty, even if it has a large capacity, the search for a free cluster will be very quick. As the card is filled it will take longer (few seconds).

Contents

  1. SD Card Pins
  2. Schematic Interface
  3. Return Values
  4. File Object Structures
  5. File names
  6. The Buffer, the Writing and the Flush
  7. Code Examples
  8. Library Configuration
  9. Functions and their usage
    1. Volume Management
    2. Directory Access
    3. File Access
  10. Download SD Card Library

SD Card Pinout

Both MMC/SD standards have their own proprietary protocols but they also support SPI which can be selected during card initialization. Since microcontrollers have SPI integrated hardware, this is the most used interface for memory cards.