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

📄 usb._c

📁 diy硬盘mp3的程序
💻 _C
字号:
#include <iom128v.h>
#include "usb.h"
#include "ata.h"
#include "generic.h"
#include <macros.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

//******************************************************************
//*	Init USB Serial Port 
//*   
//*   
//******************************************************************
void InitUSB(void)
{
UCSR0C = 0x86;  // Asyn,NoParity,1StopBit,8Bit,
UBRR0L = 16;    // 8->115200 at 16Mhz // 16->57600 at 16Mhz // 25->38400bps at 16Mhz
UCSR0B = 0x98;  // Rx enable Tx Enable RxInt Enable
}

/*********************************************************/
#pragma interrupt_handler USBRxChar:19
void USBRxChar(void)
{
extern volatile unsigned char IsPlaying;
static unsigned char InChar;
static unsigned char USBCommand[15];
unsigned long Sector;
unsigned char i;

if (IsPlaying == TRUE) return;

InChar = UDR0;

for (i=1;i<12;i++) USBCommand[i-1] = USBCommand[i]; // Shift buffer
USBCommand[11] = InChar;

if (strncmp(&USBCommand[0],"+++R",4) == 0)
   {
   USBCommand[12] = 0x00;
   Sector = atol(&USBCommand[4]);
   USBReadSector(Sector);
   }
   
if (strncmp(&USBCommand[0],"+++W",4) == 0)
   {
   USBCommand[12] = 0x00;
   Sector = atol(&USBCommand[4]);
   USBWriteSector(Sector);
   }   
}

/*********************************************************/

void USBReadSector(unsigned long Sector)
{
unsigned int i;
extern unsigned char SectorBuffer[512];

ata_read_sector_byte(0, Sector, 0, 512, (unsigned char*)&SectorBuffer);
for (i=0;i<512;i++) USBTxChar(SectorBuffer[i]);
}

/*********************************************************/

void USBWriteSector(unsigned long Sector)
{
unsigned int i;
unsigned long TimeOut;
extern unsigned char SectorBuffer[512];

UCSR0B = 0x18;  // Rx enable Tx Enable RxInt Disable
for (i=0;i<512;i++) 
	{
	TimeOut = 0;
	while (USBIsChar() == 0) 
		  {
		  WDR();
		  TimeOut++;
		  if (TimeOut == 1000000)  // Wait 3 second for each char,
		  	 {
			 UCSR0B = 0x98;  // Rx enable Tx Enable RxInt Enable
			 return;
			 }
		  }
	SectorBuffer[i] = UDR0;
	}
ata_write_sector(0, Sector, &SectorBuffer[0]);
USBTxChar('!');
UCSR0B = 0x98;  // Rx enable Tx Enable RxInt Enable
}

/*********************************************************/

unsigned char USBIsChar(void)
{
if (UCSR0A & 0x80) return 1;
return 0;
}

/*********************************************************/

void USBTxChar(unsigned char ch)
{
while (!(UCSR0A & 0x20)) WDR();     // Wait for empty transmit buffer
UDR0 = ch;	  		   		  		// Write char
}

/*********************************************************/

⌨️ 快捷键说明

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