main.c

来自「M16C瑞萨平台」· C语言 代码 · 共 884 行 · 第 1/2 页

C
884
字号
	WriteOneByteRV8564(0x0F,0x80);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)	
	idle();idle();idle();idle();idle();idle();idle();idle();
/***********This segmetn is used for writing the RAM byte by byte ***************************************/  	



while (U2SMR.BIT.BBS == 1); 


U2TB.BYTE.U2TBL=0x00;


	 	
 while(1){
 i2c_start();
 
 idle();idle();idle();idle();
 i2c_stop();
 idle();
 if(sema_status(INT4_SEM) == SEMA_SET) {
      sema_unset(INT4_SEM);
	  
	  break;}	
 }
	
 
while(1){// Debugging Pause...		
	WriteOneByteRV8564(0x00,0x00);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)
	WriteOneByteRV8564(0x01,0x13);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)		
	WriteOneByteRV8564(0x0E,0x81);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)	
	WriteOneByteRV8564(0x0F,0x80);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)	
	idle();idle();idle();idle();idle();idle();idle();idle();	
}





	i2c_start();
	 int j;	
	for(j=0;j<18;j++)
	{
    U2TB.WORD= ( 0x0100 | RV_8564ram[j]);	
    I2C_WAIT_UNTIL_FINISHED();
	}	//end transmission
	
	

	i2c_disable_rx();
	i2c_stop(); 




	U2SMR4.BIT.ACKD=1;	
	U2SMR4.BIT.ACKC=1;


idle();	idle();	idle();	idle();	idle();	idle();	idle();	

	
/* ****************  read the value stored in the specified address   ********************************************/ 			
	i2c_start();
	  
	U2TB.WORD= ( 0x0100 | 0xa2);	
    I2C_WAIT_UNTIL_FINISHED();
	
	U2TB.WORD= ( 0x0100 | 0x09);	
    I2C_WAIT_UNTIL_FINISHED();
	
	i2c_start();
	
	U2TB.WORD= ( 0x0100 | 0xa3);	
    I2C_WAIT_UNTIL_FINISHED();
//***** at this point, the master becomes a Receiver, the slave becomes the transmitter****************************/ 	


    
//	U2TB.BYTE.U2TBL=0xa2; //slave address for RV 8564 RTC chip	
//U2TB.BYTE.U2TBH=0x01;	if(U2C1.BIT.RI_U2C1)	
//	U2TB.BYTE.U2TBL=RV_8564ram[i];           u2tb = ( 0x0100 | I2CLocalStatus.SlaveAddrMsgOne);

	i2c_stop();
	
	 Readback_8564ram[0] =	U2RB.BYTE.U2RBL;
/* ****************  read the value stored in the specified address   ********************************************/ 


	 
	if(Readback_8564ram[0]==0xFF)  TwinkleLedP27(); 
//	else TwinkleLedP26();
	
	
	
	
	// i=0;
//	for(i=0;i<16;i++)
//	{
//	Readback_8564ram[i] =	U2RB.BYTE.U2RBL;
//	while(U2C1.BIT.RI_U2C1);//	
//	TwinkleLedP27();
//	}
	 
	
	 

//***************************************************************************
/*	P4.BIT.P4_7 = HIGH;// enable clock output interrupt
 	
	
	i2c_start();
	i2c_enable_rx();
	U2TB.WORD= ( 0x0100 | 0xa2);
	I2C_WAIT_UNTIL_FINISHED();
	
	
	i2c_start();
	U2TB.WORD= ( 0x0100 | 0xa3);
	I2C_WAIT_UNTIL_FINISHED();
	
	
 	
	i=0;
	for(i=0;i<16;i++)
	{
	Readback_8564ram[i] =	U2RB.BYTE.U2RBL;
	while(U2C1.BIT.RI_U2C1);//	
	TwinkleLedP27();
	}
	
	i2c_disable_rx();
	i2c_stop(); 
 		
 	if(Readback_8564ram[1]==RV_8564ram[3]) 
	{for(;;)
	{TwinkleLedP27();}
	}
	
	
	
*/ 	
	
	while(U2C1.BIT.TI_U2C1);// wait for tx register to be loaded,with data !TI_U2C1 is 0
 
		   
	while(!U2C1.BIT.TI_U2C1);// wait for tx register to empty,with data !tept is 1, infinite loop

//    blink(2);
	
//	while(1){		
//		if(U2C1.BIT.TI_U2C1){
//		U2TB.BYTE.U2TBL=0xde;
//		toggle(1);}
//	};
//	
   
