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

📄 dma_sio_tr.c

📁 瑞萨CAN通讯单片机程序
💻 C
字号:
/*  FILE COMMENT   ****************************************************
 *	M32R C Programming		Rev. 1.00
 *		< Sample Program for AD-DMAC-SIO >
 *
 *	Copyright (c) 2003 Renesas Technology Corporation
 *			   And Renesas Solutions Corporation
 *			   All Rights Reserved
 **********************************************************************/

/************************************************************************/
/*		Include file						*/
/************************************************************************/

#include		"..\inc\sfr32170_pragma.h"

/************************************************************************/
/*		Definition of external reference			*/
/************************************************************************/

extern	void		DisInt( void );				/* Interrupt disable function */
extern	void		EnInt( void );				/* Interrupt enable function */

/************************************************************************/
/*		Function prototype declaration				*/
/************************************************************************/

	void		main(void);				/* Main function */
	void		init_func(void);			/* Initial setup function */
	void		port_init(void);			/* Initialize port  */
	void		timer_init(void);			/* Timer initialization */
	void		dma_init(void);				/* Initialize DMA */
	void		sio0tr_init(void);			/* Initialize serial I/O */
	void		ad_init(void);				/* Initialize A-D converter */

/************************************************************************/
/*		Define macro						*/
/************************************************************************/

/* DMAC setting data */

						/* 0123 4567	*/
#define	DMA0_INIT	(unsigned char)0x9c	/* 1001 1100B	DMA0 channel control register	*/
						/* |||| |||+---	Destination address fixed	*/
						/* |||| ||+----	Source address fixed		*/
						/* |||| |+-----	Transfer size of 8-bit		*/
						/* |||| +------	Transfer enabled			*/
						/* ||++--------	01: Start upon completion of A-D conversion */
						/* |+----------	No transfer request			*/
						/* +-----------	Ring buffer mode		*/

/* Serial I/O setting data */

						/* 0123 4567	*/
#define	P8MOD_SCI0	(unsigned char)0x30	/* 0011 0000B	P8 operation mode register	*/
						/* |||| |||+--- P87				*/
						/* |||| ||+---- P86				*/
						/* |||| |+----- P85				*/
						/* |||| +------ P84				*/
						/* |||+-------- RXD0				*/
						/* ||+--------- TXD0				*/
						/* ++---------- don't care			*/

						/* 0123 4567	*/
#define	SnTCNT_INI	(unsigned char)0x00	/* 0000 0000B	SIOn transmit control register	*/
						/* |||| |||+--- Disable transmission	*/
						/* |||| +++---- don't care		*/
						/* ||++-------- f(BCLK)			*/
						/* ++---------- don't care		*/

#define	SnMOD_INI	(unsigned char)0x20	/* 0010 0000B	SIOn mode register	*/
						/* |||| |||+--- Sleep function disabled	*/
						/* |||| ||+---- Parity inhibited	*/
						/* |||| |+----- don't care(odd)		*/
						/* |||| +------ 1 stop bit		*/
						/* |||+-------- Internal clock		*/
						/* +++---------	8 bit UART		*/

/* Setting baud rate (Be sure to check actually set value when using) */

#define	XIN		10			/* 10MHz */
#define	BAUD_19200	(unsigned char)(XIN * 2000000 / 16 / 19200 - 1)	/* 19200bps */

/* A-D converter setting data */

						/* 0123 4567	*/
#define	AD0SIM0_INIT	(unsigned char)0x38	/* 0011 1000B	AD0 single mode register 0	*/
						/* |||| |||+---	No operation			*/
						/* |||| ||+----	No operation			*/
						/* |||| |+-----	Writing invalid			*/
						/* |||| +------	DMA transfer request		*/
						/* |||+--------	Hardware trigger		*/
						/* ||+---------	Start output event bus 3	*/
						/* ++----------	don't care			*/

#define	AD0SIM1_INIT	(unsigned char)0x40	/* 0100 0000B	AD0 single mode register 1	*/
						/* |||| ++++---- ch0 selected			*/
						/* ||++--------- don't care			*/
						/* |+----------- Double speed			*/
						/* +------------ AD conversion mode		*/

#define	AD0SCM0_INIT	(unsigned char)0x60	/* 0110 0000B	AD0 scan mode register 0	*/
						/* |||| |||+---	No operation			*/
						/* |||| ||+----	No operation			*/
						/* |||| |+-----	Writing invalid			*/
						/* |||| +------	A-D0 interrupt request		*/
						/* |||+--------	Hardware trigger		*/
						/* ||+---------	Start output event bus 3	*/
						/* |+----------	Continuous mode			*/
						/* +-----------	don't care			*/

#define	AD0SCM1_INIT	(unsigned char)0x44	/* 0100 0100B	AD0 scan mode register 1	*/
						/* |||| ++++---	4ch scan			*/
						/* ||++--------	don't care			*/
						/* |+----------	Double speed mode		*/
						/* +-----------	don't care			*/

/* MJT setting data */

						/* 0123 4567	*/
#define	OEB3TOP8	(unsigned char)0xC0	/* 1100 0000B	Output event bus control register	*/
						/* ||++ ++++---	don't care				*/
						/* ++----------	Select output event bus 3 for input: TOP8 for output */

						/* 0123 4567 89AB CDEF	*/
#define	TOP8_MASK	(unsigned short)0x0313	/* 0000 0011 0001 0011B	TOP8-10control register		*/
#define	TOP8_CntOutput	(unsigned short)0x0210	/* 0000 0010 0001 0000B					*/
						/*	  ||	|   ++- Select clock bus 0		*/
						/*	  ||	+------ Select enable source		*/
						/*	  ++----------- TOP8 continuous output mode	*/

#define	TOP8_ILevel		(unsigned char)0	/* MJT output interrupt 5 (TOP8) interrupt priority level */

#define	TOP8_Cycle		(unsigned short)5000 - 1	/* TOP8 interrupt period 5000 		*/

/*""FUNC COMMENT""*******************************************************
 * Function name : init_func()
 *-----------------------------------------------------------------------
 * Description	: - Initialize ICU
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: The timer acts as a first trigger is started at the last
 *""FUNC COMMENT END""***************************************************/
void init_func(void)
{
	port_init();					/* Initialize those related to port */
	dma_init();					/* Initialize DMA */
	sio0tr_init();					/* Initialize serial I/O */
	ad_init();					/* Initialize A-D converter  */
	timer_init();					/* Timer initial setting */
}

/*""FUNC COMMENT""*******************************************************
 * Function name : port_init()
 *-----------------------------------------------------------------------
 * Description	: - Initialize port
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: -
 *""FUNC COMMENT END""***************************************************/
void port_init(void)
{
	PIEN = PIEN0;					/* Enable port input */

/*** LED output port ***/

	P11DATA = 0x00;					/* Output data (must be set prior to mode) */
	P11DIR = 0xff; 					/* P110-P117 :Output mode */
	P11MOD = 0x00; 					/* P110-P117 :Input/output port */

/*** Switch input port ***/

	P13DIR = 0x00;					/* P130-P137 :Input mode */
	P13MOD = 0x00;					/* P130-P137 :Input/output port */
}

/*""FUNC COMMENT""*******************************************************
 * Function name : dma_init()
 *-----------------------------------------------------------------------
 * Description	: - Initialize DMAC
 *		:   DMA0: Transfer A-D0ch0 conversion result to S100 
 *		:   transmit buffer (infinite)
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: -
 *""FUNC COMMENT END""***************************************************/
void dma_init(void)
{
/*** DMA0 initial setting ***/

	DM04ITMK |= DMITMK0;				/* Inhibit DMA0 interrupt */
	DM04ITST = ~DMITST0;				/* Clear DMA0 interrupt request */

	DM0SA = (unsigned short)&AD08DT0;		/* Source address -> A-D convert ch0 8-bit data */
	DM0DA = (unsigned short)&S0TXB_L;		/* Destination address -> SIO0 transmit buffer */

	DM0CNT = DMA0_INIT;				/* Start upon the completion of A-D0 conversion, enable ring buffer mode and transfer */
}

/*""FUNC COMMENT""*******************************************************
 * Function name : sio0tr_init()
 *-----------------------------------------------------------------------
 * Description	: - Set 8-bit UART transmission for SIO0
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: No reception setting has done
 *		: For M32R/E#1,2,3, PnMOD cannot be accessed for R/M/W
 *		: The function must be executed while interrupt is inhibited
 *""FUNC COMMENT END""***************************************************/
void sio0tr_init(void)
{
/*** Setting transfer mode */

	S0TCNT = SnTCNT_INI;				/* f(BCLK), disable transmission */
	P8MOD |= P8MOD_SCI0;				/* TXD0 and RXD0 are valid */
	S0MOD = SnMOD_INI;				/* Set data format */
	S0BAUR = BAUD_19200;				/* Set baud rate */

/*** Interrupt related settings ***/

	ISIO0TXCR = (unsigned char)7;			/* Set SIO0 transmit interrupt priority level (inhibit) */
	SI03SEL &= ~ISR0;				/* Select reception-finished interrupt */
	SI03MASK &= ~T0MASK;				/* Disable SIO1 transmit interrupt request */

/*** Starting transmission ***/

	S0TCNT |= TEN;					/* Enable transmission */
}

