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

📄 an1023_mwire.c

📁 To use the MSSP port to communicate with 3-wire devices, the bytes to be output must be aligned
💻 C
字号:
#include <p18f4520.h>
#include "AN1023.h"

/********************************************************************
*     Function Name:    x16ByteWrite                                *
*     Parameters:       EE memory control, address and data         *
*     Description:      Writes Data Byte to Mwire EE memory device  *
*												Can be used with any Microwire in X16 mode  *
*												HiByte and LoByte will need to be modified  *
*  											for alignment of Op Code  									*
********************************************************************/
unsigned char x16ByteWrite(unsigned char HiByte, unsigned char LoByte, unsigned char HiData, unsigned char LoData )
{
  CS = 1;                             	// Select Device
  WriteMwire ( HiByte );              	// Send HiByte (Dummy bits, SB, and Op Code)
  WriteMwire ( LoByte );              	// Send LoByte (Address for the 93LC66C)	
  WriteMwire ( HiData );              	// Send HiData to EEPROM
  WriteMwire ( LoData );              	// Send LoData to EEPROM
  CS = 0;                             	// Deselect Device and initiate write
	CS = 1;																// Select Device
	while (!DataRdyMwire());            	// Wait for Write to complete
	CS = 0;																// Deselect device
  return ( 0 );
}

/********************************************************************
*     Function Name:    x16ByteRead                                 *
*     Parameters:       EE memory control, address, pointer and     *
*                       length bytes.                               *
*     Description:      Reads two data bytes from the Microwire     *
*                       device. Can be used for any Microwire in    *
*                       x16 mode. Hibyte and Lobyte will need to    *
*												modified for alignment of Op Code						*
********************************************************************/  
unsigned char x16ByteRead(unsigned char HiByte, unsigned char LoByte, unsigned char *ReadData, unsigned char Length )
{
  CS = 1;                               // Select Device
  WriteMwire ( HiByte );                // Send Read OpCode 
  WriteMwire ( LoByte );                // WRITE word address to EEPROM
  getsMwire ( ReadData, Length);				// Reads two bytes for x16 word
  CS = 0;                               // Deselect Device
  return ( 0 );                                 
}

/********************************************************************
*     Function Name:    EWEN                                        *
*     Return Value:     void                                        *
*     Parameters:       void                                        *
*     Description:      This routine sets the Write Enable Latch    *  
********************************************************************/

void EWEN (void)
{
    CS = 1;                             //Select Device
    WriteMwire ( 0x04 );              	//EWEN Command
    WriteMwire ( 0xC0 );              	//for 4K Microwire
    CS = 0;                           	//Deselect Device
}

/********************************************************************
*     Function Name:    WriteMwire                                  *
*     Return Value:     Status byte for WCOL detection.             *
*     Parameters:       Single data byte for SPI bus.               *
*     Description:      This routine writes a single byte to the    * 
*                       Mwire bus.                                    *
********************************************************************/
unsigned char WriteMwire( unsigned char data_out )
{
  SSPBUF = data_out;                    // write byte to SSPBUF register
  if ( SSPCON1 & 0x80 )        					// test if write collision occurred
   return ( -1 );              					// if WCOL bit is set return negative #
  else
  {
    while( !SSPSTATbits.BF );  					// wait until bus cycle complete 
  }
  return ( 0 );                					// if WCOL bit is not set return non-negative#
}

/********************************************************************
*     Function Name:    getsMwire                                   *
*     Return Value:     void                                        *
*     Parameters:       address of read string storage location and *
*                       length of string bytes to read              *
*     Description:      This routine reads a string from the        *
*                       Microwire device. User must first issue     *
*                       start bit, opcode and address before reading*
*                       a string. 																	*
********************************************************************/
void getsMwire( unsigned char *rdptr, unsigned char length )
{
  while ( length )                			// stay in loop until length = 0
  {
    SSPBUF = 0x00;                			// initiate bus cycle
    while( !SSPSTATbits.BF );     			// wait until byte has been received
    *rdptr++ = SSPBUF;            			// save byte
    length--;                     			// reduce string byte count by 1
  }  
}

/********************************************************************
*     Function Name:    DataRdyMwire                                *
*     Return Value:     status byte to indicate ready/busy          *
*     Parameters:       void                                        *
*     Description:      Determine if Microwire device is ready,     *
*                       write cycle complete.                       *
********************************************************************/
unsigned char DataRdyMwire( void )
{
  if ( PORTCbits.RC4 )            			// test if DI line is high    
    return ( 1 );                			  // return ready = true
  else
    return ( 0 );                 			// else ready = false
}

/********************************************************************
*     Function Name:    EWDS                                        *
*     Return Value:     void                                        *
*     Parameters:       void                                        *
*     Description:      This routine sets the Write Enable Latch    *  
********************************************************************/

void EWDS (void)
{
    CS = 1;                           	//Select Device
    WriteMwire ( 0x04 );              	//EWDS Command
    WriteMwire ( 0x00 );              	//for 4K Microwire
    CS = 0;                           	//Deselect Device
}

⌨️ 快捷键说明

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