ZYNQ7000开发板(zedboard)定时器中断实验——LED闪烁

vidoda设计

和gpio_mio实验硬件相同
ZYNQ7000开发板(zedboard)定时器中断实验——LED闪烁_第1张图片
ZYNQ7000开发板(zedboard)定时器中断实验——LED闪烁_第2张图片
ZYNQ7000开发板(zedboard)定时器中断实验——LED闪烁_第3张图片
ZYNQ7000开发板(zedboard)定时器中断实验——LED闪烁_第4张图片
PS-PL Configuration ->general
ZYNQ7000开发板(zedboard)定时器中断实验——LED闪烁_第5张图片

SDK设计


#include 
#include "xparameters.h"
#include "xgpiops.h"
#include "xstatus.h"
#include "xplatform_info.h"
#include 
#include "sleep.h"
#include "xscutimer.h"
#include "xscugic.h"

#define GPIO_DEVICE_ID		XPAR_XGPIOPS_0_DEVICE_ID
#define MIO7_LED            7    //ps端LED
#define LED_DELAY		10000000
#define TIMER_DEVICE_ID		XPAR_XSCUTIMER_0_DEVICE_ID
#define TIMER_IRPT_INTR		XPAR_SCUTIMER_INTR

#define TIMER_LOAD_VALUE	0x3F83C3F    //0.2s 闪烁一次,0.2*1000_000_000/(1000/333) - 1 = 3F83C3F
#define INTC_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID


XGpioPs Gpio;	/* The driver instance for GPIO Device. */
XScuTimer TimerInstance;
XScuGic IntcInstance;

//	中断处理函数
void TimerIntrHandler(void *CallBackRef)
{
	//LED 状态,用于控制 LED 灯状态翻转
	static int led_state = 0;
	XScuTimer *timer_ptr = (XScuTimer *) CallBackRef;
	if(led_state == 0)
		led_state = 1;
	else
		led_state = 0;
	//向指定引脚写入数据: 0 或 1
	XGpioPs_WritePin(&Gpio, MIO7_LED,led_state);
	//清除定时器中断标志
	XScuTimer_ClearInterruptStatus(timer_ptr);
}
//	mio初始化
void mio_init()
{
	XGpioPs_Config *ConfigPtr;
	//初始化gpio驱动
	//根据器件id,查找器件配置信息
	ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
	//初始化gpio参数
	XGpioPs_CfgInitialize(&Gpio, ConfigPtr,
						ConfigPtr->BaseAddr);

	//gpio方向设置为输出(0:输入、1:输出)
	XGpioPs_SetDirectionPin(&Gpio, MIO7_LED, 1);
//	XGpioPs_SetDirectionPin(&Gpio, 50, 0);

	//设置输出使能(0:关闭、1:打开)
	XGpioPs_SetOutputEnablePin(&Gpio, MIO7_LED, 1);
//	XGpioPs_WritePin(&Gpio, MIO7_LED, 0x0);
}
//	定时器中断初始化
void TimerSetupIntrSystem(XScuGic *IntcInstancePtr, XScuTimer *TimerInstancePtr, u16 TimerIntrId)
{
	XScuGic_Config *IntcConfig;
//	定时器中断初始化
	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
	XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
						IntcConfig->CpuBaseAddress);

	Xil_ExceptionInit();
//	关联中断函数
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
					(Xil_ExceptionHandler)XScuGic_InterruptHandler,
					IntcInstancePtr);
//	关联中断函数
	XScuGic_Connect(IntcInstancePtr, TimerIntrId,
					(Xil_ExceptionHandler)TimerIntrHandler,
					(void *)TimerInstancePtr);
//	使能GIC定时器中断
	XScuGic_Enable(IntcInstancePtr, TimerIntrId);
//	使能定时器中断
	XScuTimer_EnableInterrupt(TimerInstancePtr);
	Xil_ExceptionEnable();
}

// 	定时器初始化
int timer_init(XScuGic *IntcInstancePtr, XScuTimer * TimerInstancePtr, u16 TimerDeviceId, u16 TimerIntrId)
{
	int Status;
//	int LastTimerExpired = 0;
	XScuTimer_Config *ConfigPtr;
//	初始化私有定时器
	ConfigPtr = XScuTimer_LookupConfig(TimerDeviceId);
	XScuTimer_CfgInitialize(TimerInstancePtr, ConfigPtr,
						ConfigPtr->BaseAddr);
//	硬件自测
	Status = XScuTimer_SelfTest(TimerInstancePtr);
		if (Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
//		设置中断系统
	TimerSetupIntrSystem(IntcInstancePtr,
							TimerInstancePtr, TimerIntrId);
//		使能自动重下载模式
	XScuTimer_EnableAutoReload(TimerInstancePtr);
//		加载定时器计数器
	XScuTimer_LoadTimer(TimerInstancePtr, TIMER_LOAD_VALUE);
//		开始定时器计数
	XScuTimer_Start(TimerInstancePtr);

	return 0;
}

int main()
{
//	u32 Data;

	printf("***hello,hello,hello,hello,hello,hello***\r\n");
	mio_init();
	timer_init(&IntcInstance, &TimerInstance,TIMER_DEVICE_ID, TIMER_IRPT_INTR);

	XScuTimer_Start(&TimerInstance); //启动定时器
	return 0;
}


你可能感兴趣的:(ZYNQ)