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

📄 usb_main.c

📁 REVISIONS: 11/22/02 - DM: Added support for switches and sample USB
💻 C
📖 第 1 页 / 共 2 页
字号:
		CKCON |=  0x08;	}
	else if (SYSCLK_EXT/BAUDRATE/2/256 < 4)
	{		TH1 = -(SYSCLK_EXT/BAUDRATE/2/4);		CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01		CKCON |=  0x09;	}
	else if (SYSCLK_EXT/BAUDRATE/2/256 < 12)
	{		TH1 = -(SYSCLK_EXT/BAUDRATE/2/12);		CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00	}
	else 
	{		TH1 = -(SYSCLK_EXT/BAUDRATE/2/48);		CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10		CKCON |=  0x02;	}
	TI0 = 0;	// Indicate TX0 ready
	ES0 = 1;  }

//------------------------------------------------------------------------------------
// Timer1_Init()
//------------------------------------------------------------------------------------
//
// Timer1 configured as the UART0 clock source as follows:
// - Timer1 in 8-bit auto-reload mode
//
void Timer1_Init (void)
{
	TL1		= TH1;		// init Timer1	TMOD	&= ~0xf0;	// TMOD: timer 1 in 8-bit autoreload	TMOD	|= 0x20;
	TR1		= 1;		// START Timer1
}

/*

//-----------------------------------------------------------------------------// SPI0_Init//-----------------------------------------------------------------------------// Configure the SPI0 .//void SPI0_Init (void){	SPI0CFG	= 0xEF;
	SPI0CKR	= 0xF9;			// 48K;
	SPI0CN	= 0x00;
	SPIEN	= 1;
}

//------------------------------------------------------------------------------------
// Timer0_Init()
//------------------------------------------------------------------------------------
//
// Timer0 configured as the SMBus clock source as follows:
// - Timer0 in 8-bit auto-reload mode
// - SYSCLK / 12 as Timer1 clock source
// - Timer0 overflow rate => 3 * SMB_FREQUENCY
// - The resulting SCL clock rate will be ~1/3 the Timer1 overflow rate
// - Timer0 enabled
//
void Timer0_Init (void)
{
   CKCON &= ~0x07;                           // Timer1 clock source = SYSCLK / 12
   TMOD = 0x02;                              // Timer1 in 8-bit auto-reload mode

   TH0 = -(SYSCLK/SMB_FREQUENCY/12/3);       // Timer1 configured to overflow at 1/3
                                             // the rate defined by SMB_FREQUENCY

   TL0 = -(SYSCLK/SMB_FREQUENCY/12/3);       // Timer1 preloaded to overflow at 1/3
                                             // the rate defined by SMB_FREQUENCY

   TR0 = 1;                                  // Timer1 enabled
}

*/

//------------------------------------------------------------------------------------
// Timer3_Init()
//------------------------------------------------------------------------------------
//
// Timer3 configured for use by the SMBus low timeout detect feature as follows:
// - Timer3 in 16-bit auto-reload mode
// - SYSCLK/12 as Timer3 clock source
// - Timer3 reload registers loaded for a 25ms overflow period
// - Timer3 pre-loaded to overflow after 25ms
// - Timer3 enabled
//
void Timer3_Init (void)
{
	TMR3CN = 0x00;			// Timer3 configured for 16-bit auto-reload, low-byte interrupt disabled
	TMR3 = -(SYSCLK/12/20);	// Timer3 configured to overflow after
	TMR3RL = -(SYSCLK/12/20);	// ~50ms (for SMBus low timeout detect)

	CKCON &= ~0x40;		// Timer3 uses SYSCLK/12
//	TMR3CN |= 0x04;		// Start Timer3
}

/*

//-----------------------------------------------------------------------------// IIC_Init//-----------------------------------------------------------------------------// Configure the IIc .//
void SMBus_Init(void)
{
	SMB0CF = 0x5C;	// Use Timer1 overflows as SMBus clock source;
					// Disable slave mode; Enable setup & hold time extensions;
					// Enable SMBus Free timeout detect; Enable SCL low timeout detect;
	SMB0CF |= 0x80;	// Enable SMBus;
}

*/

