[TUT][C] Simple FAT and SD tutorial

Go To Last Post
55 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using atmega32 and rus at 8MHz . I change the code according that. But when i input 1,2,3 to putty it doesn’t give any output. why? please help me.

 

#include <avr/io.h>

#define F_CPU 8000000UL
#include <util/delay.h>

void USARTInit(unsigned int ubrr_value) {
    //Set Baud rate
    UBRRH = (unsigned char)(ubrr_value >> 8);
    UBRRL = (unsigned char)(ubrr_value & 255);
    // Frame Format: asynchronous, no parity, 1 stop bit, char size 8
    UCSRC = (3 << UCSZ0);
    //Enable The receiver and transmitter
    UCSRB = (1 << RXEN) | (1 << TXEN);
}

char USARTReadChar() { // blocking
    while(!(UCSRA & (1<<RXC))) {}
    return UDR;
}

void USARTWriteChar(char data) { // blocking
    while(!(UCSRA & (1<<UDRE))) {}
    UDR=data;
}
void uwrite_hex(unsigned char n) {
    if(((n>>4) & 15) < 10)
    USARTWriteChar('0' + ((n>>4)&15));
    else
    USARTWriteChar('A' + ((n>>4)&15) - 10);
    n <<= 4;
    if(((n>>4) & 15) < 10)
    USARTWriteChar('0' + ((n>>4)&15));
    else
    USARTWriteChar('A' + ((n>>4)&15) - 10);
}

void uwrite_str(char *str) {
    char i;
    
    for(i=0; str[i] ; i++)
    USARTWriteChar(str[i]);
}

#define CS (1<<PINB4)
#define MOSI (1<<PINB5)
#define MISO (1<<PINB6)
#define SCK (1<<PINB7)
#define CS_DDR DDRB
#define CS_ENABLE() (PORTB &= ~CS)
#define CS_DISABLE() (PORTB |= CS)

void SPI_init() {
    CS_DDR |= CS; // SD card circuit select as output
    DDRB |= MOSI + SCK; // MOSI and SCK as outputs
    PORTB |= MISO; // pull up in MISO, might not be needed
    
    // Enable SPI, master, set clock rate fck/128
    SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0) | (1<<SPR1);
}

unsigned char SPI_write(unsigned char ch) {
    SPDR = ch;
    while(!(SPSR & (1<<SPIF))) {}
    return SPDR;
}

void SD_command(unsigned char cmd, unsigned long arg, unsigned char crc, unsigned char read) {
    unsigned char i, buffer[8];
    
    uwrite_str("CMD ");
    uwrite_hex(cmd);
    
    CS_ENABLE();
    SPI_write(cmd);
    SPI_write(arg>>24);
    SPI_write(arg>>16);
    SPI_write(arg>>8);
    SPI_write(arg);
    SPI_write(crc);
    
    for(i=0; i<read; i++)
    buffer[i] = SPI_write(0xFF);
    
    CS_DISABLE();
    
    for(i=0; i<read; i++) {
        USARTWriteChar(' ');
        uwrite_hex(buffer[i]);
    }
    
    uwrite_str("\r\n");
}

int main(int argc, char *argv[]) {
    char i;
    
    USARTInit(25); // 8 MHz / (16 * 19200 baud) - 1 = 25
    SPI_init();

    // ]r:10
    CS_DISABLE();
    for(i=0; i<10; i++) // idle for 1 bytes / 80 clocks
    SPI_write(0xFF);
    
    while(1) {
        switch(USARTReadChar()) {
            case '1':
            SD_command(0x40, 0x00000000, 0x95, 8);
            break;
            case '2':
            SD_command(0x41, 0x00000000, 0xFF, 8);
            break;
            case '3':
            SD_command(0x50, 0x00000200, 0xFF, 8);
            break;
        }
    }
    
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That code is not using FatFs?!? This tutorial is about using FatFs. Did you post in the wrong place?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just edit spi88.c file to atmega32 & 8MHz. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simple FAT and SD tutorial provided spi88.c file. I just edit this to atmega32 with  8MHz. when i input 1,2,3 there nothing happened to README.TXT

Pages