Using UART and PC communication to realize ultrasonic distance measurement of msp430g2553 single-chip computer sample _c language

Source: Internet
Author: User

It is suitable for msp430g2553 MCU to use the HC-SR04 ultrasonic ranging module and communicates with PC using UART.

Copy Code code as follows:

#include <msp430.h>
The last time a long current_time;//was measured
/*mypro*/
#define LED_1 BIT0
#define SW_2 BIT3
#define Ta1_1 BIT2//ta0.1 hc-sr04 Echo
#define TRIG BIT4//hc-sr04 TRIG
#define ACCU_GRD 7 Grade "xxx.x" + '//accuracy '
#define MIN_UNIT_TO1M 1000//1 m to 1 mm

/*timer0*/
#define SOUR_CLK 1045000
#define DIV_CLK_1 1
#define SYS_CLK_SIG_1 Sour_clk/div_clk_1
#define DISTANCE 45//DM
#define TIMER_RIG_MAX 0xFFFF
#define Max_time_1 distance*2/34*sys_clk_sig_1
#ifdef Max_time_1
#define SYS_CLK sys_clk_sig_1
#endif
#define UART_TXD BIT2

void Init_timer1 ()
{
P2sel |= ta1_1; TA1.1 cci1b be used
P2dir &= ~ta1_1;
Ta1ctl = Mc_0 + tassel_2; Timera_0 Stop Mode,clock=1mhz
TA1CCTL1 = CCIE + Scci + ccis_1 + SCS + CAP + cm_2; TA0CC1 capture mode + down edge
}
/*uart*/
void Init_uart ()//usci initialization function
{
UCA0CTL1 |= Ucswrst; Initializing serial port Registers
UCA0CTL1 |= ucssel_2; Select Subsystem Clock 1.045MHz
Uca0br0 = 0x6d; Baud rate is 9600
UCA0BR1 = 0x00;
P1sel |= Uart_txd;
P1sel2 |= Uart_txd;
P1dir |= Uart_txd; Set the P1.2 to the second function
UCA0CTL1 &= ~ucswrst; Initialization end
}
Long cal_distance ()//distance calculated by measured time
{
Return (long) ((340* (0.5000*CURRENT_TIME/SYS_CLK) *min_unit_to1m));
}
void delay ()//Rough Latency
{
unsigned char i,j;
for (i=124;i>0;i--)
for (j=8;j>0;j--);
}
Static char * Translater (long distance)//storing the measured distance as a string
{
static Char TRANS[ACCU_GRD];
int i;
Long F;
Trans[0]= ';
if (distance)
for (i=1,f=min_unit_to1m;i<accu_grd-1;i++)//Transformation Core algorithm
{
if (i==4)
{
Trans[i] = '. ';
Continue
}
Trans[i] = ' 0 ' + distance/f;
Distance%= F;
F/= 10;
}
Trans[accu_grd-1] = ' the ';
return trans;
}
void Once_pro ()//Send an ultrasound
{
if (TA1CCTL1 & COV)
TA1CCTL1 &= ~cov;
if (!) ( p1in & BIT3))
{
ta1r = 0;
P1out |= TRIG; Trig US High level

_eint ();
Ta1ctl |= mc_2; Continue mode
P1out &= ~trig;
P1out |= led_1;
while (TA1CCTL1 & CCIFG); Wait for capture interrupt to end
}
Else
{
P1out &= ~led_1;
_dint ();
}
}
void Uart_txstring (char *string)//UART_TX sends a string
{
int i=0;
while (string[i++])
{
switch (i)//filter invalid ' 0 '
{
Case 1:if (string[i]== ' 0 ') continue;
Case 2:if (string[i]== ' 0 ' &&string[i-1]== ' 0 ') continue;
}
Uca0txbuf = String[i];
Delay ();
}
}
/*uart_isr*/
#pragma vector = Usciab0tx_vector
__interrupt void Usci_txdistance ()//send measured distance to PC
{
Uart_txstring ("\n\r");
Uart_txstring ("current");
Uart_txstring ("Distance:");
Uart_txstring (Translater (Cal_distance ()));
Uart_txstring ("cm");
IE2 &= ~uca0txie;
}
/*timer0_int_isr*/
#pragma vector = Timer1_a1_vector
__interrupt void Capture ()
{
Current_time = TA1CCR1;
Ta1ctl &= ~mc_2;
TA1CCTL1 &= ~CCIFG; Clear CC1 interrupt Mark Bit
IE2 |= Uca0txie;
}
/*main*/
void Main ()
{
Wdtctl = Wdtpw + wdthold; Shut the dog.
Dcoctl = 0;
BCSCTL1 = Calbc1_1mhz;
Dcoctl = Caldco_1mhz;
p1out = 0;
p2out = 0;
P1ren |= sw_2;
P2ren |= ta1_1;
P1out |= sw_2;
P1dir &= ~sw_2;
P1dir = TRIG + led_1;
Init_timer1 ();
Init_uart ();
while (1)
{
int c = 8;
while (c--)
Delay ();
if (TA1CCTL1 & CCIFG)
TA1CCTL1 &= ~CCIFG;
Once_pro ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.