Mc9s12xs128 Ultrasonic Ranging

Source: Internet
Author: User

Let's talk about the pins of the Ultrasonic Ranging module:

 

VCC, echo (receiving end), tring (control end), Out (suspended), Gnd

Sequence diagram:

 

Reference code:

Development Environment: freescalcodewarrior 5.0

/*
* Authour: flyreally
*
* A self-written Ultrasonic Ranging module, which is made using the query method,
* Of course, you can also use the interrupt.
*
*/

# Include # Include <mc9s12xs128. h>/* derivative information */
# Pragma link_info derivative "mc9s12xs128"

/* You can change the timer value to implement timing at different times,
The timer length is timer * 10us */
# Define timer 1
# Define tring porta_pa0 // Control End
# Define echo porta_pa1 // Acceptor

Ulong time = 0;

/* N * 10us latency */
Void delay_us (ulong N)
{
Time = N;
While (time );
}

/* IO port initialization */
Void io_init (void)
{
Ddra = 0x02; // A0 output, A1 Input
}


/* Pit initialization to implement 10us timing */
Void pit_init (void)
{
Pitcflmt_pite = 0; // scheduled interrupt channel 0
Pitce_pce0 = 1; // timer channel 0 Enabled
Pitmtld0 = 160-1; // set the initial value of the 8-bit timer, with a 160-bit Frequency Division
Pitld0 = timer-1; // 16-bit timer Initial Value Setting, Timer * 10us
Pitinte_pinte0 = 1; // timer interrupt channel 0 interrupt enabling
Pitcflmt_pite = 1; // timer channel 0 Enabled
}

Void main (void)
{
Ulong T;
Double DIS;

Io_init (); // Io Initialization
Pit_init (); // pit Initialization
Enableinterrupts; // enable interrupt

/* Wait For 200us to complete ultrasonic module initialization */
Delay_us (20 );

While (1)
{
/* Give the control end a high level that exceeds 10 us, and then set it to low */
Tring = 1;
Delay_us (2 );
Tring = 0;

/* Wait for the receiving end to generate a high level and timing */
While (! Echo );
Time = 0xff;
While (ECHO );
T = 0xff-time; // timing
Dis = 1.7 * t; // calculates the distance.
// The delay is 10 ms. For this reason, we can see the time sequence diagram. Different hardware may vary.
Delay_us (1000 );}
}

/* Scheduled pit interruption */
# Pragma code_seg _ near_seg non_banked
Void interrupt 66 pit0 (void)
{
Pittf_ptf0 = 1; // clear the interrupt flag
-- Time;
}

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.