⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd.s

📁 介绍AVR单片机与SD卡及MP3芯片的通信接口代码。
💻 S
字号:
	.module sd.c
	.area data(ram, con, rel)
_reading::
	.blkb 1
	.area idata
	.byte 0
	.area data(ram, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
	.dbsym e reading _reading c
_a::
	.blkb 1
	.area idata
	.byte 0
	.area data(ram, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
	.dbsym e a _a c
_pointer::
	.blkb 1
	.area idata
	.byte 0
	.area data(ram, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
	.dbsym e pointer _pointer c
	.area text(rom, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
	.dbfunc e sd_port_init _sd_port_init fV
	.even
_sd_port_init::
	.dbline -1
	.dbline 17
; /************************************************************/
; /*		         SD Code for   M32L					        */
; /*				   By   pasyong								*/
; /*				   2006-4									*/
; /*				   Base ICC6.31A					        */
; /************************************************************/
; #include <iom32v.h>
; #include <macros.h>
; #include "1011.h"
; #define uchar unsigned char
; #define uint unsigned int
; #define MMC_CS_PIN     BIT(4)    //PORTB.4 
; #define MMC_PORT       PORTB    
; 
;  uchar reading=0,a=0,pointer=0;
; void sd_port_init()
; {
	.dbline 18
; MMC_PORT|=MMC_CS_PIN;
	sbi 0x18,4
	.dbline -2
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.area data(ram, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
_i::
	.blkb 2
	.area idata
	.word 0
	.area data(ram, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
	.dbsym e i _i i
	.area text(rom, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
	.dbfunc e delay_nus _delay_nus fV
;              b -> R20
;              n -> R16,R17
	.even
_delay_nus::
	xcall push_gset1
	.dbline -1
	.dbline 23
; }
; uchar BUFFER[512];                   //扇区缓冲区
; uint i=0;
; void delay_nus(uint n)
; 	{
	.dbline 25
; 	unsigned char  b;
; 		for (b = 1; b<n; b++)
	ldi R20,1
	xjmp L6
L3:
	.dbline 26
L4:
	.dbline 25
	inc R20
L6:
	.dbline 25
	mov R2,R20
	clr R3
	cp R2,R16
	cpc R3,R17
	brlo L3
	.dbline -2
L2:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r b 20 c
	.dbsym r n 16 i
	.dbend
	.dbfunc e SD_Write_Command _SD_Write_Command fc
;          retry -> R12
;            tmp -> R10
;            arg -> y+10
;            cmd -> R14
	.even
_SD_Write_Command::
	st -y,r19
	st -y,r18
	xcall push_gset5
	mov R14,R16
	.dbline -1
	.dbline 33
; 			;
; 	} 
; //****************************************************************************
; //Send a Command to MMC/SD-Card
; //Return: the second byte of response register of MMC/SD-Card
; //****************************************************************************
; uchar SD_Write_Command(uchar cmd,unsigned long arg)
; {
	.dbline 35
;    uchar tmp;
;    uchar retry=0;
	clr R12
	.dbline 39
;    
;    //MMC_PORT|=MMC_CS_PIN;       //SD卡关闭   
;    //send 8 Clock Impulse
;    Write_Byte_SPI(0xFF);
	ldi R16,255
	xcall _Write_Byte_SPI
	.dbline 42
;    
;    //set MMC_Chip_Select to low (MMC/SD-Card active)
;   MMC_PORT&=~MMC_CS_PIN;       //SD卡使能
	cbi 0x18,4
	.dbline 44
; 
;    Write_Byte_SPI(cmd|0x40);   //送头命令
	mov R16,R14
	ori R16,64
	xcall _Write_Byte_SPI
	.dbline 45
;    Write_Byte_SPI(arg>>24);
	ldi R24,24
	ldi R25,0
	movw R30,R28
	ldd R2,z+10
	ldd R3,z+11
	ldd R4,z+12
	ldd R5,z+13
	st -y,R24
	movw R16,R2
	movw R18,R4
	xcall lsr32
	xcall _Write_Byte_SPI
	.dbline 46
;    Write_Byte_SPI(arg>>16);     //send 6 Byte Command to MMC/SD-Card
	movw R30,R28
	ldd R2,z+10
	ldd R3,z+11
	ldd R4,z+12
	ldd R5,z+13
	movw R2,R4
	clr R4
	clr R5
	mov R16,R2
	xcall _Write_Byte_SPI
	.dbline 47
;    Write_Byte_SPI(arg>>8);
	ldi R24,8
	ldi R25,0
	movw R30,R28
	ldd R2,z+10
	ldd R3,z+11
	ldd R4,z+12
	ldd R5,z+13
	st -y,R24
	movw R16,R2
	movw R18,R4
	xcall lsr32
	xcall _Write_Byte_SPI
	.dbline 48
;    Write_Byte_SPI(arg&0xff);
	ldi R20,255
	ldi R21,0
	ldi R22,0
	ldi R23,0
	movw R30,R28
	ldd R2,z+10
	ldd R3,z+11
	ldd R4,z+12
	ldd R5,z+13
	and R2,R20
	and R3,R21
	and R4,R22
	and R5,R23
	mov R16,R2
	xcall _Write_Byte_SPI
	.dbline 49
;    Write_Byte_SPI(0x95);       //仅仅对RESET有效的CRC效验码
	ldi R16,149
	xcall _Write_Byte_SPI
L8:
	.dbline 54
;   
;    //get 8 bit response
;    //Read_Byte_MMC(); //read the first byte,ignore it. 
;    do 
;    {  //Only last 8 bit is used here.Read it out. 
	.dbline 55
;       tmp = Read_Byte_SPI();
	xcall _Read_Byte_SPI
	mov R10,R16
	.dbline 56
;       retry++;
	inc R12
	.dbline 57
;    }
L9:
	.dbline 58
;    while((tmp==0xff)&&(retry<100));  //当没有收到有效的命令的时候
	mov R24,R10
	cpi R24,255
	brne L11
	mov R24,R12
	cpi R24,100
	brlo L8
L11:
	.dbline 60
;    
;    if(reading==0)
	lds R2,_reading
	tst R2
	brne L12
	.dbline 61
;    MMC_PORT|=MMC_CS_PIN;            //MMC_CS_PIN=1;
	sbi 0x18,4
	xjmp L13
L12:
	.dbline 62
;    else MMC_PORT&=~MMC_CS_PIN;      //MMC_CS_PIN=0;   
	cbi 0x18,4
L13:
	.dbline 63
;    return(tmp);
	mov R16,R10
	.dbline -2
L7:
	xcall pop_gset5
	adiw R28,2
	.dbline 0 ; func end
	ret
	.dbsym r retry 12 c
	.dbsym r tmp 10 c
	.dbsym l arg 10 l
	.dbsym r cmd 14 c
	.dbend
	.dbfunc e SD_Init _SD_Init fc
;              i -> R10
;           temp -> R12
;          retry -> R14
	.even
_SD_Init::
	xcall push_gset5
	sbiw R28,2
	.dbline -1
	.dbline 69
; }
; //****************************************************************************
; //SD卡初始化(SPI-MODE)
; //****************************************************************************
; uchar SD_Init(void)
; {  
	.dbline 72
;    uchar retry,temp;
;    uchar i;
;    MMC_PORT&=~MMC_CS_PIN;      //SD卡使能
	cbi 0x18,4
	.dbline 74
;   
;   delay_nus(250);  //Wait MMC/SD ready...
	ldi R16,250
	ldi R17,0
	xcall _delay_nus
	.dbline 75
;    for (i=0;i<0x0f;i++) 
	clr R10
	xjmp L18
L15:
	.dbline 76
	.dbline 77
	ldi R16,255
	xcall _Write_Byte_SPI
	.dbline 78
L16:
	.dbline 75
	inc R10
L18:
	.dbline 75
	mov R24,R10
	cpi R24,15
	brlo L15
	.dbline 80
;    {
;       Write_Byte_SPI(0xff); //send 74 clock at least!!!
;    }
;    //Send Command CMD0 to MMC/SD Card
;    retry=0;
	clr R14
L19:
	.dbline 83
;    
;    do
;    { //retry 200 times to send CMD0 command 
	.dbline 84
;      temp=SD_Write_Command(0,0);
	ldi R20,0
	ldi R21,0
	ldi R22,0
	ldi R23,0
	std y+0,R22
	std y+1,R23
	movw R18,R20
	clr R16
	xcall _SD_Write_Command
	mov R12,R16
	.dbline 85
;      retry++;
	inc R14
	.dbline 86
;      if(retry==100) 
	mov R24,R14
	cpi R24,100
	brne L22
	.dbline 87
;      { 
	.dbline 88
;       ;//CMD0 Error!
	.dbline 89
;      }
L22:
	.dbline 90
;    } 
L20:
	.dbline 91
;    while(temp!=1);  
	mov R24,R12
	cpi R24,1
	brne L19
	.dbline 94
;    
;    //Send Command CMD1 to MMC/SD-Card
;    retry=0;
	clr R14
L24:
	.dbline 96
;    do
;    { //retry 100 times to send CMD1 command 
	.dbline 97
;      temp=SD_Write_Command(1,0);
	ldi R20,0
	ldi R21,0
	ldi R22,0
	ldi R23,0
	std y+0,R22
	std y+1,R23
	movw R18,R20
	ldi R16,1
	xcall _SD_Write_Command
	mov R12,R16
	.dbline 98
;      retry++;
	inc R14
	.dbline 99
;      if(retry==100) 
	mov R24,R14
	cpi R24,100
	brne L27
	.dbline 100
;      { 
	.dbline 101
;      ;
	.dbline 102
;      }
L27:
	.dbline 103
;    } 
L25:
	.dbline 104
;    while(temp!=0); 
	tst R12
	brne L24
	.dbline 105
;    retry=0; 
	clr R14
	.dbline 106
;     SD_Write_Command(16,512);     //设置一次读写BLOCK的长度为512个字节
	ldi R20,0
	ldi R21,2
	ldi R22,0
	ldi R23,0
	std y+0,R22
	std y+1,R23
	movw R18,R20
	ldi R16,16
	xcall _SD_Write_Command
	.dbline 108
;   
;    MMC_PORT|=MMC_CS_PIN;   //MMC_CS_PIN=1;  //set MMC_Chip_Select to high 
	sbi 0x18,4
	.dbline 109
;    return(0); //All commands have been taken.
	clr R16
	.dbline -2
L14:
	adiw R28,2
	xcall pop_gset5
	.dbline 0 ; func end
	ret
	.dbsym r i 10 c
	.dbsym r temp 12 c
	.dbsym r retry 14 c
	.dbend
	.dbfunc e SD_Read_Block _SD_Read_Block fc
;           temp -> R20
;              i -> R22,R23
;        address -> y+6
	.even
_SD_Read_Block::
	xcall push_arg4
	xcall push_gset2
	sbiw R28,2
	.dbline -1
	.dbline 115
; } 
; //****************************************************************************
; //从SD卡读一个扇区  Return 0 if no Error.
; //****************************************************************************
; uchar SD_Read_Block(unsigned long address)
; {  
	.dbline 116
;    uchar temp=0;uint i=0;
	clr R20
	.dbline 116
	clr R22
	clr R23
	.dbline 117
;    reading=1; 
	ldi R24,1
	sts _reading,R24
	.dbline 118
;    temp=SD_Write_Command(17,address);     //读出RESPONSE     
	movw R30,R28
	ldd R2,z+6
	ldd R3,z+7
	ldd R4,z+8
	ldd R5,z+9
	std y+0,R4
	std y+1,R5
	movw R18,R2
	ldi R16,17
	xcall _SD_Write_Command
	mov R20,R16
L30:
	.dbline 120
	.dbline 120
	.dbline 120
L31:
	.dbline 119
;    while (Read_Byte_SPI()!= 0xfe)
	xcall _Read_Byte_SPI
	cpi R16,254
	brne L30
	.dbline 121
;    {;}  //直到读取到了数据的开始头0XFE,才继续
;    for(i=0; i<512; i++)
	clr R22
	clr R23
	xjmp L36
L33:
	.dbline 122
	.dbline 123
	xcall _Read_Byte_SPI
	ldi R24,<_BUFFER
	ldi R25,>_BUFFER
	movw R30,R22
	add R30,R24
	adc R31,R25
	std z+0,R16
	.dbline 124
L34:
	.dbline 121
	subi R22,255  ; offset = 1
	sbci R23,255
L36:
	.dbline 121
	cpi R22,0
	ldi R30,2
	cpc R23,R30
	brlo L33
	.dbline 125
; 	{
; 	BUFFER[i]=Read_Byte_SPI();
; 	}
;    Read_Byte_SPI();//CRC - Byte 
	xcall _Read_Byte_SPI
	.dbline 126
;    Read_Byte_SPI();//CRC - Byte
	xcall _Read_Byte_SPI
	.dbline 127
;    reading=0; 
	clr R2
	sts _reading,R2
	.dbline 128
;    MMC_PORT|=MMC_CS_PIN;        //关闭SD卡
	sbi 0x18,4
	.dbline 129
;    return(temp);
	mov R16,R20
	.dbline -2
L29:
	adiw R28,2
	xcall pop_gset2
	adiw R28,4
	.dbline 0 ; func end
	ret
	.dbsym r temp 20 c
	.dbsym r i 22 i
	.dbsym l address 6 l
	.dbend
	.area bss(ram, con, rel)
	.dbfile C:\DOCUME~1\pas\桌面\SD卡MP3程序\sd.c
_BUFFER::
	.blkb 512
	.dbsym e BUFFER _BUFFER A[512:512]c

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -