>_<:Ultrasonic Range Finder module:
>_<:51 single-chip microcomputer, 11.0592mhz crystal oscillator, the basic example of sending the collected data to the serial port:
>_<:Code:
1 /************************************** **************************************** * // Crystal Oscillator: 11. 0592 // wiring: module trig connection p1.2 ech0 connection p1.1 5 // serial port baud rate 9600 6 /************************* **************************************** **************************************** **/7 # include <at89x51. h> 8 # include <intrins. h> 9 # include <stdio. h> 10 11 # define uchar unsigned char 12 # define uint unsigned INT 13 # define RX p1_1 14 # define TX p1_2 15 16 17 unsigned int time = 0; 18 unsigned int timer = 0; 19 float S = 0; 20 Bit Flag = 0; 21 22 23 /************************************ * ******************/24 void conut (void) 25 {26 time = th0 * 256 + tl0; 27 th0 = 0; 28 tl0 = 0; 29 s = (time * 1.87)/100; // calculated as cm 30 if (flag = 1) // exceeds measurement 31 {32 flag = 0; 33 printf ("----- \ n "); 34} 35 printf ("s = % F \ n", S ); 36} 37 /************************************ * *****************/38 void delayms (unsigned int MS) 39 {40 unsigned char I = 100, J; 41 For (; MS; Ms --) 42 {43 while (-- I) 44 {45 J = 10; 46 While (-- J ); 47} 48} 49} 50 /******************************** * **********************/51 void zd0 () interrupt 1 // t0 interrupt is used for Counter Overflow, beyond the range of 52 {53 flag = 1; // interruption overflow mark 54} 55 /******************************* * ***********************/56 void startmodule () // T1 interrupt is used to scan the digital tube and 800 ms boot module 57 {58 Tx = 1; // Ms start module 59 _ NOP _ (); 60 _ NOP _(); 61 _ NOP _ (); 62 _ NOP _( ); 63 _ NOP _ (); 64 _ NOP _ (); 65 _ NOP _ (); 66 _ NOP _ (); 67 _ NOP _(); 68 _ NOP _ (); 69 _ NOP _ (); 70 _ NOP _ (); 71 _ NOP _ (); 72 _ NOP _(); 73 _ NOP _ (); 74 _ NOP _ (); 75 _ NOP _ (); 76 _ NOP _ (); 77 _ NOP _(); 78 _ NOP _ (); 79 _ NOP _ (); 80 Tx = 0; 81} 82 /************************************ * ******************/83 void main (void) 84 {85 tmod = 0x21; // set t0 as Mode 1, gate = 1; 86 scon = 0x50; 87 Th1 = 0xfd; 88 TL1 = 0xfd; 89 th0 = 0; 90 tl0 = 0; 9 1 tr0 = 1; 92 et0 = 1; // allows t0 to interrupt 93 tr1 = 1; // enables the timer 94 Ti = 1; 95 96 Ea = 1; // enable the total interrupt 97 98 99 while (1) 100 {101 startmodule (); 102 while (! RX); // wait for 103 tr0 = 1 when Rx is set to 0; // enable 104 while (RX) When Rx is set to 1 and wait for 105 tr0 = 0; // disable count 106 conut (); // calculate 107 delayms (10); // 100ms108} 109}