找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2356|回复: 0
打印 上一主题 下一主题
收起左侧

基于51单片机的电子秤源程序

[复制链接]
跳转到指定楼层
楼主
基于51的电子秤资料,基于HX711芯片,传感器数据采集板和主控板的完整源码与原理图pcb文件都有提供,欢迎大家下载.

#include <reg52.h>
#include <intrins.h>
#include <string.h>
#include "main.h"
#include "HX711.h"
#include "eeprom52.h"

#define uchar unsigned char
#define uint  unsigned int

unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
unsigned long Weight_Maopi_0 = 0;
long Weight_Shiwu = 0;
unsigned int qupi=0;
unsigned char p=0;
//¼üÅì′|àí±äá¿
unsigned char keycode;
unsigned char key_press_num=0;

uint GapValue,GapValue1;
bit flag_cz=0;

uchar code  LEDData[]={0x5F,0x44,0x9D,0xD4,0xC6,0xD3,0xDB,0x57,0xDF,0xD7,0xCF,0xaA,0x9B,0xDC,0x9B,0x8B}; //êyÂë1üÏÔê¾Âë±í
//¶¨òå±êê¶
volatile bit FlagTest = 0;                //¶¨ê±2aêÔ±êÖ¾£¬Ã¿0.5ÃëÖÃ룬2aíêÇå0
volatile bit FlagKeyPress = 0;  //óD¼ü°′ϱêÖ¾£¬′|àííê±ÏÇå0
//D£×¼2Îêy
//òòÎa2»í¬μÄ′«¸DÆ÷ìØDÔÇúÏß2»êÇoüò»Ö£¬òò′Ë£¬Ã¿ò»¸ö′«¸DÆ÷Dèòa½ÃÕyÕaàïÕa¸ö2Îêy2ÅÄüê12aá¿Öμoü׼跡£
//μ±·¢ÏÖ2aêÔ3öà′μÄÖØá¿Æ«′óê±£¬Ôö¼ó¸ÃêyÖμ¡£
//èç1û2aêÔ3öà′μÄÖØá¿Æ«D¡ê±£¬¼õD¡¸ÄêyÖμ¡£
//¸ÃÖμ¿éòÔÎaD¡êy
//#define GapValue 349
sbit LED=P1^1;
sbit ROW1=P3^0;
sbit ROW2=P3^1;
sbit ROW3=P3^2;
sbit DIAN = P0^6;                        //D¡êyμã

volatile bit ClearWeighFlag = 0; //′«¸DÆ÷μ÷áã±ê־룬Çå3y0Æˉ

/******************°Ñêy¾Y±£′æμ½μ¥Ƭ»úÄú2¿eepromÖD******************/
void write_eeprom()
{
        SectorErase(0x1080);
        GapValue1=GapValue&0x00ff;
        byte_write(0x1000, GapValue1);
        GapValue1=(GapValue&0xff00)>>8;
        byte_write(0x1001, GapValue1);
        byte_write(0x1060, a_a);       
}

/******************°Ñêy¾Y′óμ¥Ƭ»úÄú2¿eepromÖD¶á3öà′*****************/
void read_eeprom()
{
        GapValue   = byte_read(0x1001);
        GapValue   = (GapValue<<8)|byte_read(0x1000);
        a_a      = byte_read(0x1060);
}

/**************¿a»ú×Ô¼ìeeprom3õê¼»ˉ*****************/
void init_eeprom()
{
        read_eeprom();                //Ïè¶á
        if(a_a == 1)                //DÂμÄμ¥Ƭ»ú3õê¼μ¥Ƭ»úÄúÎêeeprom
        {
                GapValue  = 3500;
                a_a = 1;
                write_eeprom();           //±£′æêy¾Y
        }       
}
/*****ÏÔ꾿a»ú3õê¼»ˉμè′y»-Ãæ*****/
void Disp_init(void)   
{
        P0 = 0x80;      //ÏÔê¾----
        P2 = 0xBF;                 //òà′Î′ò¿a¸÷λ
        Delay_ms(1);                 //Ñóê±
        P2 = 0xEF;
        Delay_ms(1);   
        P2 = 0xFB;
        Delay_ms(1);
        P2 = 0xFE;
        Delay_ms(1);
        P2 = 0xFF;         //1رÕÏÔê¾
}

