本帖最后由 码克 于 2021-1-27 23:36 编辑
年底很忙,一直没时间做测评。正好手头的项目有一个RGB灯带的,实际应用中是使用延时加nop()指令实现的,看了下这款开发板的参数,应该可以使用SPI实现,所以做下尝试。
这是灯带的控制时序。
可以巧妙的运用SPI发送特定时许的数值,就可以代替nop()延时。
我是用的keil平台,工程文件用的SDK里的MD驱动实例,ALD的驱动不太习惯,还是MD用来更顺手。
首先设置下SPI波特率,
void md_spi_struct_init(md_spi_init_t *init){
init->SPIx = SPI1;
init->mode = MD_SPI_MODE_MASTER;
init->dir = MD_SPI_DIRECTION_2LINES;
init->data_size = MD_SPI_DATA_SIZE_8;
init->baud = MD_SPI_BAUD_2;//这里改下波特率
init->phase = MD_SPI_CPHA_FIRST;
init->polarity = MD_SPI_CPOL_HIGH;
init->first_bit = MD_SPI_FIRSTBIT_MSB;
init->ss_en = DISABLE;
init->crc_calc = DISABLE;
init->crc_poly = 0;
}
设置完,抓个波形看下,有一点误差,但是在误差范围内。0波形
1波形
写代码实现功能
/**
*********************************************************************************
*
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=247401]@brief[/url] Main file for DEMO
*
* [url=home.php?mod=space&uid=895143]@version[/url] V1.0
* [url=home.php?mod=space&uid=212281]@date[/url] 19 Feb 2019
* [url=home.php?mod=space&uid=187600]@author[/url] AE Team
* @note
*
* Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
*
*********************************************************************************
*/
#include <string.h>
#include "main.h"
/** @addtogroup Projects_Examples_MD
* @{
*/
/** @addtogroup Examples
* @{
*/
md_spi_init_t h_spi;
/**
* @brief Initializate pin of spi module.
* @retval None
*/
void spi_pin_init(void)
{
md_gpio_init_t x;
/* Initialize nss pin */
x.mode = MD_GPIO_MODE_OUTPUT;
x.odos = MD_GPIO_PUSH_PULL;
x.pupd = MD_GPIO_PUSH_UP;
x.podrv = MD_GPIO_OUT_DRIVE_1;
x.nodrv = MD_GPIO_OUT_DRIVE_1;
x.flt = MD_GPIO_FILTER_DISABLE;
x.type = MD_GPIO_TYPE_TTL;
x.func = MD_GPIO_FUNC_1;
md_gpio_init(GPIOC, MD_GPIO_PIN_0, &x);
/* Initialize sck pin */
x.mode = MD_GPIO_MODE_OUTPUT;
x.odos = MD_GPIO_PUSH_PULL;
x.pupd = MD_GPIO_PUSH_UP;
x.podrv = MD_GPIO_OUT_DRIVE_1;
x.nodrv = MD_GPIO_OUT_DRIVE_1;
x.flt = MD_GPIO_FILTER_DISABLE;
x.type = MD_GPIO_TYPE_TTL;
x.func = MD_GPIO_FUNC_4;
md_gpio_init(GPIOC, MD_GPIO_PIN_1, &x);
/* Initialize miso pin */
x.mode = MD_GPIO_MODE_INPUT;
x.odos = MD_GPIO_PUSH_PULL;
x.pupd = MD_GPIO_PUSH_UP;
x.podrv = MD_GPIO_OUT_DRIVE_1;
x.nodrv = MD_GPIO_OUT_DRIVE_1;
x.flt = MD_GPIO_FILTER_DISABLE;
x.type = MD_GPIO_TYPE_TTL;
x.func = MD_GPIO_FUNC_4;
md_gpio_init(GPIOC, MD_GPIO_PIN_2, &x);
/* Initialize mosi pin */
x.mode = MD_GPIO_MODE_OUTPUT;
x.odos = MD_GPIO_PUSH_PULL;
x.pupd = MD_GPIO_PUSH_UP;
x.podrv = MD_GPIO_OUT_DRIVE_1;
x.nodrv = MD_GPIO_OUT_DRIVE_1;
x.flt = MD_GPIO_FILTER_DISABLE;
x.type = MD_GPIO_TYPE_TTL;
x.func = MD_GPIO_FUNC_4;
md_gpio_init(GPIOC, MD_GPIO_PIN_3, &x);
return;
}
/**
* @brief Test main function
* @retval Status.
*/
int i;
struct RGBW_t
{
unsigned char R;
unsigned char G;
unsigned char B;
unsigned char W;
} RGBW=
{
.R = 255,
.G = 0,
.B = 0,
.W = 0,
};
void Send_SPI(unsigned char color)
{
unsigned char i,temp;
temp=color;
for(i=0;i<8;i++)
{
if(temp & 0x80)
{
md_spi_send_byte_fast(&h_spi, 0xFF);
}
else
{
md_spi_send_byte_fast(&h_spi, 0xE0);
}
temp=temp<<1;
}
}
void Send_LED(void)
{
unsigned char i;
i=8;
do
{
Send_SPI(RGBW.G);
Send_SPI(RGBW.R);
Send_SPI(RGBW.B);
Send_SPI(RGBW.W);
i--;
}while(i>0);
}
int main()
{
/* Configure system clock */
md_cmu_clock_config_default();
/* Initialize SysTick Interrupt */
md_init_1ms_tick();
/* Enable ALL peripheral */
SYSCFG_UNLOCK();
md_cmu_enable_perh_all();
SYSCFG_LOCK();
spi_pin_init();
memset(&h_spi, 0, sizeof(h_spi));
md_spi_struct_init(&h_spi);
md_spi_init(&h_spi);
while (1)
{
Send_LED();
md_delay_1ms(100);
}
}
/**
* @}
*/
/**
* @}
*/
发个图
说几个要注意的点:
1、这里最好加上--C99
2、调试的时候,下载完成,是要再按一下复位按键才有效果的
下一步准备用DMA+SPI配合,调试下。
|