#include <reg52.h>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
#define KeyPort P0
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0
sbit ADDO = P1^6;
sbit ADSK = P1^7;
unsigned int num;
unsigned int dnf;
float i;
unsigned int priceV;
//////////////////////////
/*-----------------------------------------------
LCD12864引腳接口
------------------------------------------------*/
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
sbit RES = P2^3;
sbit PSB = P2^1;
sbit PAUSE = P3^0;
unsigned char temp[16];
unsigned char lol[16];
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延時1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
檢測忙位
------------------------------------------------*/
void Check_Busy()
{
RS=0;
RW=1;
E=1;
DataPort=0xff;
while((DataPort&0x80)==0x80);//忙則等待
E=0;
}
/*------------------------------------------------
寫命令
------------------------------------------------*/
void Write_Cmd(unsigned char Cmd)
{
Check_Busy();
RS=0;
RW=0;
E=1;
DataPort=Cmd;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
寫數據
------------------------------------------------*/
void Write_Data(unsigned char Data)
{
Check_Busy();
RS=1;
RW=0;
E=1;
DataPort=Data;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
液晶屏初始化
------------------------------------------------*/
void Init_ST7920()
{
DelayMs(40); //大于40MS的延時程序
PSB=1; //設置為8BIT并口工作模式
DelayMs(1); //延時
RES=0; //復位
DelayMs(1); //延時
RES=1; //復位置高
DelayMs(10);
Write_Cmd(0x30); //選擇基本指令集
DelayUs2x(50); //延時大于100us
Write_Cmd(0x30); //選擇8bit數據流
DelayUs2x(20); //延時大于37us
Write_Cmd(0x0c); //開顯示(無游標、不反白)
DelayUs2x(50); //延時大于100us
Write_Cmd(0x01); //清除顯示,并且設定地址指針為00H
DelayMs(15); //延時大于10ms
Write_Cmd(0x06); //指定在資料的讀取及寫入時,設定游標的移動方向及指定顯示的移位,光標從右向左加1位移動
DelayUs2x(50); //延時大于100us
}
/*------------------------------------------------
顯示字符串
x:橫坐標值,范圍0~8
y:縱坐標值,范圍1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,unsigned char *s)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
while(*s>0)
{
Write_Data(*s);
s++;
DelayUs2x(50);
}
}
/*------------------------------------------------
清屏
------------------------------------------------*/
void ClrScreen()
{
Write_Cmd(0x01);
DelayMs(15);
}
/*------------------------------------------------
延時函數
------------------------------------------------*/
void delay(uchar z)
{
uchar x;
for(;z>0;z--)
for(x=22;x>0;x--);
}
unsigned long ReadCount(void)
{
unsigned long Count; unsigned char i;
ADSK=0; //使能AD(PD_SCK 置低)
Count=0;
while(ADDO); //AD轉換未結束則等待,否則開始讀取
for (i=0;i<24;i++)
{
ADSK=1; //PD_SCK 置 高 ( 發 送 脈 沖 )
Count=Count<<1; //下降沿來時變量Count左移一位,右側補零
ADSK=0; //PD_SCK 置低
if(ADDO) Count++;
}
ADSK=1;
Count=Count^0x800000;//第25個脈沖下降沿來時,轉換數據
ADSK=0;
return(Count);
}
///////////////////////////////////////////////////////////////// //////////////////////
/*------------------------------------------------
按鍵掃描函數,返回掃描鍵值
------------------------------------------------*/
unsigned char KeyScan(void) //鍵盤掃描函數,使用行列反轉掃描法
{
unsigned char cord_h,cord_l;//行列值中間變量
KeyPort=0x0f; //行線輸出全為0
cord_h=KeyPort&0x0f; //讀入列線值
if(cord_h!=0x0f) //先檢測有無按鍵按下
{
DelayMs(10); //去抖
if((KeyPort&0x0f)!=0x0f)
{
cord_h=KeyPort&0x0f; //讀入列線值
KeyPort=cord_h|0xf0; //輸出當前列線值
cord_l=KeyPort&0xf0; //讀入行線值
while((KeyPort&0xf0)!=0xf0);//等待松開并輸出
return(cord_h+cord_l);//鍵盤最后組合碼值
}
}return(0xff); //返回該值
}
/*------------------------------------------------
按鍵值處理函數,返回掃鍵值
可以根據需要改變返回值
| 1 | 2 | 3 | + |
| 4 | 5 | 6 | - |
| 7 | 8 | 9 | * |
| 0 | . | = | / |
------------------------------------------------*/
unsigned char KeyPro(void)
{
switch(KeyScan())
{
case 0x77:return 1 ;break;//1
case 0x7b:return 2 ;break;//2
case 0x7d:return 3 ;break;//3
case 0xb7:return 4 ;break;//4
case 0xbb:return 5 ;break;//5
case 0xbd:return 6 ;break;//6
case 0xd7:return 7 ;break;//7
case 0xdb:return 8 ;break;//8
case 0xdd:return 9 ;break;//9
case 0xe7:return 10 ;break;//*
case 0x7e:return 11;break;//A 設置單價
case 0xbe:return 12 ;break;//B 去皮
case 0xde:return 13 ;break;//C 附加功能
case 0xee:return 14 ;break;//D 重置
case 0xed:return 15 ;break;//#
case 0xeb:return 16 ;break;//0
default:return 0;break;
}
}
/*------------------------------------------------
主程序
------------------------------------------------*/
main()
{
int priceV;
char v=0;
unsigned long weight ;
unsigned long heavy ;
float heavy1 ;
float poi;
int d1,d2;
ClrScreen();
Init_ST7920(); //初始化
heavy= ReadCount(); DelayMs(100);
heavy= ReadCount(); DelayMs(100);
heavy= ReadCount(); DelayMs(100);
while (1)
{
for( v=0;v<6;v++)
{ weight += ReadCount(); }
weight =weight/7;
heavy1= weight-heavy ;
heavy1= (float)heavy1/1834.5;
DelayMs(150); DelayMs(150);
if(heavy1>10000)heavy1=0;
d1 = weight / 1000; d2 = weight % 1000;
num=KeyPro();
poi=priceV*heavy1*10;
switch(num)
{
case 1: priceV*=10; priceV+=1; break; // 1
case 2: priceV*=10; priceV+=2; break; // 2
case 3: priceV*=10; priceV+=3; break; // 3
case 4: priceV*=10; priceV+=4; break; // 4
case 5: priceV*=10; priceV+=5; break; // 5
case 6: priceV*=10; priceV+=6; break; // 6
case 7: priceV*=10; priceV+=7; break; // 7
case 8: priceV*=10; priceV+=8; break; // 8
case 9: priceV*=10; priceV+=9; break; // 9
case 16: priceV*=10; break;
case 15: priceV=0; break; // 0
case 11: heavy= ReadCount(); break;
case 12: i+=poi; break;
case 13: i=0; break;
}
sprintf(temp,"重量:%5.2f g " ,heavy1 );
LCD_PutString(0,1, temp);
sprintf(lol,"單價:%4d 元/g ",priceV);
LCD_PutString(0,2, lol);
sprintf(lol,"總價:%4.1f 元/g ",poi/10);
LCD_PutString(0,3, lol);
// sprintf(lol,"共計:%4.1f 元/g ",i/10);
// LCD_PutString(0,4, lol);
sprintf(temp," %4d%-d ",d1,d2);
LCD_PutString(0,4, temp);
DelayMs(200);
if(heavy1>503)
{
ClrScreen();
sprintf(lol,"警告警告警告警告");
LCD_PutString(0,1, lol);
sprintf(lol,"警告警告警告警告");
LCD_PutString(0,2, lol);
sprintf(lol,"警告警告警告警告");
LCD_PutString(0,3, lol);
sprintf(lol,"警告警告警告警告");
LCD_PutString(0,4, lol);
DelayMs(200);
}
}
}
現在不會外接12864
|