- 硬件连接
采用SPI2连接。
VCC 3.3V
GND 地线
RST PB11
CS PB8
WR/SPIS PB12
P2/SDCK PB13
P1/SDO PB14
P0/SDI PB15
IRQ PB9
- stm32F103C8T6设置
芯片时钟频率设置为12Mhz。
- 核心代码
(1)、ld3320模块io口配置
void LD3320_GPIO_Cfg(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB ,ENABLE);
//LD_CS /RSET
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8|GPIO_Pin_11;//;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
(2)、ld3320模块io口配置
void LD3320_Spi_cfg(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//spi端口配置
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB2Periph_GPIOB,ENABLE); //使能SPI2外设时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
//P13/P14/P15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //spis 片选 WR
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
LD_CS_H();
//spi功能配置
SPI_Cmd(SPI2, DISABLE);
/* SPI2 配置 */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //时钟极性 空闲状态时,SCK保持低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟相位 数据采样从第一个时钟边沿开始
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件产生NSS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; //波特率控制 SYSCLK/128
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC多项式寄存器初始值为7
SPI_Init(SPI2, &SPI_InitStructure);
/* 使能SPI2 */
SPI_Cmd(SPI2, ENABLE);
}
(3)、ld3320时钟输入值配置
#define CLK_IN 22 /* user need modify this value according to clock in */
(4)、ld3320中断配置代码
void LD3320_EXTI_Cfg(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//中断引脚配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//外部中断线配置
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger =EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_GenerateSWInterrupt(EXTI_Line9);
GPIO_SetBits(GPIOB,GPIO_Pin_9); //默认拉高中断引脚
EXTI_ClearFlag(EXTI_Line9);
EXTI_ClearITPendingBit(EXTI_Line9);
//中断嵌套配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
因为PB9中断引脚在5-9范围内,因此中断通道名称是EXTI9_5_IRQn。注意不是EXTI5_9_IRQn。
(4)、ld3320中断函数代码
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line9)!= RESET )
{
ProcessInt0();
PrintCom(USART1,"进入中断\r\n"); /*text........................*/
EXTI_ClearFlag(EXTI_Line9);
EXTI_ClearITPendingBit(EXTI_Line9);
}
}
函数名为EXTI9_5_IRQHandler。
(4)、ld3320引脚宏代码
#define LD_RST_H() GPIO_SetBits(GPIOB, GPIO_Pin_11)
#define LD_RST_L() GPIO_ResetBits(GPIOB, GPIO_Pin_11)
#define LD_CS_H() GPIO_SetBits(GPIOB, GPIO_Pin_8)
#define LD_CS_L() GPIO_ResetBits(GPIOB, GPIO_Pin_8)
#define LD_SPIS_H() GPIO_SetBits(GPIOB, GPIO_Pin_12)
#define LD_SPIS_L() GPIO_ResetBits(GPIOB, GPIO_Pin_12)
因为在LD3320里多处要操作这些引脚的电平,因此统一用宏来处理。
(5)、ld3320主函数代码
void LD3320_main(void)
{
uint8 nAsrRes=0;
LD3320_Init();
PrintCom(USART1," 串口运行中....\r\n"); /*text.....*/
PrintCom(USART1,"口令:1、流水灯右移\r\n"); /*text.....*/
PrintCom(USART1,"2、闪烁\r\n"); /*text.....*/
PrintCom(USART1,"3、流水灯左移\r\n"); /*text.....*/
PrintCom(USART1,"4、全灭\r\n"); /*text.....*/
nAsrStatus = LD_ASR_NONE; // 初始状态:没有在作ASR
while(1)
{
switch(nAsrStatus)
{
case LD_ASR_RUNING:
case LD_ASR_ERROR:
break;
case LD_ASR_NONE:
nAsrStatus=LD_ASR_RUNING;
if (RunASR()==0) // 启动一次ASR识别流程:ASR初始化,ASR添加关键词语,启动ASR运算
{
nAsrStatus = LD_ASR_ERROR;
}
break;
case LD_ASR_FOUNDOK:
nAsrRes = LD_GetResult( );//一次ASR识别流程结束,取ASR识别结果
PrintCom(USART1,"\r\n识别码:"); /*text.....*/
USART_SendData(USART1,nAsrRes+0x30); /*text.....*/
switch(nAsrRes) /*对结果执行相关操作,客户修改*/
{
case CODE_LSDYY: /*命令“打开”*/ PrintCom(USART1,"“流水灯右移”命令识别成功\r\n"); /*text.....*/
Glide_LED_Right(); //实现流水灯右移功能
break;
case CODE_SS: /*命令“闪烁”*/ PrintCom(USART1,"“闪烁”命令识别成功\r\n"); /*text.....*/
Flicker_LED(); //实现灯闪烁功能 break;
case CODE_LSDZY: /*命令“流水灯左移”*/ PrintCom(USART1,"“流水灯左移”命令识别成功\r\n"); /*text.....*/
Glide_LED_Left(); //实现流水灯左移功能 break;
case CODE_QM: /*命令“全灭”*/ PrintCom(USART1,"“全灭”命令识别成功\r\n"); /*text.....*/
Off_LED(); //关闭全部LED break;
default:break;
}
nAsrStatus = LD_ASR_NONE;
break;
case LD_ASR_FOUNDZERO:
default:
nAsrStatus = LD_ASR_NONE;
break;
}//switch
//开发板测试延时部分(用户可删除)
Board_text(nAsrRes ); /*text....................*/
}// while
}
- 完整代码,请加QQ(395702361)索取。