#include "bflb_adc.h" #include "bflb_mtimer.h" #include "board.h" struct bflb_device_s adc; #define TEST_ADC_CHANNELS 2 #define TEST_COUNT 10 struct bflb_adc_channel_s chan[] = { { .pos_chan = ADC_CHANNEL_2, .neg_chan = ADC_CHANNEL_GND }, { .pos_chan = ADC_CHANNEL_GND, .neg_chan = ADC_CHANNEL_3 }, }; int main(void) { board_init(); board_adc_gpio_init(); adc = bflb_device_get_by_name("adc"); / adc clock = XCLK / 2 / 32 */ struct bflb_adc_config_s cfg; cfg.clk_div = ADC_CLK_DIV_32; cfg.scan_conv_mode = true; cfg.continuous_conv_mode = false; cfg.differential_mode = true; cfg.resolution = ADC_RESOLUTION_16B; cfg.vref = ADC_VREF_3P2V; bflb_adc_init(adc, &cfg); bflb_adc_channel_config(adc, chan, TEST_ADC_CHANNELS); for (uint32_t i = 0; i < TEST_COUNT; i++) { bflb_adc_start_conversion(adc); while (bflb_adc_get_count(adc) < TEST_ADC_CHANNELS) { bflb_mtimer_delay_ms(1); } for (size_t j = 0; j < TEST_ADC_CHANNELS; j++) { struct bflb_adc_result_s result; uint32_t raw_data = bflb_adc_read_raw(adc); printf("raw data:%08x\r\n", raw_data); bflb_adc_parse_result(adc, &raw_data, &result, 1); printf("pos chan %d,neg chan %d,%d mv \r\n", result.pos_chan, result.neg_chan, result.millivolt); } bflb_adc_stop_conversion(adc); bflb_mtimer_delay_ms(100); } while (1) { } }根据以上代码对bl618程序的编写对以下stm32中代码#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_Configuration(); uart_init(9600); Adc_Init(); delay_ms(1000); printf("Test start\r\n"); while(1) { 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); 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; }移植到bl618进行改写

