基于Atmega128单片机SD卡读写程序.docx
《基于Atmega128单片机SD卡读写程序.docx》由会员分享,可在线阅读,更多相关《基于Atmega128单片机SD卡读写程序.docx(13页珍藏版)》请在三一办公上搜索。
1、基于Atmega128单片机SD卡读写程序基于Atmega128单片机SD卡读写程序 实物图对照 接线图 以下是一个简单的测试SD卡读写的程序,程序是基于Atmega128单片机编写的,对于Atmega的其他单片机仅需要做管脚改动就可以使用,其他单片机更改要更大。 sd.h /* /SPI各线所占用的端口 #define SD_SS PB6 #define SD_SCK PB1 #define SD_MOSI PB2 #define SD_MISO PB3 /* #define SD_DDR DDRB #define SD_PORT PORTB #define SD_PIN PINB #def
2、ine SD_SS_H SD_PORT |= (1#define SDSS_L SD_PORT &= (1#define SD_SCK_H SD_PORT |= (1#define SD_SCK_L SD_PORT &= (1#define SD_MOSI_H SD_PORT |= (1#define SD_MOSI_L SD_PORT &= (1 #define SD_MISO_IN (SD_PIN&(1/- / 错误号 /- #define INIT_CMD0_ERROR 0xFF #define INIT_CMD1_ERROR 0xFE #define WRITE_BLOCK_ERROR
3、 0xFD #define READ_BLOCK_ERROR 0xFC #define TRUE 0x01 /- / MMC/SD 命令(命令号从40开始,只列出基本命令,并没有都使用) /- #define SD_RESET 0x40 + 0 #define SD_INIT 0x40 + 1 #define SD_READ_CSD 0x40 + 9 #define SD_READ_CID 0x40 + 10 #define SD_STOP_TRANSMISSION 0x40 + 12 #define SD_SEND_STATUS 0x40 + 13 #define SD_SET_BLOCKL
4、EN 0x40 + 16 #define SD_READ_BLOCK 0x40 + 17 #define SD_READ_MULTI_BLOCK 0x40 + 18 #define SD_WRITE_BLOCK 0x40 + 24 #define SD_WRITE_MULTI_BLOCK 0x40 + 25 /片选关(MMC/SD-Card Invalid) #define SD_Disable SD_SS_H /片选开 (MMC/SD-Card Active) #define SD_Enable SD_SS_L SD_TEST.C /*/ /ICC-AVR application build
5、er : 03-5-20 8:39:11 / Target : M128 / Crystal: 3.6864Mhz #include #include #include sd.h void uart0_init(void); void putchar(unsigned char content); void putstr(unsigned char *s); void SD_Port_Init(void); unsigned char SD_Init(void); unsigned char SD_write_sector(unsigned long addr,unsigned char *B
6、uffer); unsigned char SD_read_sector(unsigned long addr,unsigned char *Buffer); unsigned char SPI_TransferByte(unsigned char byte); unsigned char Write_Command_SD(unsigned char cmd,unsigned long address); unsigned long SD_find(void); /* /串口调试程序 /* void uart0_init(void) UCSR0B = 0x00; /disable while
7、setting baud rate UCSR0A = 0x00; UCSR0C = 0x06; / 00000110 UART0设置为异步模式、无奇偶校验、1位停止位、8位数据位 UBRR0L = 0x17; /set baud rate lo UBRR0H = 0x00; /set baud rate hi 设置UART0口通信速率9600 UCSR0B = 0x18; void putchar(unsigned char content) while(!(UCSR0A & (1 UDRE0); /* 判断上次发送有没有完成 */ UDR0 = content; /* 发送数据 */ voi
8、d putstr(unsigned char *s) while(*s) putchar(*s); s+; /* /端口初始化 void SD_Port_Init(void) /* SD_PORT |= (1 SD_DDR |= (1 SD_DDR &= (1 /* /初始化 MMC/SD 卡为SPI模式 unsigned char SD_Init(void) /* unsigned char retry,temp; unsigned char i; SPCR=0x53; /设定SPI为128分频,慢速进行初始化 SPSR=0x00; for (i=0;i24); SPI_TransferBy
9、te(address16); SPI_TransferByte(address8); SPI_TransferByte(address); SPI_TransferByte(0xFF); SPI_TransferByte(0xFF); do tmp = SPI_TransferByte(0xFF); /发送8个时钟接受最后一个字节 retry+; while(tmp=0xff)&(retry8); return(tmp); /* /写一个扇区(512Byte) to MMC/SD-Card /如果写完成返回TRUE unsigned char SD_write_sector(unsigned
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 Atmega128 单片机 SD 读写 程序
链接地址:https://www.31ppt.com/p-3384980.html