Main.c
资源名称:51-SD.rar [点击查看]
上传用户:jcsy2001
上传日期:2013-11-29
资源大小:201k
文件大小:3k
源码类别:
嵌入式/单片机编程
开发平台:
C/C++
- #include "common.H"
- #include "DEVICE.H"
- #include "HAL.H"
- #include "HPI.H"
- #include "FAT.H"
- #include "FAT32.H"
- #include "HPI32.H"
- FLAGS bdata bFlags;
- extern unsigned char xdata DBUF[BUFFER_LENGTH];
- /////////////////////////////////////////////////
- extern unsigned long xdata DirStartCluster,DirStartCluster32;
- extern unsigned char xdata UARTBUF[UARTBUF_LENGTH];
- extern Command_Def xdata Command;
- extern SYS_INFO_BLOCK xdata DeviceInfo;
- extern FILE_INFO xdata ThisFile;
- //////////////////////////////////////////
- void Init_Port()
- {
- P0 = 0xFF;
- P1 = 0xFF;
- P2 = 0xFF;
- P3 = 0xFF;
- }
- /*Serial Port */
- /*Mode = 1 /8-bit UART
- Serial Port Interrupt = Disabled */
- /*Receive = Enabled */
- /*Auto Addressing = Disabled */
- void Init_COMM(void)
- {
- SCON = 0x53;
- PCON = 0x80 | PCON;
- T2CON=0x30;
- RCAP2H=0xFF; // 57600 @ 21.1184MHz: 24000000/(32*(65536-(RCAP2H,RCAP2L)))
- RCAP2L=0xF4;
- TI=0;
- RI=0;
- TR2=1;
- ES = 1;
- }
- void main(void)
- {
- unsigned char temp;
- unsigned long xdata time_count;
- Init_Port();
- Init_COMM();
- bFlags.bits.bUartInDone=0;
- DirStartCluster=0;
- DirStartCluster32=0;
- for(temp=0;temp<64;temp++)
- DBUF[temp]=0;
- ENABLE_INTERRUPTS;
- while(TRUE)
- {
- time_count++;
- if (time_count>1000) // 过一段时间查询一下SD/MMC卡是否在线 www.mcusky.com
- {
- DISABLE_INTERRUPTS;
- time_count = 0;
- ENABLE_INTERRUPTS;
- if(SdInit()) //初始化SD卡
- {
- if(bFlags.bits.SLAVE_ONLINE == FALSE)
- {bFlags.bits.SLAVE_FOUND=TRUE;
- bFlags.bits.SLAVE_ONLINE =TRUE;
- }
- }
- else
- {
- if(bFlags.bits.SLAVE_ONLINE ==TRUE)
- {bFlags.bits.SLAVE_REMOVED=TRUE;
- bFlags.bits.SLAVE_ONLINE =FALSE;
- }
- }
- }
- if(bFlags.bits.SLAVE_FOUND){
- DISABLE_INTERRUPTS;
- bFlags.bits.SLAVE_FOUND=FALSE;
- bFlags.bits.SLAVE_ENUMERATED = TRUE;
- DirStartCluster=0;
- DirStartCluster32=0;
- ThisFile.FatSectorPointer=0;
- DeviceInfo.LastFreeCluster=0;
- MCU_LED1=0;
- bFlags.bits.bMassDevice=TRUE;
- ENABLE_INTERRUPTS;
- }
- if(bFlags.bits.SLAVE_REMOVED)
- {
- DISABLE_INTERRUPTS;
- bFlags.bits.SLAVE_REMOVED=FALSE;
- bFlags.bits.SLAVE_ENUMERATED = FALSE;
- bFlags.bits.SLAVE_IS_ATTACHED = FALSE;
- MCU_LED1=1;MCU_LED2=1;
- ENABLE_INTERRUPTS;
- }
- if(bFlags.bits.bUartInDone){
- DISABLE_INTERRUPTS;
- bFlags.bits.bUartInDone=0;
- if(DeviceInfo.FAT) //FAT32
- UartHandler32();
- else
- UartHandler(); //FAT16
- ENABLE_INTERRUPTS;
- }
- if(bFlags.bits.bMassDevice)
- {
- DISABLE_INTERRUPTS;
- bFlags.bits.bMassDevice=FALSE;
- if(InitFileSystem())
- {
- bFlags.bits.SLAVE_IS_ATTACHED = TRUE;
- MCU_LED2=0;
- }
- else
- {
- bFlags.bits.SLAVE_IS_ATTACHED = FALSE;
- }
- ENABLE_INTERRUPTS;
- }
- }
- }