📄 main.c
字号:
#include <avr/io.h>
#include "global.h"
#include "keyboard.h"
#include <avr/delay.h>
#include "valve.h"
unsigned char procKey(void);
void initLED(void);
void setLEDState(unsigned char on,unsigned char mask);
struct key keyboard[4] = {
{KEY1_PRESS_MASK,KEY1_RELEASE_MASK,0x00,0x00,&onKey1Pressed,&onKey1Default},
{KEY2_PRESS_MASK,KEY2_RELEASE_MASK,0x00,0x00,&onKey2Pressed,&onKey2Default},
{KEY3_PRESS_MASK,KEY3_RELEASE_MASK,0x00,0x00,&onKey3Pressed,&onKey3Default},
{KEY4_PRESS_MASK,KEY4_RELEASE_MASK,0x00,0x00,&onKey4Pressed,&onKey4Default}
};
int main(void)
{
initLED();
initKeyboard();
initAD();
initValve();
while(1)
{
procKey();
}
}
/////////////////////////////////////////////////////////////////////
//Deal with key actions.
//If there are key actions then deal with them and return 1,
//otherwise just do nothing and return 0.
//Note: I know the design of struct key is complex, but it is for the
//future extends.At the moment, we only need to see whether a certain
//key is pressed.
//So, I will ignore the other parts.
/////////////////////////////////////////////////////////////////////
unsigned char procKey()
{
unsigned char i;
for(i=0; i<4; ++i)
{
if(!(PIND & (keyboard[i].keyPressMask)))
{
_delay_ms(KEY_CHECK_DELAY);
if(!(PIND & (keyboard[i].keyPressMask)))
{
keyboard[i].p_callFunction();
return 1; //That mean at the moment, we only serve
//for the first come key action.
}
}
}
return 0; //Urgel, I did nothing.
}
////////////////////////////////////////////////////////////////
//If on = 1 , that means we wanna turn on a LED.
//If on = 0, that means we wanna turn off a LED.
//////////////////////////////////////////////////////////////////
void setLEDState(unsigned char on, unsigned char mask)
{
if(on == 1)
{
PORTC &= mask;
}
else if(on == 0)
{
PORTC |= mask;
}
}
void initLED()
{
unsigned char i;
PORTC = 0x00;
DDRC = 0xff;
for(i=0; i<2; ++i)
{
setLEDState(1, LED1_ON | LED2_ON | LED3_ON | LED4_ON);
_delay_ms(LED_FLASH_PERIOD);
setLEDState(0, LED1_OFF & LED2_OFF & LED3_OFF & LED4_OFF);
_delay_ms(LED_FLASH_PERIOD);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -