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

📄 main.c

📁 Mitsubishi M30245 SampleCode
💻 C
字号:
/*****************************************************************************
*
*	File Name: main.c                                          
*                                                                  
*	Content:   This program blinks the four LEDs from left to right.
*			   The blink rate is controlled by the photocell (R23)
* 			   that is connected to an A/D input. Cover the photocell
*			   to see the blink rate change.	 
*
*  	Date:  01-02-02
*  	This program was written to run on the MDECE0245
*  	evaluation board
*                  
*                                                                  
*	Copyright 2002 Mitsubishi Electric & Electronics US                           
*	All rights reserved                                            
*                                                                  
*=============================================================================
*	$Log:$
*===========================================================================*/

#include "sfr245.h"
#pragma INTERRUPT /B	TimerA1_ISR

int out1;
int out2;
int out3;
int value;


/*****************************************************************************
Name:   main        
Parameters:                     
Returns:        
Description:  main program loop and initialization

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

main() {


	pd7 = 0xFF;		/* Change port 7 to all outputs (connected to LEDs) */
	p7 = 0x0e;		/* Initialize LEDs on port7 */

	/* ********** USE A/D FOR READING POT. VALUE ***********
	 Set up A/D register for AN0	 
	 - AN0 selected
	 - One shot mode
	 - Software trigger
	 - Frequency /4   */
	adcon0 = 0x00;

	adcon1 = 0x20;		    /* Set up 8 bit conversion & Vref connected */
	adcon2 = 0x01;			/* Set up sample and hold */

	/* ****** USE TIMERS FOR SETTING BLINK RATE ********
	 Timer A0 mode register
	 - Timer mode
	 - No pulse output
	 - Not using Gate function
	 - Count source is f32  */
	ta0mr = 0x80;
	ta0ud = 0;			    /* Set timer count direction to DOWN */
	ta0 = 0x19F;		  	/* Preload Timer A0 */
	
	/* 
	 Timer A1 mode register
	 - event counter mode
	 - count on falling edge 
	 - Setup timer A1 to trigger on A0 overflow */
	ta1mr = 0x01;
	ta1tgh = 1;
	ta1tgl = 0;
	ta1ud = 0;	 			/* count down */
	ta1 = 0x00;	 			/* preload Timer A1 */
				
	ta1ic = 0x05;			/* TA1 Interrupt enabled, Level 5 */
	ta0s = 1;				/* Start the timers	*/
	ta1s = 1;

	asm("FSET I");			/* Turn on interrupts */
	out1 = 0x01;			/* Initialize LEDs */
	
	/*************** PROGRAM LOOP ***********************/
	while(1) {

		adst = 1;			/* Start A2D conversion	*/
		while(adst == 1);	/* wait for A/D conversion start bit to return to 0 */

		value = ad0;		/* read value from A/D register */
		value << 8;			/*   and preload TimerA1, this value */
		ta1 = value;		/*   is used to vary the blink rate */
	}
}


/*****************************************************************************
Name:   TimerA1_ISR        
Parameters:                     
Returns:        
Description:  TIMER INTERRUPT ROUTINE  -  This routine turns on one LED at a
 			time and shifts that LED right to left (D1 to D4).  The LEDs are 
 			connected to the lower byte of P7 and we don't want to change the 
 			data on	the upper byte.

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

void TimerA1_ISR( void) {

 
   	if (out1 == 0x010){		/* Is the last LED on? */
		out1 = 0x01;		/*  yes, start over	*/
	}
	else {
		out1 <<= 1;			/*  no, shift to next LED in line */
	}		

	out2 = ~out1;			/* Save LED count */
	out3 = p7;				/* Get current data on P7  */
	out3 |= 0x0f;			/* clear out LED data (lower byte) and */
	out2 &= out3;			/*   replace with new LED data */
	p7 = out2;				/* Output new value to P7 */
 }

⌨️ 快捷键说明

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