#define HIGH_VAL_8BIT MAX_F_BIT(8)
时间: 2023-08-09 22:07:13 浏览: 112
根据你提供的代码,`HIGH_VAL_8BIT` 被定义为 `MAX_F_BIT(8)`。这表示 `HIGH_VAL_8BIT` 的值是 `MAX_F_BIT` 函数的返回值,该函数接受参数 `8`。
要了解 `HIGH_VAL_8BIT` 的具体值,我们需要查看 `MAX_F_BIT` 函数的定义或上下文中关于该函数的信息。`MAX_F_BIT` 函数可能是一个宏或函数,它可能被用来计算给定位数的二进制位所能表示的最大值。在这种情况下,`MAX_F_BIT(8)` 可能返回表示 8 位二进制位最大值的某个常量或表达式。
相关问题
如何将以下代码移植到BL618-G0中// 包含头文件 #include "stm32f10x.h" #include "delay.h" #include "FSR.h" #include "usart.h" #include "adc.h" // 定义常量 #define PRESS_MIN 20 #define PRESS_MAX 6000 #define VOLTAGE_MIN 150 #define VOLTAGE_MAX 3300 // 定义变量 u8 state = 0; u16 val = 0; u16 value_AD = 0; long PRESS_AO = 0; int VOLTAGE_AO = 0; // 声明函数 long map(long x, long in_min, long in_max, long out_min, long out_max); int main(void) { // 初始化延时函数 delay_init(); // 配置NVIC NVIC_Configuration(); // 初始化串口 uart_init(9600); // 初始化ADC Adc_Init(); delay_ms(1000); printf("Test start\r\n"); while(1) { // 获取ADC采样值 value_AD = Get_Adc_Average(1,10); // 转换为电压值 VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300); if(VOLTAGE_AO < VOLTAGE_MIN) { PRESS_AO = 0; } else if(VOLTAGE_AO > VOLTAGE_MAX) { PRESS_AO = PRESS_MAX; } else { // 根据电压值计算压力值 PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX); } // 输出结果 printf("ADÖµ = %d,µçѹ = %d mv,ѹÁ¦ = %ld g\r\n",value_AD,VOLTAGE_AO,PRESS_AO); // 延时500ms delay_ms(500); } } // 实现map函数 long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }
BL618-G0是一款基于Arm Cortex-M3内核的低功耗无线SoC,它的硬件结构和STM32F10x有所不同,因此需要对代码进行相应的修改和适配。
首先需要根据BL618-G0的硬件结构和引脚分配,重新定义头文件和常量:
```c
#include "bl_mcu_sdk.h"
#include "bl_gpio.h"
#include "bl_adc.h"
#include "bl_uart.h"
#include "bl_delay.h"
#define PRESS_MIN 20
#define PRESS_MAX 6000
#define VOLTAGE_MIN 150
#define VOLTAGE_MAX 3300
```
接着需要修改主函数中的一些部分,比如将NVIC配置函数删除,因为BL618-G0不需要进行NVIC中断配置。需要将串口初始化函数和ADC初始化函数改为BL618-G0的初始化函数,并且将printf语句中的\r\n改为BL618-G0的换行符:
```c
int main(void)
{
// 初始化延时函数
bl_delay_init();
// 初始化串口
bl_uart_init(0, 9600, UART_BITWIDTH_8BIT, UART_STOP_BIT_1, UART_PARITY_DISABLE);
// 初始化ADC
bl_adc_init(ADC_CLK_DIV_2, ADC_RESOLUTION_12BIT, ADC_SCALE_4096, ADC_INPUT_MODE_SINGLE, ADC_ATVCC);
bl_delay_ms(1000);
bl_uart_printf("Test start\n");
while(1)
{
// 获取ADC采样值
uint16_t value_AD = bl_adc_single_get_value(ADC_0, ADC_CHANNEL_1);
// 转换为电压值
int VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300);
if(VOLTAGE_AO < VOLTAGE_MIN)
{
PRESS_AO = 0;
}
else if(VOLTAGE_AO > VOLTAGE_MAX)
{
PRESS_AO = PRESS_MAX;
}
else
{
// 根据电压值计算压力值
PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX);
}
// 输出结果
bl_uart_printf("AD value = %d, voltage = %d mV, pressure = %ld g\n", value_AD, VOLTAGE_AO, PRESS_AO);
// 延时500ms
bl_delay_ms(500);
}
}
```
最后需要保留实现map函数的代码,并且在程序开头添加函数声明:
```c
long map(long x, long in_min, long in_max, long out_min, long out_max);
```
完整的代码如下:
```c
#include "bl_mcu_sdk.h"
#include "bl_gpio.h"
#include "bl_adc.h"
#include "bl_uart.h"
#include "bl_delay.h"
#define PRESS_MIN 20
#define PRESS_MAX 6000
#define VOLTAGE_MIN 150
#define VOLTAGE_MAX 3300
long map(long x, long in_min, long in_max, long out_min, long out_max);
int main(void)
{
// 初始化延时函数
bl_delay_init();
// 初始化串口
bl_uart_init(0, 9600, UART_BITWIDTH_8BIT, UART_STOP_BIT_1, UART_PARITY_DISABLE);
// 初始化ADC
bl_adc_init(ADC_CLK_DIV_2, ADC_RESOLUTION_12BIT, ADC_SCALE_4096, ADC_INPUT_MODE_SINGLE, ADC_ATVCC);
bl_delay_ms(1000);
bl_uart_printf("Test start\n");
while(1)
{
// 获取ADC采样值
uint16_t value_AD = bl_adc_single_get_value(ADC_0, ADC_CHANNEL_1);
// 转换为电压值
int VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300);
if(VOLTAGE_AO < VOLTAGE_MIN)
{
PRESS_AO = 0;
}
else if(VOLTAGE_AO > VOLTAGE_MAX)
{
PRESS_AO = PRESS_MAX;
}
else
{
// 根据电压值计算压力值
PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX);
}
// 输出结果
bl_uart_printf("AD value = %d, voltage = %d mV, pressure = %ld g\n", value_AD, VOLTAGE_AO, PRESS_AO);
// 延时500ms
bl_delay_ms(500);
}
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
```
esp32 蓝牙启动流程_ESP32教你简单进行蓝牙串口调试
ESP32的蓝牙启动流程如下:
1. 初始化蓝牙协议栈
2. 配置蓝牙参数,如设备名称、服务UUID等
3. 注册蓝牙事件回调函数
4. 启动蓝牙广播
5. 开启蓝牙可见性
6. 等待连接
以下是一个简单的ESP32蓝牙串口调试的示例:
```C
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#define GATTS_TAG "GATTS_DEMO"
#define TEST_DEVICE_NAME "ESP32_BLE_UART"
#define TEST_MANUFACTURER_DATA_LEN 17
/* The max length of characteristic value. When the gatt client write or prepare write,
* the data length must be less than MAX_VALUE_LENGTH.
*/
#define MAX_VALUE_LENGTH 500
/* Declare global variable */
static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x4c, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD};
static uint8_t test_service_uuid128[32] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x13, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x14, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_service_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x01, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x01, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_char_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x02, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x02, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static esp_gatt_char_prop_t test_property = 0;
static uint8_t char1_str[] = {0x11,0x22,0x33};
static esp_attr_value_t gatts_demo_char1_val = {
.attr_max_len = MAX_VALUE_LENGTH,
.attr_len = sizeof(char1_str),
.attr_value = char1_str,
};
static uint16_t gatts_demo_handle_table[3];
/* Full Database Description - Used to add attributes into the database */
static const esp_gatts_attr_db_t gatt_db[HRS_IDX_NB] =
{
// Service Declaration
[IDX_SVC] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
sizeof(test_service_uuid), sizeof(test_service_uuid), (uint8_t *)&test_service_uuid},
ESP_GATT_UUID_PRI_SERVICE,
ESP_GATT_PERM_READ,
sizeof(test_service_uuid),
sizeof(test_service_uuid),
(uint8_t *)&test_service_uuid,
0
},
/* Characteristic Declaration */
[IDX_CHAR_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&test_property},
ESP_GATT_UUID_CHAR_DECLARE,
ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,
CHAR_DECLARATION_SIZE,
(uint8_t *)&test_property,
0
},
/* Characteristic Value */
[IDX_CHAR_VAL_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_128, (uint8_t *)&test_char_uuid, ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH, sizeof(gatts_demo_char1_val), gatts_demo_char1_val.attr_value},
ESP_UUID_LEN_128,
ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH,
sizeof(gatts_demo_char1_val),
gatts_demo_char1_val.attr_value,
0
},
};
static esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = true,
.include_txpower = true,
.min_interval = 0x20,
.max_interval = 0x40,
.appearance = 0x00,
.manufacturer_len = TEST_MANUFACTURER_DATA_LEN,
.p_manufacturer_data = test_manufacturer,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = sizeof(test_service_uuid),
.p_service_uuid = test_service_uuid,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
.peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE(GATTS_TAG, "advertising start failed");
}
break;
default:
break;
}
}
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
switch (event) {
case ESP_GATTS_REG_EVT:
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
esp_ble_gap_config_adv_data(&adv_data);
break;
case ESP_GATTS_CREAT_ATTR_TAB_EVT:
if (param->add_attr_tab.status != ESP_GATT_OK){
ESP_LOGE(GATTS_TAG, "create attribute table failed, error code=0x%x", param->add_attr_tab.status);
}
else if (param->add_attr_tab.num_handle != HRS_IDX_NB) {
ESP_LOGE(GATTS_TAG, "create attribute table abnormally, num_handle (%d) \
doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, HRS_IDX_NB);
}
else {
ESP_LOGI(GATTS_TAG, "create attribute table successfully, the number handle = %d\n",param->add_attr_tab.num_handle);
memcpy(gatts_demo_handle_table, param->add_attr_tab.handles, sizeof(gatts_demo_handle_table));
esp_ble_gatts_start_service(gatts_demo_handle_table[IDX_SVC]);
}
break;
case ESP_GATTS_CONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONNECT_EVT");
break;
case ESP_GATTS_DISCONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT");
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GATTS_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_WRITE_EVT");
break;
case ESP_GATTS_MTU_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
break;
case ESP_GATTS_CONF_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT");
break;
case ESP_GATTS_EXEC_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_EXEC_WRITE_EVT");
break;
case ESP_GATTS_START_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_START_EVT");
break;
case ESP_GATTS_STOP_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_STOP_EVT");
break;
case ESP_GATTS_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_OPEN_EVT");
break;
case ESP_GATTS_CANCEL_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CANCEL_OPEN_EVT");
break;
case ESP_GATTS_CLOSE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CLOSE_EVT");
break;
case ESP_GATTS_LISTEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_LISTEN_EVT");
break;
case ESP_GATTS_CONGEST_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONGEST_EVT");
break;
case ESP_GATTS_UNREG_EVT:
case ESP_GATTS_DELETE_EVT:
default:
break;
}
}
void app_main()
{
esp_err_t ret;
ESP_LOGI(GATTS_TAG, "ESP_BLUETOOTH_BLE example started.");
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
return;
}
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
return;
}
ret = esp_ble_gatts_register_callback(gatts_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
return;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
return;
}
ret = esp_ble_gatts_app_register(ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
return;
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret){
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
ret = esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, HRS_IDX_NB, ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "create attr table failed, error code = %x", ret);
}
}
```
在这个示例中,我们使用了ESP-IDF提供的蓝牙协议栈和GATT Server框架,实现了一个简单的GATT Server,并且开启了蓝牙广播和可见性,使得其他蓝牙设备可以扫描到并连接我们的设备。在连接建立后,我们可以向GATT Server中的特定Characteristic写入数据,也可以从特定Characteristic读取数据。
阅读全文