pany2008
Junior Member level 3
Hi,
I am looking for the high speed proximity event counter c software code for at89c52. The application is to count high speed pulse and the after it will be multiplied with the multiplication factor and then the result to be displayed.
result = count * multiplication factor.
100 = 1000 * 0.1
I am posting my source code if any one correct it. In my code when the pulse above 20 pulse per second increase it starts giving the wrong result.
#include <stdio.h>
#include <reg51.h>
unsigned char i,dis_up[4],ms10,b;
unsigned char set_count,auto_out,ms1,ms2;
unsigned char serial_out,serial_in,eeprom_counter,nop,dly,dly1;
unsigned char ms51,ms52,key_count;
signed int mul_cnt,dis_count_reg,max_count_reg,dummy;
signed long int count_reg;
extern keycheck(),display_value(),reset_all_flag();
extern up_key(),shift_key(),reset_key(),set_key();
extern enter_proc(),mul_cnt_proc(),reset_key1();
extern read_set_eeprom(),read_max_eeprom();
extern write_set_eeprom(),write_max_eeprom();
extern stop(),start(),read_current(),shout(),shin(),nak();
extern mul_mess_proc();
bit relay_flag,max_flag,max1_flag;
bit dis_flag,down_flag,first_time_flag,delay_flag;
bit key_sense,new_flag,mul_cnt_flag,tar_flag;
bit mul_mess_flag;
#include <c:\c51\inc\sub.h>
#include <c:\c51\inc\estd.h>
#include <c:\c51\inc\display.h>
void main()
{
SP = 0xa0;
IE = 0X0b;
IP = 0x01;
TMOD = 0x11;
TH0 = 0Xec;
TL0 = 0X90;
TH1 = 0XEC;
TL1 = 0X90;
read_max_eeprom();
read_set_eeprom();
dis_flag = 1;
first_time_flag = 1;
TR1 = 1;
TR0 = 1;
IT0 = 1;
count_reg = dis_count_reg = 0;
max_flag = max1_flag = 0;
max_count_reg = 0;
read_max_eeprom();
for(;
{
EA = 1;
}
}
display() interrupt 1 using 1
{
EA = 0;
P2 = 0xff;
TH0 = 0Xec;
TL0 = 0X90;
P00 = select_digit & 0X01;
P01 = select_digit & 0X02;
P02 = select_digit & 0X04;
P03 = select_digit & 0X08;
P2 = dis_tab[dis_up];
if((i == 1) && (dis_flag)) P27 = 0;
if((i == 2) && (!dis_flag) && (mul_cnt_flag)) P27 = 0;
++i;
if(i >= 4)
{
i = 0;
if(first_time_flag)
{
first_time_flag = 0;
read_max_eeprom();
}
}
display_value();
}
timer_time() interrupt 3 using 3
{
EA = 0;
TH1 = 0xec;
TL1 = 0x90;
++ms2;
if(ms2 >= 55)
{
ms2 = 0;
if((!P36) && (!dis_flag))
{
down_flag = 1;
up_key();
}
if((!P35) && (dis_flag) && (!key_sense))
{
key_sense = 1;
reset_key1();
}
if((key_sense) && (P35)) key_sense = 0;
if((!P35) && (!dis_flag))
{
down_flag = 0;
up_key();
}
if((P35) && (P36))
{
key_count = 0;
}
if(delay_flag) delay_flag = 0;
}
++ms1;
if(ms1 >= 55)
{
ms1 = 0;
keycheck();
if(!dis_flag)
{
++auto_out;
if(auto_out >= 55)
{
reset_all_flag();
auto_out = set_count = 0;
dis_flag = 1;
read_set_eeprom();
}
}
}
}
counter() interrupt 0 using 0
{
EA = 0;
if((!first_time_flag) && (!relay_flag) && (!tar_flag) && (!delay_flag))
{
count_reg += (unsigned long int) mul_cnt;
if(count_reg > 99999)
{
count_reg = 0;
}
if(count_reg < 0) count_reg = 0;
dis_count_reg = (unsigned int) count_reg / 10;
}
}
keycheck()
{
if(!P34) set_key();
if((!P37) && (!dis_flag))
{
enter_proc();
}
if((!P37) && (dis_flag))
{
reset_key();
}
}
set_key()
{
auto_out = 0;
++set_count;
switch(set_count/5)
{
case 1:
if(!mul_mess_flag) mul_mess_proc();
break;
case 2:
if(!mul_cnt_flag) mul_cnt_proc();
break;
case 3:
enter_proc();
break;
}
}
mul_mess_proc()
{
dis_flag = auto_out = 0;
reset_all_flag();
mul_mess_flag = 1;
dis_up[0] = 47;
dis_up[1] = 23;
dis_up[2] = 43;
dis_up[3] = 25;
}
mul_cnt_proc()
{
reset_all_flag();
dummy = mul_cnt;
mul_cnt_flag = 1;
}
up_key()
{
auto_out = 0;
if (dummy == 0) key_count = 0;
if (down_flag) dummy = dummy * -1;
++key_count;
switch(key_count/10)
{
case 0: ++dummy;
break;
case 1: dummy = (dummy/10) * 10;
dummy = dummy + 10;
break;
case 2: dummy = (dummy/100) * 100;
dummy = dummy + 100;
key_count = 20;
break;
}
if(down_flag) dummy = dummy * -1;
if(mul_cnt_flag) set_count = 10;
if(mul_cnt_flag)
{
if(dummy > 9999)
{
dummy = 9999;
key_count = 0;
}
if(dummy <= 1)
{
dummy = 1;
key_count = 0;
}
}
}
enter_proc()
{
if(!dis_flag)
{
reset_all_flag();
dis_flag = 1;
set_count = 0;
write_set_eeprom();
}
}
reset_all_flag()
{
if(mul_cnt_flag) mul_cnt = dummy;
mul_mess_flag = mul_cnt_flag = 0;
}
reset_key()
{
count_reg = dis_count_reg = 0;
max_flag = max1_flag = 0;
new_flag = 1;
}
reset_key1()
{
count_reg = dis_count_reg = 0;
}
display_value()
{
if(dis_flag)
{
if(!P36)
{
read_max_eeprom();
binbcd(max_count_reg);
// max_count_reg = 0;
}
else binbcd(dis_count_reg);
dis_up[0] = unithb;
dis_up[1] = tenhb;
dis_up[2] = hundhb;
dis_up[3] = thoushb;
}
else if(!dis_flag)
{
if(mul_cnt_flag)
{
binbcd(dummy);
dis_up[0] = unithb;
dis_up[1] = tenhb;
dis_up[2] = hundhb;
dis_up[3] = thoushb;
}
}
if(new_flag)
{
max_count_reg = 0;
new_flag = 0;
}
if(max_count_reg < dis_count_reg)
{
max_count_reg = dis_count_reg;
max_flag = 1;
}
if((dis_count_reg <= max_count_reg) && (max_flag))
{
max_flag = 0;
write_max_eeprom();
}
}
write_max_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x01;
shout();
serial_out = (max_count_reg >> 8) & 0x00ff;
shout();
serial_out = max_count_reg;
shout();
stop();
}
write_set_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x10;
shout();
serial_out = (mul_cnt >> 8) & 0x00ff;
shout();
serial_out = mul_cnt;
shout();
stop();
}
read_max_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x01;
shout();
read_current();
max_count_reg = serial_in;
read_current();
max_count_reg = (max_count_reg << 8) | serial_in;
stop();
}
read_set_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x10;
shout();
read_current();
mul_cnt = serial_in;
read_current();
mul_cnt = (mul_cnt << 8) | serial_in;
stop();
}
Please someone help.
I am looking for the high speed proximity event counter c software code for at89c52. The application is to count high speed pulse and the after it will be multiplied with the multiplication factor and then the result to be displayed.
result = count * multiplication factor.
100 = 1000 * 0.1
I am posting my source code if any one correct it. In my code when the pulse above 20 pulse per second increase it starts giving the wrong result.
#include <stdio.h>
#include <reg51.h>
unsigned char i,dis_up[4],ms10,b;
unsigned char set_count,auto_out,ms1,ms2;
unsigned char serial_out,serial_in,eeprom_counter,nop,dly,dly1;
unsigned char ms51,ms52,key_count;
signed int mul_cnt,dis_count_reg,max_count_reg,dummy;
signed long int count_reg;
extern keycheck(),display_value(),reset_all_flag();
extern up_key(),shift_key(),reset_key(),set_key();
extern enter_proc(),mul_cnt_proc(),reset_key1();
extern read_set_eeprom(),read_max_eeprom();
extern write_set_eeprom(),write_max_eeprom();
extern stop(),start(),read_current(),shout(),shin(),nak();
extern mul_mess_proc();
bit relay_flag,max_flag,max1_flag;
bit dis_flag,down_flag,first_time_flag,delay_flag;
bit key_sense,new_flag,mul_cnt_flag,tar_flag;
bit mul_mess_flag;
#include <c:\c51\inc\sub.h>
#include <c:\c51\inc\estd.h>
#include <c:\c51\inc\display.h>
void main()
{
SP = 0xa0;
IE = 0X0b;
IP = 0x01;
TMOD = 0x11;
TH0 = 0Xec;
TL0 = 0X90;
TH1 = 0XEC;
TL1 = 0X90;
read_max_eeprom();
read_set_eeprom();
dis_flag = 1;
first_time_flag = 1;
TR1 = 1;
TR0 = 1;
IT0 = 1;
count_reg = dis_count_reg = 0;
max_flag = max1_flag = 0;
max_count_reg = 0;
read_max_eeprom();
for(;
{
EA = 1;
}
}
display() interrupt 1 using 1
{
EA = 0;
P2 = 0xff;
TH0 = 0Xec;
TL0 = 0X90;
P00 = select_digit & 0X01;
P01 = select_digit & 0X02;
P02 = select_digit & 0X04;
P03 = select_digit & 0X08;
P2 = dis_tab[dis_up];
if((i == 1) && (dis_flag)) P27 = 0;
if((i == 2) && (!dis_flag) && (mul_cnt_flag)) P27 = 0;
++i;
if(i >= 4)
{
i = 0;
if(first_time_flag)
{
first_time_flag = 0;
read_max_eeprom();
}
}
display_value();
}
timer_time() interrupt 3 using 3
{
EA = 0;
TH1 = 0xec;
TL1 = 0x90;
++ms2;
if(ms2 >= 55)
{
ms2 = 0;
if((!P36) && (!dis_flag))
{
down_flag = 1;
up_key();
}
if((!P35) && (dis_flag) && (!key_sense))
{
key_sense = 1;
reset_key1();
}
if((key_sense) && (P35)) key_sense = 0;
if((!P35) && (!dis_flag))
{
down_flag = 0;
up_key();
}
if((P35) && (P36))
{
key_count = 0;
}
if(delay_flag) delay_flag = 0;
}
++ms1;
if(ms1 >= 55)
{
ms1 = 0;
keycheck();
if(!dis_flag)
{
++auto_out;
if(auto_out >= 55)
{
reset_all_flag();
auto_out = set_count = 0;
dis_flag = 1;
read_set_eeprom();
}
}
}
}
counter() interrupt 0 using 0
{
EA = 0;
if((!first_time_flag) && (!relay_flag) && (!tar_flag) && (!delay_flag))
{
count_reg += (unsigned long int) mul_cnt;
if(count_reg > 99999)
{
count_reg = 0;
}
if(count_reg < 0) count_reg = 0;
dis_count_reg = (unsigned int) count_reg / 10;
}
}
keycheck()
{
if(!P34) set_key();
if((!P37) && (!dis_flag))
{
enter_proc();
}
if((!P37) && (dis_flag))
{
reset_key();
}
}
set_key()
{
auto_out = 0;
++set_count;
switch(set_count/5)
{
case 1:
if(!mul_mess_flag) mul_mess_proc();
break;
case 2:
if(!mul_cnt_flag) mul_cnt_proc();
break;
case 3:
enter_proc();
break;
}
}
mul_mess_proc()
{
dis_flag = auto_out = 0;
reset_all_flag();
mul_mess_flag = 1;
dis_up[0] = 47;
dis_up[1] = 23;
dis_up[2] = 43;
dis_up[3] = 25;
}
mul_cnt_proc()
{
reset_all_flag();
dummy = mul_cnt;
mul_cnt_flag = 1;
}
up_key()
{
auto_out = 0;
if (dummy == 0) key_count = 0;
if (down_flag) dummy = dummy * -1;
++key_count;
switch(key_count/10)
{
case 0: ++dummy;
break;
case 1: dummy = (dummy/10) * 10;
dummy = dummy + 10;
break;
case 2: dummy = (dummy/100) * 100;
dummy = dummy + 100;
key_count = 20;
break;
}
if(down_flag) dummy = dummy * -1;
if(mul_cnt_flag) set_count = 10;
if(mul_cnt_flag)
{
if(dummy > 9999)
{
dummy = 9999;
key_count = 0;
}
if(dummy <= 1)
{
dummy = 1;
key_count = 0;
}
}
}
enter_proc()
{
if(!dis_flag)
{
reset_all_flag();
dis_flag = 1;
set_count = 0;
write_set_eeprom();
}
}
reset_all_flag()
{
if(mul_cnt_flag) mul_cnt = dummy;
mul_mess_flag = mul_cnt_flag = 0;
}
reset_key()
{
count_reg = dis_count_reg = 0;
max_flag = max1_flag = 0;
new_flag = 1;
}
reset_key1()
{
count_reg = dis_count_reg = 0;
}
display_value()
{
if(dis_flag)
{
if(!P36)
{
read_max_eeprom();
binbcd(max_count_reg);
// max_count_reg = 0;
}
else binbcd(dis_count_reg);
dis_up[0] = unithb;
dis_up[1] = tenhb;
dis_up[2] = hundhb;
dis_up[3] = thoushb;
}
else if(!dis_flag)
{
if(mul_cnt_flag)
{
binbcd(dummy);
dis_up[0] = unithb;
dis_up[1] = tenhb;
dis_up[2] = hundhb;
dis_up[3] = thoushb;
}
}
if(new_flag)
{
max_count_reg = 0;
new_flag = 0;
}
if(max_count_reg < dis_count_reg)
{
max_count_reg = dis_count_reg;
max_flag = 1;
}
if((dis_count_reg <= max_count_reg) && (max_flag))
{
max_flag = 0;
write_max_eeprom();
}
}
write_max_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x01;
shout();
serial_out = (max_count_reg >> 8) & 0x00ff;
shout();
serial_out = max_count_reg;
shout();
stop();
}
write_set_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x10;
shout();
serial_out = (mul_cnt >> 8) & 0x00ff;
shout();
serial_out = mul_cnt;
shout();
stop();
}
read_max_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x01;
shout();
read_current();
max_count_reg = serial_in;
read_current();
max_count_reg = (max_count_reg << 8) | serial_in;
stop();
}
read_set_eeprom()
{
start();
serial_out = 0xA0;
shout();
serial_out = 0x10;
shout();
read_current();
mul_cnt = serial_in;
read_current();
mul_cnt = (mul_cnt << 8) | serial_in;
stop();
}
Please someone help.