脉冲发生器, 100hz_2路互补

[复制链接]
 楼主| 34af9uc 发表于 2025-7-27 21:56 | 显示全部楼层 |阅读模式
  1. /*************脉冲发生器, 100hz_2路互补****************/
  2. /*************EPM240T100C5N************************/
  3. /*************ID:共同学习FPGA***********************/
  4. /*************20250715*****************************/
  5. //系统时钟100Mhz,设定频率100k,定时时间0-511个脉冲,死区时间32个脉冲;
  6. //B+----------------------------------
  7. //        |        |        |        |
  8. //      s1|      s3|      S5|      s7|
  9. //        |---L1---|        |---L2---|
  10. //      s2|      s4|      S6|      s8|
  11. //        |        |        |        |
  12. //B-----------------------------------

  13. //num        |0___________________511|
  14. //num        |__________50%__________|100%
  15. //num        |12345|6789a|12345|6789a|
  16. //L1         |___0-255___|___________|
  17. //L2         |___________|_256-511___|

  18. //s1         |¯¯¯¯¯|_________________|
  19. //s2         |_____|¯¯¯¯¯|___________|
  20. //s3         |_____|¯¯¯¯¯|___________
  21. //s4         |¯¯¯¯¯|_________________|
  22. //s5         |___________|¯¯¯¯¯|_____|
  23. //s6         |_________________|¯¯¯¯¯|
  24. //s7         |___________|¯¯¯¯¯|_____|
  25. //s8         |_________________|¯¯¯¯¯|
  26. `timescale 1ns/10ps
  27. module maichong
  28.                                         (
  29.                                         clk,                //系统时钟100MHz;
  30.                                         res,                //复位信号,低电平有效;
  31.                                         s1,                //桥臂1上管;
  32.                                         s2,                //桥臂1下管;
  33.                                         s3,                //桥臂2上管;
  34.                                         s4,                //桥臂2下管;
  35.                                         s5,                //桥臂3上管;
  36.                                         s6,                //桥臂3下管;
  37.                                         s7,                //桥臂4上管;
  38.                                         s8                 //桥臂4下管;
  39.                                         );

  40. input                                clk;
  41. input                                res;

  42. output reg                s1;
  43. output reg                s2;
  44. output reg                s3;
  45. output reg                s4;
  46. output reg                s5;
  47. output reg                s6;
  48. output reg                s7;
  49. output reg                s8;

  50. reg[8:0]                        num;                //计数器;
  51. reg[8:0]                        power;                //功率;


  52. parameter                siqu                =16;                //死区时间;
  53. parameter                zuixiao                =128;                //最小数50%-100%;
  54. parameter                xiang1                =5;                        //第一相起始点;
  55. parameter                xiang2                =260;                //第二相起始点;

  56. `define                        shu                        (zuixiao +power)
  57. `define                        shu_ban                ((zuixiao +power) >>1)

  58. always@(posedge clk or negedge res)
  59. if(~res)
  60.         begin
  61.         s1                <=0;
  62.         s2                <=0;
  63.         s3                <=0;
  64.         s4                <=0;
  65.         s5                <=0;
  66.         s6                <=0;
  67.         s7                <=0;
  68.         s8                <=0;
  69.         num                <=0;
  70.         power        <=60;
  71.         end

  72. else
  73. begin
  74. /**************************************************/
  75. num                <=num        +1;
  76. /**************************************************/                //第一相

  77.         if((num >xiang1) && (num <(`shu_ban -siqu) ))                        //前半个周期
  78.                 begin
  79.                 s1                <=1;
  80.                 s4                <=1;
  81.                 end
  82.         else
  83.                 begin
  84.                 s1                <=0;
  85.                 s4                <=0;
  86.                 end
  87.                
  88.         if((num >`shu_ban) && ( num <(`shu -siqu)))                                //后半个周期
  89.                 begin
  90.                 s2                <=1;
  91.                 s3                <=1;
  92.                 end
  93.         else
  94.                 begin
  95.                 s2                <=0;
  96.                 s3                <=0;
  97.                 end
  98. /**************************************************/                //第二相
  99.         if((num >xiang2) && (num <(xiang2 +(`shu_ban -siqu)) ))                        //前半个周期
  100.                 begin
  101.                 s5                <=1;
  102.                 s8                <=1;
  103.                 end
  104.         else
  105.                 begin
  106.                 s5                <=0;
  107.                 s8                <=0;
  108.                 end

  109.         if((num >(xiang2 +(`shu_ban -siqu)) ) && (num <(xiang2 +(`shu -siqu))) )                                //后半个周期
  110.                 begin
  111.                 s6                <=1;
  112.                 s7                <=1;
  113.                 end
  114.         else
  115.                 begin
  116.                 s6                <=0;
  117.                 s7                <=0;
  118.                 end


  119. /**************************************************/

  120. end
  121. endmodule

  122. /******************testbench of maichong***********/
  123. module maichong_tb;
  124. reg                clk, res;
  125. wire        s1, s2, s3, s4, s5, s6, s7, s8;
  126. maichong        maichong
  127.                                         (
  128.                                         .clk(clk),                //系统时钟100MHz;
  129.                                         .res(res),                //复位信号,低电平有效;
  130.                                         .s1(s1),                //桥臂1上管;
  131.                                         .s2(s2),                //桥臂1下管;
  132.                                         .s3(s3),                //桥臂2上管;
  133.                                         .s4(s4),                //桥臂2下管;
  134.                                         .s5(s5),                //桥臂3上管;
  135.                                         .s6(s6),                //桥臂3下管;
  136.                                         .s7(s7),                //桥臂4上管;
  137.                                         .s8(s8)                 //桥臂4下管;
  138.                                         );

  139. initial
  140. begin
  141.                                 clk                <=0;
  142.                                 res                <=0;
  143.                 #20                res                <=1;
  144.                 #10000        $stop;
  145.                
  146. end
  147. always                #5        clk                <=~clk;
  148. endmodule
  149. /**************************************************/
  150. /**************************************************/
  151. /**************************************************/
  152. /**************************************************/

























319

主题

487

帖子

63

粉丝
快速回复 在线客服 返回列表 返回顶部