基于炜煌单片机的称重感应模块源代码,芯片类型stc12c5a60s2

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41006598/article/details/90170188

 /*本程序采用lcd12864显示,接入称重感应模块后会在lcd12864第一行显示重量,初始化时重量不为零,需要用户自己调这个函数unsigned int Fact_Value(void)中的 unsigned long int Base_Weight=60000;,从等于5000开始调整,一次加五千直到显示全为零,然后找一个已知重量的物品,比如手机,调整 return((unsigned int)((float)((Weight-Base_Weight)/100) *0.119));,调整0.119直到显示重量相差不超过0.01;接口已定义好,可自行更改*/

#include"reg51.h"

#include <intrins.h>

sbit HX711_SCL=P3^6;

sbit HX711_SDA=P3^7;

sbit RS=P2^5;    //命令/数据选择

sbit RW=P2^4;    //读写口

sbit  E=P2^3;    //锁存控制

sbit RES = P2^0; //复位

sbit PSB = P2^2; //串并选择

#define uchar unsigned char

扫描二维码关注公众号,回复: 7651261 查看本文章

#define uint  unsigned int

uint k;

delay(uint time)         //int型数据为16位,所以最大值为65535            

 {

  uint  i,j;             //定义变量i,j,用于循环语句

  for(i=0;i<time;i++)    //for循环,循环50*time次

     for(j=0;j<100;j++); //for循环,循环50次

 }

//**************************************************************************************************

//查忙

//**************************************************************************************************

checkbusy()           

{

   RS=0;                   //命令/数据选择,为0时选择命令    

   RW=1;                   //读/写选择,为1时选择读

   E=1;                    //使能

   while((P0&0x80)==0x80); //查忙标志位,等待标志位为0,即表示写入完毕

   E=0;                    //关闭读写

}

//**************************************************************************************************

//向LCD写一命令

//**************************************************************************************************

wcode(uchar cmdcode)

{

   checkbusy();            //查忙

   RS=0;                   //命令/数据选择,为0时选择命令

   RW=0;                   //读/写选择,为0时选择写

   E=1;                    //使能

   P0=cmdcode;             //送入命令

   delay(10);              //等待写入

   E=0;                    //关闭读写

}

//**************************************************************************************************

//向LCD写一数据

//**************************************************************************************************

wdata(uchar dispdata)

{

   checkbusy();            //查忙

   RS=1;                   //命令/数据选择,为1时选择数据

   RW=0;                   //读/写选择,为0时选择写

   E=1;                    //使能

   P0=dispdata;            //送入数据

   delay(10);              //等待写入

   E=0;                    //关闭读写

}

//**************************************************************************************************

//LCD 初始化

//**************************************************************************************************

InitLCD()

   {

   PSB=1;             //设置为8BIT并口工作模式

   RES=0;             //复位

   delay(10);         //延时

   RES=1;             //关复位

   wcode(0x30);       //选择基本指令集

   wcode(0x0c);       //开显示(无游标、不反白)

   wcode(0x01);       //清除显示,并且设定地址指针为00H

   wcode(0x06);       //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位

   }  

//**************************************************************************************************

//任意位置显示字符串

//**************************************************************************************************

void dis(uchar x,uchar y,uchar code *s)   

{                                     //x为横坐标,y位纵坐标,*s表示指针,为数据的首地址

 switch(y)                            //选择纵坐标            

     {

  case 0: wcode(0x80+x);break;    //第1行

  case 1: wcode(0x90+x);break;    //第2行

  case 2: wcode(0x88+x);break;    //第3行

  case 3: wcode(0x98+x);break;    //第4行

      default:break;

 }

   while(*s>0)                        //写入数据,直到数据为空

     {  

      wdata(*s);                      //写数据

      delay(10);                      //等待写入

      s++;                            //下一字符

     }

}

//**************************************************************************************************

//主函数

//**************************************************************************************************

unsigned long int get_ADValue(void)

{

unsigned char i;

unsigned long value=0;

HX711_SDA=1; //51CPU I/O inputenable

HX711_SCL=0; //enable AD

while(HX711_SDA);

_nop_(); //delay T1>0.1us

for(i=0;i<24;i++)

{

HX711_SCL=1;

_nop_(); //delay T3>0.2us

if(HX711_SDA)

value++;

value=value<<1;

HX711_SCL=0;

_nop_(); //delay T4>0.2us

}

HX711_SCL=1;

_nop_(); //delay T3>0.2us

HX711_SCL=0;

_nop_(); //delay T4>0.2us

value=value&0x007FFFFF;

return(value);

}

unsigned int Fact_Value(void)

{

  unsigned long int Weight;

  unsigned long int Base_Weight=60000;

  Weight=get_ADValue();

   if( Weight<Base_Weight)

   {

 

Weight= Base_Weight;

   }

   if((Weight>Base_Weight)&&(Weight<(Base_Weight+1000)))

   {

 

Weight= Base_Weight;

   }

//   Display_String(0,1,"重量:");

//   Display_Int(3,0,(unsigned int)((float)((Weight-Base_Weight)/100) *0.119));

//   Display_Char(5,0,'g');

//   if((Weight-Base_Weight)>0)

//   {

//   Array[0]=((Weight-Base_Weight)/1000)%10+0x30;

//   Array[1]=((Weight-Base_Weight)/100 )%10+0x30;

//   Array[2]=((Weight-Base_Weight)/10  )%10+0x30;

//   Array[3]=((Weight-Base_Weight)/1   )%10+0x30;

//   Array[4]='g';

//   Array[5]=0x00;

//   SYN6288_Play(Array);

//   }

     return((unsigned int)((float)((Weight-Base_Weight)/100) *0.119));

 

 

}

void main()

{

      InitLCD();

   while(1)

   {    

         k=Fact_Value();

        wcode(0x80);

        wdata(k%10000/1000 + 0x30);

         wdata('.');

         wdata(k%1000/100 + 0x30);

         wdata(k%100/10 + 0x30);

         wdata(k%10 + 0x30);

   }

        

}

猜你喜欢

转载自blog.csdn.net/qq_41006598/article/details/90170188