Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

start and stop timer when a pin high for that program error .

Status
Not open for further replies.

azarutz

New Member
hi, i want to start the timer when a pin high and stop the timer when another pin high and display the count in lcd for that i written a c program in hi-tech c compiler . first i didnt know how to convert hex value to decimal . my program my simulation as follow,
Code:
#include <pic.h>
#define RS RB0
#define RW RB1
#define E RB2
void pic_init(void);
void lcd_init(void);
void delay(int);
void command(char);
void send_data(char);
void display(void);
int a=0,b=0,c=0,d=0,v;
void main()
{
	T1CON=0X18;
	TMR1H=0X3C;
    TMR1L=0xB0;
	pic_init();
	lcd_init();
	while(1)
	{  
		if(RC3==1)
		{
		 TMR1ON=1;
		 if(RC4==1)
		 {
			TMR1ON=0;
		 }	
	}
   display();
 }		
	
}
void pic_init(void)
{
	TRISA=0xff;
	PORTA=0x00;
	TRISB=0x00;
	PORTB=0x00;
	TRISD=0x00;
	PORTD=0x00;
}
void lcd_init(void)
{
	command(0x38);
	command(0x01);
	command(0x0c);
}
void command(char comm)
{
	PORTD=comm;
    RS=0; //register select
    RW=0;//read or write
    E=1; //enable data line
    delay(1000);
    E=0; //disable data line
   delay(1000);
}
void send_data(char data)
{
   PORTD=data;
   RS=1;
   RW=0;
   E=1;
   delay(1000);
   E=0;
   delay(1000);
}	
void display()
{ 
    command(0x80);
    //v=TMR1H;
    v=TMR1-0x3cb0;
    a=v/10;
    d=v%10;
    v=v/10;
    b=v%10;
    v=v/10;
    c=v%10;
    send_data(0x30+a);
    send_data(0x30+b);
    send_data(0x30+c);
    send_data(0x30+d);
}	
void delay(int d)
{
   int i;
   for(i=0;i<=d;i++);
} [ATTACH=CONFIG]62027[/ATTACH]
in program display function i wrote to display the hex value but it didnt seem correct .
 
To display Hex values, you must work in base 16 not 10. Also use a conversion array.