/*

//-------------------------
//Read_25aa_status
//--------------------------
//read 25aaxxx status
//
BYTE Read_25aa_status(void)
{
	BYTE val;

	CS_25AA	= 0;
	Delay();
	SPI0DAT = RDSR_25AA;
	SPIEN = 0;
	SPI0CFG	=0xCF;
	SPIEN = 0;
	SPIEN = 1;
	SPI0DAT = 0xFF;
	Delay();
	CS_25AA	=1;
	SPIEN = 0;
	SPI0CFG	= 0xEF;
	SPIEN = 1;
	val	= SPI0DAT;
	return val;
}

//--------------------------------
//Read_25aa_data
//----------------------------------
//Read data from memory array beginning at selected address
BYTE Read_25aa_data(BYTE address)
{
	BYTE val;

	CS_25AA	= 0;
	Delay();
	SPI0DAT = READ_25AA;
	while(!SPIF)
	{}
	SPIF=0;
	SPI0DAT = address;
	while(!SPIF)
	{}
	SPIF=0;
  	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	Delay();
	SPIEN = 0;
	SPI0CFG	=0xCF;
	SPIEN = 1;
	SPI0DAT = 0x00;
	while(!SPIF)
	{}
	SPIF=0;
	CS_25AA	=1;
	SPIEN = 0;
	SPI0CFG	= 0xEF;
	SPIEN = 1;
	val	= SPI0DAT;
	return val;
}

//-------------------------
// Write_25aa_enable
//-------------------------
//set the write enagle latch(enable write operations)
void Write_25aa_enable(void)
{
	CS_25AA	= 0;
	Delay();
	SPI0DAT = WREN_25AA;
	Delay();
	while(!SPIF)
	{}
	SPIF=0;
	CS_25AA	=1;
}

//-------------------------
// Write_25aa_disable
//-------------------------
//Reset the write enable latch(disable write operation)
void Write_25aa_disable(void)
{
	CS_25AA	= 0;
	Delay();
	SPI0DAT = WREN_25AA;
	Delay();
	CS_25AA	=1;
}

//------------------------
//Write_25aa_data
//---------------------------
//Write data to memory array beginning at selected address
void Write_25aa_data(BYTE address,BYTE wdata)
{
	CS_25AA	= 0;
	Delay();
	SPI0DAT = WRITE_25AA;
	while(!SPIF)
	{}
	SPIF=0;
	SPI0DAT = address;
	while(!SPIF)
	{}
	SPIF=0;
	SPI0DAT = wdata;
	Delay();
	while(!SPIF)
	{}
	SPIF=0;
	CS_25AA	=1;
}

//-------------------
//Write_25aa_status
//---------------------
//Write status registet
void Write_25aa_status(BYTE wdata)
{
	CS_25AA	= 0;
	Delay();
	SPI0DAT = WRSR_25AA;
	SPI0DAT = wdata;
	Delay();
	CS_25AA	=1;
}

*/