/*""FUNC COMMENT""*******************************************************
 * Function name : ad_init()
 *-----------------------------------------------------------------------
 * Description	: - Initialize A-D converter
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: -
 *""FUNC COMMENT END""***************************************************/
void ad_init(void)
{
/*** Interrupt related settings ***/

	IAD0CCR = (unsigned char)7;			/* Set AD0 transmit interrupt priority level */

/*** Single mode setting ***/

	AD0SIM0 = AD0SIM0_INIT;				/* Request hardware trigger and DMA0 transfer  */
	AD0SIM1 = AD0SIM1_INIT;				/* Select double speed, A-D conversion mode and ch.0 */

/*** Scan mode setting ***/

	AD0SCM0 = AD0SCM0_INIT;				/* Select continuous mode and software trigger */
	AD0SCM1 = AD0SCM1_INIT;				/* Double speed, 4-channel scan */

	AD0SCM0 |= 0x01;				/* Start scan mode A-D conversion */
}

/*""FUNC COMMENT""*******************************************************
 * Function name : timer_init()
 *-----------------------------------------------------------------------
 * Description	: - Initialize timer
 *		:   Count TOP8 in continuous mode for starting AD conversion
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: -
 *""FUNC COMMENT END""***************************************************/
void timer_init(void)
{
	unsigned char	temp;

/*** Disabling TOP8 (TO8) output ***/

	P10MOD &= ~0x80;			/* Disable TO8 (TOP8 output) against output .. Select port P100 output */

/*** Setting disable interrupt ***/

	temp = TOPIR3;
	temp |= ( TOPIS9 | TOPIM8);				/* TOP8 Disable interrupt */
	TOPIR3 = temp;

/*** Setting continuous output mode(TOP8) ***/

	OEBCR &= ~OEB3TOP8;					/* Select output event bus 3 for input -> TOP8 for output */

	TOP810CR = ( TOP810CR & ~TOP8_MASK) | TOP8_CntOutput;	/* Set TOP8 continuous output mode */

	PRS0 = (200 - 1);					/* Set prescaler(20us@10MHz) */
	TOP8CT = TOP8_Cycle;					/* Set initial value in TOP8 counter */
	TOP8RL = TOP8_Cycle;					/* Set initial value in TOP8 reload register */

	TOPPRO = ~TOP8PRO;					/* Enable TOP8 enable protect */
	TOPCEN = 0xffff;					/* Enable TOP8 count */
}

/*""FUNC COMMENT""*******************************************************
 * Function name : main()
 *-----------------------------------------------------------------------
 * Description	 : Single mode is forcibly executed by TOP8 during A-D scan 
 *		: mode operation and the conversion result is outputted from
 *		: serial I/O (A-D conversion result is 8-bit).
 *		: - A-D0 converter operates in 4-channel scan mode
 *		: - Output underflow (100ms@10MHz) of the TOP8 continuous
 *		:   output mode to the output event bus 3
 *		: - Output event bus 3 triggers A-D0ch0 conversion single mode to start
 *		: - DMA0 is started upon the completion of A-D conversion
 *		:   (the conversion result is transferred to transmit buffer)
 *		: - Transmit serial 0 upon completion of DMA0 
 *		:   (transferred infinitely in ring buffer mode)
 *		: - For A-D conversion result in scan mode, 
 *		:   the result specified with PORT13 is outputted to LED (PORT11) 
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: Interrupt is not actually used 
 *		: A-D conversion result is not ASCII converted
 *""FUNC COMMENT END""***************************************************/
void main(void)
{
/*** Initialize microcomputer ***/

	DisInt();						/* Disable interrupt */

	init_func();

	EnInt();						/* Enable interrupt */

/*** Display A-D conversion result on LED ***/

	while(1) {
		switch (P13DATA) {
			case 0x01:
				P11DATA = AD08DT3;
				break;
			case 0x02:
				P11DATA = AD08DT2;
				break;
			case 0x04:
				P11DATA = AD08DT1;
				break;
			case 0x08:
				P11DATA = AD08DT0;
				break;
			default:
				break;
		}
	}
}

⌨️ 快捷键说明

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