|
在学习 GRBL 中,涉及到很多 AVR 中断、串口等相关知识。参考资料 MAKE:AVR PROGRAMMING
如果单独列出来,会减轻 学习 GRBL 的负担。
*************************
AVR 中断
*************************
使用中断,而不是 polling (轮询方式),
One problem with polling in event loops is that there’s no concept of priority.
Handling multiple jobs at once is where interrupts excel.
Interrupts do just what they sound like—they interrupt the normal flow of the program.
When an interrupt is triggered, all of the variables that you’re using are stashed in memory and then a special function, called an interrupt service routine (ISRs), is run.
There are two flavors of external interrupts: the fancy interrupts, INT0 and INT1, and the pin-change interrupts.
The INT0 interrupt mechanism has more versatile triggering possibilities, allowing you trigger the interrupt on a rising voltage, a falling voltage, any change, or continuously for a low-voltage level.
there are only two of
these type interrupts: INT0 and INT1, on pins PD2 and PD3.
The PCINT system, on the other hand, allows you to treat every I/O pin on the AVR as an interrupt source if you so desire, but only detects changes (not their direction.
The pin-change interrupts are grouped together in banks, so it’s more accurate to say that there’s a pin-change interrupt for the “B” pins, one for the “C” pins, and one for the “D” pins.
We use pinmask registers to select which of the pins from within the bank we want to trigger the interrupt.
ISRs are special routines that run when their interrupt flag is set, and their interrupt vector is called.
这里,vector 表示 8位 无符号数,是地址。
Pin-Change Interrupt Example
ISR(PCINT2_vect){
....
}
void initPinChangeInterrupt18(void){
PCICR |= (1 << PCIE2); /* set pin-change interrupt for D pins */
PCMSK2 |= (1 << PCINT18); /* set mask to look for PCINT18 / PD2 */
// PCMSK2 |= (1 << PD2); /* this will also work for the pin mask */
sei(); /* set (global) interrupt enable bit */
}
we’ll need to tell the AVR which pins we’d like for it to watch specifically.
This is done through the pin mask, which is just a normal 8-bit byte where the corresponding bit is set for each pin we’d like to be able to trigger the interrupt.
So if we want to trigger on PD2 and PD4, we can set the pin mask one of two ways.
We can either use the pins’ PCINTxx aliases, or the normal PD2 type pin references.
For instance:
PCMSK2 |= ( (1 << PCINT18) | (1 << PCINT20) );
or
PCMSK2 |= ( (1 << PD2) | (1 << PD4) );
both do the same thing: configure the pin-change interrupt to trigger if either PD2 and PD4 changes state.
结论,3段式
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|