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

📄 sleeptest.c

📁 使用winavr+studio进行开发的测试程序
💻 C
字号:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
/*
这是一段测试程序。睡眠模式的设定、唤醒以及按键的使用。但是在用gcc编译时不压缩的
情况下为Program:    3638 bytes (44.4% Full),而代码压缩后为Program:238 bytes (2.9% Full)
原因待查。

*/
ISR(INT0_vect)
{
    cli();
}

void Sleep(void)//待机#唤醒
{
//传送设定到eeprom
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);//掉电模式
  cli();
  sleep_enable();
  sei();
  GICR=0x40;                     //允许外部中断INT0,低电平触发
  sleep_cpu();
  sleep_disable();
//读取设定
  GICR=0x00;
  sei();
}
void _isr(void)
{
sei();
GICR=0x40;                     //允许外部中断INT0,低电平触发
MCUCR=0xa0;                    //打开SE和SM,允许睡眠模式
asm("sleep");                  //进入睡眠状态
MCUCR=0x00;
GICR=0x00;
}



int main(void)
{
   unsigned char flash=1;
   DDRD|=_BV(PD0);
   PORTD|=_BV(PD0);
   DDRD&=~_BV(PD2);
   PORTD|=_BV(PD2);
   //if(!(PIND&_BV(PD2))){ PORTD&=~_BV(PD0);}  
while(1)
  {
   Sleep();
   if(!(PIND&_BV(PD2)))
   {  
      _delay_ms(10);
	  while(!(PIND&_BV(PD2))){}
      if(flash){PORTD&=~_BV(PD0);}
      else{PORTD|=_BV(PD0);}
      flash=!flash;
      _delay_ms(10);
   }
  }
}

⌨️ 快捷键说明

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