//ÏÔê¾ÖØá¿£¬μ¥λkg£¬á½Î»Õûêy£¬èyλD¡êy
void Display_Weight()
{
    P0 = LEDData[Weight_Shiwu%10];      //
        P2 = ~0xBF;                 //′ò¿aλ
        Delay_ms(1);                 //Ñóê±
        P2 = ~0xff;         //1رÕÏÔê¾
        P0=LEDData[Weight_Shiwu%100/10];    //ÏÔ꾸öλ
        P2 = ~0xEF;
        Delay_ms(1);
        P2 = ~0xff;         //1رÕÏÔê¾
        P0 =LEDData[Weight_Shiwu%1000/100];    //ÏÔê¾ê®Î»
        P2 = ~0xFB;
        Delay_ms(1);
        P2 = ~0xff;         //1رÕÏÔê¾
        P0 =LEDData[Weight_Shiwu%10000/1000];    //ÏÔê¾°ùλ
        DIAN = 0;         //ÏÔê¾D¡êyμã
        P2 = ~0xFE;
        Delay_ms(1);
        P2 = ~0xff;         //1رÕÏÔê¾
}

//¶¨ê±Æ÷03õê¼»ˉ
void Timer0_Init()
{
        ET0 = 1;        //ÔêDí¶¨ê±Æ÷0ÖD¶Ï
        TMOD = 1;       //¶¨ê±Æ÷1¤×÷·½ê½Ñ¡Ôñ
        TH0 = 0xb0;     
        TL0 = 0x3c;     //¶¨ê±Æ÷¸3óè3õÖμ
        TR0 = 1;        //Æô¶ˉ¶¨ê±Æ÷
}

//¶¨ê±Æ÷0ÖD¶Ï
void Timer0_ISR (void) interrupt 1 using 0
{
uchar Counter;
        TH0 = 0xb0;
        TL0 = 0x3c;     //¶¨ê±Æ÷¸3óè3õÖμ

        //ÿ0.5ÃëÖóË¢DÂÖØá¿
    Counter ++;
    if (Counter >= 10)
    {
       FlagTest = 1;
           Counter = 0;
    }
}

void KeyPress()
{
        if(ROW1==0)   //è¥Ƥ¼ü
        {
                Display_Weight();
                if(ROW1==0)
                {
                        if(qupi==0)
                        qupi=Weight_Shiwu;
                        else
                        qupi=0;
                        Buzzer=1;
                        for(p=0;p<10;p++)
                        Display_Weight();
                        Buzzer=0;       
                        while(ROW1==0) Display_Weight();
                }
        }
        if(ROW2==0)           //¼ó
        {
                Display_Weight();
                if(ROW2==0)
                {
                        while(!ROW2)
                        {
                                key_press_num++;
                                if(key_press_num>=100)
                                {
                                        key_press_num=0;
                                        while(ROW2)
                                        {
                                                if(GapValue<10000)
                                                GapValue+=10;
                                                Buzzer=0;
                                                for(p=0;p<2;p++)
                                                Display_Weight();
                                                Buzzer=1;
                                                for(p=0;p<2;p++)
                                                Display_Weight();
                                                Get_Weight();
                                        }
                                }
                                Display_Weight();
                                Delay_ms(8);
                        }
                        if(key_press_num==0)
                        {
                                key_press_num=0;
                                if(GapValue>10000)
                                GapValue++;
                                Buzzer=0;
                                for(p=0;p<10;p++)
                                Display_Weight();
                                Buzzer=1;
                        }
                        write_eeprom();               
                }
        }
        if(ROW3==0)  //¼õ
        {
                Display_Weight();
                if(ROW3==0)
                {
                        while(ROW3)
                        {
                                key_press_num++;
                                if(key_press_num>=100)
                                {
                                        key_press_num=0;
                                        while(ROW3)
                                        {
                                                if(GapValue>1)
                                                GapValue-=10;
                                                Buzzer=0;
                                                for(p=0;p<2;p++)
                                                Display_Weight();
                                                Buzzer=1;
                                                for(p=0;p<2;p++)
                                                Display_Weight();
                                                Get_Weight();
                                        }
                                }
                                Display_Weight();
                                Delay_ms(8);
                        }
                        if(key_press_num!=0)
                        {
                                key_press_num=0;
                                if(GapValue>1)
                                GapValue--;
                                Buzzer=0;
                                for(p=0;p<10;p++)
                                Display_Weight();
                                Buzzer=1;
                        }
                        write_eeprom();                           //±£′æêyY
                }
        }
}

  • …………限于本文篇幅 余下代码请从51黑下载附件…………

[color=rgb(51, 102, 153) !important]复制代码
[color=rgb(51, 102, 153) !important]

123.jpg.png (95.02 KB, 下载次数: 29)

123.jpg.png

1-程序长按.zip

76.31 KB, 下载次数: 18, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表