/* ********************************************************************/ 
   
  
#if MULLE_BOARD == MULLEv1
  PD8.BIT.PD8_0 = OUTPUT;		    /* setting port P80 as OUTPUT port*/		
  P8.BIT.P8_0  = LOW;			    /* set LED on port P80 ON initially*/
  
  sema_unset(INT1_SEM);			    /* unset semaphore initially */
  PUR2.BIT.PU20 = 1;
  INT1IC.BYTE = 0x03;			    /* enable interrupt1, level 3, rising edge */
  

	
#elif MULLE_BOARD == MULLEv3
  PD8.BIT.PD8_0 = OUTPUT;		    /* setting port P80 as OUTPUT port*/		
  P8.BIT.P8_0  = HIGH;			    /* set LED on port P80 ON initially*/
  
  sema_unset(INT4_SEM);			    /* unset semaphore initially */
  PD1.BIT.PD1_6 = INPUT;
  INT4IC.BYTE = 0x03;			    /* enable interrupt4, level 3, rising edge */
#endif
  /* main loop */

  for (;;) {

    /* check if semaphore status is set*/
#if MULLE_BOARD == MULLEv1
    if(sema_status(INT1_SEM) == SEMA_SET) {
      sema_unset(INT1_SEM);
#elif MULLE_BOARD == MULLEv3
    if(sema_status(INT4_SEM) == SEMA_SET) {
      sema_unset(INT4_SEM);
#endif
   

   
if(INT1IC.BIT.IR==1) {P9.BIT.P9_1 = HIGH;
    idle(); idle();	
	P9.BIT.P9_1 = LOW;}


	  
    }//end if
		

  }//end main FOR loop

}//end main


 
/*********    IIC bus master status initialization   *********************/
void iic_mr_ini(){
	
  asm("fclr i");
  	
  PD7.BIT.PD7_1 = INPUT;// SCL2 input port
  PD7.BIT.PD7_0 = INPUT;// SDA2 input port
  
  P7.BIT.P7_1 = LOW;// SCL2 input port
  P7.BIT.P7_0 = LOW;// SDA2 input port
  
  U2BRG=0xFF;
  // U2BRG=(((MAIN_CLOCK/(16.0 * SBIT_SPEED))+ 0.5) - 1.0);	// set after U2C0 CLKi is set.
  	

  U2MR.BYTE=0x02;// SMD2-SMD0“010b”I2C MODE,  CKDIR “0” master internal clock,IOPOL“0”  no conversion  
  U2C0.BYTE=0x90;//Uartform:msb,transmission/receiption at SCL falling/rising edge,SDA2 SCL2 n channel, f8  
  U2C1.BYTE=0x05;// //notice that initially TE,RE are set to 0, set 1 to start for the future usage. 
  
  U2SMR.BYTE=0x07;//IIC mode, BBS“0” STOP condition detected 
  U2SMR2.BYTE=0x00;// 0110 1110b
  U2SMR3.BYTE=0x00;// colck phase set bit set to 0, adjudsted according to the load on SCL2 and DSA2 pins
  //U2SMR4.BYTE=0x00;// 
  
  IFSR2A.BIT.IFSR26=1;// UART0 bus confliction detection
  IFSR2A.BIT.IFSR27=1;// UART1 bus confliction detection
  
  S2TIC.BYTE=4;	 
  S2RIC.BYTE=4;	 //Set IIC_IPL=4; 
  BCNIC.BYTE=4;// 
  
  
  //U2RB.BIT.ACK_NACK=1;//ACK or NACK is set in this bit
  
  // UCON.BYTE=0x00;//reset to 0. 
  asm("fset i"); 
  
}


 

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

void toggle(b)
{
#if MULLE_BOARD == MULLEv1
	int i;
	for(i=0;i<2*b;i++)
	{
      if(toggle_led == 0) {
	  P8.BIT.P8_0 = HIGH;		/* set LED on port P80 off */
	  toggle_led = 1;
                          }//end  if
	  else {
	  P8.BIT.P8_0 = LOW;		/* set LED on port P80 on */
	  toggle_led = 0;
            }//end else
			
	idle();
	}// end for

#endif
}//end toggle


void blink(l)
{
	int i;
	for(i=0;i<l;i++)
	{
	
	P9.BIT.P9_1 = HIGH;
    idle(); 
	P9.BIT.P9_1 = LOW;
	idle();
	}// end for
}//end blink	
	
/***************************************************************************************/


void idle()
{
	volatile long int i;
	for(i=0;i<10000;i++)
	{asm("NOP ");}	
}

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

//I2C-bus can be transferred at rates of up to 100 kbit/s in the Standard-mode	
// 10m main clock frequency

void transition()
{
 	int i;
	for(i=0;i<50;i++)
	{asm("NOP ");}	
	
}


