Schematic Software Freuency meter and pulse width measurement | Controller Circuit

Sunday, February 5, 2012

Schematic Software Freuency meter and pulse width measurement

Software Freuency meter and pulse width measurement.
#include //please includes at89x52 . h
#include //please include intrins . h for _nop_() instructions
#include //please include stdio . h
/********* Function Prototype Declarations********************/
void init_serial_port(void);
void lcd_start_messeges(void);
void init_timer(void);
void init_interrupts(void);
void serial_start_messeges(void);
void waitms(unsigned int );
void waitUS (unsigned char );
void init_lcd(void);
void clearlcd(void);
void putcharlcd(unsigned char);
void putstringlcd(unsigned char *);
void print_str_lcd(unsigned char *);
void write_lcd(unsigned char ) ;
void positioncursor(unsigned char);
/******** END of Declarations*********************************/
sbit rs_lcd = P3^6; // Register Select LCD, 1= Data, 0 = Instruction
sbit en_lcd = P3^7; // Enable LCD H->L enable
sbit rw = P2^2;
sbit osc = P0^1;
sbit heart_beat = P0^0;
sfr16 TIMER2=0xCC; sfr16 RCAP2=0xCA;sfr16 DP=0x82;
/******* END of Definitions ******************************/
/************ Text Messeges ********************************/
unsigned char code msg0[]= "microcontroller.circuitlab.org ";
unsigned char code msg1[]= " At89c52 $";
unsigned char code msg2[]= " Frequency meter $";
unsigned char code msg3[]= " Microcontroller PROJECT $";
unsigned char code msg4[]= " Engineering students$";
unsigned char code msg5[]= "Frequncy Measurement$";
unsigned char code msg6[]= "Cum Digital Clock $";
unsigned char code msg7[]= " Heart beats $";
unsigned char code msg8[]= " are counted$ ";
unsigned char code msg9[]= " which are then $ ";
unsigned char code msg10[]=" displayed on LCD$";
unsigned char code msg11[]=" and as well as$";
unsigned char code msg12[]=" sent to PC$";
unsigned char code msg13[]=" Through Serial port$";
unsigned char code msg14[]=" RS-232$";
unsigned char code msg15[]="Freq=$";
unsigned char code msg16[]="P.W=$";
unsigned char buff [10]; //define 10 byte buffer
unsigned char n; //sprintf return variable
/* end of text messeges block */
// global variables declarations
unsigned char beat1,beat2; //counter for heart beat
unsigned int frequency,pw;
bit count_flag=0, disp_time=1, serial=0, time_incorrect=1;
struct timestruct
{
unsigned int sec,minute,hour;
}time;
unsigned char t2count=20;
/******** Start of Main*****************************/
void main (void)
{
rw = 0 ;//LCD is always Written and never read;
waitms(20); //wait for lcd to get ready
init_serial_port();
serial_start_messeges();
init_lcd();
lcd_start_messeges();
init_timer();
init_interrupts();
RI=1;
while (1)
{
if(disp_time)
{
bit temp_flag; temp_flag=ES; ES=0;
n=sprintf(buff,"%02d:%02d:%02d",time.hour,time.minute,time.sec);
//save time to buffer;
disp_time=0;// clear flag
positioncursor(0x00);
putstringlcd(msg13);// Clear the First LCD Line
positioncursor(0x05);
print_str_lcd(buff);
putchar(0x0D);
puts(buff);
/* time updated*/
if(count_flag)
{
positioncursor(0x40);
putstringlcd(msg13); // Clear LCD Second Line
count_flag=0;
n = sprintf (buff, "%03d",frequency); //conversion to buff
positioncursor(0x40); //Second line
putstringlcd(msg15);
print_str_lcd(buff); //print frequency at second line
putstringlcd("KHz$");
positioncursor(0x4A); //2nd line
putstringlcd(msg16);
puts(msg15);
puts(buff);
n = sprintf (buff, "%05d",pw);
print_str_lcd(buff);
putstringlcd("uSec$");
puts(msg16);
puts(buff);
// printing complete,
/************************************************/
EX0=1; //enable interrupt for further measurements
}
ES=temp_flag;
}
if(beat1++==250){
if(beat2++==100){
beat1=beat2=0;
heart_beat = ~heart_beat;
}}}}
void timer2(void) interrupt 5
{
//every 50 msec
t2count--;
TF2=0;
if(t2count==0){
t2count=20; // 20x50=1000ms=1 Sec
disp_time=1;// update the display on return from interrupt
time.sec=time.sec+1; // increment second
if(time.sec>=60){
time.sec=0; //if sec=60, increment minutes
time.minute=time.minute+1;
if(time.minute>=60){
time.minute=0; time.hour=time.hour+1;
if(time.hour>=13){
time.hour=0;
}}}}}
void exter_intr_0(void) interrupt 0
{
unsigned char temp=TMOD;
TR1=TF1=TF0=0; //clear timer flags
EX0=0; //Disable further interrupts for one sec
serial=0; //timer1 is not available for baud rate generation now
DP=-1000; //1msec delay
TMOD=0x15; //timer 0,1 both 16 bit mode, timer0 as counter
TH1=DPH; TL1=DPL; //load timer 1 to create 1 sec delay
TH0=TL0=0;
TR1=TR0=1; //start timers
while(!TF1); // wait for 1msec
TR0=TR1=TF1=0; // Stop timers
DPL=TL0; DPH=TH0; frequency=DP;
/************ Frequency measured, now measure pulse width*****/
TMOD=0x09; //timer 0,1 both 16 bit mode, timer0 as counter
TH0=TL0=TF0=0;
while(!T0); //wait for pulse to go high
TR0=1; //start timer0
while(T0); // wait for pulse to go low
TR0=0; //stop timer
DPL=TL0; DPH=TH0; pw=DP; //save the timer count to pulse width
/*******************End of measurement*****************************/
TMOD=temp;
TH1=TL1=-26; // for 1200 bps, but it is doubled by setting SMOD bit
TR1=1; // spare timer to generate baud rate
count_flag=1; // display frequency and pulse width on return from interrupt.
}
void print_str_lcd(unsigned char *d)
{
while (n>0)
{
write_lcd(*d);
d++;
n--;
}
}
void clearlcd(void)
{
rs_lcd =0;
write_lcd(0x01);
rs_lcd =1;
}
void positioncursor(unsigned char c )
{
rs_lcd = 0;
write_lcd(0x80 | c);// 1xxx xxxx set address of cursor
waitms(50);
rs_lcd =1;
}
void putstringlcd(unsigned char *d)
{
while(!(*d == '$'))
{
write_lcd(*d);
d++;
}
}
void write_lcd(unsigned char a)
{
P1 = a;
waitUS(250);
en_lcd = 0;
waitUS(250);
waitUS(250);
waitUS(250);
waitUS(250);
en_lcd = 1;
}
void waitUS(unsigned char a)
{
while(--a != 0); /* wait = a * 2 + 5 usec @ 12 MHz*/
}
void waitms(unsigned int a)
{
while (--a !=0)
{
waitUS(247); //500us delay
waitUS(247);
waitUS(247); //500us delay
waitUS(247);
}
}
void lcd_start_messeges(void)
{
positioncursor(0x00); //first line
putstringlcd(msg1);
positioncursor(0x40); //2nd line
putstringlcd(msg2);
waitms(800);
clearlcd();
positioncursor(0x00); //first line
putstringlcd(msg3);
positioncursor(0x40); //2nd line
putstringlcd(msg4);
waitms(800);
clearlcd();
positioncursor(0x00); //first line
putstringlcd(msg5);
positioncursor(0x40); //2nd line
putstringlcd(msg6);
waitms(800);
clearlcd();
positioncursor(0x00); //first line
putstringlcd(msg7);
waitms(800);
clearlcd();
positioncursor(0x00); //first line
putstringlcd(msg8);
positioncursor(0x40); //2nd line
putstringlcd(msg9);
waitms(800);
clearlcd();
positioncursor(0x00); //first line
putstringlcd(msg10);
positioncursor(0x40); //2nd line
putstringlcd(msg11);
waitms(800);
clearlcd();
positioncursor(0x00); //first line
putstringlcd(msg12);
positioncursor(0x40); //2nd line
putstringlcd(msg13);
waitms(800);
}
void init_serial_port(void)
{
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
PCON |= 0x80; /* set SMOD = 1 for double buad rate */
TMOD |= 0x20; /* timer 1 autoreload mode */
TH1 = -26; /* TH1: -26reload value for 1200 baud @ 12MHz */
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
}
void init_timer(void)
{
TH2=0x0FF;
TL2=0x00;
T2CON=0x00; // Timer2 auto reload mode
RCAP2=-50000; // for debugging only , actual value =-50000;
TR2=1; // start timer2 to start generating 50ms delays
}
void init_interrupts(void)
{
IE=0xA1; // enable globle, timer2 interrupt, external0 interrupt
PT2=1; //set priority for timer2
ES=1; //Enable serial interrupt
TCON |= 0x05; // low edge triggered for external 0 and external 1 int
}
void init_lcd(void)
{
//LCD module
// D0-D7 -> P1
// RS -> P2.0
// EN -> P2.1
rs_lcd = 0 ; //for cmd
waitms(500);
write_lcd(0x38); //Function Set 0011 1000
waitms(100);
write_lcd(0x38); //Function Set 0011 1000
waitms(100);
write_lcd(0x38); //Function Set 0011 1000
waitms(100);
write_lcd(0x0C); //display off/ON No Cursor No Blinking at cursor
waitms(100);
write_lcd(0x01); //clear Display
waitms(100);
write_lcd(0x06); //Entry Mode Set
rs_lcd = 1 ; // for data
}
void serial_start_messeges(void)
{
bit temp_flag; temp_flag=ES; ES=0;
puts(msg0);
puts (msg2);
puts (msg3);
puts (msg4);
puts (msg5);
puts (msg6);
puts (msg7);
puts (msg8);
puts (msg9);
puts (msg10);
puts (msg11);
puts (msg12);
puts (msg13);
ES=temp_flag;
}
void serial_recieve(void) interrupt 4
{
if(RI){RI= 0;
if(SBUF == 'i'| SBUF == 'I'){puts("Please Enter the Current Time\n");
scanf("%d %d %d",&time.hour,&time.minute,&time.sec);
while(time.hour>12)
time.hour-=12;
ES=0; //once time has been entered the serial interrupt should be disabled
}}}
.
pulse monitor,pulse oxygen monitor,exercise pulse monitor,draft project management software,frequency measurement help desk software,frequency of shopping cart software developement measure ac frequency circuit programming 8051 microcontroller circuit diagram

No comments:

Post a Comment