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

📄 twowire.c

📁 T112 MCU源程码,用于T112外接MCU篇程,用来驱动模拟屏
💻 C
字号:
//---------------------------------------------------------------------------
// Terawins Inc. Company Confidential Strictly Private
//
// $Archive: $
// $Revision: 1.9 $
// $Author: jwang $
// $Date: 2002/07/08 22:34:39 $
//
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Copyright 2002 (c) Terawins Inc.
// --------------------------------------------------------------------------
				
				#include "reg51.h"
				#include "common.h"
				#include "System.h"
				#include "TW10xReg.h"
				#include "TwoWire.h"


				#define TW_SLOW_SPEED
				//----------------------------------------
				// S_Condition for TWD protocol
				//----------------------------------------
				#if defined(TW_SLOW_SPEED)
				void StartCondition(void)
				{
				    uCHAR ix;
				    uCHAR cTWtrytime=0;
				    EA=0;
				    while(++cTWtrytime)
				    {
				        Set_SDA_High;
				        Set_SCL_High;        /* make sure two line is release */
				        for(ix = 0; ix < TWD_LONG_TIME; ix++)
				        { };  /* Delay 12us */
				
				        Set_SDA_Low;
				        for(ix = 0; ix < TWD_LONG_TIME; ix++)
				        { };  /* Delay 12us */
				
				        if((SCL_High)&&(SDA_Low))
				            break;
				    }
				
				    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
				    { };  /* Delay 12us */
				}
				
				//----------------------------------------
				// P_Condition for TWD protocol
				//----------------------------------------
				void StopCondition(void)
				{
				    uCHAR ix;
				    uCHAR cTWtrytime=0;
				
				    Set_SDA_Low;
				    Set_SCL_High;
				    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
				    { };  /* delay 12us */
				
				    while(SCL_Low && ++cTWtrytime)
				    { };
				
				    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
				    { };  /* delay 12us */
				
				    Set_SDA_High;
				    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
				    { };  /* delay 12us */
					EA=1;
				}
				
				//--------------------------------------------------
				// 	  Send_Byte
				//    Send a byte to master with a acknowledge bit
				//--------------------------------------------------
				uCHAR Send_Byte(uCHAR cData)
				{
				    uCHAR ix, j, cAcknowledge;
				    uCHAR cTWtrytime=0;
				
				    cAcknowledge = 0;
				
				    for(ix = 0; ix < 8; ix++)
				    {
				        Set_SCL_Low;
				        for(j = 0; j < TWD_SHORT_TIME; j++)
				        { };
				
				        if(cData&0x80)Set_SDA_High;
						else Set_SDA_Low;
						cData<<=1;
				
				        for(j = 0; j < TWD_SHORT_TIME; j++)
				        { };
				
				        Set_SCL_High;
				        while(SCL_Low && ++cTWtrytime)
				        { };
				
				        for(j = 0; j < TWD_SHORT_TIME; j++)
				        { };
				    }
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    Set_SCL_Low;
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    Set_SDA_High;                /* release data line for acknowledge */
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    Set_SCL_High;                /* Send a clock for Acknowledge */
				    while(SCL_Low)
				    { };
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    if(SDA_High) 
				       cAcknowledge = 1; /* No Acknowledge */
				    Set_SCL_Low;                   /* Finish Acknoledge */
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    return(cAcknowledge);
				}
				
				//--------------------------------------------------
				// Read_Byte
				//    Read a byte from master with a acknowledge bit
				//--------------------------------------------------
				uCHAR Read_Byte(uCHAR cNum)
				{
				    uCHAR ix, j;
				    uCHAR cRetval=0;
				
				    for(ix=0;ix<8;ix++){
				          Set_SCL_High;
				          while(SCL_Low){};
				          for(j=0;j<TWD_SHORT_TIME;j++){};
				          cRetval = (SDA_High)? (cRetval|(1<<(7-ix))):cRetval ;  /* MSB First */
						  Set_SCL_Low;
					  	  
				     }
				
				     if(cNum==1)
				     	Set_SDA_High;
				     else
						Set_SDA_Low;
				     //SDA = (cNum==1) 1:0;
				     Set_SCL_High;
				     while(SCL_Low){};
				     for(j = 0; j < TWD_SHORT_TIME; j++)
				     { };
				
				    Set_SCL_Low;
				
				    Set_SDA_High;
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    return cRetval;
				}
				
				#else
				// for fast Two Wire Bus Speed
				
				void StartCondition(void)
				{
				//    uCHAR ix;
				    uCHAR cTWtrytime=0;
				
				    while(++cTWtrytime)
				    {
				        Set_SDA_High;
				        Set_SCL_High;        /* make sure two line is release */
				
				        Set_SDA_Low;
				
				        if((SCL_High)&&(SDA_Low))
				            break;
				    }
				}
				
				//----------------------------------------
				// P_Condition for TWD protocol
				//----------------------------------------
				void StopCondition(void)
				{
				//    uCHAR ix;
				    uCHAR cTWtrytime=0;
				
				    Set_SDA_Low;
				    Set_SCL_High;
				
				    Set_SDA_High;
				}
				
				//--------------------------------------------------
				// Send_Byte
				//    Send a byte to master with a acknowledge bit
				//--------------------------------------------------
				uCHAR Send_Byte(uCHAR cData)
				{
				    uCHAR ix;
				    uCHAR cAcknowledge;
				    uCHAR cTWtrytime=0;
				
				    cAcknowledge = 0;
				
				    for(ix = 0; ix < 8; ix++)
				    {
				        Set_SCL_Low;
				        //        (cData&(1<<(7-ix)))?(Set_SDA_High):(Set_SDA_Low);  /* MSB First */
				        if(cData&0x80)Set_SDA_High;
						else Set_SDA_Low;
						cData<<=1;
				
				
				        Set_SCL_High;
				    }
				
				    Set_SCL_Low;
				    Set_SDA_High;                /* release data line for acknowledge */
				    Set_SCL_High;                /* Send a clock for Acknowledge */
				
				    if(SDA_High) cAcknowledge = 1; /* No Acknowledge */
				    Set_SCL_Low;                   /* Finish Acknoledge */
				
				    return(cAcknowledge);
				}							
				
				//--------------------------------------------------
				// Read_Byte
				//    Read a byte from master with a acknowledge bit
				//--------------------------------------------------
				uCHAR Read_Byte(void)
				{
				    uCHAR ix, j;
				    uCHAR cRetval;
				    uCHAR cTWtrytime=0;
				
				//    cRetval = 0;
				    Set_SDA_High;
				
				    for(ix = 0; ix < 8; ix++)
				    {
				        Set_SCL_Low;
				        for(j = 0; j < TWD_SHORT_TIME; j++)
				        { };
				
				        Set_SCL_High;
				        while(SCL_Low)
				        { };
				
				        for(j = 0; j < TWD_SHORT_TIME; j++)
				        { };
				
						cRetval<<=1;
						if(SDA_High) cRetval++;
					}
				
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    Set_SCL_Low;
				    for(j = 0; j < TWD_SHORT_TIME; j++)
				    { };
				
				    return cRetval;
				}
				
				
				#endif
				
				
				//--------------------------------------------------
				// Read Byte to Device
				//--------------------------------------------------
				uCHAR I2CReadByte(uCHAR cDevAddr, uCHAR cReg)
				{
				    uCHAR cResult = 0;
				
				
				    /* write reg offset */
				    StartCondition();
				    if(Send_Byte(cDevAddr))
						return 0;  // Write address
				    if(Send_Byte(cReg))
						return 0;
				
				    /* read data */
				    StartCondition();
				    if (Send_Byte(cDevAddr | 0x01)) // Read address
						return 0;
				    cResult = Read_Byte(1);
				
				    StopCondition();
				   		return cResult;
				}
				//---------------------------------------------------

				//--------------------------------------------------
				// Write Byte to Device
				//--------------------------------------------------
				
				uCHAR I2CWriteByte(uCHAR cDevAddr, uCHAR cReg, uCHAR cData)
				{
				
				
				    /* start condition */
				    StartCondition();
				  	cDevAddr &= 0xFE;   // Force WRITE address
				    /* send device ID and write data */
				    if(!Send_Byte(cDevAddr))
				    {
				        if(!Send_Byte(cReg))
				        {
				            if(!Send_Byte(cData))
				            {
				                StopCondition();
				                return(0);      /* success */
				            }
				        }
				    }
				    StopCondition();
				    return(1);
				}

				//--------------------------------------------------
				// 041901, Added for TWD Burst Write
				// 1. PT8655X_Wr_Burst_A    (Start + Slave_ID + Address)
				// 2. PT8655X_Wr_Burst_D    (Data)
				// 3. PT8655X_Wr_Burst_P    (stop)
				//--------------------------------------------------

				uCHAR twdWr_Burst_A(uCHAR cReg)
				{
				    StartCondition();
				    if(!Send_Byte(TW101))
				    {
				        if(!Send_Byte(cReg))
				        {
				            return(0);               //
				        }
				    }
				    return(1);                  //
				}

				void twdWr_Burst_D(uCHAR cData)
				{
				    uCHAR ix;
				//uCHAR cAcknowledge;
				//    uCHAR cTWtrytime=0;
				//    cAcknowledge = 0;
				
				    for(ix = 0; ix < 8; ix++)
				    {
				        Set_SCL2Low;
				        if(cData&0x80)Set_SDA2High;
						else Set_SDA2Low;
						cData<<=1;
				        Set_SCL2High;
				    }
				    Set_SCL2Low;
				    Set_SDA2High;                // release data line for acknowledge 
				    Set_SCL2High;                // Send a clock for Acknowledge 
				//    if(SDA_High) cAcknowledge = 1; // No Acknowledge 
				    Set_SCL2Low;                   // Finish Acknoledge 
				
				//    return(cAcknowledge);
				
				}
	//			#endif				
				void twdWr_Burst_P(void)
				{
				    StopCondition();
				}
	/*			
				//twdWriteTable() is used for burst write for
				//any I2C standar device
				#if VIDEO_AVAILABLE
				#ifdef VIDEO_SAA7114
				#define EOT	-1
				BOOL twdWriteTable(uCHAR cDevAddr, char *pString)
				{
					uCHAR  cReg=0;
				  	uCHAR  cNum=0;
				  	while(*pString != EOT)
				  	{
						cReg = *pString++;
						cNum = *pString++;
				    	if(!I2CWriteBytes(cDevAddr, cReg, cNum, pString))
							break;
						else
							pString += cNum;
					}
				}
				#endif
				#endif
	*/		
				//--------------------------------------------------
				// Read Multiple Bytes to Device
				//--------------------------------------------------
	/*			
				void twdDelay(uWORD wLoops)
				{
					uWORD wTemp;
				
				    while (wLoops--) {
					    wTemp = 1000/6;	// one loop below takes about 11 us
				        while (wTemp--);
				    }
				}
				
			*/	
				void twdDelay1(uWORD wLoops)
				{
					uWORD wTemp;
				
				    while (wLoops--) {
					    wTemp = 1000/6;	// one loop below takes about 11 us
				        while (wTemp--);
				    }					
				}
				
				// OSD config register Write
				void OSDCfgWr(uCHAR index,uCHAR dat)		///OSD_CFG_INDEX=0xa0;
				{
				 I2CWriteByte(TW101,OSD_CFG_INDEX,index);
				 I2CWriteByte(TW101,OSD_CFG_DATA,dat);
				}


⌨️ 快捷键说明

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