loki3118
Junior Member level 2
- Joined
- Jun 26, 2013
- Messages
- 23
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 238
Hello,
I recently switched from VHDL to Verilog to more utilize my background in programming in C++. I was given a project in which i need to implement PWM on a FPGA. I've been trying my best to do my research and look at examples of PWM that are floating around the internet. I written some code that I believe does what I would like, however Xilinx ISE compiler shot down my hopes.
If anyone could provide some feedback regarding the code that I have written it would be much appreciated. There are lots of errors with my code but at this moment I am reading up on the proper syntax of the Verilog.
Project specifications:
Xilinx FPGA Spartan-6
ISE Webpack
Switching Frequency of 5MHz (next iteration would duplicate this code to allow for 4 phases at 5MHz each)
I recently switched from VHDL to Verilog to more utilize my background in programming in C++. I was given a project in which i need to implement PWM on a FPGA. I've been trying my best to do my research and look at examples of PWM that are floating around the internet. I written some code that I believe does what I would like, however Xilinx ISE compiler shot down my hopes.
If anyone could provide some feedback regarding the code that I have written it would be much appreciated. There are lots of errors with my code but at this moment I am reading up on the proper syntax of the Verilog.
Project specifications:
Xilinx FPGA Spartan-6
ISE Webpack
Switching Frequency of 5MHz (next iteration would duplicate this code to allow for 4 phases at 5MHz each)
Code:
module pwm(
input clk,
input write_data[31:0],
input cs,
input write_n,
input addr,
input clr_n,
output read_data[31:0],
output pwm_out
);
reg period;
reg counter;
wire [31:0] pulse_width;
wire [31:0] off;
wire [31:0] period_en;
wire [31:0] pulse_width_en;
// Define period and pulse_width registers
always @(posedge clk or negedge clr_n)
begin
if (clr_n == 0)
begin
period <= 32'h // Requires a period of 0.03 nanosecond
pulse_width <= 32'h00000000;
end
else
begin
if (period_en)
period <=> // Same period issue
else
period <= period;=""> // Same period issue
if (pulse_width_en)
pulse_width <= write_data[31:0];
else
pulse_width <= pulse_width;
end
end
// Read access for period and pulse_width registers
if (addr == 0)
read_data = period;
else
read_data = pulse_width;
//Counter
always @(posedge clk or negedge clr_n)
begin
if (clr_n == 0)
counter <= 0;
else
if (counter >= period - 1) // count from 0 to (period-1)
counter <= 0;
else
counter <= counter="" +="" 1;// Is this proper syntax?
end
// Changes output while counter is less than pulse_width
always @(posedge clk or negedge clr_n)
begin
if (clr_n == 0)
off <= 0;
else
if (counter >= pulse_width)
off <= 1;
else
if (counter == 0)
off <= 0;
else
off <= off;
end
//Enables writing to period and pulse_width registers
assign period_en = cs & !write_n & !addr;
assign pulse_width_en = cs & !write_n & addr;
// Output
assign pwm_out = !off;
endmodule