/**************************************/
/* IHRCO 12.0MHz */
/* MA82G5B32-QFP32 */
/* ISP空间:1.5K */
/* 7A00~7FFF */
/* IAP空间:1.0K */
/* 7600~77FF */
/* 7800~79FF */
/* 电子天平,AD使用XM24 */
/**************************************/
#include <REG_MA82G5B32.h>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char /*宏定义*/
#define uint unsigned int
#define j_cal 0x08 //去皮、校称键键码
#define j_cou 0x04 //计数键键码
/**************************************/
sbit dio = P1^6;
sbit clk = P1^5;
sbit stb = P1^4;
sbit dout = P3^2;
sbit sclk = P3^3;
sbit pdwn = P3^4;
sbit k_cal = P2^2; //去皮、校称键
sbit k_cou = P2^3; //计数键
/**************************************/
unsigned char led1 _at_ 0x1f;
unsigned char led2 _at_ 0x1e;
unsigned char led3 _at_ 0x1d;
unsigned char led4 _at_ 0x1c;
unsigned char led5 _at_ 0x1b;
unsigned char led6 _at_ 0x1a;
unsigned char k _at_ 0x19; //一阶滤波系数
unsigned char dely _at_ 0x18;
unsigned char star _at_ 0x17;
unsigned char keybuf _at_ 0x16;
unsigned char keyct _at_ 0x15;
unsigned char timer _at_ 0x14; //显示闪烁用
unsigned char key _at_ 0x13;
unsigned char temp _at_ 0x12;
unsigned char adjs _at_ 0x11; //ad稳定计数
unsigned char wejs _at_ 0x10; //判断重量是否稳定用
signed long zeroad _at_ 0x0c; //零位AD值
signed long adold _at_ 0x08; //上一次AD内码
idata signed long ad1; //新读取的AD值
idata signed long ad2; //8次递推平均滤波后的AD值
idata signed long ad[8]; //AD代列
idata signed long adx; //一阶滤波后的AD值
idata signed long weight; //实际测量重量
idata signed long weight1; //显示重量
idata signed long weight2; //内部重量,用于重量跟踪
idata signed long weight3; //内部重量,用于重量跟踪
idata signed long weightfz; //自动跟零用
idata unsigned char xy1; //一阶滤波变化方向1
idata unsigned char xy2; //一阶滤波变化方向2
idata unsigned char filterjs; //一阶滤波计数器
idata unsigned char filtermax; //一阶滤波计数器最大值
idata unsigned char filterx; //一阶滤波差值
idata unsigned char filterf; //一阶滤波阀值
idata unsigned char filterkx; //一阶滤波系数增量
idata unsigned char FolZeroTime; //零位跟踪计数
idata signed long slop;
idata unsigned char xzjs; //校正计数
idata unsigned char timer1; //菜单转换计时
idata signed int wesc; //用于蠕变修正
idata unsigned char xuds; //0x7600 小数点显示
//xuds=0x01: 1个小数位
//xuds=0x02: 2个小数位
//xuds=0x04: 3个小数位
idata signed char zero; //0x7601 零位跟踪范围
idata unsigned char zerotm; //0x7602 零位跟踪时间
idata unsigned char wefc; //0x7605 重量跟踪范围
idata unsigned char xzds; //0x7606 校正点数
idata signed long lc; //0x7610 量程
idata signed long max; //0x7614 最大量程
idata signed long wch; //0x7618 超过设定值,重量跟踪清零
idata signed long xz; //0x761C 单点校正砝码重量
idata signed long xz1; //0x7620 多点校正砝码重量1
idata signed long xz2; //0x7630 多点校正砝码重量2
idata signed long xz3; //0x7640 多点校正砝码重量3
idata signed long xz4; //0x7650 多点校正砝码重量4
idata signed long slop1; //0x7800 校正后的内码1
idata signed long slop2; //0x7810 校正后的内码2
idata signed long slop3; //0x7820 校正后的内码3
idata signed long slop4; //0x7830 校正后的内码4
idata signed long dslop; //校正曲线用
idata unsigned int cou_js; //设定计数个数
idata signed long cou_wt; //设定计数重量
idata signed long cou; //实际计数数量
bit fh; //显示负号
bit adok; //读AD结果完成
bdata unsigned char xud;
sbit xud1 = xud^0;
sbit xud2 = xud^1;
sbit xud3 = xud^2;
//d0=a;d1=b;d2=c;d3=d;d4=e;d5=f;d6=g;d7=h
code char seg[]={
0X3F,/*0*/
0X06,/*1*/
0X5B,/*2*/
0X4F,/*3*/
0X66,/*4*/
0X6D,/*5*/
0X7D,/*6*/
0X07,/*7*/
0X7F,/*8*/
0X6F,/*9*/
0X00,/* */
0X77,/*A -11*/
0X39,/*C -12*/
0X71,/*F -13*/
0X76,/*H -14*/
0X38,/*L -15*/
0X3E,/*U -16*/
0X40,/*- -17*/
0X01,/*~ -18*/
0X08,/*_ -19*/
0X7C,/*b -20*/
0X5E,/*d -21*/
0X79,/*E -22*/
0X73,/*P -23*/
0X78 /*t -24*/
};
/*-----------------------------------------------*/
/* 短延时子程序 */
/*-----------------------------------------------*/
void ys()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
/*-----------------------------------------------*/
/* AD转换子程序 */
/*-----------------------------------------------*/
void adc()
{
unsigned char bitCount;
ad1=0;
sclk=0;
for(bitCount=0; bitCount<24; bitCount++) //取24位最大分辨率
{
ad1 *= 2;
sclk=1;
ys();
sclk=0;
ys();
if(dout==1) ad1++;
}
sclk=1; //第21个时钟脉冲,强制数据线拉高
ys();
sclk=0;
}
/*-----------------------------------------------*/
/* 外部中断0服务程序 */
/*-----------------------------------------------*/
void int0int() interrupt 0
{
IE0=0;
EX0=0;
XICFG |= 0x30; //设置P3.2为普通IO
ys();
adc();
ad1=ad1/8;
adok=1;
XICFG &= 0xcf; //设置P3.2为INT0
// Filter1();
EX0=1;
}
/*-----------------------------------------------*/
/* 定时器0中断服务程序 */
/*-----------------------------------------------*/
void timer0int() interrupt 1 //5ms
{
TL0=0x78;
TH0=0xec;
++timer; // 0.5s
if(timer>100) timer=0;
if(dely>0) --dely;
}
/*-----------------------------------------------*/
/* 延时子程序 */
/*-----------------------------------------------*/
void delay ( unsigned char dey )
{
dely=dey;
while(dely!=0);
}
/*-----------------------------------------------*/
void ledout(unsigned char yy)
{
unsigned char j;
stb=0;
for (j=0;j<8;j++)
{
if (yy & 0x01) dio=1;
else dio=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
clk=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
clk=0;
yy=yy>>1;
_nop_();
_nop_();
}
}
/*-----------------------------------------------*/
void ledint()
{
ledout(0x03); //设置显示模式
stb=1;
}
/*-----------------------------------------------*/
void sendled()
{
unsigned char xx;
ledout(0x40); //设置写显存的数据命令,地址自动加1
stb=1;
ledout(0xc0); //设置起始地址
//1
ledout(seg[led6]); //0 - GRID1(SEG1~8)
ledout(0); //0 - GRID1(SEG9~10)
//2
xx=0; //显示小数点
if(xud1) xx=0x80;
ledout(seg[led5]+xx); //0 - GRID2(SEG1~8)
ledout(0); //0 - GRID2(SEG9~10)
//3
xx=0; //显示小数点
if(xud2) xx=0x80;
ledout(seg[led4]+xx); //0 - GRID3(SEG1~8)
ledout(0); //0 - GRID3(SEG9~10)
//4
xx=0; //显示小数点
if(xud3) xx=0x80;
ledout(seg[led3]+xx); //0 - GRID4(SEG1~8)
ledout(0); //0 -