標(biāo)題: 單片機超聲波測距源程序 [打印本頁]

作者: kengkeng    時間: 2018-2-3 20:43
標(biāo)題: 單片機超聲波測距源程序
單片機源程序如下:
  1. #include"lcd.h"


  2. void Read_Busy()           //忙檢測函數(shù),判斷bit7是0,允許執(zhí)行;1禁止
  3. {
  4.     unsigned char sta;      //
  5.     LCD1602_DB = 0xff;
  6.     LCD1602_RS = 0;
  7.     LCD1602_RW = 1;
  8.     do
  9.     {
  10.         LCD1602_EN = 1;
  11.         sta = LCD1602_DB;
  12.         LCD1602_EN = 0;    //使能,用完就拉低,釋放總線
  13.     }while(sta & 0x80);
  14. }

  15. void Lcd1602_Write_Cmd(unsigned char cmd)     //寫命令
  16. {
  17.     Read_Busy();
  18.     LCD1602_RS = 0;
  19.     LCD1602_RW = 0;
  20.     LCD1602_DB = cmd;
  21.     LCD1602_EN = 1;
  22.     LCD1602_EN = 0;   
  23. }

  24. void Lcd1602_Write_Data(unsigned char dat)   //寫數(shù)據(jù)
  25. {
  26.       Read_Busy();
  27.       LCD1602_RS = 1;
  28.       LCD1602_RW = 0;
  29.       LCD1602_DB = dat;
  30.       LCD1602_EN = 1;
  31.       LCD1602_EN = 0;
  32. }

  33. void LcdSetCursor(unsigned char x,unsigned char y)  //坐標(biāo)顯示
  34. {
  35.     unsigned char addr;
  36.     if(y == 0)
  37.         addr = 0x00 + x;
  38.     else
  39.         addr = 0x40 + x;
  40.    
  41.     Lcd1602_Write_Cmd(addr|0x80);
  42. }

  43. //按指定位置顯示一個字符
  44. void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
  45. {
  46.     Y &= 0x1;
  47.     X &= 0xF; //限制X不能大于15,Y不能大于1
  48.     if (Y) X |= 0x40; //當(dāng)要顯示第二行時地址碼+0x40;
  49.     X |= 0x80; //算出指令碼
  50.     Lcd1602_Write_Cmd(X); //發(fā)命令字
  51.     Lcd1602_Write_Data(DData); //發(fā)數(shù)據(jù)
  52. }


  53. void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str)     //顯示字符串
  54. {
  55.     LcdSetCursor(x,y);      //當(dāng)前字符的坐標(biāo)
  56.     while(*str != '\0')
  57.     {
  58.         Lcd1602_Write_Data(*str++);
  59.     }
  60. }

  61. void InitLcd1602()              //1602初始化
  62. {
  63.     Lcd1602_Write_Cmd(0x38);    //打開,5*8,8位數(shù)據(jù)
  64.     Lcd1602_Write_Cmd(0x0c);
  65.     Lcd1602_Write_Cmd(0x06);
  66.     Lcd1602_Write_Cmd(0x01);    //清屏   
  67. }

復(fù)制代碼









歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1