پاسخ : نحوه ی ارتباط مموری sdبا میکرو AVR
سلام،اقا من تو پروژه که تو پوشه examplesخود کدویژن هستش با نام sdcard4دیدم که پایه ی CD رو به تایمر 1 وصل کرده و اونوقت تایمر 1 رو پیکربندی کرده.ضمنا اصلا ابتدای برنامه spi رو کانفیک نکرده .میشه یه توضیح راجع بهش بدید.آخه من مدارم رو بستم و CD رو هم به پایه SS متصل کردم.الیته من با atmega32 بستم.
سلام،اقا من تو پروژه که تو پوشه examplesخود کدویژن هستش با نام sdcard4دیدم که پایه ی CD رو به تایمر 1 وصل کرده و اونوقت تایمر 1 رو پیکربندی کرده.ضمنا اصلا ابتدای برنامه spi رو کانفیک نکرده .میشه یه توضیح راجع بهش بدید.آخه من مدارم رو بستم و CD رو هم به پایه SS متصل کردم.الیته من با atmega32 بستم.
کد:
[right]/****************************************************************************** Creation, attribute and time stamp changing of a file on a MMC/SD/SD HC card example for the CodeVisionAVR V2.04.4+ compiler (C) 2009 HP InfoTech s.r.l., Pavel Haiduc The example is intended to be run on the Mega128-Dev development board from www.priio.com. The development board uses an ATmega128 chip running at 14.745600 MHz The SD card socket on the development board is connected to the following I/O ports: SD socket CD4050 ATmega128 pin1 /CS -----<|----- PORTD bit6 pin2 SI -----<|----- MOSI pin3 GND pin4 +3.3V pin5 SCK -----<|----- SCK pin6 GND pin7 SO ------------ MISO pin9 GND pin10 CD ------------ PORTG bit4 with 10k pull-up resistor to +5V pin11 WP ------------ PORTG bit3 with 10k pull-up resistor to +5V pin12 GND The CD4050 CMOS buffer is powered from a +3.3V supply and performs logic level translation from 5V (AVR) to 3.3V (SD card). For other I/O port connections, you need to make appropriate changes in the "Project|Configure|C Compiler|Libraries|MMC/SD/SD HC Card and FAT Support" menu. The development board also uses a PCF8563 RTC connected to the following I/O ports: PCF8563 ATmega128 pin5 SDA ------------- PORTD bit1 with 10k pull-up resistor to +5V pin6 SCL ------------- PORTD bit0 with 10k pull-up resistor to +5V The development board's serial socket P9 must be connected to the PC COM port. The CodeVisionAVR Terminal is used for displaying data received from the development board. The Terminal must be configured for: Baud Rate: 19200 Data Bits: 8 Parity: None Stop Bits: 1 Emulation: TTY Handshaking: None Append LF on Reception: OFF Appearance|Rows: 40 Appearance|Columns: 80 If you are using the CodeVisionAVR Evaluation version, you may program into the FLASH the precompiled SDCARD4.HEX file found in the \Examples\SDCARD4\EXE directory ******************************************************************************/ /* ATmega128 I/O register definitions */ #include <mega128.h> /* FAT on MMC/SD/SD HC card support */ #include <ff.h> /* printf */ #include <stdio.h> /* PCF8563 RTC functions*/ #include <PCF8563.h> /* The PCF8563 RTC on the I2C bus is connected to ATmega128 PORTD SDA - PORTD.1 SCL - PORTD.0 */ #asm .equ __i2c_port=0x12 .equ __sda_bit=1 .equ __scl_bit=0 #endasm /* Timer1 overflow interrupt frequency [Hz] */ #define T1_OVF_FREQ 100 /* Timer1 clock prescaler value */ #define T1_PRESC 1024L /* Timer1 initialization value after overflow */ #define T1_INIT (0x10000L-(_MCU_CLOCK_FREQUENCY_/(T1_PRESC*T1_OVF_FREQ))) /* USART Baud rate */ #define BAUD_RATE 19200 #define BAUD_INIT (_MCU_CLOCK_FREQUENCY_/(BAUD_RATE*16L)-1) /* FAT function result */ FRESULT res; /* number of bytes written/read to the file */ unsigned int nbytes; /* will hold the information for logical drive 0: */ FATFS fat; /* will hold the file information */ FIL file; /* will hold file attributes, time stamp information */ FILINFO finfo; /* file path */ char path[]="0:/file.txt"; /* text to be written to the file */ char text[]="I like CodeVisionAVR!"; /* file read buffer */ char buffer[256]; /* 100Hz timer interrupt generated by ATmega128 Timer1 overflow */ interrupt [TIM1_OVF] void timer_comp_isr(void) { /* re-initialize Timer1 */ TCNT1H=T1_INIT>>8; TCNT1L=T1_INIT&0xFF; /* card access low level timing function */ disk_timerproc(); } /* error message list */ flash char * flash error_msg[]= { "", /* not used */ "FR_DISK_ERR", "FR_INT_ERR", "FR_INT_ERR", "FR_NOT_READY", "FR_NO_FILE", "FR_NO_PATH", "FR_INVALID_NAME", "FR_DENIED", "FR_EXIST", "FR_INVALID_OBJECT", "FR_WRITE_PROTECTED", "FR_INVALID_DRIVE", "FR_NOT_ENABLED", "FR_NO_FILESYSTEM", "FR_MKFS_ABORTED", "FR_TIMEOUT" }; /* display error message and stop */ void error(FRESULT res) { if ((res>=FR_DISK_ERR) && (res<=FR_TIMEOUT)) printf("ERROR: %p\r\n",error_msg[res]); /* stop here */ while(1); } /* display file's attribute, size and time stamp */ void display_status(char *file_name) { if ((res=f_stat(file_name,&finfo))==FR_OK) printf("File: %s, Attributes: %c%c%c%c%c\r\n" "Date: %02u/%02u/%u, Time: %02u:%02u:%02u\r\n" "Size: %lu bytes\r\n", finfo.fname, (finfo.fattrib & AM_DIR) ? 'D' : '-', (finfo.fattrib & AM_RDO) ? 'R' : '-', (finfo.fattrib & AM_HID) ? 'H' : '-', (finfo.fattrib & AM_SYS) ? 'S' : '-', (finfo.fattrib & AM_ARC) ? 'A' : '-', finfo.fdate & 0x1F, (finfo.fdate >> 5) & 0xF, (finfo.fdate >> 9) + 1980, (finfo.ftime >> 11), (finfo.ftime >> 5) & 0x3F, (finfo.ftime & 0xF) << 1, finfo.fsize); else /* an error occured, display it and stop */ error(res); } void main(void) { /* initialize Timer1 overflow interrupts in Mode 0 (Normal) */ TCCR1A=0x00; /* clkio/1024 */ TCCR1B=(1<<CS12)|(1<<CS10); /* timer overflow interrupts will occur with 100Hz frequency */ TCNT1H=T1_INIT>>8; TCNT1L=T1_INIT&0xFF; /* enable Timer1 overflow interrupt */ TIMSK=1<<TOIE1; /* initialize the USART0 TX, 8N1, Baud rate: 19200 */ UCSR0A=0; UCSR0B=1<<TXEN0; UCSR0C=(1<<UCSZ01)|(1<<UCSZ00); UBRR0H=BAUD_INIT>>8; UBRR0L=BAUD_INIT&0xFF; /* init the PCF8563 RTC */ rtc_init(0,RTC_CLKOUT_OFF,RTC_TIMER_OFF); /* init the pointer to the RTC function used for reading time */ prtc_get_time=(void (*)(unsigned char *,unsigned char *,unsigned char *)) rtc_get_time; /* init the pointer to the RTC function used for reading time */ prtc_get_date=(void (*)(unsigned char *,unsigned char *,unsigned int *)) rtc_get_date; /* globally enable interrupts */ #asm("sei") /* mount logical drive 0: */ if ((res=f_mount(0,&fat))==FR_OK) printf("Logical drive 0: mounted OK\r\n"); else /* an error occured, display it and stop */ error(res); /* create a new file in the root of drive 0: and set write access mode */ if ((res=f_open(&file,path,FA_CREATE_ALWAYS | FA_WRITE))==FR_OK) printf("File %s created OK\r\n",path); else /* an error occured, display it and stop */ error(res); /* write some text to the file, without the NULL string terminator sizeof(data)-1 */ if ((res=f_write(&file,text,sizeof(text)-1,&nbytes))==FR_OK) printf("%u bytes written of %u\r\n",nbytes,sizeof(text)-1); else /* an error occured, display it and stop */ error(res); /* close the file */ if ((res=f_close(&file))==FR_OK) printf("File %s closed OK\r\n",path); else /* an error occured, display it and stop */ error(res); /* open the file in read mode */ if ((res=f_open(&file,path,FA_READ))==FR_OK) printf("File %s opened OK\r\n",path); else /* an error occured, display it and stop */ error(res); /* read and display the file's content. make sure to leave space for a NULL terminator in the buffer, so maximum sizeof(buffer)-1 bytes can be read */ if ((res=f_read(&file,buffer,sizeof(buffer)-1,&nbytes))==FR_OK) { printf("%u bytes read\r\n",nbytes); /* NULL terminate the char string in the buffer */ buffer[nbytes+1]=NULL; /* display the buffer contents */ printf("Read text: \"%s\"\r\n",buffer); } else /* an error occured, display it and stop */ error(res); /* close the file */ if ((res=f_close(&file))==FR_OK) printf("File %s closed OK\r\n",path); else /* an error occured, display it and stop */ error(res); /* display file's attribute, size and time stamp */ display_status(path); /* change file's attributes, set the file to be Read-Only */ if ((res=f_chmod(path,AM_RDO,AM_RDO))==FR_OK) printf("Read-Only attribute set OK\r\n",path); else /* an error occured, display it and stop */ error(res); /* change file's time stamp */ #define DAY (6) #define MONTH (3) #define YEAR (2000) #define SECOND (0) #define MINUTE (40) #define HOUR (14) finfo.fdate=DAY | (MONTH<<5) | ((YEAR-1980)<<9); finfo.ftime=(SECOND>>1) | (MINUTE<<5) | (HOUR<<11); if ((res=f_utime(path,&finfo))==FR_OK) printf("New time stamp %02u/%02u/%u %02u:%02u:%02u set OK\r\n", DAY,MONTH,YEAR,HOUR,MINUTE,SECOND); else /* an error occured, display it and stop */ error(res); /* display file's new attribute and time stamp */ display_status(path); /* change file's attributes, clear the Read-Only attribute */ if ((res=f_chmod(path,0,AM_RDO))==FR_OK) printf("Read-Only attribute cleared OK\r\n",path); else /* an error occured, display it and stop */ error(res); /* display file's new attribute and time stamp */ display_status(path); /* stop here */ while(1); } [/right]
دیدگاه