#include "msp430x14x.h"
#include "string.h"
#include "FAT16.h"
void open_xt2(void)
{
BCSCTL1 &= ~XT2OFF;
char n;
do
{
IFG1 &= ~OFIFG;
for(n=0xee;n>0;n--)
{
;
}
}
while((IFG1&OFIFG)!=0);
BCSCTL2 |= SELM_2 + SELS;
}
void SPI_init(void)
{
open_xt2();
UCTL1=SWRST;//开启设置
UTCTL1=CKPH+SSEL1+STC;//选择SMCLK
UCTL1|=CHAR+SYNC+MM;//8为数据 SPI模式
UBR01=0x03;//波特率设置
UBR11=0X00;
UMCTL1=0X00;
UCTL1&=~SWRST;//关闭设置
ME2|=USPIE1;//使能SPI
}
//SD卡初始化
char SD_init(void)
{
int i;
int time=5;
unsigned char response1=0xff,response2=0xff;
P5SEL|=0X0e;//0000 1110 P5.0.1.2.3选择第二功能
P5DIR|=0X0B;//0000 1011
SPI_init();
CS_1;
do
{
if(!time)
return (failed);
time--;
for(i=0;i<10;i++)
Sendbyte(0xff);//延时80个CLK
CS_0;
Sendcmd(cmd0,0,0x95);
}
while(getresponse1()!=0x01);
while((response1!=0x01)||(response2!=0x00))
{
CS_1;
Sendbyte(0xff);//延时8个CLK
CS_0;
Sendcmd(cmd55,0,0xff);
response1=getresponse1();
CS_1;
Sendbyte(0xff);
CS_0;
Sendcmd(acmd41,0,0xff);
response2=getresponse0();
}
CS_1;
Sendbyte(0xff);
return (SUCCESS);
}
//发送命令字
void Sendcmd(unsigned char cmd,unsigned long data,unsigned char CRC)//cmd 命令字 data 命令指令 CRC 校验位
{
unsigned char command[6];
int i;
command[0]=(cmd|0x40);
for(i=1;i<5;i++)
command[i]=(char)(data>>8*(4-i));
command[5]=CRC;
for(i=0;i<6;i++)
Sendbyte(command[i]);
}
//发送八位字节 同时用于延时八个CLK
char Sendbyte(unsigned char byte)
{
while((IFG2&UTXIFG1)==0);//等待...直到发送中断标志位置位
TXBUF1=byte;
while((IFG2&URXIFG1)==0);//等待...直到接受缓存数据满 再取回数据
return (RXBUF1);
}
//读取SD卡容量信息,此信息存储在CSD寄存器中 通过发送cmd9来读取
//需要数据为80-84/SD_len 62-73/SD_size 47-49/SD_size_mult
unsigned long Read_SD_size(void)
{
unsigned int data,i,j,SD_size;
unsigned char SD_len,SD_size_mult;
unsigned long SD_card_size;
do
{CS_1;
Sendbyte(0xff);
CS_0;
Sendcmd(cmd9,0,0xff);
}
while(getresponse0()!=0x00);
do
data=response=Sendbyte(0xff);
while(data!=0xfe);
for(i=0;i<5;i++)//88-127
data=Sendbyte(0xff);
data=Sendbyte(0xff);//80-87
SD_len=data&0x0F;//取出80-83
data=Sendbyte(0xff);//72-79
SD_size=(data&0x03)<<10;//取出72-72位 并放到最高位
data=Sendbyte(0xff);//64-71
SD_size+=data<<2;
data=Sendbyte(0xff);//56-63
SD_size+=data>>6;
data=Sendbyte(0xff);//48-55
SD_size_mult=(data&0x3)<<1;//取出48-49
data=Sendbyte(0xff);//40-47
SD_size_mult+=data>>7;
for(i=0;i<5;i++)
data=Sendbyte(0xff);//0-39
for(i=0;i<4;i++)//延时
Sendbyte(0xff);
data=Sendbyte(0xff);
CS_0;
//开始计算容量
SD_card_size=SD_size+1;
for(i=2,j=SD_size_mult+2;j>1;j--)
i<<=1;
SD_card_size*=i;
for(i=2,j=SD_len;j>1;j--)
i<<=1;
SD_card_size*=i;
return SD_card_size;
}
//回应0x00
unsigned char getresponse0()
{
int i=0;
while(i<10)
{
response=Sendbyte(0xff);
if(response==0x00)break;
i++;
}
return (response);
}
//回应0x01
unsigned char getresponse1()
{
int i=0;
while(i<10)
{
response=Sendbyte(0xff);
if(response==0x01)break;
i++;
}
return (response);
}
//设置数据长度 cmd16
char Set_block_length(unsigned long length)
{
CS_0;
Sendcmd(cmd16,length,0xff);
while(getresponse0()!=0x00)
{
SD_init();
CS_0;
Sendcmd(cmd16,length,0xff);
}
CS_1;
Sendbyte(0xff);
return (SUCCESS);
}
//向SD卡写数据(单块) 写入的地址 数据长度 要写入的数据
void Write_data(unsigned long address,unsigned long length ,unsigned char *buffer)
{
int check;
unsigned long i;
check=Set_block_length(length);
if(check==SUCCESS)
{
do
{
Sendbyte(0xff);
CS_0;
Sendcmd(cmd24,address,0xff);
}
while(getresponse0()!=0x00);
Sendbyte(0xff);
Sendbyte(0xff);
Sendbyte(0xfe);
for(i=0;i<length;i++)
Sendbyte(*buffer++);
Sendbyte(0xff);
Sendbyte(0xff);
}
}
//读取SD卡数据(单块)
char Read_data(unsigned long address,unsigned long length,unsigned char *buffer)
{
int check;
unsigned long i;
check=Set_block_length(length);
if(check==SUCCESS)
{
do
{
Sendbyte(0xff);
CS_0;
Sendcmd(cmd17,address,0xff);
}
while(getresponse0()!=0x00);
while(Sendbyte(0xff)!=0xfe);
for(i=0;i<length;i++)
buffer[i]=Sendbyte(0xff);
}
return (*buffer);
}
//FAT系统初始化
void FAT_init(void)
{
unsigned char buffer[512];
Read_BPB(buffer);
sector_per_cluster=buffer[13];
resvered_sector=(long)((buffer[15]<<8)|buffer[14]);
hidden_sector=((long)(buffer[31])<<24)|((long)(buffer[30])<<16)|((long)(buffer[29])<<8)|(long)(buffer[28]);
num_sector_FAT16=(long)((buffer[20]<<8)|buffer[19]);
num_FAT=buffer[16];
num_sector_FAT=(long)((buffer[23]<<8)|buffer[22]);
FAT1_begin_sector=resvered_sector+hidden_sector;
FAT2_begin_sector=FAT1_begin_sector+num_sector_FAT;
FDT_begin_sector=FAT1_begin_sector+num_sector_FAT*num_FAT;
data_begin_sector=FDT_begin_sector+32;
}
//读取BPB表
char Read_BPB(unsigned char *p)
{
unsigned long table;
Read_data(0,512,p);
if((p[0]!=0xeb)&&(p[510]==0x55)&&(p[511]==0xAA))
{
table=(long)(p[457])<<24|(long)(p[456])<<16|(long)(p[455])<<8|p[454];
Read_data(table*512,512,p);
return (*p);
}
else
return (*p);
}
//创建文件
void creat_file(unsigned char *file_name)
{
int j=0;
long i;
unsigned long cluster;
unsigned char file_fdt[512];
unsigned char file_fat[512];
buffer_fdt[92]=(unsigned char)num_byte;
buffer_fdt[93]=(unsigned char)(num_byte>>8);
buffer_fdt[94]=(unsigned char)(num_byte>>16);
buffer_fdt[95]=(unsigned char)(num_byte>>24);
for(i=0;i<512;i++)
{
if((i>=0&&i<64)||(i>=72&&i<96))
file_fdt[i]=buffer_fdt[i];
else if(i>=64&&i<72)
{
file_fdt[i]=file_name[j];
j++;
}
else
file_fdt[i]=0x00;
}
Write_data(FDT_begin_sector*512,512,file_fdt);
Write_data(FDT_begin_sector*512,512,file_fdt);
cluster=num_byte/512/16;
if(num_byte%8192)
cluster+=1;
for(i=0;i<4;i++)
file_fat[i]=buffer_fat[i];
if(cluster==0)
{
for(;i<512;i++)
file_fat[i]=0x00;
}
else if(cluster==1)
{
file_fat[i++]=0xff;
file_fat[i++]=0xff;
for(;i<512;i++)
file_fat[i]=0x00;
}
else
{
for(;cluster>1;cluster--)
{
file_fat[i]=(unsigned char)((i+2)/2);
i++;
file_fat[i]=(unsigned char)(((i+1)/2)>>8);
i++;
}
file_fat[i++]=0xff;
file_fat[i++]=0xff;
for(;i<512;i++)
file_fat[i]=0x00;
}
Write_data(FAT1_begin_sector*512,512,file_fat);
Write_data(FAT1_begin_sector*512,512,file_fat);
Write_data(FAT2_begin_sector*512,512,file_fat);
Write_data(FAT2_begin_sector*512,512,file_fat);
}
//串口初始化
void uart_init(void)
{
U0CTL |= SWRST; //开启设置
U0TCTL = SSEL1; //选择 SMCLK=8M 波特率=9600
U0BR0 =0x41;
U0BR1 =0x03;
U0MCTL =0x92; //00000000
U0CTL |= CHAR; //长度8位
U0CTL &= ~SWRST;
}
- 1
- 2
前往页