时间: 2023-06-17 12:06:38 浏览: 47
以下是将上述STM32代码移植到BL618进行改写后的代码: ```c #include "bl602.h" #include "bl_adc.h" #include "bl_gpio.h" #include "bl_mtimer.h" #include "stdio.h" #define PRESS_MIN 20 #define PRESS_MAX 6000 #define VOLTAGE_MIN 150 #define VOLTAGE_MAX 3300 #define ADC_CHANNEL 0 #define ADC_CLK_DIV 5 #define ADC_RESOLUTION ADC_RESOLUTION_12B #define ADC_VREF ADC_VREF_3V3 long map(long x, long in_min, long in_max, long out_min, long out_max); int main(void) { bl_mtimer_init(); bl_gpio_enable_output(PIN_11, 1); bl_adc_init(); bl_adc_config_t cfg = { .inputMode = ADC_INPUT_SINGLE_END_MODE, .input = ADC_INPUT_CH0, .clkDiv = ADC_CLK_DIV, .resolution = ADC_RESOLUTION, .refSel = ADC_VREF, }; bl_adc_channel_config(ADC_CHANNEL, &cfg); printf("Test start\r\n"); while (1) { bl_adc_start(); while (!bl_adc_fifo_data_count()) { bl_mtimer_delay_us(100); } uint16_t value_AD = bl_adc_read_fifo_data(); int VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300); long PRESS_AO = 0; 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 value = %d, voltage = %d mV, pressure = %ld g\r\n", value_AD, VOLTAGE_AO, PRESS_AO); bl_mtimer_delay_us(500000); } } 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中,需要使用`bl_adc_init()`进行ADC模块的初始化,并通过`bl_adc_channel_config()`配置ADC通道。需要注意的是,BL618的ADC模块不支持差分模式,因此需要将原来的差分模式改为单端模式。在进行ADC转换时,可以使用`bl_adc_start()`触发转换,并通过`bl_adc_fifo_data_count()`和`bl_adc_read_fifo_data()`获取转换结果。此外,由于BL618中没有提供延时函数,需要使用`bl_mtimer_delay_us()`进行延时。

相关推荐

注释以下每一行代码#include "bflb_mtimer.h" #include "bflb_uart.h" #include "bflb_clock.h" #include "board.h" struct bflb_device_s *uartx; void uart_isr(int irq, void *arg) { uint32_t intstatus = bflb_uart_get_intstatus(uartx); int ret; uint32_t baudrate; if (intstatus & UART_INTSTS_RX_AD5) { bflb_uart_int_clear(uartx, UART_INTCLR_RX_AD5); ret = bflb_uart_feature_control(uartx, UART_CMD_GET_AUTO_BAUD, UART_AUTO_BAUD_0X55); baudrate = bflb_clk_get_peripheral_clock(BFLB_DEVICE_TYPE_UART, uartx->idx) / (ret + 1); printf("Detected baudrate by 0x55 is %d\r\n", baudrate); } if (intstatus & UART_INTSTS_RX_ADS) { bflb_uart_int_clear(uartx, UART_INTCLR_RX_ADS); ret = bflb_uart_feature_control(uartx, UART_CMD_GET_AUTO_BAUD, UART_AUTO_BAUD_START); baudrate = bflb_clk_get_peripheral_clock(BFLB_DEVICE_TYPE_UART, uartx->idx) / (ret + 1); printf("Detected baudrate by startbit is %d\r\n", baudrate); } } int main(void) { board_init(); board_uartx_gpio_init(); uartx = bflb_device_get_by_name(DEFAULT_TEST_UART); struct bflb_uart_config_s cfg; cfg.baudrate = 2000000; cfg.data_bits = UART_DATA_BITS_8; cfg.stop_bits = UART_STOP_BITS_1; cfg.parity = UART_PARITY_NONE; cfg.flow_ctrl = 0; cfg.tx_fifo_threshold = 7; cfg.rx_fifo_threshold = 7; bflb_uart_init(uartx, &cfg); bflb_uart_feature_control(uartx, UART_CMD_SET_AUTO_BAUD, 1); bflb_uart_feature_control(uartx, UART_CMD_SET_ABR_ALLOWABLE_ERROR, 3); bflb_irq_attach(uartx->irq_num, uart_isr, NULL); bflb_irq_enable(uartx->irq_num); while (1) { } }

#include "shell.h" #include <FreeRTOS.h> #include "task.h" #include "board.h" #include "bluetooth.h" #include "conn.h" #if defined(BL702) || defined(BL602) #include "ble_lib_api.h" #elif defined(BL616) #include "btble_lib_api.h" #endif #include "hci_driver.h" #include "bl616_glb.h" #include "ble_cli_cmds.h" #include "rfparam_adapter.h" #include "hci_core.h" static struct bflb_device_s uart0; extern void shell_init_with_task(struct bflb_device_s shell); static int btblecontroller_em_config(void) { extern uint8_t __LD_CONFIG_EM_SEL; volatile uint32_t em_size; em_size = (uint32_t)&__LD_CONFIG_EM_SEL; if (em_size == 0) { GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB); } else if (em_size == 321024) { GLB_Set_EM_Sel(GLB_WRAM128KB_EM32KB); } else if (em_size == 641024) { GLB_Set_EM_Sel(GLB_WRAM96KB_EM64KB); } else { GLB_Set_EM_Sel(GLB_WRAM96KB_EM64KB); } return 0; } void bt_enable_cb(int err) { if (!err) { bt_addr_le_t bt_addr; bt_get_local_public_address(&bt_addr); printf("BD_ADDR:(MSB)%02x:%02x:%02x:%02x:%02x:%02x(LSB) \n", bt_addr.a.val[5], bt_addr.a.val[4], bt_addr.a.val[3], bt_addr.a.val[2], bt_addr.a.val[1], bt_addr.a.val[0]); ble_cli_register(); } } int main(void) { board_init(); configASSERT((configMAX_PRIORITIES > 4)); uart0 = bflb_device_get_by_name("uart0"); shell_init_with_task(uart0); /* set ble controller EM Size / btblecontroller_em_config(); / Init rf */ if (0 != rfparam_init(0, NULL, 0)) { printf("PHY RF init failed!\r\n"); return 0; } // Initialize BLE controller #if defined(BL702) || defined(BL602) ble_controller_init(configMAX_PRIORITIES - 1); #else btble_controller_init(configMAX_PRIORITIES - 1); #endif // Initialize BLE Host stack hci_driver_init(); bt_enable(bt_enable_cb); vTaskStartScheduler(); while (1) { } }如何修改运行该代码的单片机能被设备扫描到给出完整代码

以下代码有什么错误static struct bflb_device_s uart0; extern void shell_init_with_task(struct bflb_device_s shell); static int btblecontroller_em_config(void) { extern uint8_t __LD_CONFIG_EM_SEL; volatile uint32_t em_size; em_size = (uint32_t)&__LD_CONFIG_EM_SEL; if (em_size == 0) { GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB); } else if (em_size == 321024) { GLB_Set_EM_Sel(GLB_WRAM128KB_EM32KB); } else if (em_size == 641024) { GLB_Set_EM_Sel(GLB_WRAM96KB_EM64KB); } else { GLB_Set_EM_Sel(GLB_WRAM96KB_EM64KB); } return 0; } void bt_enable_cb(int err) { if (!err) { bt_addr_le_t bt_addr; bt_get_local_public_address(&bt_addr); printf("BD_ADDR:(MSB)%02x:%02x:%02x:%02x:%02x:%02x(LSB) \n", bt_addr.a.val[5], bt_addr.a.val[4], bt_addr.a.val[3], bt_addr.a.val[2], bt_addr.a.val[1], bt_addr.a.val[0]); ble_cli_register(); } } int main(void) { board_init(); configASSERT((configMAX_PRIORITIES > 4)); uart0 = bflb_device_get_by_name("uart0"); shell_init_with_task(uart0); /* set ble controller EM Size / btblecontroller_em_config(); / Init rf */ if (0 != rfparam_init(0, NULL, 0)) { printf("PHY RF init failed!\r\n"); return 0; } // Initialize BLE controller #if defined(BL702) || defined(BL602) ble_controller_init(configMAX_PRIORITIES - 1); #else btble_controller_init(configMAX_PRIORITIES - 1); #endif // Initialize BLE Host stack hci_driver_init(); bt_enable(bt_enable_cb); vTaskStartScheduler();#define DEVICE_NAME "BL618_GATT" #define PROFILE_NUM 1 #define PROFILE_A_APP_ID 0 static void gap_event_handler(ble_event_t *event); static void gatt_event_handler(ble_event_t *event); int main(void) { bluetooth_init(gap_event_handler, gatt_event_handler); bluetooth_set_device_name(DEVICE_NAME); bluetooth_gatt_create_service(PROFILE_NUM); bluetooth_gatt_add_char(PROFILE_A_APP_ID, "CHAR_A", 0xFF01, 0x20, NULL); bluetooth_start_advertising(); while (1) { bluetooth_wait_for_event(); } return 0; } static void gap_event_handler(ble_event_t *event) { switch (event->type) { case BLE_GAP_EVENT_ADV_IND: { ble_gap_connect(&event->gap_event.adv_ind.address); break; } case BLE_GAP_EVENT_CONNECTED: { // 连接成功,可以开始 GATT 操作 break; } case BLE_GAP_EVENT_DISCONNECTED: { // 断开连接,重新开始广播 bluetooth_start_advertising(); break; } default: break; } } static void gatt_event_handler(ble_event_t *event) { switch (event->type) { case BLE_GATT_EVENT_READ: { // 处理读操作 break; } case BLE_GATT_EVENT_WRITE: { ble_err_t err = ble_gatt_server_send_indication(event->conn_handle, 0x1234, raw_data, sizeof(raw_data)); // 发送通知给主机 if (err != BLE_ERR_NONE) { // 发送失败,需要处理错误 break; } break; } default: break; } }

根据以下api和数据结构写出一个将adc转换出来的数据通过GATT发给手机端的代码void ble_controller_init(uint8_t task_priority) int hci_driver_init(void) int bt_enable(bt_ready_cb_t cb)int bt_le_adv_start(const struct bt_le_adv_param *param,const struct bt_data *ad, size_t ad_len, const struct bt_data *sd, size_t sd_len)int bt_le_adv_update_data(const struct bt_data *ad, size_t ad_len,const struct bt_data *sd, size_t sd_len)int bt_le_adv_stop(void)int bt_le_scan_start(const struct bt_le_scan_param *param, bt_le_scan_cb_t cb)int bt_le_scan_stop(void)int bt_le_whitelist_add(const bt_addr_le_t *addr)int bt_le_whitelist_rem(const bt_addr_le_t *addr)int bt_le_whitelist_clear(void)int bt_le_set_chan_map(u8_t chan_map[5])int bt_unpair(u8_t id, const bt_addr_le_t *addr)int bt_conn_get_info(const struct bt_conn *conn, struct bt_conn_info *info)int bt_conn_get_remote_dev_info(struct bt_conn_info *info)int bt_conn_le_param_update(struct bt_conn *conn,const struct bt_le_conn_param *param)int bt_conn_disconnect(struct bt_conn *conn, u8_t reason)struct bt_conn *bt_conn_create_le(const bt_addr_le_t *peer,const struct bt_le_conn_param *param)int bt_conn_create_auto_le(const struct bt_le_conn_param *param)int bt_conn_create_auto_stop(void)int bt_le_set_auto_conn(const bt_addr_le_t *addr,const struct bt_le_conn_param *param)struct bt_conn *bt_conn_create_slave_le(const bt_addr_le_t *peer,const struct bt_le_adv_param *param)int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec)bt_security_t bt_conn_get_security(struct bt_conn *conn)u8_t bt_conn_enc_key_size(struct bt_conn *conn)void bt_conn_cb_register(struct bt_conn_cb *cb)void bt_set_bondable(bool enable)int bt_conn_auth_cb_register(const struct bt_conn_auth_cb *cb)int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey)int bt_conn_auth_cancel(struct bt_conn *conn)int bt_conn_auth_passkey_confirm(struct bt_conn *conn)int bt_conn_auth_pincode_entry(struct bt_conn *conn, const char *pin)int bt_le_read_rssi(u16_t handle,int8_t *rssi)int bt_get_local_address(bt_addr_le_t *adv_addr)int bt_set_tx_pwr(int8_t power)bt_le_adv_parambt_databt_le_scan_parambt_le_conn_parambt_conn,并加入已经写好的adc代码bflb_adc_init(adc, &cfg); bflb_adc_channel_config(adc, chan, TEST_ADC_CHANNEL); for (uint32_t i = 0; i < 10; i++) { bflb_adc_start_conversion(adc); struct bflb_adc_result_s result; uint32_t raw_data = bflb_adc_read_raw(adc); bflb_adc_parse_result(adc, &raw_data, &result, 1); printf("pos chan %d,%d mv \r\n", result.pos_chan, result.millivolt); // char data[20]; // sprintf(data,"ADC result:%d",result.millivolt); // if(conn){ // bt_gatt_notify(conn,&attrs[1],data,sizeof(data)); // } bflb_mtimer_delay_ms(250); }

最新推荐

2018年数学建模赛题及优秀论文.zip

数学建模真题及优秀论文资料

全球二氧化碳排放量数据集.zip

数据分析数据集

口语数字数据集.zip

数据分析数据集

Kubernetes安装自动化脚本

附件是我自己提炼的ubuntu安装k8s的自动化脚本和配置项

数据仓库数据挖掘综述.ppt

数据仓库数据挖掘综述.ppt

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire

springboot新闻信息管理系统开发技术文档更新

# 1. 系统概述 ## 1.1 项目背景 在当今信息爆炸的时代,新闻信息是人们获取信息的重要渠道之一。为了满足用户对新闻阅读的需求,我们决定开发一个新闻信息管理系统,该系统旨在提供便捷的新闻发布、浏览与管理功能,同时也要保证系统的性能和安全防护。 ## 1.2 系统目标与功能需求 系统的目标是构建一个高效、稳定、安全的新闻信息管理平台,主要包括但不限于以下功能需求: - 新闻信息的增加、修改、删除、查询 - 用户的注册、登录与权限控制 - 数据库性能优化与缓存机制实现 - 安全防护措施的设计与漏洞修复 ## 1.3 技术选型与架构设计 在系统设计中,我们选择采用Java

hive 分区字段获取10天账期数据

假设你的 Hive 表名为 `my_table`,分区字段为 `account_date`,需要获取最近 10 天的数据,可以按照以下步骤操作: 1. 首先,获取当前日期并减去 10 天,得到起始日期,比如: ``` start_date=$(date -d "10 days ago" +"%Y-%m-%d") ``` 2. 接下来,使用 Hive 查询语句从分区中筛选出符合条件的数据。查询语句如下: ``` SELECT * FROM my_table WHERE account_date >= '${start_date}' ```

生活垃圾卫生填埋场运营管理手册.pdf

生活垃圾卫生填埋场运营管理手册.pdf

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依