ADGAN
Full Member level 5
- Joined
- Oct 9, 2013
- Messages
- 295
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 18
- Activity points
- 1,837
Hello everybody! I need some help for my project software. What I want to do is this. I'm accumulating pulses coming from an energy measurement IC and its multiply with some value to determine the KWh. It will only display the monthly usage. After the month is passed it will be zero. It will be saved to the EEPROM and also will be send to the GSM modem. The algorithm is like this.
. First it will read the monthly usage from the EEPROM
while(1)
. The pulses that are currently coming will be added to this.
. Calculate Kwh
. Save the monthly usage to the EEPROM
I'm facing some problems in my code. When I tried it in Proteus, the pulses are counting very swiftly even though the pulse frequency is low as 0.34Hz. When I tried it in hardware LCD shows garbage data and some time the whole LCD blinks. When I tested each and every part separately they all worked well. i.e. I tested the energy measurement ic, EEPROM, RTCC and GSM modem. All this parts are working fine separately. I can't find the error that I have done. I'm using PIC16F887 with 4MHz crystal. This has been written in MikroC pro. Pls help me since this is the last part of the project.
. First it will read the monthly usage from the EEPROM
while(1)
. The pulses that are currently coming will be added to this.
. Calculate Kwh
. Save the monthly usage to the EEPROM
I'm facing some problems in my code. When I tried it in Proteus, the pulses are counting very swiftly even though the pulse frequency is low as 0.34Hz. When I tried it in hardware LCD shows garbage data and some time the whole LCD blinks. When I tested each and every part separately they all worked well. i.e. I tested the energy measurement ic, EEPROM, RTCC and GSM modem. All this parts are working fine separately. I can't find the error that I have done. I'm using PIC16F887 with 4MHz crystal. This has been written in MikroC pro. Pls help me since this is the last part of the project.
Code:
// RTC Definitions
#define RTC_ADDR 0xD0
#define Highest(param) ((char *)¶m)[3]
#define Higher(param) ((char *)¶m)[2]
#define Hi(param) ((char *)¶m)[1]
#define Lo(param) ((char *)¶m)[0]
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
unsigned char txt1[] = "ENERGY CONSUMPTION";
unsigned char txt2[] = "FROM";
unsigned char txt3[] = "TO";
unsigned char txt4[] = "20";
unsigned char txt5[] = "KWh";
unsigned short lowByte, highByte, higherByte, highestByte, p_address;
unsigned char txt[14],txt6[14];
unsigned char SMS[14] ;
unsigned long count = 0;
unsigned long pulses = 0;
unsigned long monthly_consumption;
double Meter_reading = 0;
const double Meter_Constant = 0.001;
void EEPROMWriteInt(unsigned short p_address, unsigned long int p_value) {
lowByte = Lo(p_value);
highByte = Hi(p_value);
higherByte = Higher(p_value);
highestByte = Highest(p_value);
EEPROM_write(p_address, lowByte);
EEPROM_write(p_address + 1, highByte);
EEPROM_write(p_address + 2, higherByte);
EEPROM_write(p_address + 3, highestByte);
lowByte = 0x00;
highByte = 0x00;
higherByte = 0x00;
highestByte = 0x00;
}
unsigned long int EEPROMReadInt(unsigned short p_address)
{
Lo(Monthly_consumption) = EEPROM_read(p_address);
Hi(Monthly_consumption) = EEPROM_read(p_address + 1);
Higher(Monthly_consumption) = EEPROM_read(p_address + 2);
Highest(Monthly_consumption) = EEPROM_read(p_address + 3);
return Monthly_consumption;
}
/**************************************************************************************************
* Interrupt service routine
**************************************************************************************************/
void interrupt(){
if (PIR1.TMR1IF)
{
count++;
PIR1.TMR1IF=0;
}
}
void Energydisplay(double V){
unsigned char a,b,c,d,d1,d2,d3;
double n;
if(v==0000.00){
Lcd_Chr(4,5,48);
Lcd_Chr(4,6,48);
Lcd_Chr(4,7,48);
Lcd_Chr(4,8,48);
Lcd_Chr_CP('.');
Lcd_Chr(4,10,48);
Lcd_Chr(4,11,48);
Lcd_Chr(4,12,48);
}
else{
a=(V/1000);
b=((V-(1000*a))/100);
c=((V-(1000*a)-(100*b))/10);
d=V-(1000*a)-(100*b)-(10*c);
n=((V-(1000*a)-(100*b)-(10*c)-d)*10000);
d1=(n/1000);
d2=((n-(d1*1000))/100);
d3=((n-(d1*1000)-(d2*100))/10);
Lcd_Chr(4,5,a+48);
Lcd_Chr(4,6,b+48);
Lcd_Chr(4,7,c+48);
Lcd_Chr(4,8,d+48);
Lcd_Chr_CP('.');
Lcd_Chr(4,10,d1+48);
Lcd_Chr(4,11,d2+48);
Lcd_Chr(4,12,d3+48);
}
}
/**************************************************************************************************
* Main function
**************************************************************************************************/
void main()
{
ANSEL = 0;
ANSELH = 0;
TRISA = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
PORTA.RA0 = 1;
TRISB = 0;
PORTB = 0;
PORTC = 0;
TRISC = 0x99;
INTCON = 0xC0;
PIE1.TMR1IE = 1;
PIR1.TMR1IF = 0;
T1CON = 0x03;
TMR1L = 0;
TMR1H = 0;
Lcd_Init();
Delay_ms(100); // Delay 100ms
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
I2C1_Init(100000);
LCD_Out(1,2,txt1);
LCD_Out(2,1,txt2);
LCD_Out(3,1,txt3);
LCD_Out(2,14,txt4);
LCD_Out(3,14,txt4);
LCD_Out(4,14,txt5);
UART1_Init(9600);
Delay_ms (100);
Monthly_consumption = EEPROMReadInt(0x00);
while(1){
pulses = (count*65535)+ (TMR1H<<8 | TMR1L);
Monthly_consumption += pulses;
if(day == 31 && hours == 23 && minutes == 59 && seconds == 57)
{
SMS_Send(Meter_reading);
}
if(day == 1 && hours == 0 && minutes == 0 && seconds == 1)
{
Monthly_consumption = 0;
}
Meter_reading = Monthly_consumption*Meter_Constant;
Energydisplay( Meter_reading);
EEPROMWriteInt(0x00, Monthly_consumption);
Delay_ms(20);
}
}
Last edited: