写了一个使用FRDM-KW41Z驱动74HC595控制8段数码管实现计数器的程序
数码管板子的原理图
main.c
- /*
- * Copyright (c) 2015, Freescale Semiconductor, Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
- *
- * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "fsl_device_registers.h"
- #include "fsl_debug_console.h"
- #include "fsl_dspi.h"
- #include "board.h"
- #include "fsl_dspi_edma.h"
- #include "fsl_edma.h"
- #include "fsl_dmamux.h"
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "led.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- #define EXAMPLE_DSPI_MASTER_BASEADDR SPI0
- #define EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR DMAMUX0
- #define EXAMPLE_DSPI_MASTER_DMA_BASEADDR DMA0
- #define EXAMPLE_DSPI_MASTER_DMA_RX_REQUEST_SOURCE 16U
- #define EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE 17U
- #define DSPI_MASTER_CLK_SRC DSPI0_CLK_SRC
- #define EXAMPLE_DSPI_MASTER_PCS_FOR_INIT kDSPI_Pcs0
- #define EXAMPLE_DSPI_MASTER_PCS_FOR_TRANSFER kDSPI_MasterPcs0
- #define EXAMPLE_DSPI_DEALY_COUNT 0xfffffU
- #define TRANSFER_BAUDRATE 500000U /* Transfer baudrate - 500k */
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /* DSPI user callback */
- void DSPI_MasterUserCallback(SPI_Type *base, dspi_master_edma_handle_t *handle, status_t status, void *userData);
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- dspi_master_edma_handle_t g_dspi_edma_m_handle;
- edma_handle_t dspiEdmaMasterRxRegToRxDataHandle;
- edma_handle_t dspiEdmaMasterTxDataToIntermediaryHandle;
- edma_handle_t dspiEdmaMasterIntermediaryToTxRegHandle;
- volatile bool isTransferCompleted = false;
- uint32_t loopCounter = 0;
- uint32_t counter = 0;
- /*******************************************************************************
- * Code
- ******************************************************************************/
- void DSPI_MasterUserCallback(SPI_Type *base, dspi_master_edma_handle_t *handle, status_t status, void *userData)
- {
- isTransferCompleted = true;
- }
- void spi_write_byte(uint8_t *buf,uint16_t len)
- {
- dspi_transfer_t masterXfer;
- masterXfer.txData = buf;
- masterXfer.rxData = NULL;
- masterXfer.dataSize = len;
- masterXfer.configFlags = kDSPI_MasterCtar0 | EXAMPLE_DSPI_MASTER_PCS_FOR_TRANSFER | kDSPI_MasterPcsContinuous;
- /* Start master transfer, send data to slave */
- isTransferCompleted = false;
- if (kStatus_Success !=
- DSPI_MasterTransferEDMA(EXAMPLE_DSPI_MASTER_BASEADDR, &g_dspi_edma_m_handle, &masterXfer))
- {
- PRINTF("There is error when start DSPI_MasterTransferEDMA \r\n ");
- }
- /* Wait until transfer completed */
- while (!isTransferCompleted)
- {
- __NOP();
- }
- }
- void trigger(void)
- {
- GPIO_WritePinOutput(GPIOC, 19UL,1);
- GPIO_WritePinOutput(GPIOC, 19UL,0);
- }
- /*!
- * [url=home.php?mod=space&uid=159083]@brief[/url] Main function
- */
- int main(void)
- {
- BOARD_InitPins();
- BOARD_BootClockRUN();
- BOARD_InitDebugConsole();
- PRINTF(" FRDM-KW41Z -- Nixie tube \r\n");
- PRINTF("---------------------------------------------\r\n");
- PRINTF(" J1->PIN4->PC16(CLK) -- SCLK \r\n");
- PRINTF(" J1->PIN6->PC17(SOUT) -- DIO \r\n");
- PRINTF(" J1->PIN3->PTC19(GPIO) -- RCLK \r\n");
- PRINTF(" J2->PIN8->VDD -- VDD \r\n");
- PRINTF(" J2->PIN7->GND -- GND \r\n");
- /* DMA Mux setting and EDMA init */
- uint32_t masterRxChannel, masterIntermediaryChannel, masterTxChannel;
- edma_config_t userConfig;
- masterRxChannel = 0U;
- masterIntermediaryChannel = 1U;
- masterTxChannel = 2U;
- /* DMA MUX init */
- DMAMUX_Init(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR);
- DMAMUX_SetSource(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterRxChannel, EXAMPLE_DSPI_MASTER_DMA_RX_REQUEST_SOURCE);
- DMAMUX_EnableChannel(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterRxChannel);
- #if (defined EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE)
- DMAMUX_SetSource(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterTxChannel, EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE);
- DMAMUX_EnableChannel(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterTxChannel);
- #endif
- /* EDMA init */
- /*
- * userConfig.enableRoundRobinArbitration = false;
- * userConfig.enableHaltOnError = true;
- * userConfig.enableContinuousLinkMode = false;
- * userConfig.enableDebugMode = false;
- */
- EDMA_GetDefaultConfig(&userConfig);
- EDMA_Init(EXAMPLE_DSPI_MASTER_DMA_BASEADDR, &userConfig);
- uint32_t srcClock_Hz;
- dspi_master_config_t masterConfig;
- /* Master config */
- masterConfig.whichCtar = kDSPI_Ctar0;
- masterConfig.ctarConfig.baudRate = TRANSFER_BAUDRATE;
- masterConfig.ctarConfig.bitsPerFrame = 8;
- masterConfig.ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
- masterConfig.ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
- masterConfig.ctarConfig.direction = kDSPI_MsbFirst;
- masterConfig.ctarConfig.pcsToSckDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE;
- masterConfig.ctarConfig.lastSckToPcsDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE;
- masterConfig.ctarConfig.betweenTransferDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE;
- masterConfig.whichPcs = EXAMPLE_DSPI_MASTER_PCS_FOR_INIT;
- masterConfig.pcsActiveHighOrLow = kDSPI_PcsActiveLow;
- masterConfig.enableContinuousSCK = false;
- masterConfig.enableRxFifoOverWrite = false;
- masterConfig.enableModifiedTimingFormat = false;
- masterConfig.samplePoint = kDSPI_SckToSin0Clock;
- srcClock_Hz = CLOCK_GetFreq(DSPI_MASTER_CLK_SRC);
- DSPI_MasterInit(EXAMPLE_DSPI_MASTER_BASEADDR, &masterConfig, srcClock_Hz);
- /* Set up dspi master */
- memset(&(dspiEdmaMasterRxRegToRxDataHandle), 0, sizeof(dspiEdmaMasterRxRegToRxDataHandle));
- memset(&(dspiEdmaMasterTxDataToIntermediaryHandle), 0, sizeof(dspiEdmaMasterTxDataToIntermediaryHandle));
- memset(&(dspiEdmaMasterIntermediaryToTxRegHandle), 0, sizeof(dspiEdmaMasterIntermediaryToTxRegHandle));
- EDMA_CreateHandle(&(dspiEdmaMasterRxRegToRxDataHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR, masterRxChannel);
- EDMA_CreateHandle(&(dspiEdmaMasterTxDataToIntermediaryHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR,
- masterIntermediaryChannel);
- EDMA_CreateHandle(&(dspiEdmaMasterIntermediaryToTxRegHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR, masterTxChannel);
- DSPI_MasterTransferCreateHandleEDMA(EXAMPLE_DSPI_MASTER_BASEADDR, &g_dspi_edma_m_handle, DSPI_MasterUserCallback,
- NULL, &dspiEdmaMasterRxRegToRxDataHandle,
- &dspiEdmaMasterTxDataToIntermediaryHandle,
- &dspiEdmaMasterIntermediaryToTxRegHandle);
- /* Define the init structure for the output RCLK pin*/
- gpio_pin_config_t rclk_config = {
- kGPIO_DigitalOutput, 0,
- };
- /* Init output RCLK GPIO. */
- GPIO_PinInit(GPIOC, 19UL, &rclk_config);
- while(1)
- {
- if(loopCounter ++ % 500 == 0)
- {
- counter++;
- PRINTF("%d\r\n",counter);
- }
- setValue(counter,10000-counter,0);
- ledDisplay(123);
- }
- }
- #include "led.h"
- #include <stdint.h>
- void spi_write_byte(uint8_t *buf,uint16_t len);
- extern void trigger(void);
- uint8_t fseg[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
- //反向显示
- //uint8_t fseg[]={0x3F,0x30,0x5B,0x79,0x74,0x6D,0x6F,0x38,0x7F,0x7D};
- uint16_t tube1,tube2;
- uint8_t ledTwinkle = 0;//LED是否闪烁
- uint8_t ledTwinkleFlag = 0;//LED闪烁标志
- uint8_t isEnable = 1;//LED是否显示
- uint8_t ledUnitCount = 1;//LED显示单元
- uint8_t displayUnitValue = 0;//LED单元数值
- uint32_t ledCounter = 0;
- void setValue(uint16_t t1,uint16_t t2,uint8_t twinkle)
- {
- ledTwinkle = twinkle;
- tube1 = t1;
- tube2 = t2;
- }
- void putNumber(uint8_t num,uint8_t bit)
- {
- uint8_t buf[2] = {~(1<<(7-bit)),fseg[num]};
- spi_write_byte(buf,2);
- trigger();
- }
- //清除残影
- void ledClearPartNumberValue(void)
- {
- uint8_t buf[2] = {0,0};
- spi_write_byte(buf,2);
- trigger();
- }
- //设置小数点
- void ledSetPartDotValue(uint8_t dot)
- {
- }
- //设置显示单元
- void ledSetUnit(uint8_t unit)
- {
- switch(unit)
- {
- case 0:
- break;
- case 1:
- break;
- case 2:
- break;
- case 3:;
- break;
-
- }
- }
- //刷新数据
- void ledDisplay(uint32_t delay)
- {
- // ledClearPartNumberValue();//清除残影
- if(isEnable && (ledTwinkle == 0 || ledTwinkleFlag))//判断显示标志
- {
- switch(ledUnitCount)
- {
- case 0:
- displayUnitValue = tube2%10;//个位
- break;
- case 1:
- displayUnitValue = (tube2%100)/10;//十位
- break;
- case 2:
- displayUnitValue = (tube2%1000)/100;//百位
- break;
- case 3:
- displayUnitValue = (tube2%10000)/1000;//千位
- break;
- case 4:
- displayUnitValue = tube1%10;//个位
- break;
- case 5:
- displayUnitValue = (tube1%100)/10;//十位
- break;
- case 6:
- displayUnitValue = (tube1%1000)/100;//百位
- break;
- case 7:
- displayUnitValue = (tube1%10000)/1000;//千位
- break;
- }
- putNumber(displayUnitValue,ledUnitCount);//显示单元数值
- if(ledUnitCount++>=7) ledUnitCount = 0;//下一单元
- }
- else
- {
- ledClearPartNumberValue();//清除残影
- }
- if(ledCounter % delay == 0)
- {
- ledTwinkleFlag = !ledTwinkleFlag;
- }
- ledCounter++;
- }
- //使能LED显示
- void ledEnable(void)
- {
- isEnable = 1;
- }
- //禁能LED显示
- void ledDisable(void)
- {
- isEnable = 0;
- }
- void SetLedTwinkle(uint8_t twinkle)
- {
- ledTwinkle = twinkle;
- }
代码
此内容由EEWORLD论坛网友littleshrimp原创,如需转载或用于商业用途需征得作者同意并注明出处