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

📄 main.c

📁 mb90540 fujitsu
💻 C
字号:
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
/* ELIGIBILITY FOR ANY PURPOSES.                                             */
/*                 (C) Fujitsu Microelectronics Europe GmbH                  */
/*------------------------------------------------------------------------
  MAIN.C
  - description
  - See README.TXT for project description and disclaimer.

/*----------------------------------------------------------------------*/

/******************************* Includes ***************************************/
#include "mb90540.h"

/******************************* Defines ****************************************/
#define MASTER 
//#define SLAVE1
//#define SLAVE2 

/******************************* Gloabals ***************************************/
unsigned char rbuf = 0;
unsigned char temp = 0;
unsigned char address = 0;
unsigned char tbuf = 0;
unsigned char slaves[] = {'1','2'};
unsigned char *p_string;
char index,length;
char counter = 0;

/******************************* Prototyps **************************************/
void InitUart_Master(void);
void InitUart_Slave(void);

/*============================== PROCEDURES ====================================*/
void InitUart_Master(void)
{
	/* initialize UART0 */	
	UMC0 = 0x39;		/* serial mode control register (00111001) */
	USR0 = 0x0C;		/* status register (00011100) (master)     */
	URD0 = 0x4C;		/* rate and data register  (01001100) 	   */
	DDR4_D40 = 1;		/* set SOT0 working 					   */
}

void InitUart_Slave(void)
{
	/* initialize UART0 */	
	UMC0 = 0x28;	/* serial mode control register (00101000) 			*/
	USR0 = 0x08;	/* status register (00011000) only in receive mode  */
	URD0 = 0x4C;	/* rate and data register  (01001100) 	   			*/
}
/*============================== END OF PROCEDURES =============================*/

/******************************** M A I N *************************************************/
void main(void)
{
   	InitIrqLevels();
    __set_il(7);                /* allow all levels */
    __EI();                     /* globaly enable interrupts */
    
    /* Master mode */
    #ifdef MASTER 
       	InitUart_Master();
    	while(1)
    	{
	   		URD0_D8 = 1;							/* set address bit */
    		send(slaves[address]);					/* send address */
    		index = wait(slaves[address]);			/* wait for acknowlegde */
    		echo_ch();								/* echo acknowlegde */
       		send('d');								/* send data */
    		index = wait(slaves[address++]);		/* wait for acknowlegde */
    		echo_ch();							
        	if(address > 1)							
    			address = 0;						/* start communication with slave1 */
    		tbuf = 'e';								/* end of communication */
    		while(tbuf != 0);
       	}
    #endif
    	
    /* Slave 1 */
    #ifdef SLAVE1
       	InitUart_Slave();
    	index = 0;
       	while(1)
    	{
    		while(rbuf != '1');		/* waiting for the address */
       		receive_adr();			/* detecting the address */
    		UMC0 = 0x39;			/* change mode to 3 */
			DDR4_D40 = 1;			/* set SOT0 working */
			USR0 = 0x0C;			/* enable transmit interrupt */
   		
    		while(rbuf != 'd');		/* waiting for data */	
    		receive_data();			/* start communication */
			while(rbuf != 'e');		/* wait for communication end */
    		UMC0 = 0x28;			/* change back to mode 2 */
			USR0 = 0x08;			/* disable transmit interrupt*/
			DDR4_D40 = 0;	 		/* change to input */
    	}
    #endif
    
    /* Slave 2 */
    #ifdef SLAVE2
    	InitUart_Slave();
      	while(1)
    	{	
    		while(rbuf != '2');		/* waiting for the address */
    		receive_adr();			/* detecting the address */
			UMC0 = 0x39;			/* change mode to 3 */
			DDR4_D40 = 1;			/* set SOT0 working */
			USR0 = 0x0C;			/* enable transmit interrupt */
	
    		while(rbuf != 'd');		/* waiting for data */	
    		receive_data();			/* start communication */
			while(rbuf != 'e');		/*wait for communication end */
    		UMC0 = 0x28;			/* change back to mode 2 */
			USR0 = 0x08;			/* disable transmit interrupt*/
			DDR4_D40 = 0;	 		/* change to input */
    	}
    #endif
}
/******************************** end of main ********************************************/

/*********************************** interrupts *****************************************/
void __interrupt Uart0_Transmit(void)
{
	if(index != 0)			/* transmit string */
	{
		if(counter < length)
		{
			temp = p_string[counter++];
			UODR0 = temp;
		}
		if(counter >= length)
		{
			counter = 0;
			index = 0;
		}
	}
	else
	{
		UODR0 = tbuf;			/* write data to the output buffer */
		tbuf = 0;				/* clear tbuf */
	}
}

void __interrupt Uart0_Receive(void)
{
	if(USR0_ORFE)
	{
		temp = UIDR0;			/* clear input buffer */
		UMC0_RFC = 0;			/* clear error flags */ 
	}
	else
	{
		rbuf = UIDR0;			/* read data from the input buffer */
	}
}

⌨️ 快捷键说明

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