我正在Silicon Labs IDE的8051微控制器上编写代码。
在我的代码中,我试图与HC-06模块进行通信,并通过与UART连接到我的微控制器板的android应用发送和接收数据。
但是我无法接收从微控制器向android应用发送的数据,而且我也无法接收从微控制器上的android应用接收的数据。
当我检查UART的Rx和Tx时,数据已正确发送。因此问题出在HC-06模块中。
这是我的UART中断代码(在Silicon Labs IDE中):
void UART0_BT_ISR(void) interrupt 4
{
if(TI0==1)
{
TI0=0;
while(TI0 == 0);
TI0=0;
}
if(RI0==1)
{
UART0_Buffer[BTi]=SBUF0;
BTi++;
RI0 = 0;
}
}
void SendUARTData(char* FunkKey)
{
i=0;
while(1)
{
if(TI0==0)
{
SBUF0=FunkKey[i];
i++;
TI0=1;
}
if(i>16)
{
return;
i=0;
}
}
}
这是我的init_devide代码:
#include "compiler_defs.h"
#include "C8051F020_defs.h"
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Reset_Sources_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD;
}
void Timer_Init()
{
CKCON = 0x30;
TCON = 0x40;
TMOD = 0x20;
TH1 = 0xB8;
T2CON = 0x34;
RCAP2L = 0xB8;
RCAP2H = 0xFF;
}
void UART_Init()
{
SCON0 = 0x50;
SCON1 = 0x40;
}
void Port_IO_Init()
{
// P0.0 - TX0 (UART0), Push-Pull, Digital
// P0.1 - RX0 (UART0), Open-Drain, Digital
// P0.2 - TX1 (UART1), Push-Pull, Digital
// P0.3 - RX1 (UART1), Open-Drain, Digital
// P0.4 - Unassigned, Open-Drain, Digital
// P0.5 - Unassigned, Open-Drain, Digital
// P0.6 - Unassigned, Open-Drain, Digital
// P0.7 - Unassigned, Open-Drain, Digital
// P1.0 - Unassigned, Push-Pull, Digital
// P1.1 - Unassigned, Push-Pull, Digital
// P1.2 - Unassigned, Push-Pull, Digital
// P1.3 - Unassigned, Open-Drain, Digital
// P1.4 - Unassigned, Open-Drain, Digital
// P1.5 - Unassigned, Push-Pull, Digital
// P1.6 - Unassigned, Push-Pull, Digital
// P1.7 - Unassigned, Push-Pull, Digital
// P2.0 - Unassigned, Open-Drain, Digital
// P2.1 - Unassigned, Open-Drain, Digital
// P2.2 - Unassigned, Open-Drain, Digital
// P2.3 - Unassigned, Open-Drain, Digital
// P2.4 - Unassigned, Open-Drain, Digital
// P2.5 - Unassigned, Open-Drain, Digital
// P2.6 - Unassigned, Open-Drain, Digital
// P2.7 - Unassigned, Open-Drain, Digital
// P3.0 - Unassigned, Open-Drain, Digital
// P3.1 - Unassigned, Open-Drain, Digital
// P3.2 - Unassigned, Open-Drain, Digital
// P3.3 - Unassigned, Open-Drain, Digital
// P3.4 - Unassigned, Push-Pull, Digital
// P3.5 - Unassigned, Push-Pull, Digital
// P3.6 - Unassigned, Push-Pull, Digital
// P3.7 - Unassigned, Push-Pull, Digital
P0MDOUT = 0x05;
P1MDOUT = 0xE7;
P3MDOUT = 0xF0;
XBR0 = 0x04;
XBR2 = 0x44;
}
void Oscillator_Init()
{
int i = 0;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
OSCICN = 0x0C;
}
void Interrupts_Init()
{
IE = 0x90;
IP = 0x10;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Reset_Sources_Init();
Timer_Init();
UART_Init();
Port_IO_Init();
Oscillator_Init();
Interrupts_Init();
}
我的波特率设置为9600。
BT HC-06连接到UART0。
您能告诉我我做错了什么,为什么我的BT HC-06无法正常工作吗?
非常感谢你 棚架
答案 0 :(得分:1)
确保满足以下基本条件,
连接
代码检查