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

📄 lqsdmain.c

📁 mc9s12dg128b单片机操作SD的源码
💻 C
字号:
/**************************************************************************************
Code Warrior 4.6: 2008-11-14 20:42:26
Target : MC9S12DG128B
Crystal: 16.000Mhz
busclock:16.000MHz      
pllclock:32.000MHz;
Designed by Chiu Sir      
E-mail:chiusir@yahoo.cn       

博客:  http://longqiu.21ic.org
淘宝店:http://shop36265907.taobao.com

下面的类型定义请拷贝到左侧窗口的Libraries文件加下面的mc9s12dg128.h中:
typedef unsigned char U8;
typedef unsigned int  U16;
typedef unsigned long U32;
typedef signed char   S8;
typedef signed int    S16;

程序使用说明:
SD卡读写操作试验,默认为1G SD卡
串口波特率:19200 
按UP    对块的读操作增加    200 毫秒延时;
按DWON  对块的读操作键增加  200 毫秒延时;
按OK    对块的读操作为      0   毫秒延时;
按ESC   对块的读操作恢复为  2000毫秒延时;

使用步骤:
1.格式化SD卡,文件系统最好用FAT(不是FAT32);
2.在电脑上建立一个LONGQIU0.TXT文件;
3.将文件的内容反复复制,大小为所需存储大小,最好大于1M字节;
4.讲给文件复制到SD卡;
5.将SD卡插入开发板SD卡插槽;
6.上电后数据自动保存;
7.数据采集完毕后关闭开发板电源,取出SD卡;
8.将SD卡插入电脑,拷贝LONGQIU0.TXT文件到电脑上;
9.复制LONGQIU0.TXT文件中相应的内容到一个新建的WORD文档中,可见相应格式很有规律;
10.重新复制WORD中的有用内容到任意TXT文档中保存和处理即可。

*****************************************************************************************/
#include <hidef.h>           /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */
#include <stdio.h>
#include <string.h>
#include "LQprintp.h" 
#include "LQfun.h"


#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"   
U8 RXBUF[512]="";
U8 TXBUF[512]="";
 
void CLS_Buf(U8 buffer[],U8 L)
{
    //-- Flushing Rx buffer 
    int i;     
    for(i=0;i<L;i++)	//128[word]*16[blk]=8192[byte]
		*(buffer+i)=' ';
}
#pragma CODE_SEG DEFAULT     
void main(void)
{ 	
	int i=0,j=0;
	U16 RdWrAdd=0xff,cnt=0;
	U8  selKey=0xff,dtime=1;
	S8  tmch[100]="You are welcome to longqiu science & technology limited company!";   
	
	Init_Dev();
	PORTB=0X00;
  Beep();    
  DisableInterrupts;
  printp("\t\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  printp("\t\nx  Time interval is    5000 ms delay!  x");
  printp("\t\nx  Press UP   to plus  500  ms delay!  x");
  printp("\t\nx  Press DOWN to minus 500  ms delay!  x");
  printp("\t\nx  Press ESC  to enter 5000 ms delay!  x");
  printp("\t\nx  Press OK   to enter 0    ms delay!  x");
  printp("\t\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 
	SD_Init();            //初始化spi口
	PORTB=0Xff;
INITSD: 			
	if(SD_Reset()==0)       //初始化SD卡
	{ 	  
	  PORTB_BIT1=0;       //绿灯亮
    PORTB_BIT0=1;    
	  printp("\nSD card init is ok! \nStartint to writing...\n");
	}
	else
	{
	  PORTB_BIT1=1;
    PORTB_BIT0=0;       //红灯亮
	  printp("\nSD card init failed!Please reinsert your card and press OK button!\n");
	  while((selKey=Scan_Key())!='o');
	  Dly_ms(500); 
	  goto INITSD; 
	}
	  
  //读取SD卡引导区数据
	if(!(Rd_Block(0x00, RXBUF))){
	  
	  PORTB_BIT1=0;       //读块成功绿灯亮
    PORTB_BIT0=1; 
	  printp("\nSD card reading is ok!\nBootload area is as fellow:\n");
	}
	else
	{
	  PORTB_BIT1=1;
    PORTB_BIT0=0;       //读取失败红灯亮
	  printp("\nSD card reading failed!\n"); 
	}
  View_Buf8(RXBUF);
  
  //写/清空SD卡0X1FF区的数据
  CLS_Buf8(RXBUF);       //清空接收缓冲器    
  if(!(Wt_Block(0x1FF,RXBUF))){
	  
	  PORTB_BIT1=0;       //写块成功绿灯亮
    PORTB_BIT0=1; 
	  printp("\nSD card writing is ok!\n");
	}
	else
	{
	  PORTB_BIT1=1;
    PORTB_BIT0=0;       //写块失败红灯亮
	  printp("\nSD card writing failed!\n"); 
	} 	
  
  Dly_ms(3000);
  
  RdWrAdd=0X200;
  while(1)
  {    
    selKey=Scan_Key();    
    if(selKey=='u')
    {
      dtime=((dtime>50)?50: ++dtime);
      printp("\nTime interval is %d ms!",dtime*500);
      Beep();
    }
    else if(selKey=='d')
    {
      dtime=((dtime>0)? --dtime:0);
      printp("\nTime interval is %d ms!",dtime*500);
      Beep(); 
    }
    else if(selKey=='c')
    {
      dtime=10;
      printp("\nTime interval is 5000 ms!");
      Beep(); 
    }
    else if(selKey=='o')
    {
      dtime=0;      
      Beep();
      Rd_Block(RdWrAdd,RXBUF);
      //printp("\n\n====================================================");
      printp("\nReading from address: 0x%04x,Block size is:512 bytes",RdWrAdd++);
      //printp("\n====================================================");
      if(RXBUF[2]) View_Buf8(RXBUF);
    }
    else
    { 
      CLS_Buf((U8*)tmch,64);
      sprintf(tmch,"\nNo %05d--voltage:%03dV,IOC:%03d,DTY:%03dV,PER:%03dV  2008-11-18          ",cnt,ReadATD(0),PACN0,PWMDTY1,PWMPER1);
      printp("\n%s\n",tmch);
      if(!WriteFile((U8*)tmch,64))
      {	  
    	  PORTB_BIT1=~PORTB_BIT1;       //读块成功绿灯亮
        PORTB_BIT0=1; 
    	  printp("\nWriting file is successful!\n");
    	  cnt++;
    	}
    	else
    	{
    	  PORTB_BIT1=1;
        PORTB_BIT0=~PORTB_BIT0;       //度快失败红灯亮
    	  printp("\nWriting file failed!\n"); 
    	}   
    }
    printp("\nTime interval is 0 ms!");    
    Dly_ms(dtime*500);
  }
return;
}//end




⌨️ 快捷键说明

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