当前位置:   article > 正文

FPGA呼吸灯PS PL_fpga ps

fpga ps

去年测试分频器写法的玩具,呼吸得比较喘,所以不叫呼吸灯了,调整占空比区域可以转换成正常呼吸灯。

assign r_LED_pwn = (switch)?(LED_pwn_2):(LED_pwn);//控制亮灭

1.assign r_LED_pwn = (switch)?(LED_pwn_2):(LED_pwn);

        LED_pwn是led_cnt == div_cnt时反转的灯,

        led_cnt每个时钟周期增加一次的计数器;

        div_cnt则是每毫秒增加一次的计数器;

        随着时间的增加,反转速度会越来越慢。

2.LED_pwn_2是led_cnt_2 == (div_max - div_cnt)时反转的灯,

随着时间的增加,反转速度会越来越慢。

3.

  1. module breatheled
  2. #(
  3. parameter time_period = 20,
  4. parameter div_max = 1000_000,
  5. parameter one_route = 2_000_000_000,
  6. parameter cnt_max = ((one_route/time_period)/div_max),
  7. parameter div_width = $clog2(div_max),
  8. parameter cnt_width = $clog2(cnt_max)
  9. )
  10. (
  11. input wire Sys_clk,
  12. input wire Rst_n ,
  13. output wire r_LED_pwn
  14. );
  15. reg [cnt_width-1:0] Change_cnt ;
  16. reg Change_flag;
  17. reg [div_width-1:0] div_cnt ;
  18. reg [div_width-1:0] led_cnt ;
  19. reg [div_width-1:0] led_cnt_2 ;
  20. reg LED_pwn ;
  21. reg LED_pwn_2 ;
  22. reg switch ;
  23. assign r_LED_pwn = (switch)?(LED_pwn_2):(LED_pwn);
  24. //switch
  25. always @(posedge Sys_clk or negedge Rst_n)
  26. begin
  27. if(!Rst_n)
  28. switch <= 1'b0;
  29. else if(div_cnt == div_max -1'b1)
  30. switch <= ~switch;
  31. else
  32. switch <= switch;
  33. end
  34. //change
  35. always @(posedge Sys_clk or negedge Rst_n)
  36. begin
  37. if(!Rst_n)
  38. Change_cnt <= 0;
  39. else if(Change_cnt == cnt_max -1'b1)
  40. Change_cnt <= 0;
  41. else
  42. Change_cnt <= Change_cnt + 1'b1;
  43. end
  44. //flag
  45. always @(posedge Sys_clk or negedge Rst_n)
  46. begin
  47. if(!Rst_n)
  48. Change_flag <= 1'b0;
  49. else if(Change_cnt == cnt_max -1'b1)
  50. Change_flag <= 1'b1;
  51. else
  52. Change_flag <= 1'b0;
  53. end
  54. //div_cnt
  55. always @(posedge Sys_clk or negedge Rst_n)
  56. begin
  57. if(!Rst_n)
  58. div_cnt <= 1'b0;
  59. else if(div_cnt == div_max -1'b1)
  60. div_cnt <= 1'b0;
  61. else if(Change_flag)
  62. div_cnt <= div_cnt + 1'b1;
  63. end
  64. //led_cnt
  65. always @(posedge Sys_clk or negedge Rst_n)
  66. begin
  67. if(!Rst_n)
  68. led_cnt <= 1'b0;
  69. else if(led_cnt == div_cnt)
  70. led_cnt <= 1'b0;
  71. else
  72. led_cnt <= led_cnt + 1'b1;
  73. end
  74. //led
  75. always @(posedge Sys_clk or negedge Rst_n)
  76. begin
  77. if(!Rst_n)
  78. LED_pwn <= 1'b0;
  79. else if(led_cnt == div_cnt)
  80. LED_pwn <= ~LED_pwn;
  81. else
  82. LED_pwn <= LED_pwn ;
  83. end
  84. //led_cnt_2
  85. always @(posedge Sys_clk or negedge Rst_n)
  86. begin
  87. if(!Rst_n)
  88. led_cnt_2 <= 1'b0;
  89. else if(led_cnt_2 == (div_max - div_cnt))
  90. led_cnt_2 <= 1'b0;
  91. else
  92. led_cnt_2 <= led_cnt_2 + 1'b1;
  93. end
  94. //led_2
  95. always @(posedge Sys_clk or negedge Rst_n)
  96. begin
  97. if(!Rst_n)
  98. LED_pwn_2 <= 1'b0;
  99. else if(led_cnt_2 == (div_max - div_cnt))
  100. LED_pwn_2 <= ~LED_pwn_2;
  101. else
  102. LED_pwn_2 <= LED_pwn_2 ;
  103. end
  104. endmodule

 真呼吸灯

    assign r_LED_pwn = (pwn_flag == 0)?

                        ((pwn_cnt > div_1000_cnt)?1'b0:1'b1):

                        ((pwn_cnt > div_1000_cnt)?1'b1:1'b0);

div_1000_cnt 100ns计数器,100周期增加一次。

pwn_cnt 1000*100即100000周期增加一次

pwn_flag 1000*1000*100 100周期反转一次

pwn_flag = 0 随着时间增加,闪烁越来越快。

pwn_flag = 1随着时间增加,闪烁越来越慢。

  1. module pwm_breathe_led
  2. #(
  3. parameter time_period = 20,
  4. parameter div_1000 = 1000,
  5. parameter div_1000_100= 100 ,
  6. parameter div_width = $clog2(div_1000),
  7. parameter cnt_width = $clog2(div_1000_100)
  8. )
  9. (
  10. input wire Sys_clk,
  11. input wire Rst_n ,
  12. output wire r_LED_pwn
  13. );
  14. reg [div_width-1:0] div_1000_cnt;
  15. reg [div_width-1:0] pwn_cnt ;
  16. reg [cnt_width-1:0] div_1000_100_cnt;
  17. reg pwn_flag;
  18. assign r_LED_pwn = (pwn_flag == 0)?
  19. ((pwn_cnt > div_1000_cnt)?1'b0:1'b1):
  20. ((pwn_cnt > div_1000_cnt)?1'b1:1'b0);
  21. //cnt
  22. always @(posedge Sys_clk or negedge Rst_n)
  23. begin
  24. if(!Rst_n)
  25. div_1000_100_cnt <= 0;
  26. else if(div_1000_100_cnt == div_1000_100 - 1'b1)
  27. div_1000_100_cnt <= 0;
  28. else
  29. div_1000_100_cnt <= div_1000_100_cnt + 1'b1;
  30. end
  31. always @(posedge Sys_clk or negedge Rst_n)
  32. begin
  33. if(!Rst_n)
  34. div_1000_cnt <= 0;
  35. else if((div_1000_cnt == div_1000 - 1'b1)
  36. &&(div_1000_100_cnt == div_1000_100 - 1'b1))
  37. div_1000_cnt <= 0;
  38. else if(div_1000_100_cnt == div_1000_100 - 1'b1)
  39. div_1000_cnt <= div_1000_cnt + 1'b1;
  40. else
  41. div_1000_cnt <= div_1000_cnt;
  42. end
  43. always @(posedge Sys_clk or negedge Rst_n)
  44. begin
  45. if(!Rst_n)
  46. pwn_cnt <= 0;
  47. else if((pwn_cnt == div_1000 - 1'b1)
  48. &&(div_1000_cnt == div_1000 - 1'b1)
  49. &&(div_1000_100_cnt == div_1000_100 - 1'b1))
  50. pwn_cnt <= 0;
  51. else if((div_1000_cnt == div_1000 - 1'b1)
  52. &&(div_1000_100_cnt == div_1000_100 - 1'b1))
  53. pwn_cnt <= pwn_cnt + 1'b1;
  54. else
  55. pwn_cnt <= pwn_cnt;
  56. end
  57. //flag
  58. always @(posedge Sys_clk or negedge Rst_n)
  59. begin
  60. if(!Rst_n)
  61. pwn_flag <= 1'b0;
  62. else if((pwn_cnt == div_1000 - 1'b1)
  63. &&(div_1000_cnt == div_1000 - 1'b1)
  64. &&(div_1000_100_cnt == div_1000_100 - 1'b1))
  65. pwn_flag <= ~pwn_flag;
  66. else
  67. pwn_flag <= pwn_flag;
  68. end
  69. endmodule

PS呼吸灯 

  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * Use of the Software is limited solely to applications:
  16. * (a) running on a Xilinx device, or
  17. * (b) that interact with a Xilinx device through a bus or interconnect.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  24. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *
  27. * Except as contained in this notice, the name of the Xilinx shall not be used
  28. * in advertising or otherwise to promote the sale, use or other dealings in
  29. * this Software without prior written authorization from Xilinx.
  30. *
  31. ******************************************************************************/
  32. /*
  33. * helloworld.c: simple test application
  34. *
  35. * This application configures UART 16550 to baud rate 9600.
  36. * PS7 UART (Zynq) is not initialized by this application, since
  37. * bootrom/bsp configures it to baud rate 115200
  38. *
  39. * ------------------------------------------------
  40. * | UART TYPE BAUD RATE |
  41. * ------------------------------------------------
  42. * uartns550 9600
  43. * uartlite Configurable only in HW design
  44. * ps7_uart 115200 (configured by bootrom/bsp)
  45. */
  46. #include <stdio.h>
  47. #include "platform.h"
  48. #include "xil_printf.h"
  49. #include "xparameters.h"
  50. #include "xgpiops.h"
  51. #define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
  52. u32 LED0 = 54;
  53. u32 LED1 = 55;
  54. u32 Bottom = 56;
  55. static XGpioPs GPIO_Decive;
  56. static XGpioPs_Config *GPIO_Device_ConFig;
  57. int GPIO_init_TEST(void);
  58. int main()
  59. {
  60. int LED = 0;
  61. int i;
  62. int j;
  63. int k = 0;
  64. init_platform();
  65. GPIO_init_TEST();
  66. while (1)
  67. {
  68. switch(k)
  69. {
  70. case 0:
  71. for(i=0;i<500;i++){
  72. for(j=0;j<500;j++){
  73. usleep(1);
  74. if(j>i){
  75. XGpioPs_WritePin(&GPIO_Decive,LED0,0x00);}
  76. else{
  77. XGpioPs_WritePin(&GPIO_Decive,LED0,0x01);}}}
  78. k = 1;
  79. break;
  80. case 1:
  81. for(i=0;i<500;i++){
  82. for(j=0;j<500;j++){
  83. usleep(1);
  84. if(j>i){
  85. XGpioPs_WritePin(&GPIO_Decive,LED0,0x01);}
  86. else{
  87. XGpioPs_WritePin(&GPIO_Decive,LED0,0x00);}}}
  88. k = 0;
  89. break;
  90. }
  91. }
  92. cleanup_platform();
  93. return 0;
  94. }
  95. int GPIO_init_TEST(void)
  96. {
  97. int Status;
  98. GPIO_Device_ConFig = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
  99. if (GPIO_Device_ConFig == NULL) {
  100. return XST_FAILURE;
  101. }
  102. Status = XGpioPs_CfgInitialize(&GPIO_Decive,
  103. GPIO_Device_ConFig,
  104. GPIO_Device_ConFig->BaseAddr);
  105. if (Status != XST_SUCCESS) {
  106. return XST_FAILURE;
  107. }
  108. //LED0
  109. XGpioPs_SetDirectionPin(&GPIO_Decive,
  110. LED0,
  111. 0x01);
  112. XGpioPs_SetOutputEnablePin(&GPIO_Decive,
  113. LED0,
  114. 0x01);
  115. //LED1
  116. XGpioPs_SetDirectionPin(&GPIO_Decive,
  117. LED1,
  118. 0x01);
  119. XGpioPs_SetOutputEnablePin(&GPIO_Decive,
  120. LED1,
  121. 0x01);
  122. //Bottom
  123. XGpioPs_SetDirectionPin(&GPIO_Decive,
  124. Bottom,
  125. 0x0);
  126. XGpioPs_SetOutputEnablePin(&GPIO_Decive,
  127. Bottom,
  128. 0x01);
  129. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/791697
推荐阅读
相关标签
  

闽ICP备14008679号