📄 iic_test.c
字号:
//ICC-AVR application builder : 2006-10-16 下午 09:35:42
// Target : M88
// Crystal: 20.0000Mhz
#include "common.h"
void KeyScanTask1(void);
void KeyScanTask2(void);
void KeyPressedTask1(void);
void KeyPressedTask2(void);
void port_init(void)
{
PORTB = 0x01; //只用PB0口,并且是输入口
DDRB = 0xFE; //0是输入
PORTC = 0x08; //0000 1000// PORTC |= 0x08; PORTC &= ~0x08;
DDRC = 0x08; //1是输出 0000 1000
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x83; //set count
TCCR0A = 0x00;
TCCR0B = 0x03; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:17 //TIMER0 has overflowed
void timer0_ovf_isr(void)
{
static uint s_unLoopTimeCnt = 0;
TCNT0 = 0x83; //set count
s_unLoopTimeCnt++;
if(s_unLoopTimeCnt > 2)
{
s_unLoopTimeCnt = 0;
g_ucLoopTimeFlag = true;
}
}
void KeyScanTask(void)
{
static uchar s_ucKeyDitheringCnt = 0; //avoid dithering
static uchar s_ucKeyPressCnt = 0; //count the sequence of the pressed key
static uint s_undisabletime = 0;
if(PINB & 0x01) //if the input voltage is high
{
s_ucKeyDitheringCnt++;
if(s_ucKeyDitheringCnt > 5) //delay time 5x2=10ms;avoid dithering
{
s_ucKeyDitheringCnt = 0;
if(PINB & 0x01)
g_ucKeyPressed = true;
}
s_undisabletime = 0;
PORTC |= 0x08;
}
else
{
s_ucKeyDitheringCnt = 0;
s_undisabletime++;
if(s_undisabletime > 30000)
{
s_undisabletime = 0;
PORTC &= ~0x08;
}
if(g_ucKeyPressed)
{
s_ucKeyPressCnt++;
if(s_ucKeyPressCnt > 3)
{
s_ucKeyPressCnt = 1;
g_ucFlashEnd = false;
}
g_ucKeyPressed = false;
}
}
g_ucKeyPressedCnt = s_ucKeyPressCnt;
}
void KeyPressedTask(void)
{
static uint s_unLedFlashTimeDly = 0;
static uchar s_ucLedFlashCnt = 4;
if(g_ucKeyPressedCnt == 1) //press the first
IICSendStr(0x60,g_ucTorchIICData,2);
else if(g_ucKeyPressedCnt == 2)
IICSendStr(0x60,g_ucLightOFFData,2);
else if((g_ucKeyPressedCnt == 3) && (!g_ucFlashEnd)) //press the second
{
if(s_ucLedFlashCnt > 0)
{
s_unLedFlashTimeDly++;
if(s_unLedFlashTimeDly < 50)
IICSendStr(0x60,g_ucIledIICData,2); //led is on
else if(s_unLedFlashTimeDly < 100)
IICSendStr(0x60,g_ucLightOFFData,2); //led is off
else
{
s_unLedFlashTimeDly = 0;
s_ucLedFlashCnt--;
}
}
else
{
s_ucLedFlashCnt = 4;
g_ucFlashEnd = true;
IICSendStr(0x60,g_ucFlashEnIICData,2); //enalble flash
IICSendStr(0x60,g_ucFlashTimerIICData,2);
IICSendStr(0x60,g_ucFlashIICData,2); //flash is on
}
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
twi_init();
timer0_init();
MCUCR = 0x00;
EICRA = 0x0A; //extended ext ints
EIMSK = 0x03;
TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x21; //timer 1 interrupt sources
TIMSK2 = 0x01; //timer 2 interrupt sources
PCMSK0 = 0x02; //pin change mask 0 //允许外部电平中断
PCMSK1 = 0x02; //pin change mask 1
PCMSK2 = 0x00; //pin change mask
PCICR = 0x03; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
while(1)
{
if(g_ucLoopTimeFlag)
{
g_ucLoopTimeFlag = false;
// disablechip();
KeyScanTask(); //key scan
KeyPressedTask(); //key process
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -