|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
module PWM( clk,//系统时钟
1 ]6 V5 Z% O$ H) V7 v, ^ Lrst_n,//复位
6 Q2 P, p8 @; ~' q5 y8 J9 c3 Q$ ncs,//DSP片选! p* f# T$ t" b, O1 _2 D
fault_and_out,//故障! q+ I! Q3 ?- w6 g, F! I! x
overcurrent,//过流,逐波限流
+ \ | Y" ]* R" q- MePWM_sync,//同步,即EPWM时基起始计时点
+ Y- l1 j% t# n* a, qpwm1,pwm2,pwm3,pwm4,//PWM输入 pwm1_out,pwm2_out,pwm3_out,pwm4_out//PWM输出 e+ T* E U7 v) r+ h
);; M8 {+ e( o- H K' b1 |6 X
input clk;//系统时钟 30M
/ u' \! k9 N$ I8 Finput rst_n;//复位 低电平有效
. P, G2 B! a1 P2 E* F# f! v" ]input cs,fault_and_out,ePWM_sync;//同步 片选 严重故障信号, v' _0 Q7 @3 R8 ^) h/ C% u8 D
input overcurrent;//过流
9 \1 Z' w) l. {/ _8 \& Ninput pwm1,pwm2,pwm3,pwm4;//PWM输入
! z& i# Z# ]4 ^- H2 j" z9 Noutput pwm1_out,pwm2_out,pwm3_out,pwm4_out;//PWM输出
7 A3 w! B5 |1 ^7 F- Z% E0 F; preg pwm1_out,pwm2_out,pwm3_out,pwm4_out;: H) U7 }! i6 q2 o
7 x$ M: J9 t( g- D
parameter CLOSE_ALL=4'b0001,//关闭4个管子 独热键编码状态机
2 D9 y. Z0 r( E2 ?- tCLOSE_OUTSIDE=4'b0010,//关闭外侧2个管子
1 R$ w% J5 u$ z3 M' Y0 m' @//SET_INSIDE=6'b000100,//内侧2个管子强制导通全置1,1字型备用
- A1 P* W, K! H% w. \OPEN_INSIDE=4'b0100,//打开内侧2个管子8 q% i- q* u9 M S& M
OPEN_ALL=4'b1000;//打开内外侧4个管子,正常工作状态* L! D, e$ p$ n( Z( j
reg[3:0] state;//状态机& M, \& O! H* v% {2 M* t
reg[5:0] cnt_close;" B4 S' |! B( k% H. P
reg[5:0] cnt_open;* \) }* l! r' ?7 i- M7 ?5 U
//reg[5:0] cnt_set; //1字型备用& y4 `4 w1 s. p" }0 T$ @
always @(posedge clk or negedge rst_n )//+ R# e; s: D+ q$ n1 k; F6 o( a
begin
. F; }: Z! F7 xif(!rst_n) //复位, D7 h4 R7 N6 i U5 c3 X3 O( i
begin
* R3 `# ]( i5 P9 D7 h' `* m{pwm1_out,pwm2_out,pwm3_out,pwm4_out}<=4'd0;//关闭管子8 d4 P; [& g3 C" z9 f
cnt_close<=6'd0;1 {4 I, P+ L$ w/ g
cnt_open<=6'd0; L2 ^4 R) b- K: i2 L. R9 |
state<=CLOSE_ALL;
- k9 u4 S6 ~' `: c& V/ F% `5 uend& m, N: g. q& _$ I4 l$ b" C% q9 x
else, R# ~+ @7 }4 z9 n; B, A) O
begin
& z* t$ o. J vcase(state)
7 |% M$ ~; i3 b3 d/ X+ R+ F( ECLOSE_ALL:
2 [; `; B; L& e1 E6 K4 d8 qbegin
, ^3 i1 b) B: M8 L) T# Z{pwm1_out,pwm2_out,pwm3_out,pwm4_out}<=4'd0;//初始化状态,关闭管子
" y2 P+ {3 S5 K' T' x* {& Eif(!cs && fault_and_out && overcurrent && ePWM_sync) state<=OPEN_INSIDE;//等待片选选中且无故障进入下一状态
; d) l( v1 Y0 x9 U1 @end1 @' A2 w2 E1 H
OPEN_INSIDE://打开内侧2个管子
" G) ?& e9 d. s: p$ T* ?% @9 mbegin
0 n4 v R- A6 N{pwm2_out,pwm3_out}<={pwm2,pwm3};//打开内侧2个管子2us/ Y9 T4 ]* @8 h4 ^3 z& p
{pwm1_out,pwm4_out}<=2'b00;//关闭外侧2个管子
* q: T) t6 r% i4 J* p2 Rcnt_open<=cnt_open+1'b1; W6 M- |$ o; K8 I# y: a- X
if(cnt_open==6'd63) state<=OPEN_ALL;& P6 f& C+ ~( z1 d9 [
end
. U1 ]4 r, T. i' j6 cOPEN_ALL://打开外侧2个管子,4个管子全部开放: W$ k9 v/ {. ^* ^" y* S' V; ?0 I
begin
+ _3 z9 O# e; e{pwm1_out,pwm2_out,pwm3_out,pwm4_out}<={pwm1,pwm2,pwm3,pwm4};
( k3 R- {8 h# \' p1 zif(cs || !fault_and_out || !overcurrent) state<=CLOSE_OUTSIDE;//故障,关闭管子
% N$ ~0 i. h" f k3 Wend# B2 x4 h' C- x$ k! i
CLOSE_OUTSIDE://关闭外侧2个管子* o/ O; D, O# n3 |2 _! `; j
begin
& k0 f) b" @. ^; C* [* |{pwm2_out,pwm3_out}<={pwm2,pwm3};//内侧2个管子保持( ]' \$ [- i- Q/ S
{pwm1_out,pwm4_out}<=2'd0;//关闭外侧2个管子2US' Y- U% c* `/ [! D* }
cnt_close<=cnt_close+1'b1;* x# a/ r3 z4 V1 _' {+ P+ G
if(cnt_close==6'd63) state<=CLOSE_ALL;1 r: C P5 ?7 T- X
end
2 Q* a3 a" ]9 S' f: x8 y9 X @/*SET_INSIDE://1字型备用$ h9 R5 J7 B4 Q1 R b/ q
begin, _$ S) ~* q R4 V" a
{pwm2_out,pwm3_out}<=2'b11;//内侧2个管子强制导通2us,Q1和Q4结电容充电至E/2
' ^, M t8 |1 N7 d; b( N7 X{pwm1_out,pwm4_out}<=2'd0;//关闭外侧2个管子0 ?; r8 |9 B' ~( M; S2 ?9 I5 C* e: r
cnt_set<=cnt_set+1'b1;
" f2 O( a( C8 h8 [' U+ X* Aif(cnt_set==6'd63) state<=OPEN_INSIDE;
* |: m) ?" H9 {2 S2 f7 s0 [: N. Yend*/
/ C4 A6 Y0 ?* c3 n7 j5 \8 D: a2 qdefault:state<=CLOSE_ALL;
8 t! y7 {0 O- |6 Cendcase( p- ~! t. O' }$ T$ C+ Z* D
end' Y: _8 f0 {/ m% N: P. L% {/ w0 b: E
end
4 V' w2 s @& x- ^) h
& i# r3 \+ c0 `, X$ W2 [endmodule
9 e) D, D$ P6 l; {- i. m
0 `/ x' D5 y, y# b2 d) m8 B7 Q5 X% w仿真时使用的是modelsim-altera,RTL仿真正确,门级仿真看波形以及通过调试发现程序总是停留的OPEN_INSIDE状态,我琢磨好久甚是不透,这里OPEN_INSIDE状态程序不可能有竞争冒险的
' s( g5 z& Z8 g8 r% ?! G |
|