char hexArray[]={'0','1','2','3','4','5','6','7','8','9','A',B','C','D','E','F'};

then a = v/16; d = v%16; etc...

Each value will index the correct ascii character to display...
 
To display Hex values, you must work in base 16 not 10. Also use a conversion array.

char hexArray[]={'0','1','2','3','4','5','6','7','8','9','A',B','C','D','E','F'};

then a = v/16; d = v%16; etc...
hi , i didn't know how to convert hex to dec using conversion array please fill the line .
 
I not sure I follow you...... Do you need to display in decimal?
You are asking for hex to decimal conversion.....

I think what you actually want is Binary to ascii....

This is how I do it ....... printf("%ld", TMR1);

You need a putch(); function in order to use printf(); In the source directory you will find the putch() source code....It is empty... just add the code to display to LCD ie... send_data(ch);

or rename YOUR send_data(char ch); to putch(char ch);

ie..
Code:
void putch(char ch)
   {
   send_data(ch);
   }
 
Last edited:
I not sure I follow you...... Do you need to display in decimal?
You are asking for hex to decimal conversion.....

I think what you actually want is Binary to ascii....

This is how I do it ....... printf("%ld", TMR1);

You need a putch(); function in order to use printf(); In the source directory you will find the putch() source code....It is empty... just add the code to display to LCD ie... send_data(ch);

or rename YOUR send_data(char ch); to putch(char ch);

hi, i am using hi-tech c compiler .. when i include such file directory it shows no such file directory .Really my problem is i want to read data from timer1 register (TMR1) which is 16 bit register as i am using pic 16f877a. that timer register counts data in binary or hex , if it is hex i want that value from hex to dec , and then i add some mathematical step to convert it to ascii . i include my source code
Code:
#include <pic.h>
//#include "putch"
#define RS RB0
#define RW RB1
#define E RB2
void pic_init(void);
void lcd_init(void);
void delay(int);
void command(char);
void send_data(char);
void display(void);
int a=0,b=0,c=0,d=0,v;
void main()
{
	T1CON=0X18;
	//TMR1H=0X3C;
    //TMR1L=0xB0;
    TMR1=0x3cb0;
	pic_init();
	lcd_init();
	while(1)
	{  
		if(RC3==1)
		{
		 TMR1ON=1;
		 if(RC4==1)
		 {
			TMR1ON=0;
		 }	
	}
   display();
 }		
	
}
void pic_init(void)
{
	TRISA=0xff;
	PORTA=0x00;
	TRISB=0x00;
	PORTB=0x00;
	TRISD=0x00;
	PORTD=0x00;
}
void lcd_init(void)
{
	command(0x38);
	command(0x01);
	command(0x0c);
}
void command(char comm)
{
	PORTD=comm;
    RS=0; //register select
    RW=0;//read or write
    E=1; //enable data line
    delay(1000);
    E=0; //disable data line
   delay(1000);
}
void send_data(char data)
{
   PORTD=data;
   RS=1;
   RW=0;
   E=1;
   delay(1000);
   E=0;
   delay(1000);
}	
void display()
{ 
    command(0x80);
    //v=TMR1H;
    v=TMR1-0x3cb0;
    a=v/16;
    d=v%16;
    v=v/10;
    b=v%10;
    v=v/10;
    c=v%10;
    send_data(a);
    send_data(0x30+b);
    send_data(0x30+c);
    send_data(0x30+d);
}	
void delay(int d)
{
   int i;
   for(i=0;i<=d;i++);
}
 
No... Like this

Code:
#include <pic.h>
#include <stdio.h>   /// Include this header
#define RS RB0
#define RW RB1
#define E RB2
void pic_init(void);
void lcd_init(void);
void delay(int);
void command(char);
void send_data(char);
void display(void);
int a=0,b=0,c=0,d=0,v;
void main()
{
	T1CON=0X18;
	//TMR1H=0X3C;
    //TMR1L=0xB0;
    TMR1=0x3cb0;
	pic_init();
	lcd_init();
	while(1)
	{  
		if(RC3==1)
		{
		 TMR1ON=1;
		 if(RC4==1)
		 {
			TMR1ON=0;
		 }	
	}
   display();
 }		
 
}
void pic_init(void)
{
	TRISA=0xff;
	PORTA=0x00;
	TRISB=0x00;
	PORTB=0x00;
	TRISD=0x00;
	PORTD=0x00;
}
void lcd_init(void)
{
	command(0x38);
	command(0x01);
	command(0x0c);
}
void command(char comm)
{
	PORTD=comm;
    RS=0; //register select
    RW=0;//read or write
    E=1; //enable data line
    delay(1000);
    E=0; //disable data line
   delay(1000);
}

void putch(char ch)   // redeclare this function
	{
	send_data(ch);   // to redirect to LCD
	}

void send_data(char data)
{
   PORTD=data;
   RS=1;
   RW=0;
   E=1;
   delay(1000);
   E=0;
   delay(1000);
}	
void display()
{ 
    command(0x80);
	printf("%d",TMR1);		// simple command to print to screen.
}							// why re-invent the wheel.
void delay(int d)
{
   int i;
   for(i=0;i<=d;i++);
}

Makes life easier.
 
Last edited:
thank you Ian Rogers , i can convert hex to dec easily but the thing is i cant able to count value when a pin is high and stop while another pin high , that means if i give high signal to one pin timer start to count and stop while another pin high , i defined both pin. same program .
 
Your code is nearly right..

Code:
   if(!RC4)    //  if RC4 is low
      {
      if(RC3) 
         TMR1ON = 1;   //If RC3 is high turn timer on
      else
         TMR1ON = 0;  // If RC3 is low turn timer off
      }
   else
      TMR1ON = 0;   // if RC4 is high turn timer off
 
hi i want to count the difference between two square signal , i given each signal to separate pin i.e RC3 and RC4 . till i cant get output , it not showing exact difference i cant able to find where i had mistake , i didnt know any freuency mismatching or else .i run 8 bit 16f877a pic microcontroller . here is my source code
Code:
#include <pic.h>
#include <stdio.h>   /// Include this header
#define RS RB0
#define RW RB1
#define E RB2
#include <string.h> 
void pic_init(void);
void lcd_init(void);
void delay(int);
void command(char);
void send_data(char);
void display(void);
//const char *byte_to_binary(int);
unsigned long int a=0,b=0,c=0,d=0,v;
void main()
{
	T1CON=0X18;              // setting prescaler 1:2
         TMR1=3cb0;                  // initial value 15536 or 3cb0 , it start from
       //a=6553;
	pic_init();
	lcd_init();
	while(1)
	{  
     if(!RC4)    //  if RC4 is low
      {
      if(RC3) 
         TMR1ON = 1;   //If RC3 is high turn timer on
      else
         TMR1ON = 0;  // If RC3 is low turn timer off
      }
   else
      TMR1ON = 0;   // if RC4 is high turn timer off
   
   while(1)
  {
  TMR1ON=0;
  display();
		}
 }		
 
}
void pic_init(void)
{
	TRISA=0xff;
	PORTA=0x00;
	TRISB=0x00;
	PORTB=0x00;
	TRISD=0x00;
	PORTD=0x00;
}
void lcd_init(void)
{
	command(0x38);
	command(0x01);
	command(0x0c);
}
void command(char comm)
{
	PORTD=comm;
    RS=0; //register select
    RW=0;//read or write
    E=1; //enable data line
    delay(1000);
    E=0; //disable data line
   delay(1000);
}
 
void putch(char ch)   // redeclare this function
	{
	send_data(ch);   // to redirect to LCD
	}
 
void send_data(char data)
{
   PORTD=data;
   RS=1;
   RW=0;
   E=1;
   delay(1000);
   E=0;
   delay(1000);
}	
void display()
{ 
    command(0x80);
	printf("%d",TMR1);		// simple command to print to screen.
}                                  		// why re-invent the wheel.
void delay(int d)
{
   int i;
   for(i=0;i<=d;i++);
}
 
I'm not quite sure what you are trying to do....

Are you trying to differentiate two square wave signals?

ie... square wave a at 5khz square wave b at 7khz.... difference 2khz?

No , i am trying to find phase difference between voltage and current .when alternating voltage and current crosses zero ZCD trigger a square wace form . Using that square wave form i am going to find phase difference bteween voltage and current . mostly voltage and current have phase lead or lag .
 
Last edited:
Right.. so you're going in the right direction... So, detect the the first pulse (square wave ) set the timer going, when the second pulse arrives.. stop the timer..work out the frequency shift.

But if the current is lagging... the wrong pulse will arrive first.... so you'll need to time between both pulses and work out which one represents the lag / lead.

You do realise this is a job for a pll (lm565) connected to the pic..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top