这是我刚刚做的一个小实验(串口中断)。 我用的是6.0版的。 可以在程序中改变波特率 #include "system.h" #include "alt_types.h" #include "altera_avalon_uart_regs.h" #include "sys/alt_irq.h" #include "stdlib.h" void uart_int(void); static void Uart_interrupt(void *context,alt_u32 id);
int main (void) __attribute__ ((weak, alias ("alt_main")));
int alt_main (void) { alt_irq_init(ALT_IRQ_BASE); alt_irq_register(UART_0_IRQ,NULL,Uart_interrupt); uart_int(); while(1); return 0; } void uart_int(void) {
alt_u16 control1; alt_u16 divisor1;
control1 = ALTERA_AVALON_UART_CONTROL_TRDY_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_E_MSK; IOWR_ALTERA_AVALON_UART_CONTROL(UART_0_BASE, control1);
divisor1 = (int)(50000000/(115200+0.5)); //根据需要自己更改 IOWR_ALTERA_AVALON_UART_DIVISOR(UART_0_BASE, divisor1);
} static void Uart_interrupt(void *context,alt_u32 id) { alt_u8 data; alt_u16 status1;
do{status1 = IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);} while (!(status1 & ALTERA_AVALON_UART_STATUS_RRDY_MSK)); data =IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);//接收数据 status1 = ALTERA_AVALON_UART_STATUS_TRDY_MSK; IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, status1); IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE, data); //将接到的信息发出去
IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, 0); }
|