XMEGA-A3BU Xplained - 使用 DMA 轮询确定是否按下按钮 1
XMEGA-A3BU Xplained - Determine if pushbutton 1 is pressed by polling using DMA
作业要求我们在使用轮询按下 SW1 时打开 LED。我相信我正在设置每个端口的方向并正确读取寄存器。但是当我按下 SW1 时没有任何反应。当代码是 运行 查看寄存器中的内容时,无法调试和断点代码。
[HWGuide] 状态:[HWGuide]:http://ww1.microchip.com/downloads/en/DeviceDoc/doc8394.pdf
//LED0 = PR0 (PORTR PIN 0)
//LED1 = PR1 (PORTR PIN 1)
//SW1 = PF1 (PORTF PIN 1)
[数据表] 状态:[数据表]:http://ww1.microchip.com/downloads/en/DeviceDoc/atmel-8362-8-and-16bit-avr-microcontroller-atxmega256a3bu_datasheet.pdf
//PORTR starts at address = 0x07E0
//PORTF starts at address = 0x06A0
[手册] 状态:[手册]:http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8331-8-and-16-bit-AVR-Microcontroller-XMEGA-AU_Manual.pdf
//Data Input Value register on I/O = (Addr) + 0x08 = 0x06A8 (PORTF)
代码:
#define PORTR *((volatile unsigned char *)0x7E0) /* I/O Port Register */
#define PORTF *((volatile unsigned char *)0x6A0) /* I/O Port Register */
#define PORTF_IN *((volatile unsigned char *)0x6A8) //PORTF Input Value Reg
#define PORTR_OUTTGL *((volatile unsigned char *)0x7E7) //LED Toggle Reg
#define ReadReg(port) (port)
#define WriteReg(port, value) (port = value)
int main(void)
{
//set PORTR direction
WriteReg(PORTR, 0xFF);
//set PORTF direction
WriteReg(PORTF, 0x00);
while(1)
{
if((ReadReg(PORTF_IN) == 0xFD)) //if PF1 = 0
{
WriteReg(PORTR_OUTTGL, 0x3); //toggle LEDs
{
{
}
我希望寄存器读取 (0x02)0000 0010 或相反的 (0xFD)1111 1101,如果按下按钮,LED 会打开或关闭。
使用位操作来隔离我试图轮询的位。不知道其他位可以设置成什么。
int main(void)
{
//set PORTR direction
WriteReg(PORTR, 0xFF);
//set PORTF direction
WriteReg(PORTF, 0xF9);
while(1)
{
char statusPF1 = (ReadReg(PORTF_IN) & 0x02) >> 1;
char statusPF2 = (ReadReg(PORTF_IN) & 0x04) >> 2;
if((statusPF1 == 0)) //if PF1 = 0
{
WriteReg(PORTR_OUTTGL, 0x01); //toggle LED0
_delay_ms(1000);
}
if((statusPF2 == 0)) //if PF2 = 0
{
WriteReg(PORTR_OUTTGL, 0x02); //toggle LED1
_delay_ms(1000);
}
if ((statusPF1 != 0) && (statusPF2 != 0))
{
_blinkLEDs();
}
}
}
作业要求我们在使用轮询按下 SW1 时打开 LED。我相信我正在设置每个端口的方向并正确读取寄存器。但是当我按下 SW1 时没有任何反应。当代码是 运行 查看寄存器中的内容时,无法调试和断点代码。
[HWGuide] 状态:[HWGuide]:http://ww1.microchip.com/downloads/en/DeviceDoc/doc8394.pdf
//LED0 = PR0 (PORTR PIN 0)
//LED1 = PR1 (PORTR PIN 1)
//SW1 = PF1 (PORTF PIN 1)
[数据表] 状态:[数据表]:http://ww1.microchip.com/downloads/en/DeviceDoc/atmel-8362-8-and-16bit-avr-microcontroller-atxmega256a3bu_datasheet.pdf
//PORTR starts at address = 0x07E0
//PORTF starts at address = 0x06A0
[手册] 状态:[手册]:http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8331-8-and-16-bit-AVR-Microcontroller-XMEGA-AU_Manual.pdf
//Data Input Value register on I/O = (Addr) + 0x08 = 0x06A8 (PORTF)
代码:
#define PORTR *((volatile unsigned char *)0x7E0) /* I/O Port Register */
#define PORTF *((volatile unsigned char *)0x6A0) /* I/O Port Register */
#define PORTF_IN *((volatile unsigned char *)0x6A8) //PORTF Input Value Reg
#define PORTR_OUTTGL *((volatile unsigned char *)0x7E7) //LED Toggle Reg
#define ReadReg(port) (port)
#define WriteReg(port, value) (port = value)
int main(void)
{
//set PORTR direction
WriteReg(PORTR, 0xFF);
//set PORTF direction
WriteReg(PORTF, 0x00);
while(1)
{
if((ReadReg(PORTF_IN) == 0xFD)) //if PF1 = 0
{
WriteReg(PORTR_OUTTGL, 0x3); //toggle LEDs
{
{
}
我希望寄存器读取 (0x02)0000 0010 或相反的 (0xFD)1111 1101,如果按下按钮,LED 会打开或关闭。
使用位操作来隔离我试图轮询的位。不知道其他位可以设置成什么。
int main(void)
{
//set PORTR direction
WriteReg(PORTR, 0xFF);
//set PORTF direction
WriteReg(PORTF, 0xF9);
while(1)
{
char statusPF1 = (ReadReg(PORTF_IN) & 0x02) >> 1;
char statusPF2 = (ReadReg(PORTF_IN) & 0x04) >> 2;
if((statusPF1 == 0)) //if PF1 = 0
{
WriteReg(PORTR_OUTTGL, 0x01); //toggle LED0
_delay_ms(1000);
}
if((statusPF2 == 0)) //if PF2 = 0
{
WriteReg(PORTR_OUTTGL, 0x02); //toggle LED1
_delay_ms(1000);
}
if ((statusPF1 != 0) && (statusPF2 != 0))
{
_blinkLEDs();
}
}
}