通用IO口常用头文件:#include"altera_avalon_pio_regs.h" 和 #include "io.h"其定义如下:
#ifndef __ALTERA_AVALON_PIO_REGS_H__
#define __ALTERA_AVALON_PIO_REGS_H__
#include<io.h>
//IO口读写。数据宽度最大一次32位读写
#defineIOADDR_ALTERA_AVALON_PIO_DATA(base)__IO_CALC_ADDRESS_NATIVE(base, 0)
#defineIORD_ALTERA_AVALON_PIO_DATA(base)IORD(base, 0)
#define IOWR_ALTERA_AVALON_PIO_DATA(base,data)IOWR(base, 0, data)
//IO口方向控制。针对定义的双向口而言的。1为输出端口,0为输入端口
#defineIOADDR_ALTERA_AVALON_PIO_DIRECTION(base)__IO_CALC_ADDRESS_NATIVE(base, 1)
#defineIORD_ALTERA_AVALON_PIO_DIRECTION(base)IORD(base, 1)
#define IOWR_ALTERA_AVALON_PIO_DIRECTION(base,data) IOWR(base, 1, data)
//IO口中断使能。按位来使能,1表示使能。
#defineIOADDR_ALTERA_AVALON_PIO_IRQ_MASK(base)__IO_CALC_ADDRESS_NATIVE(base, 2)
#defineIORD_ALTERA_AVALON_PIO_IRQ_MASK(base)IORD(base, 2)
#define IOWR_ALTERA_AVALON_PIO_IRQ_MASK(base,data) IOWR(base, 2,data)
//IO口边沿捕获寄存器函数
#defineIOADDR_ALTERA_AVALON_PIO_EDGE_CAP(base)__IO_CALC_ADDRESS_NATIVE(base, 3)
#defineIORD_ALTERA_AVALON_PIO_EDGE_CAP(base)IORD(base, 3)
#define IOWR_ALTERA_AVALON_PIO_EDGE_CAP(base,data) IOWR(base, 3,data)
#define ALTERA_AVALON_PIO_DIRECTION_INPUT 0
#define ALTERA_AVALON_PIO_DIRECTION_OUTPUT 1
#endif
#ifndef __IO_H__
#define __IO_H__
#include "alt_types.h"
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef SYSTEM_BUS_WIDTH
#error SYSTEM_BUS_WIDTH undefined
#endif
#define __IO_CALC_ADDRESS_DYNAMIC(BASE,OFFSET)
((void *)(((alt_u8*)BASE) +(OFFSET)))
#define IORD_32DIRECT(BASE, OFFSET)
__builtin_ldwio (__IO_CALC_ADDRESS_DYNAMIC((BASE), (OFFSET)))
#define IORD_16DIRECT(BASE, OFFSET)
__builtin_ldhuio (__IO_CALC_ADDRESS_DYNAMIC((BASE), (OFFSET)))
#define IORD_8DIRECT(BASE, OFFSET)
__builtin_ldbuio (__IO_CALC_ADDRESS_DYNAMIC((BASE), (OFFSET)))
#define IOWR_32DIRECT(BASE, OFFSET, DATA)
__builtin_stwio (__IO_CALC_ADDRESS_DYNAMIC((BASE), (OFFSET)), (DATA))
#define IOWR_16DIRECT(BASE, OFFSET, DATA)
__builtin_sthio (__IO_CALC_ADDRESS_DYNAMIC((BASE), (OFFSET)), (DATA))
#define IOWR_8DIRECT(BASE, OFFSET, DATA)
__builtin_stbio (__IO_CALC_ADDRESS_DYNAMIC((BASE), (OFFSET)), (DATA))
#define __IO_CALC_ADDRESS_NATIVE(BASE,REGNUM)
((void *)(((alt_u8*)BASE) + ((REGNUM) *(SYSTEM_BUS_WIDTH/8))))
#define IORD(BASE, REGNUM)
__builtin_ldwio (__IO_CALC_ADDRESS_NATIVE((BASE), (REGNUM)))
#define IOWR(BASE, REGNUM, DATA)
__builtin_stwio (__IO_CALC_ADDRESS_NATIVE((BASE), (REGNUM)), (DATA))
#ifdef __cplusplus
}
#endif
#endif
下面以一个按健中断响应程序说明IO口中断的使用
#ifndef EX_INT_ZX_H_
#define EX_INT_ZX_H_
extern unsigned char leddata[8];
extern unsigned int cnt;
void Int0_ISR (void * context,alt_u32id)//中断服务程序
{
cnt++;
leddata[7]=8;
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(INT0_PIO_BASE,0);//清状态寄存器,为下次中断作准备
//IOWR_ALTERA_AVALON_PIO_IRQ_MASK(INT0_PIO_BASE,0x0);//关中断
}
void Int0_init(void)//中断初始化函数
{
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(INT0_PIO_IRQ,0x0);//设置边沿捕获寄存器函数,用来重新设定寄存器的值,一般在读取之后会重新设定为0;
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(INT0_PIO_BASE,0x01);//中断使能函数。按位来使能,0x01表示最低位使能。
//IOWR_ALTERA_AVALON_PIO_DIRECTION(INT0_PIO_BASE,0x00);//端口方向为输入
alt_irq_register(INT0_PIO_IRQ,0,Int0_ISR);//中断注册Int0_ISR
}
#endif
每按一次键盘cnt加1,实际可能加N次,因为这里没有进行按键去抖。
IO口使用的另一种方法
Altera 的用法:
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xff)
或 IOWR(LED_PIO_BASE, 0, 0xff)
我们习愦的单片机用法
#define LED *(unsignedint*)LED_PIO_BASE//这样定义后就可以跟我们习愦的单片机使用IO口一样,用法如下:
LED =0xff;
这是对输出端口而言的,我也不知为什么对输入端口读取为撒行不通,还是只能用Altera的IORD_ALTERA_AVALON_PIO_DATA(base)或IORD(base, 0)