/*

//------------------------------------------------------------------------------------
// EEPROM_Write ()
//------------------------------------------------------------------------------------
//
// This function writes the value in <dat> to location <addr> in the EEPROM then polls
// the EEPROM until the write is complete.
//
void EEPROM_ByteWrite( unsigned char addr, unsigned char dat )
{
	while (SMB_BUSY);	// Wait for SMBus to be free.
	SMB_BUSY = 1;		// Claim SMBus (set to busy)

// Set SMBus ISR parameters
	TARGET = EEPROM_ADDR;		// Set target slave address
	SMB_RW = WRITE_24LC;		// Mark next transfer as a write
	SMB_SENDWORDADDR = 1;		// Send Word Address after Slave Address
	SMB_RANDOMREAD = 0;			// Do not send a START signal after the word address
	SMB_ACKPOLL = 1;			// Enable Acknowledge Polling (The ISR will automatically restart the transfer
								// if the slave does not acknoledge its address.
// Specify the Outgoing Data
	WORD_ADDR = addr;			// Set the target address in the EEPROM's internal memory space
	SMB_SINGLEBYTE_OUT = dat;	// store dat (local variable) in a global variable so the ISR can read it after this function exits
	pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT;	// The outgoing data pointer points to the <dat> variable.
	SMB_DATA_LEN = 1;			// Specify to ISR that the next transfer will contain one data byte
// Initiate SMBus Transfer
	STA = 1;
}

//------------------------------------------------------------------------------------
// EEPROM_WriteArray ()
//------------------------------------------------------------------------------------
// Writes <len> data bytes to the EEPROM slave specified by the <EEPROM_ADDR> constant.
//
void EEPROM_WriteArray (unsigned char dest_addr, unsigned char src_addr,
                        unsigned char len)
{
	unsigned char i;
	unsigned char* pData = (unsigned char*) src_addr;

	for( i = 0; i < len; i++ )
	{
		EEPROM_ByteWrite(dest_addr++, *pData++);
	}
}

//------------------------------------------------------------------------------------
// EEPROM_ByteRead ()
//------------------------------------------------------------------------------------
//
// This function reads a single byte from location <addr> in the EEPROM then polls
// the <SMB_BUSY> flag until the read is complete.
//
unsigned char EEPROM_ByteRead( unsigned char addr)
{
	unsigned char retval;	// Holds the return value

	while (SMB_BUSY);		// Wait for SMBus to be free.
	SMB_BUSY = 1;			// Claim SMBus (set to busy)

// Set SMBus ISR parameters
	TARGET = EEPROM_ADDR;	// Set target slave address
	SMB_RW = WRITE_24LC;	// A random read starts as a write then changes to a read after
							// the repeated start is sent. The ISR handles this switchover if
							// the <SMB_RANDOMREAD> bit is set.
	SMB_SENDWORDADDR = 1;	// Send Word Address after Slave Address
	SMB_RANDOMREAD = 1;		// Send a START after the word address
	SMB_ACKPOLL = 1;		// Enable Acknowledge Polling

// Specify the Incoming Data
	WORD_ADDR = addr;		// Set the target address in the EEPROM's internal memory space
	pSMB_DATA_IN = &retval;	// The incoming data pointer points to the <retval> variable.
	SMB_DATA_LEN = 1;		// Specify to ISR that the next transfer will contain one data byte

// Initiate SMBus Transfer
	STA = 1;
	while(SMB_BUSY);		// Wait until data is read
	return retval;
}

//------------------------------------------------------------------------------------
// EEPROM_ReadArray ()
//------------------------------------------------------------------------------------
// Reads up to 256 data bytes from the EEPROM slave specified by the <EEPROM_ADDR> constant.
//
void EEPROM_ReadArray (unsigned char dest_addr, unsigned char src_addr,
                       unsigned char len)
{
	while (SMB_BUSY);	// Wait for SMBus to be free.
	SMB_BUSY = 1;		// Claim SMBus (set to busy)

// Set SMBus ISR parameters
	TARGET = EEPROM_ADDR;	// Set target slave address
	SMB_RW = WRITE_24LC;	// A random read starts as a write then changes to a read after
							// the repeated start is sent. The ISR handles this switchover if
							// the <SMB_RANDOMREAD> bit is set.
	SMB_SENDWORDADDR = 1;	// Send Word Address after Slave Address
	SMB_RANDOMREAD = 1;		// Send a START after the word address
	SMB_ACKPOLL = 1;		// Enable Acknowledge Polling

// Specify the Incoming Data
	WORD_ADDR = src_addr;	// Set the target address in the EEPROM's internal memory space
	pSMB_DATA_IN = (unsigned char*) dest_addr;	// Set the the incoming data pointer
	SMB_DATA_LEN = len;		// Specify to ISR that the next transfer will contain one data byte

// Initiate SMBus Transfer
	STA = 1;
	while(SMB_BUSY);	// Wait until data is read
}


*/

//-------------------------
// Delay
//-------------------------
// Used for a small pause, approximately 80 us in Full Speed,
// and 1 ms when clock is configured for Low Speed
//
void Delay(void)
{
   int x;
   for(x = 0;x < 5000;x)
       x++;
}

⌨️ 快捷键说明

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