钢丝绳检测仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg51.h>
- #include <stdio.h>
- #include <intrins.h>
- #include <math.h> //Keil library
- #include <stdlib.h> //Keil library
- #define uchar unsigned char //定义一下方便使用
- #define uint unsigned int
- #define ulong unsigned long
- sbit RS = P2^0;
- sbit RW = P2^1;
- sbit EN = P2^2;
- sbit BF = P0^7; //忙碌标志位,,将BF位定义为P0.7引脚
- sbit ADOUT=P1^7; //ADC 数据输入
- sbit ADCLK=P1^6; //ADC CLK
- sbit ADCS=P1^5; //ADC CS
- sbit key1=P3^6; //定义按键
- sbit spk=P3^7; //定义蜂鸣器
- sbit led=P3^5; //定义led
- uchar count;
- uint DATA1=0; //采集值
- /*************延时函数*************/
- void delay(uint n)
- { uint x,y;
- for(x=n;x>0;x--)
- for(y=121;y>0;y--);
- }
- /***********判忙函数***********/
- bit LCD_Check_Busy()
- {
- bit result;
- RS=0; //根据规定,RS为低电平,RW为高电平时,可以读状态
- RW=1;
- EN=1; //E=1,才允许读写
- _nop_(); //空操作
- _nop_();
- _nop_();
- _nop_(); //空操作四个机器周期,给硬件反应时间
- result=BF; //将忙碌标志电平赋给result
- EN=0; //将E恢复低电平
- return result;
- }
- /***********写入命令函数***********/
- void write_com(uchar com)
- {
- while(LCD_Check_Busy()); //忙则等待
- RS=0;
- RW=0;
- EN=1;
- P0= com;
- _nop_();
- EN=0;
- }
- /**********写入数据函数**********/
- void write_dat(uchar dat)
- {
- while(LCD_Check_Busy()); //忙则等待
- RS=1;
- RW=0;
- EN=1;
- P0= dat;
- _nop_();
- EN=0;
- }
- /*******写入字符函数***********/
- void LCD_Write_Char(uchar x,uchar y,uchar dat)
- {
- if (y == 0)
- { write_com(0x80 + x); }
- else
- { write_com(0xC0 + x); }
- write_dat( dat);
- }
- /******写入字符串函数***********/
- void Write_String(uchar x,uchar y,uchar *s)
- {
- while (*s)
- {
- LCD_Write_Char(x,y,*s);
- s++;
- x++;
- }
- }
- /*****初始化函数******/
- void LCD_Init()
- {
- write_com(0x38); /*显示模式设置*/
- delay(5);
- write_com(0x06);/*显示光标移动设置*/
- delay(5);
- write_com(0x0C); /*显示开及光标设置*/
- write_com(0x01); /*显示清屏*/
- }
- /*----------------ADC采样函数程序-----------------------------------------------*/
- void adc0831()
- {
- uchar i;
- DATA1=1;
- _nop_(); _nop_(); _nop_();
- ADCS=0;
- _nop_(); _nop_(); _nop_();
- ADCLK=1;
- _nop_(); _nop_(); _nop_();
- ADCLK=0;
- _nop_(); _nop_(); _nop_();
- ADCLK=1;
- _nop_(); _nop_(); _nop_();
- ADCLK=0;
- for(i=0;i<8;i++)
- {
- DATA1<<=1;
- if(ADOUT)
- {
- DATA1++;
- }
- ADCLK=1;
- _nop_(); _nop_(); _nop_();
- ADCLK=0;
- _nop_(); _nop_(); _nop_();
- }
- ADCS=1;
- _nop_(); _nop_();
- // return DATA1;
- }
- ///***********************************************************************
- void lcd1()
- {
- // uint e;
- // uchar e0,e1,e2,e3;
- if(DATA1>340)
- {
- Write_String(0,1," OFF Normal ");
- spk=0;
- led=0;
- }
- else
- {
- Write_String(0,1," Normal ");
- spk=1;
- led=1;
- }
- /*
- e=DATA1;
- e0 = e/1000 +48;
- e1 = (e/100)%10+48;
- e2 = (e/10)%10+48;
- e3 = e%10+48;
- Write_String(8,1,"Q="); //
- LCD_Write_Char(10,1,e0);
- LCD_Write_Char(11,1,e1);
- LCD_Write_Char(12,1,e2);
- LCD_Write_Char(13,1,e3);
- Write_String(14,1,"Kg");
- */
- }
- void key()
- {
- if(key1==0) //kay5 +1
- {
- delay(1); //延时消抖
- if(key1==0)
- while(!key1);//等待松手
- // KA=!KA;
- }
- }
- /*----------------主函数-----------------------------------------------*/
- int main(void)
- {
- LCD_Init(); //初始化液晶屏
- delay(10); //延时用于稳定,可以去掉
- write_com(0x01); //清屏
- Write_String(0,0," Welcome to use "); //写入的是设备信息
- Write_String(0,1," "); //写入第二行信息,可以写自己的名字什么的
- delay(500);
- //write_com(0x01); //清屏
- while (1)
- {
- adc0831(); //质量采集
- key();
- lcd1();
- delay(100); //延时,两次发射之间要至少有10ms间隔
- }
- }
复制代码
所有资料51hei提供下载:
C052931.zip
(189.81 KB, 下载次数: 24)
|