uint32 bluetooth_ch9141_read_buff (uint8 *buff, uint32 len) { uint32 data_len = len; fifo_read_buffer(&bluetooth_ch9141_fifo, buff, &data_len, FIFO_READ_AND_CLEAN); return data_len; }uint32 bluetooth_ch9141_send_buff (uint8 *buff, uint32 len) { uint16 time_count = 0; while(len > 30) { time_count = 0; while(BLUETOOTH_CH9141_RTS_PIN && time_count++ < BLUETOOTH_CH9141_TIMEOUT_COUNT) // 如果RTS为低电平,则继续发送数据 delay_ms(1); if(time_count >= BLUETOOTH_CH9141_TIMEOUT_COUNT) return len; // 模块忙,如果允许当前程序使用while等待 则可以使用后面注释的while等待语句替换本if语句 uart_putbuff(BLUETOOTH_CH9141_INDEX, buff, 30); buff += 30; // 地址偏移 len -= 30; // 数量 } time_count = 0; while(BLUETOOTH_CH9141_RTS_PIN && time_count++ < BLUETOOTH_CH9141_TIMEOUT_COUNT) // 如果RTS为低电平,则继续发送数据 delay_ms(1); if(time_count >= BLUETOOTH_CH9141_TIMEOUT_COUNT) return len; // 模块忙,如果允许当前程序使用while等待 则可以使用后面注释的while等待语句替换本if语句 uart_putbuff(BLUETOOTH_CH9141_INDEX, buff, (uint16)len); // 发送最后的数据 return 0; }uint8 bluetooth_ch9141_init (void) { wireless_type = WIRELESS_CH9141; // 本函数使用的波特率为115200 为蓝牙转串口模块的默认波特率 如需其他波特率请使用上位机修改模块参数 fifo_init(&bluetooth_ch9141_fifo, bluetooth_ch9141_buffer, BLUETOOTH_CH9141_BUFFER_SIZE); uart_init(BLUETOOTH_CH9141_INDEX, BLUETOOTH_CH9141_TX_PIN, BLUETOOTH_CH9141_RX_PIN, BLUETOOTH_CH9141_BUAD_RATE, BLUETOOTH_CH9141_TIMER); return 0; } void bluetooth_ch9141_uart_callback (void) { // 读取无线串口的数据 并且置位接收标志 bluetooth_ch9141_data = BLUETOOTH_CH9141_DATA_BUF; fifo_write_buffer(&bluetooth_ch9141_fifo, &bluetooth_ch9141_data, 1); // 存入 FIFO } static fifo_struct bluetooth_ch9141_fifo; static uint8 bluetooth_ch9141_buffer[BLUETOOTH_CH9141_BUFFER_SIZE]; // 数据存放数组 static uint8 bluetooth_ch9141_data;以以上代码为示例编写一个适合于HC-05的蓝牙代码
时间: 2024-04-21 10:22:28 浏览: 95
以下是适用于 HC-05 蓝牙模块的修改后的代码示例:
```c
uint32 bluetooth_hc05_read_buff(uint32* buff, uint32 len)
{
uint32 data_len = len * sizeof(uint32);
fifo_read_buffer(&bluetooth_hc05_fifo, (uint8*)buff, &data_len, FIFO_READ_AND_CLEAN);
return data_len / sizeof(uint32);
}
uint32 bluetooth_hc05_send_buff(uint32* buff, uint32 len)
{
uint16 time_count = 0;
while (len > 30)
{
time_count = 0;
while (BLUETOOTH_HC05_RTS_PIN && time_count++ < BLUETOOTH_HC05_TIMEOUT_COUNT)
delay_ms(1); // 如果 RTS 为低电平,则继续发送数据
if (time_count >= BLUETOOTH_HC05_TIMEOUT_COUNT)
return len; // 模块忙,如果允许当前程序使用 while 等待,则可以使用后面注释的 while 等待语句替换本 if 语句
uart_putbuff(BLUETOOTH_HC05_INDEX, (uint8*)buff, 30);
buff += 30; // 地址偏移
len -= 30; // 数量
}
time_count = 0;
while (BLUETOOTH_HC05_RTS_PIN && time_count++ < BLUETOOTH_HC05_TIMEOUT_COUNT) // 如果 RTS 为低电平,则继续发送数据
delay_ms(1);
if (time_count >= BLUETOOTH_HC05_TIMEOUT_COUNT)
return len; // 模块忙,如果允许当前程序使用 while 等待,则可以使用后面注释的 while 等待语句替换本 if 语句
uart_putbuff(BLUETOOTH_HC05_INDEX, (uint8*)buff, (uint16)len); // 发送最后的数据
return 0;
}
uint8 bluetooth_hc05_init(void)
{
wireless_type = WIRELESS_HC05;
// 本函数使用的波特率为 9600,为 HC-05 蓝牙模块的默认波特率,如需其他波特率请使用上位机修改模块参数
fifo_init(&bluetooth_hc05_fifo, bluetooth_hc05_buffer, BLUETOOTH_HC05_BUFFER_SIZE);
uart_init(BLUETOOTH_HC05_INDEX, BLUETOOTH_HC05_TX_PIN, BLUETOOTH_HC05_RX_PIN, BLUETOOTH_HC05_BUAD_RATE, BLUETOOTH_HC05_TIMER);
return 0;
}
void bluetooth_hc05_uart_callback(void)
{
// 读取无线串口的数据,并且置位接收标志
bluetooth_hc05_data = BLUETOOTH_HC05_DATA_BUF;
fifo_write_buffer(&bluetooth_hc05_fifo, &bluetooth_hc05_data, 1); // 存入 FIFO
}
static fifo_struct bluetooth_hc05_fifo;
static uint8 bluetooth_hc05_buffer[BLUETOOTH_HC05_BUFFER_SIZE]; // 数据存放数组
static uint8 bluetooth_hc05_data;
```
请注意,上述代码仅为示例,具体的引脚定义和常量值需要根据你的硬件和需求进行修改。另外,需要根据实际情况修改函数中的波特率、缓冲区大小等参数。
阅读全文
相关推荐
















