ATTiny13 头文件
#include
#include
#define RED_SET PORTB |= (1 << PB0) //位置高
#define RED_CLR PORTB &= ~(1 << PB0) //位置低
#define GREEN_SET PORTB |= (1 << PB1) //位置高
#define GREEN_CLR PORTB &= ~(1 << PB1) //位置低
#define BLUE_SET PORTB |= (1 << PB2) //位置高
#define BLUE_CLR PORTB &= ~(1 << PB2) //位置低
#define KEY_OUT DDRB |= (1 << PB3) //位置高
#define KEY_IN DDRB &= ~(1 << PB3) //位置低
#define KEY_SET PORTB |= (1 << PB3) //位置高
#define KEY_RD PINB & (1 << PB3) //位读取
#define PUD_SET MCUCR |= (1 << PUD) //位置高
void delay2s();
int main()
{
//第一步,初始化
DDRB = 0b00000111; //设置输入输出方向,PB0-2为输出,其他为输入
PORTB = 0x00;
PUD_SET;
//第二步,按时序显示七彩跳闪
while (1)
{
// if (PINB3) //如果没有按键按下
{
// PORTB = 0b00000111; //PB0-2输出高电平供电
RED_SET;
GREEN_SET;
BLUE_SET;
delay2s();
//PORTB = 0b00000101;
GREEN_CLR;
delay2s();
//PORTB = 0b00000011;
BLUE_CLR;
GREEN_SET;
delay2s();
//PORTB = 0b00000001;
GREEN_CLR;
delay2s();
//PORTB = 0b00000110;
RED_CLR;
GREEN_SET;
BLUE_SET;
delay2s();
//PORTB = 0b00000100;
GREEN_CLR;
delay2s();
//PORTB = 0b00000010;
GREEN_SET;
BLUE_CLR;
delay2s();
}
}
}
void delay2s()
{
int i;
int j;
for(i=0xA0;i>0;i--)
{
KEY_OUT;
KEY_SET;
KEY_IN;
_delay_ms(1);
j=PINB;
if(j<0x08) //按下
{
while (j<0x08){
KEY_OUT;
KEY_SET;
KEY_IN;
_delay_ms(1);
j=PINB;} //等待按键释放
KEY_OUT;
KEY_SET;
KEY_IN;
_delay_ms(1);
j=PINB;
while (j>0x08)
{KEY_OUT;
KEY_SET;
KEY_IN;
_delay_ms(1);
j=PINB;};//等待再次按下按键
while (j<0x08){
KEY_OUT;
KEY_SET;
KEY_IN;
_delay_ms(1);
j=PINB;
} //再次按下按键
}
}
}