赞
踩
通过阅读JQ8900的数据手册,可以了解到驱动JQ8900有许多种方式,IO驱动,一线串口驱动(VPP),两线串口驱动(RX,TX),这里我使用两线串口驱动。所以我需要连接到XR806开发板的一组串口上。
通过查阅手册可以知道XR806有3组串口, 我选择串口1进行通信,也就是PB14,PB15引脚。
我们只需要将开发板的PB15引脚接到JQ8900模块的TX引脚,开发板的PB14引脚接到JQ8900模块的RX引脚。
通过串口1驱动JQ8900模块播放音乐,通过XR806的按键去控制音乐的切换(播放,暂停,上一曲,下一曲,停止等),每按下一次按键,XR806板载LED就会进行一次取反操作,用来显示按键按下效果。
目录结构如下面红框所示。
#ifndef _KEY_H #define _KEY_H #define GPIO_ID_PA11 11 #define GPIO_ID_PA21 21 extern uint8_t key_status ; extern uint8_t key_value; void key_init(void); void key_scan(void); #endif
#include <stdio.h> #include "ohos_init.h" // #include "kernel/os/os.h" #include "iot_gpio.h" // #include "key.h" uint8_t key_status = 1; uint8_t key_value = 0; void key_init(void) { printf("key test start\r\n"); IoTGpioInit(GPIO_ID_PA11); // 初始化IO口,io口复位为悬空输入 IoTGpioSetDir(GPIO_ID_PA11, IOT_GPIO_DIR_IN); IoTGpioInit(GPIO_ID_PA21); // 初始化IO口,io口复位为悬空输入 IoTGpioSetDir(GPIO_ID_PA21, IOT_GPIO_DIR_OUT); // 设置IO口为输出模式 } void key_scan(void) { uint8_t keyVal = 1; uint8_t ledVal = 0; IoTGpioGetInputVal(GPIO_ID_PA11,&keyVal); // printf("keyVal:%d\r\n",keyVal); if(keyVal == 0){ OS_MSleep(10); IoTGpioGetInputVal(GPIO_ID_PA11,&keyVal); if(keyVal == 0){ key_status = 1; key_value ++; if(key_status == 5) key_value = 1; // 按键执行操作 IoTGpioGetOutputVal(GPIO_ID_PA21,&ledVal); // printf("ledVal:%d\r\n",ledVal); if(ledVal == 1){ IoTGpioSetOutputVal(GPIO_ID_PA21, 0); // printf(" set 0\r\n"); } else{ IoTGpioSetOutputVal(GPIO_ID_PA21, 1); // printf(" set 1\r\n"); } // IoTGpioGetOutputVal(GPIO_ID_PA21,ledVal); // printf("ledVal set after:%d\r\n",ledVal); while(keyVal == 0){ IoTGpioGetInputVal(GPIO_ID_PA11,&keyVal); } } } }
#ifndef _JQ8900_H
#define _JQ8900_H
extern const uint8_t play_Buffer[4];
extern const uint8_t pause_Buffer[4];
extern const uint8_t stop_Buffer[4];
extern const uint8_t previous_Buffer[4];
extern const uint8_t next_Buffer[4];
#endif
#include <stdio.h> #include "ohos_init.h" // #include "kernel/os/os.h" #include <string.h> #include "driver/chip/hal_uart.h" #include "JQ8900.h" #define UARTID UART1_ID /* 音量为0-30级 上电默认为20 */ uint8_t JQ8900Volume = 20; const uint8_t play_Buffer[4] = {0xAA,0x02,0x00,0xAC}; const uint8_t pause_Buffer[4] = {0xAA,0x03,0x00,0xAD}; const uint8_t stop_Buffer[4] = {0xAA,0x04,0x00,0xAE}; const uint8_t previous_Buffer[4] = {0xAA,0x05,0x00,0xAF}; const uint8_t next_Buffer[4] = {0xAA,0x06,0x00,0xB0}; /* 其他命令请查看数据手册*/ /* 串口发送一个字节 */ void JQ8900_SendChar(char val) { HAL_UART_Transmit_Poll(UARTID, (uint8_t *)val, 1); }
#include <stdio.h> #include "ohos_init.h" // #include "kernel/os/os.h" #include "iot_gpio.h" // #include <string.h> #include "driver/chip/hal_uart.h" #include "key.h" #include "JQ8900.h" #define UARTID UART1_ID static OS_Thread_t g_main_thread; static int uart_init(void) { HAL_Status status = HAL_ERROR; UART_InitParam param; param.baudRate = 9600; // 波特率为9600 param.dataBits = UART_DATA_BITS_8; param.stopBits = UART_STOP_BITS_1; param.parity = UART_PARITY_NONE; param.isAutoHwFlowCtrl = 0; status = HAL_UART_Init(UARTID, ¶m); if (status != HAL_OK) printf("uart init error %d\n", status); return status; } static void MainThread(void *arg) { key_init(); uart_init(); printf("you can connect to uart1 serial com.\n"); printf("uart%d used for echo.\n", UARTID); while (1) { key_scan(); if(key_status == 1){ // 按键按下,可以进行操作 key_status = 0; switch(key_value) { case 0: HAL_UART_Transmit_Poll(UARTID, (uint8_t *)play_Buffer, 4); break; case 1: HAL_UART_Transmit_Poll(UARTID, (uint8_t *)pause_Buffer, 4); break; case 2: HAL_UART_Transmit_Poll(UARTID, (uint8_t *)play_Buffer, 4); break; case 3: HAL_UART_Transmit_Poll(UARTID, (uint8_t *)previous_Buffer, 4); break; case 4: HAL_UART_Transmit_Poll(UARTID, (uint8_t *)next_Buffer, 4); break; } } } } void UARTMain(void) { printf("UART Test Start\n"); if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL, OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) { printf("[ERR] Create MainThread Failed\n"); } } SYS_RUN(UARTMain); // Harmony线程入口
import("//device/xradio/xr806/liteos_m/config.gni") static_library("app_uart") { configs = [] sources = [ "src/main.c", "src/JQ8900.c", "src/key.c", ] cflags = board_cflags include_dirs = board_include_dirs include_dirs += [ "//kernel/liteos_m/kernel/arch/include", "include", "//base/iot_hardware/peripheral/interfaces/kits", ] }
下载过程我就不写了,可以去看上一篇文章。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。