Example of using UART to communicate with PC to implement Ultrasonic Ranging of msp430g2553 Single Chip Microcomputer

Source: Internet
Author: User

This article mainly introduces how to use UART to communicate with PC to implement the Ultrasonic Ranging example of the msp430g2553 single-chip microcomputer. If you need a friend, refer

It is applicable to the hc-sr04 Ultrasonic Ranging module and UART communication with PC.

The Code is as follows:

# Include

Long current_time; // The last measured time

/* 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 // Accuracy Grade "xxx. x" + '\ 0'

# Define MIN_UNIT_TO1M 1000 // 1 m to 1mm

/* 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 0 xffff

# 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 = 1 Mhz

TA1CCTL1 = CCIE + SCCI + CCIS_1 + SCS + CAP + CM_2; // ta0c0capture mode + down edge

}

/* UART */

Void init_uart () // USCI initialization Function

{

UCA0CTL1 | = UCSWRST; // initialize the serial register

UCA0CTL1 | = UCSSEL_2; // select the sub-system clock at 1.045 MHz.

UCA0BR0 = 0x6d; // The baud rate is 9600

UCA0BR1 = 0x00;

P1SEL | = UART_TXD;

P1SEL2 | = UART_TXD;

P1DIR | = UART_TXD; // set P1.2 to the second function

UCA0CTL1 & = ~ UCSWRST; // initialization ends

}

Long cal_distance () // calculate the distance through the 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) // store 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

{

If (I = 4)

{

Trans [I] = '.';

Continue;

}

Trans [I] = '0' + distance/f;

Distance % = f;

F/= 10;

}

Trans [ACCU_GRD-1] = '\ 0 ';

Return trans;

}

Void once_pro () // initiate an ultrasound

{

If (TA1CCTL1 & COV)

TA1CCTL1 & = ~ COV;

If (! (P1IN & BIT3 ))

{

TA1R = 0;

P1OUT | = TRIG; // Trig 10 us high

_ EINT ();

TA1CTL | = MC_2; // continue mode

P1OUT & = ~ TRIG;

P1OUT | = LED_1;

While (TA1CCTL1 & CCIFG); // wait until the capture interruption ends.

}

Else

{

P1OUT & = ~ LED_1;

_ DINT ();

}

}

Void uart_txstring (char * string) // UART_TX sends a string

{

Int I = 0;

While (string [I ++])

{

Switch (I) // The filter is 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 the measured distance to the 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; // clears the CCIFG

IE2 | = UCA0TXIE;

}

/* Main */

Void main ()

{

WDTCTL = WDTPW + WDTHOLD; // close 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 ();

}

}

Note: For more exciting articles, please follow the help houseProgramming TutorialTopic.

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.