main.c

来自「增强型51单片机fs7821sd卡、mmc卡读写程序」· C语言 代码 · 共 134 行

C
134
字号
/*
**********************************************************************************************
* Project:	TK7821
* File:		main.c
* Contents: 
*           The main and interrupt handler
*
* $Date: 02/25/05    Jason    v0.1
*        05/12/05    Mingo    v0.2   www.fameg.com
*
* Copyright (c) 2005 Fameg, Inc. All rights reserved
***********************************************************************************************
*/

#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
#include "Device.h"
#include "UsbDisk.h"
#include "Storage.h"

static bit SdGetFlashInfoFlag = false;
static bit SuspendFlag = false;
bit ResetFlag = false;
xdata UINT16 DelayTimer = 0;
xdata UINT32 HwVerNum;

//----------------------------------------------------------------------------
// Timeout every 10 ms
//----------------------------------------------------------------------------
void Timer0() interrupt 1 using 3
{
  // Restore the counter to 10 ms
  TH0 = 0xce;
  TL0 = 0xe8;
  // Inc the system tick
  if (DelayTimer)
    DelayTimer --;

  if (CmdTimer)
    CmdTimer --;

  if (SdGetFlashInfoTimer)
  {
    SdGetFlashInfoTimer --;
    if (SdGetFlashInfoTimer == 0)
      SdGetFlashInfoFlag = true;
  }
}

//----------------------------------------------------------------------------
// Enter the suspend mode
//----------------------------------------------------------------------------
void EnterSuspend()
{
  BYTE RegValue = CSRRead(USB_BASE + GCTL); //0xF000

  StorageSuspend();
  POWER_LED_CTRL = 1;
  RegValue |= SUSPEND;  //SUSPEND==0x04
  CSRWrite(USB_BASE + GCTL, RegValue);
  while (CSRRead(USB_BASE + GCTL) & SUSPEND);;
  // Ignore unstable clock stage for 100ms
  Delay(100);
  POWER_LED_CTRL = 0;
  StorageResume();
}

//----------------------------------------------------------------------------
void main()
{
  TH0 = 0xce;
  TL0 = 0xe8;
  TMOD = 0x21;
  TCON = 0x50;
  IP = 0x02;
  IE = 0x82;
  NormalSpeed();
  CSRWrite(USB_BASE + GCTL, CSRRead(USB_BASE + GCTL) | RESET);
  POWER_LED_CTRL = 0;

  HwVerNum = ((UINT32)HwId() << 16) | HwVersion();

  StorageInit();
  UsbInit();

  while (true)
  {
    BYTE Event;

    Event = CSRRead(USB_BASE + EPIE); //0xF001
    if (Event)
    {
      if (Event & (IRQ_RX0 | IRQ_TX0 | IRQ_IN0))
        UsbCtrlIntHandler(Event);   

      if (Event & (IRQ_TX1 | IRQ_RX2))
      {
        UsbBulkIntHandler(Event);
      }
    }

    Event = CSRRead(USB_BASE + USBIE) & (IRQ_USB_RST | IRQ_IDLE_3MS);//0xF005
    if (Event)
    {
      CSRWrite(USB_BASE + USBIE, ~Event);

      if (Event & IRQ_USB_RST)
      {
        CSRWrite(USB_BASE + USBADDR, 0); //0xF007
      }

      if (Event & IRQ_IDLE_3MS)
      {
          EnterSuspend();
      }
    }

    Event = CSRRead(SD_BASE + SD_IE) & SD_CARD_CHANGE;
    if (Event)
    {
      CSRWrite(SD_BASE + SD_IE, ~Event);
      SdIntHandler(Event);
    }
    if (SdGetFlashInfoFlag)
    {
      SdMediaChange();
      SdGetFlashInfoFlag = false;
    }

  }
}

⌨️ 快捷键说明

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