Part Number:MSP430F6638
将HC05蓝牙模块连接到MSP430端口无法接收主机数据,但蓝牙从机连接到电脑上是可以接收到主机的数据的
从机端口代码:
#include "driverlib.h" bool UART_Init(uint16_t baseAddress, uint32_t Baudrate) {float UART_Temp = 0;USCI_A_UART_initParam huart = {0};if(baseAddress == USCI_A0_BASE)//P3.3, P3.4 = USCI_A0 TXD/RXD{GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P3, GPIO_PIN3);GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3, GPIO_PIN4);}else if(baseAddress == USCI_A1_BASE)//P4.4, P4.5 = USCI_A1 TXD/RXD{GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P8, GPIO_PIN2);GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P8, GPIO_PIN3);}if(Baudrate <= 9600){huart.selectClockSource = USCI_A_UART_CLOCKSOURCE_ACLK;UART_Temp = (float)UCS_getACLK()/Baudrate;}else{huart.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;UART_Temp = (float)UCS_getSMCLK()/Baudrate;}if(UART_Temp < 16)huart.overSampling = USCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION;else{huart.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;UART_Temp /= 16;}huart.clockPrescalar = (int)UART_Temp;if(huart.overSampling == USCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION){huart.secondModReg = (int)((UART_Temp - huart.clockPrescalar) * 8);}else{huart.firstModReg = (int)((UART_Temp - huart.clockPrescalar) * 16);}huart.parity = USCI_A_UART_NO_PARITY;huart.msborLsbFirst = USCI_A_UART_LSB_FIRST;huart.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;huart.uartMode = USCI_A_UART_MODE;if (STATUS_FAIL == USCI_A_UART_init(baseAddress, &huart)){return STATUS_FAIL;}//Enable UART module for operationUSCI_A_UART_enable(baseAddress);//Enable Receive InterruptUSCI_A_UART_clearInterrupt(baseAddress, USCI_A_UART_RECEIVE_INTERRUPT);USCI_A_UART_enableInterrupt(baseAddress, USCI_A_UART_RECEIVE_INTERRUPT);return STATUS_SUCCESS; }
从机MAIN:
#include <msp430.h> #include <msp430f6638.h> #include "driverlib.h" #include "clk.h" #include "dr_lcdseg.h"//调用段式液晶驱动头文件 /** * main.c */ int main(void) { WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer P4DIR |=BIT5+BIT6+BIT7; //控制LED5、LED4、LED3的IO口P4.5、P4.6、P4.7设置为输出 P5DIR |=BIT7;//控制LED2的IO口P5.7设置为输出 P8DIR |=BIT0;//控制LED1的IO口P8.0设置为输出 initLcdSeg();//初始化段式液晶 clk_Init (); UART_Init(USCI_A1_BASE, 9600); __bis_SR_register(GIE);while(1){ } } #pragma vector=USCI_A1_VECTOR __interrupt void USCI_A1_ISR (void) {uint8_t receivedData = 0;switch (__even_in_range(UCA1IV,4)){//Vector 2 - RXIFGcase 2:receivedData = USCI_A_UART_receiveData(USCI_A1_BASE);//USCI_A_UART_transmitData(USCI_A1_BASE,receivedData);P8OUT &=~BIT0;P5OUT &=~BIT7;P4OUT ^=BIT7;P4OUT ^=BIT6;P4OUT ^=BIT5;break;default:break;} }
主机端口:
#include "driverlib.h" #include"UART.h" void UARTInit() {GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN3+GPIO_PIN4);USCI_A_UART_initParam param1 = {0};param1.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;param1.clockPrescalar = 52;param1.firstModReg = 1;param1.secondModReg = 0;param1.parity = USCI_A_UART_NO_PARITY;param1.msborLsbFirst = USCI_A_UART_LSB_FIRST;param1.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;param1.uartMode = USCI_A_UART_MODE;param1.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;if (STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, ¶m1)){return;}USCI_A_UART_enable(USCI_A0_BASE); } void FS(unsigned char*write)//向串口写一个字符串 {while(*write!='\0'){USCI_A_UART_transmitData (USCI_A0_BASE,*write);__delay_cycles(5);write++;}USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);USCI_A_UART_transmitData (USCI_A0_BASE,0x0a); } void FSNO(unsigned char*write)//向串口写一个字符串 {while(*write!='\0'){USCI_A_UART_transmitData (USCI_A0_BASE,*write);__delay_cycles(5);write++;} } void YS(int b) {unsigned char a0[2]="0";unsigned char a1[2]="1";unsigned char a2[2]="2";unsigned char a3[2]="3";unsigned char a4[2]="4";unsigned char a5[2]="5";unsigned char a6[2]="6";unsigned char a7[2]="7";unsigned char a8[2]="8";unsigned char a9[2]="9";if(b==0){FSNO(a0);}else if(b==1){FSNO(a1);}else if(b==2){FSNO(a2);}else if(b==3){FSNO(a3);}else if(b==4){FSNO(a4);}else if(b==5){FSNO(a5);}else if(b==6){FSNO(a6);}else if(b==7){FSNO(a7);}else if(b==8){FSNO(a8);}else if(b==9){FSNO(a9);} } void HS(int a) { int b; int c; int e; int f; unsigned char sign[2]="-"; if(a>=0) { b= a%10;//个位数 c= a/ 10 % 10;//十位数 e= a/ 100 % 10;//百位数 f= a/ 1000;//千位数 if(f==0&&e==0&&c==0&&b!=0) { YS(b); } else if(f==0&&e==0&&c!=0) { YS(c); YS(b); } else if(f==0&&e!=0) { YS(e); YS(c); YS(b); } else if(f!=0) {YS(f);YS(e);YS(c);YS(b); } } else if(a<0) { a=-a; b= a%10;//个位数 c= a/ 10 % 10;//十位数 e= a/ 100 % 10;//百位数 f= a/ 1000;//千位数 if(a<10) { FSNO(sign); YS(b); } else if(f==0&&e==0&&c==0&&b!=0) { FSNO(sign); YS(c); YS(b); } else if(f==0&&e==0&&c!=0) { FSNO(sign); YS(e); YS(c); YS(b); } else if(f!=0) {FSNO(sign);YS(f);YS(e);YS(c);YS(b); } } }
蓝牙配对成功,端口波特率均为9600,蓝牙都只能在电脑上接受数据而无法在单片机上接收数据,发送数据是没有问题的,但单片机直接使用串口通信可以接受数据顺利执行代码
Yale Li:
您好,您使用蓝牙接收数据和直接使用串口接收数据用的是一样的代码吗?
,
Su luo:
您好 是的
,
Yale Li:
串口可以接收数据说明您的配置应该是没有问题的。从机也是可以正常发送数据的吧?
,
Su luo:
是的 从机F6638和主机F5529都可以正常发送数据 但无法接收
,
Yale Li:
我建议您先检查一下蓝牙模块的配置,因为直接使用串口可以接收数据,说明问题不是出在这里
,
Su luo:
我们把问题都检查了一遍,在更换主从机(把F5529作为从机,F6638作为主机,蓝牙配置未变动)以后问题得到了解决。猜测应该是F6638单片机本身的问题,无法接收HC05的通讯。
,
Yale Li:
好的,我们已经收到了您的反馈,非常感谢!