前面已经实现了网络通讯、PL端的按键读取和点亮LED。这篇我们研究如何处理PS端的IO
4位拨动开关,拨到ON的时候电阻分压为高电平,拨到OFF的时候下拉到地为低电平
MIO44 40 39 31
MIO32
没按下的时候是1,按下后导通到地是0。因此是反相逻辑
PCB上的排列顺序从左到右依次为D9 D8 D7 D6
对应MIO为07 24 25 33
高电平MOS管导通,LED点亮。低电平不导通,LED不亮。
N沟道mos和P沟道mos参考下面链接,可以简单理解为电子开关
https://zhuanlan.zhihu.com/p/654353204
ps端的gpio控制与pl类似,只是函数和变量都多了Ps前缀
比如初始化函数:
XGpioPs_CfgInitialize
下面是实现4个LED跑马灯效果
#define LED_MAX_BLINK 10000
static u32 mio_in_sw1 = 32; /* Switch button */
static u32 mio_in_sw4[4] = {44, 40, 39, 31};
static u32 mio_out_led[4] = {7, 24, 25, 33};
static int ps_gpio_out_led(void)
{
u32 Data;
u32 LedLoop;
XGpioPs Ps_gpio_output;
int i;
int Status;
XGpioPs_Config *ConfigPtr;
ConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);
Status = XGpioPs_CfgInitialize(&Ps_gpio_output, ConfigPtr,
ConfigPtr->BaseAddr);
if (Status != XST_SUCCESS) {
xil_printf("PS Gpio Output Initialization Failed\r\n");
return XST_FAILURE;
}
/*
* Set the direction for the pin to be output and
* Enable the Output enable for the LED Pin.
*/
for(i = 0; i < 4; i++){
XGpioPs_SetDirectionPin(&Ps_gpio_output, mio_out_led[i], 1);
XGpioPs_SetOutputEnablePin(&Ps_gpio_output, mio_out_led[i], 1);
/* Set the GPIO output to be low. */
XGpioPs_WritePin(&Ps_gpio_output, mio_out_led[i], 0x0);
}
for (LedLoop = 0; LedLoop < LED_MAX_BLINK; LedLoop ++) {
for(i = 0; i < 4; i++){
XGpioPs_WritePin(&Ps_gpio_output, mio_out_led[i], 0x1);
Data = XGpioPs_ReadPin(&Ps_gpio_output, mio_out_led[i]);
if (Data != 1 ) {
return XST_FAILURE;
}
delay_ms(100);
XGpioPs_WritePin(&Ps_gpio_output, mio_out_led[i], 0x0);
Data = XGpioPs_ReadPin(&Ps_gpio_output, mio_out_led[i]);
if (Data != 0) {
return XST_FAILURE;
}
delay_ms(20);
}
}
return XST_SUCCESS;
}
程序逻辑:
static u32 mio_in_sw1 = 32;
static u32 mio_in_sw4[4] = {44, 40, 39, 31};
static u32 mio_out_led[4] = {7, 24, 25, 33};
int test_ps_io_in_out(void)
{
u32 data;
u32 data1;
u32 sw1;
XGpioPs Ps_gpio_output;
XGpioPs Ps_gpio_input;
int i;
int Status;
XGpioPs_Config *ConfigPtr;
ConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);
Status = XGpioPs_CfgInitialize(&Ps_gpio_output, ConfigPtr,
ConfigPtr->BaseAddr);
if (Status != XST_SUCCESS) {
xil_printf("PS Gpio output Initialization Failed\r\n");
return XST_FAILURE;
}
for(i = 0; i < 4; i++){
XGpioPs_SetDirectionPin(&Ps_gpio_output, mio_out_led[i], 1);
XGpioPs_SetOutputEnablePin(&Ps_gpio_output, mio_out_led[i], 1);
/* Set the GPIO output to be low. */
XGpioPs_WritePin(&Ps_gpio_output, mio_out_led[i], 0x0);
}
Status = XGpioPs_CfgInitialize(&Ps_gpio_input, ConfigPtr,
ConfigPtr->BaseAddr);
if (Status != XST_SUCCESS) {
xil_printf("PS Gpio input Initialization Failed\r\n");
return XST_FAILURE;
}
for(i = 0; i < 4; i++){
XGpioPs_SetDirectionPin(&Ps_gpio_input, mio_in_sw4[i], 0);//0输入
}
XGpioPs_SetDirectionPin(&Ps_gpio_input, mio_in_sw1, 0);//0输入
while(1){
//sw1按下时为0,没按的时候为1
sw1 = XGpioPs_ReadPin(&Ps_gpio_input, mio_in_sw1);
if(0 == sw1){//sw1按下时4个灯全部点亮
for(i = 0; i < 4; i++){
XGpioPs_WritePin(&Ps_gpio_output, mio_out_led[i], 1);
}
delay_ms(100);
}else{
for(i = 0; i < 4; i++){
data = XGpioPs_ReadPin(&Ps_gpio_input, mio_in_sw4[i]);
data = XGpioPs_ReadPin(&Ps_gpio_input, mio_in_sw4[i]);
XGpioPs_WritePin(&Ps_gpio_output, mio_out_led[i], data);
data1 = XGpioPs_ReadPin(&Ps_gpio_output, mio_out_led[i]);
if (data1 != data ) {
return XST_FAILURE;
}
delay_ms(100);
}
}
}
return XST_SUCCESS;
}
a