samfaith
Newbie level 1
- Joined
- Jun 25, 2013
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 31
Please I will like to get to see how I can display the following on 7 segment displays:
a.)the inputs KP and KI,
b.)the output sample(15:8)
module controlpi(CLOCK_50, KEY, A, B, HEX0, HEX1, HEX2, HEX3, LED, SW, rotation, KP, KI,TxD);
input [1:0] SW;
input CLOCK_50;
input [1:0] KEY;
input A, B;
output [6:0] HEX3;
output [6:0] HEX2;
output [6:0] HEX1;
output [6:0] HEX0;
output [7:0] LED;
INPUT [5:2] KP;
INPUT [9:6] KI;
output reg [1:0] rotation;
output wire TxD;//output to the transmitter serial
////////serial transmitter RS232R///////////////////
wire TxD_start;
wire BaudTick;
wire TxD_busy;
//Baud generator
divisor_freq gen_baud(.clk(CLOCK_50), .freq_base(32'd108), .freq_output(BaudTick));//fb=109-1 ... baud=229358
//Clock to start transmission
divisor_freq clk_TxDstart(.clk(CLOCK_50), .freq_base(32'd350000), .freq_output(TxD_start)); //fb=124999=200Hz
//instance Tx
async_transmitter Transmitter(.BaudTick(BaudTick), .TxD_start(TxD_start), .TxD_data(degrees[7:0]),
.TxD(TxD), .TxD_busy(TxD_busy));
/////////////////////////////////////////////////
//Instance sampling clock
wire clk_mk;
freq_divider clock_20khz(.clk(CLOCK_50), .freq_base(32'd1250), .freq_output(clk_mk));//aprox 20k
//Encoder instance
wire [16:0] degrees;
wire [16:0] actual_position;
quadrature_encoder degrees_indicator(.clk(CLOCK_50), .quadA(A), .quadB(B), .clear(~KEY[0]),
.count(actual_position));
//////SET POINT 4 positions///////
reg [16:0]set_point;
always @(posedge CLOCK_50)
case(SW[1:0])
2'b00: set_point<=17'd699; //30 degrees
2'b01: set_point<=17'd2096; //90 degrees
2'b10: set_point<=17'd4192; //180 degrees
2'b11: set_point<=17'd6288; //270 degrees
default: set_point<=17'd1048; //45 degrees
endcase
//////error////
reg [16:0] e_k_signed;
always @(posedge CLOCK_50)
e_k_signed<=(actual_position - set_point); // e(k)=R(s)-Y(s)
////Absolute value of the error////
reg [16:0] e_k_unsigned;
always @(posedge CLOCK_50)
if (e_k_signo[16]==1'b1) //if the error is negative
e_k_unsigned<=((~e_k_signed)+(1'b1)); //signed bit
else
e_k_unsigned<=(e_k_signed);
//////////////controller///////////////
wire [15:0]sample;
pidcontrolalgorithm PID_controller (.clk_mk(clk_mk),.CLOCK_50(CLOCK_50),.error(e_k_unsigned[16:0]),
.m_k_out(sample[15:0]), .reset(~KEY[0]), ,
.KP(KP[5:2]), .KI(KI[9:6]));
/////Instance PWM////
wire correction;
pwm modulation_pwm(.clk(CLOCK_50), .pwm_in(sample[15:0]), .pwm_out(correction));
wire [1:0] output_PWM_IZQ;
wire [1:0] output_PWM_DER;
assign output_PWM_IZQ[1:0]={correction,1'b0}; //concatenation for rotation
assign output_PWM_DER[1:0]={1'b0,correction}; //concatenation for rotation
///Logic of direction ///
always @(posedge CLOCK_50)
begin
if ((actual_position<17'd50)&(e_k_signed<17'd50))
rotation<=2'd0;
else if ((e_k_signed>17'd10)&(e_k_signed<17'd8384))//if the error is 0.4> e_k_1> 360
rotation<=output_PWM_IZQ;
else if ((e_k_signed<17'b1_1111_1111_1111_0110)&(e_k_signed>17'b1101_1111_0100_0000))//if the error is -360 <e_k_1 <-0.4
rotation<=output_PWM_DER;
else
rotation<=2'd0;
end
assign LED=SAMPLE[15:8];
endmodule
a.)the inputs KP and KI,
b.)the output sample(15:8)
module controlpi(CLOCK_50, KEY, A, B, HEX0, HEX1, HEX2, HEX3, LED, SW, rotation, KP, KI,TxD);
input [1:0] SW;
input CLOCK_50;
input [1:0] KEY;
input A, B;
output [6:0] HEX3;
output [6:0] HEX2;
output [6:0] HEX1;
output [6:0] HEX0;
output [7:0] LED;
INPUT [5:2] KP;
INPUT [9:6] KI;
output reg [1:0] rotation;
output wire TxD;//output to the transmitter serial
////////serial transmitter RS232R///////////////////
wire TxD_start;
wire BaudTick;
wire TxD_busy;
//Baud generator
divisor_freq gen_baud(.clk(CLOCK_50), .freq_base(32'd108), .freq_output(BaudTick));//fb=109-1 ... baud=229358
//Clock to start transmission
divisor_freq clk_TxDstart(.clk(CLOCK_50), .freq_base(32'd350000), .freq_output(TxD_start)); //fb=124999=200Hz
//instance Tx
async_transmitter Transmitter(.BaudTick(BaudTick), .TxD_start(TxD_start), .TxD_data(degrees[7:0]),
.TxD(TxD), .TxD_busy(TxD_busy));
/////////////////////////////////////////////////
//Instance sampling clock
wire clk_mk;
freq_divider clock_20khz(.clk(CLOCK_50), .freq_base(32'd1250), .freq_output(clk_mk));//aprox 20k
//Encoder instance
wire [16:0] degrees;
wire [16:0] actual_position;
quadrature_encoder degrees_indicator(.clk(CLOCK_50), .quadA(A), .quadB(B), .clear(~KEY[0]),
.count(actual_position));
//////SET POINT 4 positions///////
reg [16:0]set_point;
always @(posedge CLOCK_50)
case(SW[1:0])
2'b00: set_point<=17'd699; //30 degrees
2'b01: set_point<=17'd2096; //90 degrees
2'b10: set_point<=17'd4192; //180 degrees
2'b11: set_point<=17'd6288; //270 degrees
default: set_point<=17'd1048; //45 degrees
endcase
//////error////
reg [16:0] e_k_signed;
always @(posedge CLOCK_50)
e_k_signed<=(actual_position - set_point); // e(k)=R(s)-Y(s)
////Absolute value of the error////
reg [16:0] e_k_unsigned;
always @(posedge CLOCK_50)
if (e_k_signo[16]==1'b1) //if the error is negative
e_k_unsigned<=((~e_k_signed)+(1'b1)); //signed bit
else
e_k_unsigned<=(e_k_signed);
//////////////controller///////////////
wire [15:0]sample;
pidcontrolalgorithm PID_controller (.clk_mk(clk_mk),.CLOCK_50(CLOCK_50),.error(e_k_unsigned[16:0]),
.m_k_out(sample[15:0]), .reset(~KEY[0]), ,
.KP(KP[5:2]), .KI(KI[9:6]));
/////Instance PWM////
wire correction;
pwm modulation_pwm(.clk(CLOCK_50), .pwm_in(sample[15:0]), .pwm_out(correction));
wire [1:0] output_PWM_IZQ;
wire [1:0] output_PWM_DER;
assign output_PWM_IZQ[1:0]={correction,1'b0}; //concatenation for rotation
assign output_PWM_DER[1:0]={1'b0,correction}; //concatenation for rotation
///Logic of direction ///
always @(posedge CLOCK_50)
begin
if ((actual_position<17'd50)&(e_k_signed<17'd50))
rotation<=2'd0;
else if ((e_k_signed>17'd10)&(e_k_signed<17'd8384))//if the error is 0.4> e_k_1> 360
rotation<=output_PWM_IZQ;
else if ((e_k_signed<17'b1_1111_1111_1111_0110)&(e_k_signed>17'b1101_1111_0100_0000))//if the error is -360 <e_k_1 <-0.4
rotation<=output_PWM_DER;
else
rotation<=2'd0;
end
assign LED=SAMPLE[15:8];
endmodule