项目环境:
- 1. MSP430F5529
- 2. Code Composer Studio
- 3. 蓝牙调试助手
项目简介:
小车可分为3种工作模式,每种工作模式都会打印在OLED显示屏上,通过按键转换工作模式。
模式1: 小车红外循迹,通过超声波实时监测障碍物距离,若超出规定路线,距离障碍物相对较近时,原地停止,等待指令。
模式2: 自主驾驶,通过超声扫描各障碍物距离,当小于一定距离时原地左转。
模式3: 蓝牙远程遥控
本项目用到的模块有:
1. 蓝牙: UART
UART(A0): P3.4、P3.3(RX和TX)
UART(A1): P4.5、P4.4(RX和TX)
2. OLED : IIC
IIC(B0): P3.0、P3.1(SCL和SDA)
IIC(B1): P4.2、P4.1(SCL和SDA)
3. 超声波: TIMER
TA0:P1.1、P1.2、P1.3、P1.4、P1.5
TA1:P1.7、P2.0、P2.1
TA2:P2.3、P2.4、P2.5
TB0:P3.6、P5.6、P5.7、P7.4、P7.5、P7.6、P7.7
以上为该项目需要部分引脚分配,以下为我的模块连接示例:
P3.5、P3.6
P3.0、P3.1
P2.4、P2.5
P3.4、P6.6、P1.6
P2.0(SCL)、P2.2(SDA)
P1.2(echo)、P1.4(Trig)
P4.4(TX)、P4.5(RX)
1. motor_And_infrared_GPIOInit
P3SEL &= ~BIT0; //右轮
P3DIR |= BIT0;
P3SEL &= ~BIT1;
P3DIR |= BIT1;
P3SEL &= ~BIT5; //左轮
P3DIR |= BIT5;
P3SEL &= ~BIT6;
P3DIR |= BIT6;
P3SEL &=~BIT4;
P3DIR &=~BIT4;
P3REN |=BIT4;//左边
P1SEL &=~BIT6;
P1DIR &=~BIT6;
P1REN |=BIT6;//右边
P6SEL &=~BIT6;
P6DIR &=~BIT6;
P6REN |=BIT6;//中间
2. SetPwm_Init
P2DIR |= BIT4; //配置P2.4复用为定时器TA2.4
P2SEL |= BIT4; //配置P2.4为输出
TA2CTL = TASSEL_2+MC_1+ID_3;
TA2CCTL1 = OUTMOD_7 ;
TA2CCR1 = arr;
TA2CCR0 = psc;
P2DIR |= BIT5; //配置P2.5复用为定时器TA2.5
P2SEL |= BIT5; //配置P2.5为输出
TA2CTL = TASSEL_2+MC_1+ID_3;
TA2CCTL2 = OUTMOD_7 ;
TA2CCR2 = arr;
TA2CCR0 = psc;
3. OLED_Init
//OLED初始化函数
void OLED_Init(void)
{
P2DIR |= BIT0; //设置引脚为输出模式
P2DIR |= BIT2;
P2OUT |=BIT0; //设置为高电平
P2OUT |=BIT2;
IIC_SDA_IN0;
delay_ms(200);
IIC_SDA_IN1;
//
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
OLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current Brightness
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0脳贸脫脪路麓脰脙 0xa1脮媒鲁拢
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0脡脧脧脗路麓脰脙 0xc8脮媒鲁拢
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WR_Byte(0x00,OLED_CMD);//-not offset
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
OLED_Clear();
OLED_WR_Byte(0xAF,OLED_CMD);
}
4. BlueTooth_Init
P4SEL |=BIT4+BIT5 ; // P4.5 P4.4 = USCI_A1 TXD/RXD
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL_2; // SMCLK
UCA1BR0 = 9; // 1MHz 115200 (see User's Guide)
UCA1BR1 = 0; // 1MHz 115200
UCA1MCTL |= UCBRS_1 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA1IE |= UCRXIE; // Enable USCI_A1 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
5. TIMER_Init
TA0CCTL0 = CCIE; //CCR0中断使能
TA0CCR0 = a*1000; //设定计数值
TA0CTL =TASSEL_2+MC_1+TACLR;//SMCLK,增计数模式,清除TAR
_bis_SR_register(LPM0_bits+GIE);//低功耗模式0,使能中断
6. HCSR04_Init
USONUD_OUT |= TRIG;
USOUND_DIR |= TRIG;
USOUND_SEL |= ECHO ; //CCI0A
7. Key_Init
P1DIR &=~BIT1;//板载按键s2设为输入
P2DIR &=~BIT1;//板载按键s1设为输入
P2REN =BIT1;//上拉电阻
P1REN =BIT1;//上拉电阻
P1OUT |=BIT1;
P2OUT |=BIT1;//初始状态为高电平
//低电平触发函数
8. interrupt
// Echo back RXed character, confirm TX buffer is ready first,发送数据之前确定发送缓存准备好
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
switch(__even_in_range(UCA1IV,4))
{
case 0: //无中断
break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG 接受中断
while (!(UCA1IFG&UCTXIFG)); // USCI_A1 TX buffer ready? UCTXIFG(USCI Transmit Interrupt Flag)
UCA1TXBUF = UCA1RXBUF; //等待数据发送完成 完成UCTXIFG置1 跳出循环 // TX -> RXed character
break;
case 4:
break; // Vector 4 - TXIFG 发送中断
default: break;
}
}
// UCTXIFG=0x02,UCA1IFG&UCTXIFG,当UCA1IFG的UCTXIFG位为1时,说明UCA1TXBUF为空,
//跳出while循环循环;当UCTXIFG位为0时UCA1TXBUF不为空,停在循环。
若需项目源码可留言评论区QQ邮箱 或 私信即可。
着急的小伙伴可直接加好友联系