产生频率可变的正弦波——PS控制频率字
1.代码 2.结果 3.步骤 4.函数介绍 5.分析与感想 6.添加自己的函数
一.代码:
#include "xparameters.h"
#include "xgpio.h"
#include "xuartps.h" // if PS uart is used
XUartPs Uart_PS; /* Instance of the UART Device */
//====================================================
// 在 UART获取频率值
int GetNum (){
uint8_t byte;
uint8_t uartBuffer[16];
int validNumber;
int digitIndex;
int digit, number, sign;
int c;
while(1){
byte = 0x00;
digit = 0;
digitIndex = 0;
number = 0;
validNumber = TRUE;
//get bytes from uart until RETURN is entered
while(byte != 0x0d){
while (!XUartPs_IsReceiveData(STDIN_BASEADDRESS));
byte = XUartPs_ReadReg(STDIN_BASEADDRESS,
XUARTPS_FIFO_OFFSET);
uartBuffer[digitIndex] = byte;
XUartPs_Send(&Uart_PS, &byte, 1);
digitIndex++;
}
//calculate number from string of digits
for(c = 0; c < (digitIndex - 1); c++){
if(c == 0){
//check if first byte is a "-"
if(uartBuffer[c] == 0x2D){
sign = -1;
digit = 0;
}
//check if first byte is a digit
else if((uartBuffer[c] >> 4) == 0x03){
sign = 1;
digit = (uartBuffer[c] & 0x0F);
}
else
validNumber = FALSE;
}
else{
//check byte is a digit
if((uartBuffer[c] >> 4) == 0x03){
digit = (uartBuffer[c] & 0x0F);
}
else
validNumber = FALSE;
}
number = (number * 10) + digit;
}
number *= sign;
if(validNumber == TRUE){
print("\r\n");
return number; //number of bytes
}
print("This is not a valid number.\n\r");
}
}
int main (void)
{
XGpio freq_inn,sws;
int sws_check;
unsigned long int numfreq; // uart接收的频率值
unsigned long int k; //计算后的频率字
// AXI GPIO Initialization
XGpio_Initialize(&sws, XPAR_SWITCHES_DEVICE_ID);
XGpio_SetDataDirection(&sws, 1, 0xffffffff); //通道1, 0xfff...1为input
XGpio_Initialize(&freq_inn, XPAR_FREQWORD_DEVICE_ID);
XGpio_SetDataDirection(&freq_inn, 1, 0); // 0为output
// UART initialization
int Status;
XUartPs_Config *Config;
Config = XUartPs_LookupConfig(XPAR_PS7_UART_0_DEVICE_ID);
if (NULL == Config) {
return XST_FAILURE;
}
Status = XUartPs_CfgInitialize(&Uart_PS, Config, Config->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
xil_printf("等待输入频率值\n 1*10^6:1M\n 1*10^7:10M\n");
功能实现//
while(1){
int reg01=43; // 2^32--32位计数器
int fclk=1; //时钟频率 上下同除100M
do{
numfreq= GetNum(); // uart 输入的数字为 频率值
k = (numfreq * reg01)/fclk; //计算频率字
}while(numfreq == 0);
xil_printf("频率值:%d \n",numfreq);
xil_printf("频率字: %d \n",k);
XGpio_DiscreteWrite(&freq_inn, 1, numfreq);//将获取的频率字传入freqword对应的GPIO
sws_check=XGpio_DiscreteRead(&sws, 1); //读取按键状态,随时准备跳出循环
if((sws_check & 0x03)==0x03){
break;
}
}
xil_printf("-- end process --\r\n");
return 0;
}
二.结果:
UART串口值:输入频率值,计算出频率字并显示
ILA显示正弦波:输入频率 100M
1M:对应100个采样点(5567-5467)
2M:对应50个采样点(5524-5474)
10M:对应10个采样点(5539-5529)
三.步骤:
(1)创建block design
1.点击create block design
2.添加zynq,然后 Run Block Automation,保留UART 和GPIO的配置