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

📄 wdt.c

📁 ATMEGA8-wdt-学习AVR的朋友们挺多的
💻 C
字号:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

#include <avr/pgmspace.h>
#include <stdio.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#define K1 (PIND&0x20)
#define K2 (PIND&0x40)
#define Led1 PD2
#define Led2 PD3
#define Led3 PD4
#define sbi(x,y) (x|=(1<<y))
#define cbi(x,y)  (x&=~(1<<y))
#define uchar unsigned char
#define uint unsigned int
unsigned char temp_eeprom=0,middle=20;
unsigned int all_time=0,flag_time=0;
unsigned char flag=0;
void uart0_init(void)
      {
	  UCSRB=0X00;
	  UBRRL=0X33;
	  UCSRB=0X18;
	  
	  }
void sendchar(unsigned char c)
      {
	  UDR=c;
	  while(!(UCSRA&0X40));
	  UCSRA|=0X40;
	  
	  }


void delay(unsigned int time)	//延时子函数
{
while(time)time--;
}



SIGNAL(SIG_OVERFLOW1)
      {
	  TCNT1=65181;
// PORTD=PORTD^0x10;
all_time++;
if(all_time>=400)//20ms
  {
  all_time=0;
  }
if(all_time<middle)
   {
   PORTD=PORTD|0x10;   
   }
else
  {
  PORTD=PORTD&0xef;
  }


if(flag==1)
   {
   if(middle<40)
     {
   flag_time++;
   if(flag_time>=4000)//4000*0。05ms自加50us的宽度
     {
	 flag_time=0;
	 if(middle<40)
	   {
	   middle++;
	   }
	 }
	 }
   }
else
  {
  middle=20;
  }


	  }

 SIGNAL(SIG_OVERFLOW0)
      {
	  TCNT0=0x00;
 //      PORTD=PORTD^0x04;
	  }
 SIGNAL(SIG_OVERFLOW2)
      {
	  TCNT2=0x00;
 //      PORTD=PORTD^0x08;
	  }


int main (void)
{


	DDRD=0x1f;
    PORTD=0xff;	
	DDRC=0xf3;
	PORTC=0xf3;

///////////////////
cbi(PORTD,Led1);
delay (65000);
cbi(PORTD,Led2);
delay (65000);
cbi(PORTD,Led3);
delay (65000);
/////////////////////
sbi(PORTD,Led1);
delay (65000);
sbi(PORTD,Led2);
delay (65000);
sbi(PORTD,Led3);
delay (65000);
/////////////////////


	 TCCR1A=0X00;
	 TCCR1B|=(1<<CS10);
	 TIMSK|=(1<<TOIE1);
     TCNT1=64536;
	 	uart0_init();
        sendchar(0Xaa);
////////////////////////////
TCCR0|=(1<<CS02);//256分频
TCNT0=0;
TIMSK|=(1<<TOIE0);
////////////////////////////////////////
////////////////////////////
TCCR2|=(1<<CS22);//64分频
TCNT2=0;
TIMSK|=(1<<TOIE2);
////////////////////////////////////////
sei();
/*
SFIOR=SFIOR&0xf7;//连接到外部AN1
ACSR=ACSR&0x7f;//开比较起电源
ACSR=ACSR&0xbf;//连接到外部AN0
ACSR=ACSR&0xf7;//中断禁止
ACSR=ACSR&0xfb;//输入扑做
*/
_EEPUT(0, 0x01);//#define _EEPUT(addr, val) eeprom_write_byte 
_EEGET(temp_eeprom, 0);//#define _EEGET(var, addr) (var) = eeprom_read_byte 
wdt_enable(WDTO_1S);
middle=20;
while(1)
	{
wdt_reset();


if(K1==0)
   {
    cbi(PORTD,Led1);
	flag=1;
   }
else
  {
  sbi(PORTD,Led1);
  }
if(K2==0)
   {
    cbi(PORTD,Led2);
	flag=1;
   }
else
   {
   sbi(PORTD,Led2);
   }
if((K1!=0)&(K2!=0))
   {
   flag=0;
   }

/*
if((ACSR&0x20)==0)
  {
PORTD=PORTD|0x08;
  }
else
  {
PORTD=PORTD&0xf7;
  }
*/

	}
	return (0); 
}


⌨️ 快捷键说明

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