⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Energy meter To measure the voltage and current
💻 C
字号:
#include <REG51.H>#include <stdio.h>#include <string.h>
#include <intrins.h>
#include <float.h>
#include <math.h>
#include <ADC.c>
#include <LCD.c>


#define UnitPrice 2
sbit DevEnable =P3^1;
sbit Freq	= P2^0;

//********************Globel Variable*************************

const char Pass[6]={"123456"} ;
static unsigned char DelayCount;
bit TickFlg=0;
static unsigned int BalanceAmt;
static unsigned int PrepaidBalance;
static float UsedPower;
static unsigned char PrepidPower;
static float Voltage;
static float Current;
float temp;

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

// 			Function Decleration

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

void itoa(unsigned int value  ,unsigned char *string);
unsigned char ReadFreq();

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

// 			Timer 1 Interrupt routine

//***************************************************************
void timer1_ISR (void) interrupt 3
{
	TH1 = 0xC3;              /* Reload TL1 to count 100 clocks */
	TL1 = 0x50;
	TF1=0;
	DelayCount++;
	if(DelayCount==20)
	{
		DelayCount=0;
		TickFlg=1;
		temp=((Voltage*Current)/60);
		UsedPower+=(temp*.002);
		if(UsedPower>=1)
		{
			BalanceAmt-= (unsigned char )UsedPower;
			UsedPower=0;
		}
	}
}

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

// 			Serial Port initialization for 9600 baud

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

void SerInit()
{
	TMOD |=0x20;
	TH1=-3;
	SCON=0x50;
	TR1=1;
	TI=1;
}
//***************************************************************

// 			Timer1 initialization for 50 mS

//***************************************************************
 void Timer1Init()
 {
 	TMOD = 0x10;  /* Set Mode (8-bit timer with reload) */
	TH1 = 0x3C;              /* Reload TL1 to count 100 clocks */
	TL1 = 0xAF;
	ET1 = 1;                      /* Enable Timer 1 Interrupts */
	EA = 1;  					/* Global Interrupt Enable */
 }

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

// 			Processor Variable initialization

//***************************************************************
void ProcessorInit()
{
	DelayCount=0;
	TickFlg=0;
	BalanceAmt=0;
	UsedPower=0;
	PrepidPower=0;
	Voltage=0;
	Current=0;
	PrepaidBalance=0;
	DevEnable=0;	
}

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

// 			Delay for 1 MS

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

void Delay_MS(unsigned int value)
{
	unsigned int x,y;
	for(x=0;x<1000;x++)
		for(y=0;y<value;y++);
}	
//***************************************************************

// 			Main

//***************************************************************
	unsigned char Ascii[5];
void main()															 
{
   	unsigned char Read=0;
	unsigned char ReadKeypad=0;
	unsigned char Temp[7];
	unsigned char Count=0;
	bit Passcheck=0;
	bit PassOKFlg=0;
	bit EnterRSFlg=0;
//	unsigned int Money;
	unsigned int Temp1;

	bit KeyReadFlag=0;
//	SerInit();
	ProcessorInit();
//	Timer1Init();
	lcd_init();
	lcd_com(15); // first line
	lcd_puts("DIGITAL ENERGY");
	lcd_com(0xC5);	   //second line
	lcd_puts("METER");
   	Delay_MS(100);
	lcd_com(0x1);
	lcd_com(0x2);
	lcd_com(0x0C);
	lcd_com(0x80);
	lcd_puts("VOL    V");

 	lcd_com(0xC0);
	lcd_puts("CUR     A");

 	lcd_com(0xC8);
	lcd_puts("FREQ");
	while(1)
	{
		ADDA=0;
		AddressLatch();
		Start();
		delay(1);
		EOCCheck();
		Read=ReadADC();
		temp=Read;
		temp=(temp*.0196)*46;
		Voltage=temp;
		Temp1=temp;
		itoa(Temp1 ,Ascii);
//		lcd_com(0x84); // first line
//		lcd_puts("   ");					
		lcd_com(0x84); // first line
		lcd_puts(Ascii);
		temp=0;
		Temp1=0;
		Read=0;
	 	delay(1);
		delay(1);
		delay(1);

		ADDA=1;
		AddressLatch();
		Start();
		delay(1);
		EOCCheck();
		Read=ReadADC();
		temp=Read;
		temp=(temp*.0196)*2;
		Current=temp;
		Temp1=temp*10;
		itoa(Temp1 ,Ascii);	
		lcd_com(0xC4); // first line
		lcd_putc(Ascii[1]);
		lcd_puts(".");
		lcd_putc(Ascii[2]);
		temp=0;
		Temp1=0;
		Read=0;

		delay(1);
		delay(1);
		delay(1);
  ReadFreq();
		
 	}
}

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

// 			Integer to Ascii Conversion

//***************************************************************
 void itoa(unsigned int value  ,unsigned char *string)
{

	if(value<10)
	{

			string[2]='\0';
			string[1]=((value%10)+0x30);
			value=value/10;
			string[0]=((value%10)+0x30);

	}
	else if(value<100)
	{
			string[3]='\0';
			string[2]=((value%10)+0x30);
			value=value/10;
			string[1]=((value%10)+0x30);
			string[0]=0x30;
	}
	else if(value<1000)
	{

		string[3]='\0';
		string[2]=((value%10)+0x30);
		value=value/10;
		string[1]=((value%10)+0x30);
		value=value/10;
		string[0]=((value%10)+0x30);

	}
	else if(value<10000)
	{
		string[4]='\0';
		string[3]=((value%10)+0x30);
		value=value/10;
		string[2]=((value%10)+0x30);
		value=value/10;
		string[1]=((value%10)+0x30);
		value=value/10;
		string[0]=((value%10)+0x30);
	}
}



unsigned char ReadFreq()
{
	unsigned char Pre=0;
	unsigned char Current=0;
	unsigned int Count=0;

	if(Freq)
	{
		Pre	=Current;
		Current=1;	
	}
	else 
	{
		Pre	=Current;
		Current=0;			
	}

	if(Pre==0&&Current==1)
	{
		while(Freq!=0)	
		{
			Count++;
		}
  		while(Freq==0)
		{
			Count++;
		}

		 Count=Count;
		 	lcd_com(0xCC);
			itoa(Count,Ascii);	
			lcd_puts(Ascii);	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -