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

📄 allserial.bak

📁 此源码是用的NEC公司的MCU78F0396
💻 BAK
字号:

/******************************************************
//  Filename :	AllSerial.c
//  Abstract :	This file implements main function.
//
//  Device    :	uPD78F0396
//  CreateTime: 2006/11/01
//  Author    :  

******************************************************/
#include "UsrComm.h"
#include "AllSerial.h"

#define IIC_WAIT   100

void IIC0_Init( void )
{
	
	ClrIORBit(IICC0, 0x80); // stop IIC0 Transfer
	ClrIORBit(PM6,0x03); // port setting 
	// remove setting of P6 for 78F0397 (78K0/LG2)
	// ClrIORBit(P6, 0x03);
	SetIORBit(IICF0, 0x02); // start-condition doesn't need stop-condition 
	SetIORBit(IICF0, 0x01); // comunication reserve - disable 
	SetIORBit(IICC0, 0x10); // stop-condition interrupt - enable 
	ClrIORBit(IICC0, 0x08); // interrupt control - 8 clock falling edge 
	// transfer clock  fprs/88 
	ClrIORBit(IICCL0, 0x08); // normal mode 
	ClrIORBit(IICCL0, 0x3); // CL00 = 0 CL01 = 0  
	ClrIORBit(IICX0, 0x1); // disable extension  
	// selection interrupt priority  
	//SetIORBit(PR1H, 0x1); /* interrupt priority low */
	//ClrIORBit(MK1H, 0x1); /* enable interrupt */
	
 
}

//=============================================================================

void IIC0_StopTransfer( void )
{
	ClrIORBit(IICC0, 0x80); /* stop transfer */
	return;
}

//=============================================================================

void IIC0_Start(void)
{
	SetIORBit(IICC0, 0x02); /* generate start condition */
	return;
}

//=============================================================================

void IIC0_Stop(void)
{
	SetIORBit(IICC0, 0x02); /* generate stop condition */
}
//=============================================================================

MD_STATUS IIC0_MasterSendData(unsigned char *pIICTxbuf, unsigned int wIICTxLength)
{
	unsigned int i;
    unsigned int wWait;
	for( i = 0; i < wIICTxLength; i++) 
	{
		IIC0 = *pIICTxbuf++ ; // start transfer 
		
        wWait = IIC_WAIT; 
		do {
			wWait--;
		}
		while( (IICS0 & 0x40) && (wWait > 0) ); //check ACK
		if(wWait == 0)
			return MD_NACK;
    } 		
    return MD_OK;
}


//=============================================================================

MD_STATUS IIC0_MasterStartAndSend(unsigned char ucSAdr, unsigned char*pIICTxBuf, unsigned int wIICTxLength)
{
	MD_STATUS  status;
	unsigned int i;
	unsigned int wWait;
	//start IIC0  
	//SetIORBit(IICC0, 0x18); //SPIE0 = WTIM0 = 1 
	SetIORBit(IICC0, 0x80); // IICE0 = 1  
	SetIORBit(IICC0, 0x02); // generate start condition  
	
	IIC0 = ucSAdr; // start transfer
	
	wWait = IIC_WAIT; 
	do {
		wWait--;
	}
	while( (IICS0 & 0x40) && (wWait > 0) );
	if(wWait == 0)
		return MD_NACK;
	
	status = IIC0_MasterSendData(pIICTxBuf,wIICTxLength );				
	
	return MD_OK; // no error 
}




MD_STATUS IIC0_MasterReceiveData(unsigned char ucSAdr,unsigned char * pIICRxBuf,unsigned int wIICRxLength)
{
    
	unsigned int i;
	unsigned int wWait;
	SetIORBit(IICC0, 0x80); // IICE0 = 1  
	SetIORBit(IICC0, 0x02); // generate start condition 
    SetIORBit(IICC0, 0x20); // start receive (write WREL0 = 1 OR IIC0 = 0xff for send 9 clock )
	
	IIC0_MasterSendData(&ucSAdr,1);
	
	for (i = 0; i < wIICRxLength; i++)
	{  
		wWait = IIC_WAIT;
		do { 
			wWait--;
		} while( !((IICS0 | 0xF7) == 0xF7)); //  is receive status
		*pIICRxBuf++ = IIC0;
	}
	
	
	ClrIORBit(IICC0, 0x04); // ACK STOP 
    SetIORBit(IICC0, 0x01); // generate stop condition and the IIC bus release;
    return 0;
}















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

⌨️ 快捷键说明

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