五、代码编写
首先是管脚复用关系的初始化设置
void BOARD_InitPins(void)
{
/* Clock Gate Control: Clock enabled. The current clock selection and divider options are locked. */
CLOCK_EnableClock(kCLOCK_PortA);
/* Clock Gate Control: Clock enabled. The current clock selection and divider options are locked. */
CLOCK_EnableClock(kCLOCK_PortB);
/* Clock Gate Control: Clock enabled. The current clock selection and divider options are locked. */
CLOCK_EnableClock(kCLOCK_PortC);
/* RGB pin is configured */
PORT_SetPinMux(PORTA, 24U, kPORT_MuxAsGpio);
PORT_SetPinMux(PORTA, 23U, kPORT_MuxAsGpio);
PORT_SetPinMux(PORTA, 22U, kPORT_MuxAsGpio);
/*L298N direction pin is configured */
PORT_SetPinMux(PORTA, 31U, kPORT_MuxAsGpio);
PORT_SetPinMux(PORTB, 1U, kPORT_MuxAsGpio);
/* PORTB2 (pin D12) is configured as TPM0_CH0 */
PORT_SetPinMux(PORTB, 2U, kPORT_MuxAlt6);
/* PORTB3 (pin C1) is configured as TPM0_CH1 */
PORT_SetPinMux(PORTB, 3U, kPORT_MuxAlt6);
/* PORTB13 (pin G3) is configured as TPM3_CH0 */
PORT_SetPinMux(PORTB, 13U, kPORT_MuxAlt6);
/* PORTB14 (pin G2) is configured as TPM3_CH1 */
PORT_SetPinMux(PORTB, 14U, kPORT_MuxAlt6);
/* PORTC7 (pin N2) is configured as LPUART0_RX */
PORT_SetPinMux(PORTC, 7U, kPORT_MuxAlt3);
/* PORTA25 (pin B5) is configured as LPUART1_RX */
PORT_SetPinMux(PORTA, 25U, kPORT_MuxAlt2);
/* PORTC8 (pin P3) is configured as LPUART0_TX */
PORT_SetPinMux(PORTC, 8U, kPORT_MuxAlt3);
/* PORTA26 (pin A5) is configured as LPUART1_TX */
PORT_SetPinMux(PORTA, 26U, kPORT_MuxAlt2);
}
其次在drivers中添加进“fsl_tpm.c”源文件,void BOARD_InitHardware(void)函数中增加:
“CLOCK_SetIpSrc(kCLOCK_Tpm0, kCLOCK_IpSrcFircAsync);”完成对TPM0时钟设置。
接下来则是完成逻辑处理代码的编写,主源文件代码如下:
#include "fsl_debug_console.h"
#include "board.h"
#include "app.h"
#include "fsl_lpuart.h"
#include "fsl_tpm.h"
/**************************************************************
* Definitions
**************************************************************/
#define RX_RING_BUFFER_SIZE 20U
#define ECHO_BUFFER_SIZE 2U
#ifndef TPM_LED_ON_LEVEL
#define TPM_LED_ON_LEVEL kTPM_HighTrue
#endif
#ifndef DEMO_PWM_FREQUENCY
#define DEMO_PWM_FREQUENCY (24000U)
#endif
/*****************************************************
* Prototypes
*****************************************************/
/* LPUART user callback */
void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
/************************************************************
* Variables
***********************************************************/
lpuart_handle_t g_lpuartHandle;
uint8_t g_rxRingBuffer[RX_RING_BUFFER_SIZE] = {0}; /* RX ring buffer. */
uint8_t g_rxBuffer[ECHO_BUFFER_SIZE] = {0}; /* Buffer for receive data to echo. */
uint8_t g_txBuffer[ECHO_BUFFER_SIZE] = {0}; /* Buffer for send data to echo. */
volatile bool rxBufferEmpty = true;
volatile bool txBufferFull = false;
volatile bool txOnGoing = false;
volatile bool rxOnGoing = false;
volatile uint8_t channl;
volatile uint8_t pwmduty;
volatile uint8_t updatedDutycycle1 = 0U;
volatile uint8_t updatedDutycycle2 = 0U;
/**************************************************************
* Code
**************************************************************/
/* LPUART user callback */
void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
if (kStatus_LPUART_TxIdle == status)
{
txBufferFull = false;
txOnGoing = false;
}
if (kStatus_LPUART_RxIdle == status)
{
rxBufferEmpty = false;
rxOnGoing = false;
}
}
/*!
* @brief Main function
*/
int main(void)
{
uint32_t i;
lpuart_config_t config;
lpuart_transfer_t sendXfer;
lpuart_transfer_t receiveXfer;
size_t receivedBytes = 0U;
tpm_config_t tpmInfo;
tpm_chnl_pwm_signal_param_t tpmParam[2];
uint8_t control;
BOARD_InitHardware();
/*
* config.baudRate_Bps = 115200U;
* config.parityMode = kLPUART_ParityDisabled;
* config.stopBitCount = kLPUART_OneStopBit;
* config.txFifoWatermark = 0;
* config.rxFifoWatermark = 0;
* config.enableTx = false;
* config.enableRx = false;
*/
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;
LPUART_Init(DEMO_LPUART, &config, DEMO_LPUART_CLK_FREQ);
LPUART_TransferCreateHandle(DEMO_LPUART, &g_lpuartHandle, LPUART_UserCallback, NULL);
LPUART_TransferStartRingBuffer(DEMO_LPUART, &g_lpuartHandle, g_rxRingBuffer, RX_RING_BUFFER_SIZE);
/* Start to echo. */
sendXfer.data = g_txBuffer;
sendXfer.dataSize = ECHO_BUFFER_SIZE;
receiveXfer.data = g_rxBuffer;
receiveXfer.dataSize = ECHO_BUFFER_SIZE;
/* Define the init structure for the output LED pin*/
gpio_pin_config_t IN_config = {
kGPIO_DigitalOutput,
0,
};
/* Init output LED GPIO. */
GPIO_PinInit(BOARD_LED1_GPIO, BOARD_LED1_GPIO_PIN, &IN_config);
/* Init output L298N的IN1、IN2 GPIO. */
GPIO_PinInit(L298N_IN1_GPIO, L298N_IN1_GPIO_PIN, &IN_config);
GPIO_PinInit(L298N_IN2_GPIO, L298N_IN2_GPIO_PIN, &IN_config);
/* 设置L298N的IN1、IN2通道 */
GPIO_PinWrite(L298N_IN1_GPIO,L298N_IN1_GPIO_PIN,1);
GPIO_PinWrite(L298N_IN2_GPIO,L298N_IN2_GPIO_PIN,0);
/* Fill in the TPM config struct with the default settings */
TPM_GetDefaultConfig(&tpmInfo);
/* Calculate the clock division based on the PWM frequency to be obtained */
tpmInfo.prescale = TPM_CalculateCounterClkDiv(BOARD_TPM_BASEADDR, DEMO_PWM_FREQUENCY, TPM_SOURCE_CLOCK);
/* Initialize TPM module */
TPM_Init(BOARD_TPM_BASEADDR, &tpmInfo);
(由于发帖内容对图片有限制,精彩内容请见下篇帖子)