void TwinkleLedP26(void){	
	P2.BIT.P2_6  = HIGH;			    /* set LED on port P26 ON */
	idle(); 
    P2.BIT.P2_6  = LOW;			    /* set LED on port P26 OFF */
	idle();		    	}

void TwinkleLedP27(void){	
	P2.BIT.P2_7  = HIGH;			    /* set LED on port P27 ON */
	idle(); 
    P2.BIT.P2_7  = LOW;			    /* set LED on port P27 OFF */
	idle();		    	}





 	
/* ****************  write the value in the specified address ****************************************************/ 	
void WriteOneByteRV8564(int SpecAddress,int DataWrite){	
	
    i2c_start();

	U2TB.WORD= ( 0x0100 | 0xa2);	
    I2C_WAIT_UNTIL_FINISHED();//send out slave addr. and get an ack
	
	U2TB.WORD= ( 0x0100 | SpecAddress);	
    I2C_WAIT_UNTIL_FINISHED();//send out word addr. and get an ack

	U2TB.WORD= ( 0x0100 | DataWrite);	
    I2C_WAIT_UNTIL_FINISHED();//send out data for specified addr. and get an ack

    i2c_stop();
	
	}
/* ****************  write the value in the specified address ****************************************************/ 




void i2c_ini()
{
	asm("fclr i");
	
   	PD7.BIT.PD7_1 = INPUT;// SCL2 input port
	PD7.BIT.PD7_0 = INPUT;// SDA2 input port	
	
	
//	U2MR.BYTE=0x00;// SMD2 to SMD0 bits in the UiMR register = 000b (Serial interface disabled). 
	            	//set the initial state for the SDA2 -------------------------!
// 	P7.BIT.P7_1 = HIGH;// SCL2 level
// 	P7.BIT.P7_0 = HIGH;// SDA2 level

    U2SMR.BYTE=0x01;//IICM=1 mode
    U2SMR2.BYTE=0xD1;//clock delayed, clock out cmos, with no data delay
  

	
	
	P7.BIT.P7_0=1;
	
	U2MR.BYTE=0x0A;// SMD2-SMD0“010b”I2C MODE,  CKDIR “0” master internal clock,IOPOL“0”  no conversion-------------------------
	U2BRG= 0xFF;
		
	
	U2C0.BYTE=0x90;//Uartform:msb,transmission/receiption at SCL falling/rising edge,SDA2 SCL2 n channel, f32
	U2C1.BYTE=0x05;// enable reception and transmission
	UCON.BYTE=0x00;//reset to 0. 
	
	
	BCNIC.BYTE=0x04;//Init. disable STSP interrupt
	
//	U2SMR.BYTE=0x07;//IIC mode, BBS“0” STOP condition detected , arbitration lost by byte
	
	
//	U2SMR3.BYTE=0x00;//if IICM2=0(ACKint),CKPH=1, then the initial and ultimate values for CLK2 are LOW -------------------------Modified!
//	U2SMR4.BYTE=0x00;//clock delayed, clock out cmos, with no data delay
	  
//    IFSR2A.BIT.IFSR26=1;// UART0 bus confliction detection
//    IFSR2A.BIT.IFSR27=1;// UART1 bus confliction detection
  
//    U2SMR2.BIT.CSC=1;/* Clock-synchronous bit enable */
//    idle();
//    U2SMR2.BIT.SWC2=1;//
//    idle(); idle();
//    U2SMR2.BIT.SWC2=0;//*/

  
	 
	S2TIC.BYTE=0x04;	 
    S2RIC.BYTE=0x04;	 //Set IIC_IPL=4; 
//    BCNIC.BYTE=0x04;// 0x04;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,!!!modified!!!!!
	// ABT_U2RB
 

	asm("fset i");
 
}//end void i2c_ini()
 
void iic_mw_start(){
	
    asm("fclr i");
		
	m_start( );
	
    asm("fset i");	
	
}//end void iic_mw_start()

void m_start( ){
	
    U2RB.BIT.ABT_U2RB = 0;
//	U2TB.BYTE.U2TBH = 0x01;
    U2SMR2.BIT.STAC = 0;
	U2SMR2.BYTE = 0x8D;
	U2MR.BYTE = 0x00;
	P7.BIT.P7_0 = LOW;
	U2MR.BYTE = 0x02;
	
	Msg4Trans=RV_8564ram[0];
	U2TB.WORD = ( 0x0100 | Msg4Trans);
	
	U2SMR2.BIT.CSC = 1; //Enable Clock synchronous mode.
  	idle();
	U2SMR2.BIT.SWC2= 1; //Set SCL output to low
 	idle();
 	U2SMR2.BIT.SWC2= 0; //SWC2 :1; /* SCL wait output bit 2  


}//end void m_start()


⌨️ 快捷键说明

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