[原创] FRDM-KW41Z驱动74HC595控制8段数码管实现计数器

littleshrimp   2017-4-30 20:07 楼主
写了一个使用FRDM-KW41Z驱动74HC595控制8段数码管实现计数器的程序

ezgif-3-bb867d5d66.gif
数码管板子的原理图
QQ图片20170430195818.png

main.c
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. *   of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. *   list of conditions and the following disclaimer in the documentation and/or
  13. *   other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. *   contributors may be used to endorse or promote products derived from this
  17. *   software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */

  30. #include "fsl_device_registers.h"
  31. #include "fsl_debug_console.h"
  32. #include "fsl_dspi.h"
  33. #include "board.h"
  34. #include "fsl_dspi_edma.h"
  35. #include "fsl_edma.h"
  36. #include "fsl_dmamux.h"

  37. #include "pin_mux.h"
  38. #include "clock_config.h"
  39. #include "led.h"
  40. /*******************************************************************************
  41. * Definitions
  42. ******************************************************************************/
  43. #define EXAMPLE_DSPI_MASTER_BASEADDR SPI0
  44. #define EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR DMAMUX0
  45. #define EXAMPLE_DSPI_MASTER_DMA_BASEADDR DMA0
  46. #define EXAMPLE_DSPI_MASTER_DMA_RX_REQUEST_SOURCE 16U
  47. #define EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE 17U
  48. #define DSPI_MASTER_CLK_SRC DSPI0_CLK_SRC
  49. #define EXAMPLE_DSPI_MASTER_PCS_FOR_INIT kDSPI_Pcs0
  50. #define EXAMPLE_DSPI_MASTER_PCS_FOR_TRANSFER kDSPI_MasterPcs0
  51. #define EXAMPLE_DSPI_DEALY_COUNT 0xfffffU
  52. #define TRANSFER_BAUDRATE 500000U /* Transfer baudrate - 500k */

  53. /*******************************************************************************
  54. * Prototypes
  55. ******************************************************************************/
  56. /* DSPI user callback */
  57. void DSPI_MasterUserCallback(SPI_Type *base, dspi_master_edma_handle_t *handle, status_t status, void *userData);

  58. /*******************************************************************************
  59. * Variables
  60. ******************************************************************************/

  61. dspi_master_edma_handle_t g_dspi_edma_m_handle;
  62. edma_handle_t dspiEdmaMasterRxRegToRxDataHandle;
  63. edma_handle_t dspiEdmaMasterTxDataToIntermediaryHandle;
  64. edma_handle_t dspiEdmaMasterIntermediaryToTxRegHandle;

  65. volatile bool isTransferCompleted = false;
  66. uint32_t loopCounter = 0;
  67. uint32_t counter = 0;
  68. /*******************************************************************************
  69. * Code
  70. ******************************************************************************/

  71. void DSPI_MasterUserCallback(SPI_Type *base, dspi_master_edma_handle_t *handle, status_t status, void *userData)
  72. {
  73.     isTransferCompleted = true;
  74. }
  75. void spi_write_byte(uint8_t *buf,uint16_t len)
  76. {
  77.     dspi_transfer_t masterXfer;
  78.     masterXfer.txData = buf;
  79.     masterXfer.rxData = NULL;
  80.     masterXfer.dataSize = len;
  81.     masterXfer.configFlags = kDSPI_MasterCtar0 | EXAMPLE_DSPI_MASTER_PCS_FOR_TRANSFER | kDSPI_MasterPcsContinuous;
  82.      /* Start master transfer, send data to slave */
  83.     isTransferCompleted = false;
  84.     if (kStatus_Success !=
  85.         DSPI_MasterTransferEDMA(EXAMPLE_DSPI_MASTER_BASEADDR, &g_dspi_edma_m_handle, &masterXfer))
  86.     {
  87.         PRINTF("There is error when start DSPI_MasterTransferEDMA \r\n ");
  88.     }

  89.     /* Wait until transfer completed */
  90.     while (!isTransferCompleted)
  91.     {
  92.          __NOP();
  93.     }
  94. }
  95. void trigger(void)
  96. {
  97.     GPIO_WritePinOutput(GPIOC, 19UL,1);
  98.     GPIO_WritePinOutput(GPIOC, 19UL,0);
  99. }
  100. /*!
  101. * [url=home.php?mod=space&uid=159083]@brief[/url] Main function
  102. */
  103. int main(void)
  104. {
  105.     BOARD_InitPins();
  106.     BOARD_BootClockRUN();
  107.     BOARD_InitDebugConsole();

  108.     PRINTF("   FRDM-KW41Z               --    Nixie tube \r\n");
  109.     PRINTF("---------------------------------------------\r\n");
  110.     PRINTF("   J1->PIN4->PC16(CLK)      --    SCLK       \r\n");
  111.     PRINTF("   J1->PIN6->PC17(SOUT)     --    DIO        \r\n");
  112.     PRINTF("   J1->PIN3->PTC19(GPIO)    --    RCLK       \r\n");
  113.     PRINTF("   J2->PIN8->VDD            --    VDD        \r\n");
  114.     PRINTF("   J2->PIN7->GND            --    GND        \r\n");

  115.     /* DMA Mux setting and EDMA init */
  116.     uint32_t masterRxChannel, masterIntermediaryChannel, masterTxChannel;
  117.     edma_config_t userConfig;

  118.     masterRxChannel = 0U;
  119.     masterIntermediaryChannel = 1U;
  120.     masterTxChannel = 2U;

  121.     /* DMA MUX init */
  122.     DMAMUX_Init(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR);

  123.     DMAMUX_SetSource(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterRxChannel, EXAMPLE_DSPI_MASTER_DMA_RX_REQUEST_SOURCE);
  124.     DMAMUX_EnableChannel(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterRxChannel);

  125. #if (defined EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE)
  126.     DMAMUX_SetSource(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterTxChannel, EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE);
  127.     DMAMUX_EnableChannel(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterTxChannel);
  128. #endif

  129.     /* EDMA init */
  130.     /*
  131.      * userConfig.enableRoundRobinArbitration = false;
  132.      * userConfig.enableHaltOnError = true;
  133.      * userConfig.enableContinuousLinkMode = false;
  134.      * userConfig.enableDebugMode = false;
  135.      */
  136.     EDMA_GetDefaultConfig(&userConfig);

  137.     EDMA_Init(EXAMPLE_DSPI_MASTER_DMA_BASEADDR, &userConfig);

  138.     uint32_t srcClock_Hz;
  139.     dspi_master_config_t masterConfig;

  140.     /* Master config */
  141.     masterConfig.whichCtar = kDSPI_Ctar0;
  142.     masterConfig.ctarConfig.baudRate = TRANSFER_BAUDRATE;
  143.     masterConfig.ctarConfig.bitsPerFrame = 8;
  144.     masterConfig.ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
  145.     masterConfig.ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
  146.     masterConfig.ctarConfig.direction = kDSPI_MsbFirst;
  147.     masterConfig.ctarConfig.pcsToSckDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE;
  148.     masterConfig.ctarConfig.lastSckToPcsDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE;
  149.     masterConfig.ctarConfig.betweenTransferDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE;

  150.     masterConfig.whichPcs = EXAMPLE_DSPI_MASTER_PCS_FOR_INIT;
  151.     masterConfig.pcsActiveHighOrLow = kDSPI_PcsActiveLow;

  152.     masterConfig.enableContinuousSCK = false;
  153.     masterConfig.enableRxFifoOverWrite = false;
  154.     masterConfig.enableModifiedTimingFormat = false;
  155.     masterConfig.samplePoint = kDSPI_SckToSin0Clock;

  156.     srcClock_Hz = CLOCK_GetFreq(DSPI_MASTER_CLK_SRC);
  157.     DSPI_MasterInit(EXAMPLE_DSPI_MASTER_BASEADDR, &masterConfig, srcClock_Hz);

  158.     /* Set up dspi master */
  159.     memset(&(dspiEdmaMasterRxRegToRxDataHandle), 0, sizeof(dspiEdmaMasterRxRegToRxDataHandle));
  160.     memset(&(dspiEdmaMasterTxDataToIntermediaryHandle), 0, sizeof(dspiEdmaMasterTxDataToIntermediaryHandle));
  161.     memset(&(dspiEdmaMasterIntermediaryToTxRegHandle), 0, sizeof(dspiEdmaMasterIntermediaryToTxRegHandle));

  162.     EDMA_CreateHandle(&(dspiEdmaMasterRxRegToRxDataHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR, masterRxChannel);
  163.     EDMA_CreateHandle(&(dspiEdmaMasterTxDataToIntermediaryHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR,
  164.                       masterIntermediaryChannel);
  165.     EDMA_CreateHandle(&(dspiEdmaMasterIntermediaryToTxRegHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR, masterTxChannel);

  166.     DSPI_MasterTransferCreateHandleEDMA(EXAMPLE_DSPI_MASTER_BASEADDR, &g_dspi_edma_m_handle, DSPI_MasterUserCallback,
  167.                                         NULL, &dspiEdmaMasterRxRegToRxDataHandle,
  168.                                         &dspiEdmaMasterTxDataToIntermediaryHandle,
  169.                                         &dspiEdmaMasterIntermediaryToTxRegHandle);
  170.     /* Define the init structure for the output RCLK pin*/
  171.     gpio_pin_config_t rclk_config = {
  172.         kGPIO_DigitalOutput, 0,
  173.     };
  174.     /* Init output RCLK GPIO. */
  175.     GPIO_PinInit(GPIOC, 19UL, &rclk_config);

  176.     while(1)
  177.     {
  178.         if(loopCounter ++ % 500 == 0)
  179.         {
  180.           counter++;         
  181.           PRINTF("%d\r\n",counter);
  182.         }
  183.         setValue(counter,10000-counter,0);
  184.         ledDisplay(123);
  185.     }
  186. }


  1. #include "led.h"
  2. #include <stdint.h>
  3. void spi_write_byte(uint8_t *buf,uint16_t len);
  4. extern void trigger(void);
  5. uint8_t fseg[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  6. //反向显示
  7. //uint8_t fseg[]={0x3F,0x30,0x5B,0x79,0x74,0x6D,0x6F,0x38,0x7F,0x7D};
  8. uint16_t tube1,tube2;
  9. uint8_t ledTwinkle = 0;//LED是否闪烁
  10. uint8_t ledTwinkleFlag = 0;//LED闪烁标志
  11. uint8_t isEnable = 1;//LED是否显示
  12. uint8_t ledUnitCount = 1;//LED显示单元
  13. uint8_t displayUnitValue = 0;//LED单元数值
  14. uint32_t ledCounter = 0;

  15. void setValue(uint16_t t1,uint16_t t2,uint8_t twinkle)
  16. {
  17.     ledTwinkle = twinkle;
  18.     tube1 = t1;
  19.     tube2 = t2;
  20. }
  21. void putNumber(uint8_t num,uint8_t bit)
  22. {  
  23.     uint8_t buf[2] = {~(1<<(7-bit)),fseg[num]};
  24.     spi_write_byte(buf,2);
  25.     trigger();
  26. }

  27. //清除残影
  28. void ledClearPartNumberValue(void)
  29. {
  30.     uint8_t buf[2] = {0,0};
  31.     spi_write_byte(buf,2);
  32.     trigger();
  33. }
  34. //设置小数点
  35. void ledSetPartDotValue(uint8_t dot)
  36. {

  37. }
  38. //设置显示单元
  39. void ledSetUnit(uint8_t unit)
  40. {
  41.   switch(unit)
  42.   {
  43.     case 0:
  44.     break;
  45.     case 1:
  46.     break;
  47.     case 2:
  48.     break;
  49.     case 3:;
  50.     break;
  51.   
  52.   }
  53. }
  54. //刷新数据
  55. void ledDisplay(uint32_t delay)
  56. {
  57. //  ledClearPartNumberValue();//清除残影
  58.   if(isEnable &&  (ledTwinkle == 0 || ledTwinkleFlag))//判断显示标志
  59.   {
  60.       switch(ledUnitCount)
  61.       {
  62.          case 0:
  63.          displayUnitValue = tube2%10;//个位
  64.          break;
  65.          case 1:
  66.          displayUnitValue = (tube2%100)/10;//十位
  67.          break;
  68.          case 2:
  69.          displayUnitValue = (tube2%1000)/100;//百位
  70.          break;
  71.          case 3:
  72.          displayUnitValue = (tube2%10000)/1000;//千位
  73.          break;
  74.          case 4:
  75.          displayUnitValue = tube1%10;//个位
  76.          break;
  77.          case 5:
  78.          displayUnitValue = (tube1%100)/10;//十位
  79.          break;
  80.          case 6:
  81.          displayUnitValue = (tube1%1000)/100;//百位
  82.          break;
  83.          case 7:
  84.          displayUnitValue = (tube1%10000)/1000;//千位
  85.          break;
  86.       }
  87.       putNumber(displayUnitValue,ledUnitCount);//显示单元数值
  88.       if(ledUnitCount++>=7) ledUnitCount = 0;//下一单元
  89.   }
  90.   else
  91.   {
  92.     ledClearPartNumberValue();//清除残影
  93.   }
  94.   if(ledCounter % delay == 0)
  95.   {
  96.     ledTwinkleFlag = !ledTwinkleFlag;
  97.   }
  98.   ledCounter++;
  99. }
  100. //使能LED显示
  101. void ledEnable(void)
  102. {
  103.     isEnable = 1;
  104. }
  105. //禁能LED显示
  106. void ledDisable(void)
  107. {
  108.     isEnable = 0;
  109. }
  110. void SetLedTwinkle(uint8_t twinkle)
  111. {
  112.     ledTwinkle = twinkle;
  113. }


代码
游客,如果您要查看本帖隐藏内容请回复


此内容由EEWORLD论坛网友littleshrimp原创,如需转载或用于商业用途需征得作者同意并注明出处


虾扯蛋,蛋扯虾,虾扯蛋扯虾

回复评论 (6)

谢谢分享学习一下
点赞  2017-4-30 21:01
动手够快的呀!学习一下,回复看先下啦!
点赞  2017-4-30 22:09

4楼 nmg 

这是为halo光剑做准备吗?
点赞  2017-4-30 22:26
引用: nmg 发表于 2017-4-30 22:26
这是为halo光剑做准备吗?

向前一小步
虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2017-5-1 07:47
KW41Z学习了
点赞  2017-5-7 11:47
学习学习
点赞  2017-7-1 10:15
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复