|
为什么我这程序,我还没给它按键输入值,它灯就亮了···程序一烧写进去 就开始亮
#include <iom32v.h>
#include <macros.h>
unsigned char NUM;
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2
void delay1(unsigned int z) //delay Z*1ms
{
unsigned int x,y;
for(x = 0; x < z; x++)
for(y = 0; y < 525; y++);
}
void port_init()
{
DDRB |= BIT(PB0)|BIT(PB1);
DDRA &= ~(BIT(PA0)|BIT(1)|BIT(2));
PORTB |= BIT(PB0);
PORTA &= ~(BIT(PA0)|BIT(PA1)|BIT(PA2));
}
unsigned char keyread()
{
static unsigned char key_state = 0;
static unsigned char key_old;
unsigned char key_press;
unsigned char key_return = 0x00;
key_press = PINA & 0x07;
_NOP();
_NOP();
// delay1(10);
key_press = PINA & 0x07;
switch(key_state)
{
case key_state_0:
if(key_press != 0x00)
key_state = key_state_1;
else
key_state = key_state_0;
break;
case key_state_1:
if(key_press == key_old)
{
key_return = key_press;
key_state = key_state_2;
}
else
key_state = key_state_0;
break;
case key_state_2:
if(key_press != key_old)
key_state = key_state_0;
break;
}
key_old = key_press;
return key_return;
}
int main(void)
{
//NOP();
unsigned char key_value;
port_init();
while(1)
{
key_value = keyread();
if(key_value == 0x01)
Led_One_Full(6,1); //亮灯程序 DDRB |= BIT(PB6);
// DDRA |= BIT(PA5);
// PORTB |= BIT(PB6);
// PORTA &= ~BIT(PA5);
else
Led_One_Full(0,0); //灯灭
}
return 0;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|