唯爱洛天依
2022-07-05 15:19
采纳率: 0%
浏览 440

用hc-05蓝牙进行两msp430的串口通信失败

我们的目的是用mpu6050控制led灯的亮灭。我们用msp430f5529作为主机处理mpu6050数据,用msp430f6638作为从机接受主机数据及控制led灯电路。用蓝牙模块进行两单片机间的数据传输,ccs用的8.1版本。
实验进行中,我们先是用串口连接两单片机,然后运行ccs程序,可以观察到正确的现象,说明程序和串口没有问题。然后主机用hc05蓝牙发送数据,用电脑端口接蓝牙接受数据,可以在串口助手上看到正确的数据,说明蓝牙模块也没有问题。
然后我们用蓝牙来进行两单片机的连接,就没有了任何现象,我们实在是不知道出了什么问题。
主机:

img


从机:

img


用串口连接两单片机:

img


用电脑接受主机数据:

img

img

用蓝牙连接两单片机:

img

主机主函数及串口程序:

#include "driverlib.h"
#include "mpu6050/Mpu-6050.h"
#include "UART.h"
#include "clk.h"
#include "OLED.h"

int main(void)
{
    int a;
    int b;
    int c;
    unsigned char f[3]="°";
    unsigned char x[15]="X轴角度---  ";
    unsigned char y[15]="Y轴角度---  ";
    unsigned char z[15]="Z轴角度---  ";
    unsigned char h[30]="--------------------";


  WDT_A_hold(WDT_A_BASE);
  InitMPU6050();
  UARTInit();
  clk_Init ();
  OLED_Init();//oled初始化
  P1DIR |=BIT0;
  P1OUT |=BIT0;
  while(1)
  {

      a=Mpu6050AccelAngle(ACCEL_XOUT);
      //b=Mpu6050AccelAngle(ACCEL_YOUT);
      //c=Mpu6050AccelAngle(ACCEL_ZOUT);
      if(a<0){
          a=-a;
      }
      OLED_Clear();
      OLED_ShowNum(6,0,a,7,16);
      //OLED_ShowNum(6,2,(int)b,7,16);
      //OLED_ShowNum(6,4,(int)c,7,16);
      __delay_cycles(8000000);

      //FS(h);
      //FSNO(x);
      HS(a);
      //FS(f);
      //FSNO(y);
      //HS(b);
      //FS(f);
      //FSNO(z);
      //HS(c);
      //FS(f);


  }


}



#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, &param1)){
        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)
{
//USCI_A_UART_transmitData (USCI_A0_BASE,0x45);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x35);
YS(0);
//YS(b);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}
else if(f==0&&e==0&&c!=0)
{
YS(c);
//YS(b);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}
else if(f==0&&e!=0)
{
 YS(e);
 YS(c);
 YS(b);
 USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
 USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}
else if(f!=0)
{
    YS(f);
    YS(e);
    YS(c);
    YS(b);
    USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
    USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}
}
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(0);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}
else if(f==0&&e==0&&c==0&&b!=0)
{
//FSNO(sign);
YS(c);
//YS(b);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
//USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}
else if(f==0&&e==0&&c!=0)
{
FSNO(sign);
 YS(e);
 YS(c);
 YS(b);
 USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
 USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}
else if(f!=0)
{
    FSNO(sign);
    YS(f);
    YS(e);
    YS(c);
    YS(b);
    USCI_A_UART_transmitData (USCI_A0_BASE,0x0d);
    USCI_A_UART_transmitData (USCI_A0_BASE,0x0a);
}

从机主函数及串口程序:


#include <msp430.h> 
#include <msp430f6638.h>
#include "driverlib.h"
#include "clk.h"
unsigned char receivedData = 0;
/**
 * main.c
 */
void UART_RS232_Init(void)  //RS232接口初始化函数
{
    /*通过对P3.4。P3.5,P4.4,P4.5的配置实现通道选择
         使USCI切换到UART模式*/
    P3DIR|=(1<<4)|(1<<5);
    P4DIR|=(1<<4)|(1<<5);
    P4OUT|=(1<<4);
    P4OUT&=~(1<<5);
    P3OUT|=(1<<5);
    P3OUT&=~(1<<4);
    P8SEL|=0x0c;    //模块功能接口设置,即P8.2与P8.3作为USCI的接收口与发射口
    UCA1CTL1|=UCSWRST;  //复位USCI
    UCA1CTL1|=UCSSEL_1; //设置辅助时钟,用于发生特定波特率
    UCA1BR0=0x03;       //设置波特率
    UCA1BR1=0x00;
    UCA1MCTL=UCBRS_3+UCBRF_0;
    UCA1CTL1&=~UCSWRST; //结束复位
    UCA1IE|=UCRXIE;     //使能接收中断
}

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设置为输出
    P2DIR |=BIT1+BIT2;
    clk_Init ();
    UART_RS232_Init();
    //OLED_Init();//oled初始化
    //OLED_Clear();
    //OLED_ShowChar(6,0,'b',16);
    //UART_Init(USCI_A1_BASE, 9600);
    __bis_SR_register(GIE);
    while(1)
    {


    }
}

#pragma vector=USCI_A1_VECTOR   //USCI中断服务函数,无论是单片机串口发送还是接
//收都会进入这个中断服务程序,但是串口初始化时,只是开启了串口接收中断,只有单片
//机串口接收到数据才会进入这个中断
__interrupt void USCI_A1_ISR(void)

{
    switch(__even_in_range(UCA1IV,4))
    {
    case 0:break;           //无中断
    case 2:                 //接收中断处理
        //while(!(UCA1IFG&UCRXIFG));    //等待完成接收,加上词句运行结
//果不稳定,有时PC机和单片机通讯不起,最早用UCTXIFG没有问题,但是这个寄存器是发送
//寄存器UCRXIFG
        receivedData = UCA1RXBUF;
        if(receivedData>0x33){
           P2OUT |=BIT1;
           P2OUT &=~BIT2;
        }
        if(receivedData<0x33){
           P2OUT &=~BIT1;
           P2OUT |=BIT2;
        }
        break;
    case 4:break;           //发送中断不处理
    default:break;          //其他情况无操作

    }
}
/*#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR (void)
{
    //uint8_t receivedData = 0;
    unsigned char receivedData = 0;

    switch (__even_in_range(UCA1IV,4))
    {
        //Vector 2 - RXIFG
        case 2:
            receivedData = USCI_A_UART_receiveData(USCI_A1_BASE);
            if(receivedData>0x33){
                P2OUT |=BIT1;
                P2OUT &=~BIT2;
            }
            if(receivedData<0x33){
                P2OUT &=~BIT1;
                P2OUT |=BIT2;
            }
            //USCI_A_UART_transmitData(USCI_A1_BASE,receivedData);


            break;
        default:
            break;
    }
}
*/
#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 operation
    USCI_A_UART_enable(baseAddress);

    //Enable Receive Interrupt
    USCI_A_UART_clearInterrupt(baseAddress, USCI_A_UART_RECEIVE_INTERRUPT);
    USCI_A_UART_enableInterrupt(baseAddress, USCI_A_UART_RECEIVE_INTERRUPT);

    return STATUS_SUCCESS;
}

图片转代码服务由CSDN问答提供 功能建议

 东电机
    XcOM
 XCOMV2.6
                                DONYA
                                       串口选择
                                     COM4:USB
                                  波特室
                               停止位
                          校验位
                       串口操1

2条回答 默认 最新

相关推荐 更多相似问题