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

📄 sam9263wd.c

📁 AVR单片机ATtiny13编程示例
💻 C
字号:
//ICC-AVR application builder : 2008-10-8 上午 11:01:52
// Target : T13
// Crystal: 4.8000Mhz

#include <iot13v.h>
#include <macros.h>

//#define DEFAULT_RST_TIMEOUT		6000		//默认复位超时时间

volatile unsigned int Timer0BaseClk=0;		//Timer0基准时钟
//unsigned char RstState=0;					//复位状态,“1”进入复位状态
unsigned char PB_DataIn=0;					//PB端口读入数据
unsigned int RstTimeOut;					//9263设定的复位超时时间
unsigned int PulseChgTime=0;				//脉冲电平改变时间
unsigned int RunLedTime=0;

//unsigned char PulsePreState=0,PulseCurState=0;	//输入脉冲电平状态
unsigned char PulseChgFlag=0;					//脉冲电平改变标志
unsigned int RstPulseWidth=0;

#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
	TCNT0 = 0x45; //reload counter value
	
	Timer0BaseClk++;
	if(Timer0BaseClk>=60000)
		Timer0BaseClk=0;
}

#pragma interrupt_handler pc_isr:iv_PCINT0
void pc_isr(void)
{
	//pin change interrupt
	PulseChgFlag = 1;
}

//获取当前时间
unsigned int GetCurTime(void)
{
	volatile unsigned int timer1,timer2;
	
	//为防止读取当前时间时正好有定时中断,需读取两遍,值相同就返回当前值
	do
	{
		timer1=Timer0BaseClk;
		timer2=Timer0BaseClk;
	}while(timer1 != timer2);
	
	return timer2;
}

//计算消逝时间
//参数:	iPreTime	之前时间点
//返回值:	iPreTime与当前时间的差值
unsigned int GetPassTime(unsigned int iPreTime)
{
	unsigned int temp_i=0;
	unsigned int curtime;
	
	curtime=GetCurTime();
	
	if(curtime>=iPreTime)
		temp_i=curtime-iPreTime;
	else
		temp_i=60000-iPreTime+curtime;
		
	return temp_i;
}

void main(void)
{
	volatile unsigned int t=0;
	
//--------------init_devices--------------------
	//stop errant interrupts until set up
	CLI(); //disable all interrupts
	//--------------port_init-------------------
	PORTB = 0x18;
	DDRB  = 0x18;
	//--------------watchdog_init---------------
	WDR(); //this prevents a timout on enabling
	WDTCR = 0x18;
	WDTCR = 0x0C; //WATCHDOG ENABLED,32K,250ms
	//--------------timer0_init-----------------
	//TIMER0 initialize - prescale:256
	// WGM: Normal
	// desired value: 100Hz
	// actual value: 100.267Hz (0.3%)
	TCCR0B = 0x00; //stop
	OCR0A = 0xBB;
	OCR0B = 0xBB;
	TCNT0 = 0x45; //set count
	TCCR0A = 0x00; 
	TCCR0B = 0x04; //start timer

	MCUCR = 0x00;
	TIMSK0 = 0x02; //timer interrupt sources
	PCMSK = 0x20;
	GIMSK = 0x20; //interrupt sources
	SEI(); //re-enable interrupts
	//all peripherals are now initialized
//--------------init_devices--------------------
	
	while(1)
	{
		WDR();
		t = GetPassTime(RunLedTime);
		if(t >= 100)
		{
			PORTB ^= (1<<PB4);
			RunLedTime = GetCurTime();
		}
			
		PB_DataIn = PINB;
		switch(PB_DataIn&0x07)
		{
			case 0x00:
				RstTimeOut = 50;	//500ms
				break;
			case 0x01:
				RstTimeOut = 100;	//1s
				break;
			case 0x02:
				RstTimeOut = 200;	//2s
				break;
			case 0x03:
				RstTimeOut = 500;	//5s
				break;
			case 0x04:
				RstTimeOut = 1000;	//10s
				break;
			case 0x05:
				RstTimeOut = 2000;	//20s
				break;
			case 0x06:
				RstTimeOut = 4000;	//40s
				break;
			case 0x07:
				RstTimeOut = 6000;	//60s
				break;
			default:
				RstTimeOut = 6000;	//60s
				break;
		}

		//----------JudgePulseState------------------------
		/*PulseCurState = (PB_DataIn&(1<<PB5));
		if(PulseCurState != PulsePreState)	//电平出现翻转
		{
			PulseChgTime = GetCurTime();
			PulsePreState = PulseCurState;
		}*/
		
		if(PulseChgFlag)
		{
			PulseChgFlag = 0;
			PulseChgTime = GetCurTime();
		}
		//----------JudgePulseState------------------------
		
		t = GetPassTime(PulseChgTime);
		if(t >= RstTimeOut)
		{
			//RstState = 1;			//处于复位期间
			PORTB &= ~(1<<PB3);
			RstPulseWidth = GetCurTime();
			PulseChgTime = GetCurTime();
		}
		t= GetPassTime(RstPulseWidth);
		if(t >= 25)		//250ms
		{
			PORTB |= (1<<PB3);
			//RstState = 0;			//正常工作期间
		}
	}
}

⌨️ 快捷键说明

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