zoukankan      html  css  js  c++  java
  • 二、单片机——串口通信及超声波(下)

     1 #include "reg52.h"
     2 #include "ultrasonic.h"
     3 #include "serialcommunication.h"
     4 
     5 char begin[] = {"		开始测试(超声波开始)!!!!
    "};
     6 void main(){
     7     UsartInit();
     8     InitTimer();
     9     sendString(begin);
    10     working();
    11 }
    Main.c
     1 #include "SerialCommunication.h"
     2 
     3 unsigned int flag = 0;
     4 void UsartInit()
     5 {
     6     SCON |= 0X50;            //设置为工作方式1
     7     TMOD |= 0X20;            //设置计数器工作方式2
     8     PCON |= 0X80;            //波特率加倍
     9     TH1 = 0XF3;            //计数器初始值设置,注意波特率是4800的
    10     TL1 = 0XF3;
    11     ES = 1;                //打开接收中断
    12     EA = 1;                //打开总中断
    13     TR1 = 1;                //打开计数器
    14 }
    15 /*
    16     发送单个数据
    17 */
    18 void sendChar(unsigned char value){
    19     SBUF = value;
    20     flag = 1;
    21     while(flag);
    22 }
    23 /*
    24     发送字符串
    25 */
    26 void sendString(unsigned char *value){
    27     while( (*value)!= '' ){
    28         sendChar( (*value) );
    29         while(flag);
    30         value++;
    31     }
    32 }
    33 /*
    34     串口中断
    35 */
    36 void Usart() interrupt 4
    37 {
    38     while(!TI);
    39     TI = 0;
    40     flag = 0;
    41 }
    SerialCommunication.c
    1 #include "reg52.h"
    2 
    3 void UsartInit();
    4 void sendChar(unsigned char value);
    5 void sendString(unsigned char *value);
    SerialCommunication.h
      1 #include "ultrasonic.h"
      2 
      3 unsigned long S = 0;
      4 unsigned int time = 0;
      5 char disbuff[6];
      6 static unsigned int timeFlag = 0;
      7 
      8 void InitTimer(){
      9     TMOD |= 0x01;
     10     TH0 = 0;
     11     TL0 = 0;
     12     ET0 = 0;
     13     EA = 1;
     14 }
     15 /*
     16     终端
     17 */
     18 void timeInter() interrupt 1{
     19     timeFlag = 1;
     20 }
     21 /*
     22     延时
     23     单位:ms
     24 */
     25 void delayms(unsigned int ms){
     26     unsigned char i = 100,j;
     27     for(;ms;ms--){
     28         while(i--){
     29             j=10;
     30             while(--j);
     31         }
     32     }
     33 }
     34 /*
     35     模块时序图
     36 */
     37 void startModel(){
     38     Trig = 1;
     39     _nop_(); 
     40     _nop_(); 
     41     _nop_(); 
     42     _nop_(); 
     43     _nop_(); 
     44     _nop_(); 
     45     _nop_(); 
     46     _nop_(); 
     47     _nop_(); 
     48     _nop_(); 
     49     _nop_(); 
     50     _nop_(); 
     51     _nop_(); 
     52     _nop_(); 
     53     _nop_(); 
     54     _nop_();
     55     _nop_(); 
     56     _nop_(); 
     57     _nop_(); 
     58     _nop_();
     59     Trig = 0;
     60 }
     61 /*
     62     计算距离
     63 */
     64 void computedRange(){
     65     time = TH0*256+TL0;
     66     TH0 = 0;
     67     TL0 = 0;
     68     S = (time*1.7)/100;    
     69     if( S>=700 || timeFlag == 1){
     70         timeFlag = 0;
     71         sendError();
     72     }else{
     73         disbuff[0] = S%1000/100 + '0';
     74         disbuff[1] = S%1000%100/10 + '0';
     75         disbuff[2] = S%10 + '0';
     76     }
     77 }
     78 /*
     79     发送错误
     80 */
     81 void sendError(){
     82     sendString("ERROR
    
    ");
     83 }
     84 /*
     85     发送距离
     86 */
     87 void sendRange(){
     88     sendString("距离是: ");
     89     sendString(disbuff);
     90     sendString(" cm
    ");
     91 }
     92 /*
     93     运行
     94 */
     95 void working(){
     96     while(1){
     97         startModel();
     98         while(!Echo);        //当RX为零时等待
     99         TR0=1;                //开启计数
    100         while(Echo);            //当RX为1计数并等待
    101         TR0=0;                //关闭计数
    102         computedRange();
    103         sendRange();
    104         delayms(80);
    105     }
    106 }
    ultrasonic.c
     1 #include "reg52.h"
     2 #include "intrins.h"
     3 #include "serialcommunication.h"
     4 
     5 sbit Trig = P2^0;
     6 sbit Echo = P2^1;
     7 
     8 void InitTimer();
     9 void delayms(unsigned int ms);
    10 void startModel();
    11 void sendError();
    12 void computedRange();
    13 void sendRange();
    14 void working();
    ultrasonic.h
  • 相关阅读:
    浅谈命令混淆
    为你解惑之Silverlight经典10问详解 (转载)
    Prism 简介
    Prism学习笔记(二)简单的MVVM模式
    Prism学习笔记(一) 从Hello World开始
    修改Oracle数据库序列
    将身份证号转换为年龄
    获取文件类型
    下划线转驼峰
    驼峰转下划线
  • 原文地址:https://www.cnblogs.com/hixkill/p/8037198.html
Copyright © 2011-2022 走看看