系统框图
1 FPGA PWM源码
/================================================================================
`timescale 1ns / 1ps
module ax_pwm
#(
parameter N = 32
)
(
input clk,
input rst,
input[N - 1:0]period,
input[N - 1:0]duty,
output pwm_out
);
reg[N - 1:0] period_r;
reg[N - 1:0] duty_r;
reg[N - 1:0] period_cnt;
reg pwm_r;
assign pwm_out = pwm_r;
always@(posedge clk or posedge rst)
begin
if(rst==1)
begin
period_r <= { N {1'b0} };
duty_r <= { N {1'b0} };
end
else
begin
period_r <= period;
duty_r <= duty;
end
end
always@(posedge clk or posedge rst)
begin
if(rst==1)
period_cnt <= { N {1'b0} };
else
period_cnt <= period_cnt + period_r;
end
always@(posedge clk or posedge rst)
begin
if(rst==1)
begin
pwm_r <= 1'b0;
end
else
begin
if(period_cnt >= duty_r)
pwm_r <= 1'b1;
else
pwm_r <= 1'b0;
end
end
endmodule
ZYNQ PS端控制代码
#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "ax_pwm.h"
#include "xil_io.h"
#include "xparameters.h"
#include "sleep.h"
unsigned int duty;
int main()
{
init_platform();
print("Hello World\n\r");
AX_PWM_mWriteReg(XPAR_AX_PWM_0_S00_AXI_BASEADDR, AX_PWM_S00_AXI_SLV_REG0_OFFSET, 17179);
while (1) {
for (duty = 0x8fffffff; duty < 0xffffffff; duty = duty + 100000) {
AX_PWM_mWriteReg(XPAR_AX_PWM_0_S00_AXI_BASEADDR, AX_PWM_S00_AXI_SLV_REG1_OFFSET, duty);
usleep(100);
}
}
cleanup_platform();
return 0;
}