Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic 16f877a counter operation in timer1.

Status
Not open for further replies.

azarutz

Member level 2
Joined
Mar 4, 2012
Messages
42
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
India
Activity points
1,579
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 tmr.png in program display function i wrote to display the hex value but it didnt seem correct .
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++);
}
 

You can try this function
Code:
void print_number(unsigned int i)
{
	char u = 0;
	unsigned char lcd_buf[16];
	while(i) {
		lcd_buf[u++] = (i % 10 + '0');
		i /= 10;
	}
	while(u) 
                send_data(lcd_buf[--u]);
}
If you need to print long number of 32 bits then just need to change the int i to long int i.... But not required in your case....
 

You could use capture mode to retreive your data.
 

You can try this function
Code:
void print_number(unsigned int i)
{
	char u = 0;
	unsigned char lcd_buf[16];
	while(i) {
		lcd_buf[u++] = (i % 10 + '0');
		i /= 10;
	}
	while(u) 
                send_data(lcd_buf[--u]);
}
If you need to print long number of 32 bits then just need to change the int i to long int i.... But not required in your case....
Hi , i included this function it shows error .
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);
void print_number(int);
int a=0,b=0,c=0,d=0,v,i;
unsigned char lcd_buf[17]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void main()
{
	T1CON=0X18;
	//TMR1H=0X3C;
    //TMR1L=0xB0;
    TMR1=15536;
	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);
    i=TMR1H;
    //v=TMR1-0x3cb0;
    void print_number(unsigned int i)
  {
	char u = 0;
	unsigned char lcd_buf[16];
	while(i) {
		lcd_buf[u++] = (i % 10 + '0');
		i /= 10;
	}
	while(u) {
                send_data(lcd_buf[--u]);
}
	}
//    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++);
}

error:
Error [314] C:\Users\gobi\Desktop\New folder\pic\mplab\zcd_\duplicate.c; 77.1 ";" expected
Error [285] C:\Users\gobi\Desktop\New folder\pic\mplab\zcd_\duplicate.c; 98.1 no identifier in declaration
Warning [374] C:\Users\gobi\Desktop\New folder\pic\mplab\zcd_\duplicate.c; 98.1 missing basic type; int assumed
Error [314] C:\Users\gobi\Desktop\New folder\pic\mplab\zcd_\duplicate.c; 98.1 ";" expected
 

It seems you need to learn little more about the C programming.... For that you can check out the**broken link removed**.... The bible of C....


Don't put the code I provided inside another funtion.. Because it itself is a function... Just use it outside... just like the display function....
Then call the function with arguments as the hex which u want to print on lcd...
Also, in the declaration, use the same argument type as in the function definition...
ie
Code:
void print_number(unsigned int);

Hope you understood...

-------------------------------------------------------------------------------------

for example,
like this:


Code:
void print_number(unsigned int);
main()
{
     ........
     ........
     ........
     int a = 500;
     print_number(a);
     .......
     .......
}

void print_number(unsigned int num)
{
     ......
     ...... //this will print decimal value of a on LCD
     ......
}
 
Last edited:
Don't put the code I provided inside another funtion.. Because it itself is a function... Just use it outside... just like the display function....
Then call the function with arguments as the hex which u want to print on lcd...
Also, in the declaration, use the same argument type as in the function definition...[/CODE]
oh sorry you are correct . i didnt understand hex to dec conversion it seems complex . i compiled with no error but my program didnt read correct value from TMR1 register , it shows some value .
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);
void print_number(unsigned int);
int a=0,b=0,c=0,d=0,v,i;
//unsigned char lcd_buf[17]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void main()
{
	T1CON=0X18;
	//TMR1H=0X3C;
    //TMR1L=0xB0;
    TMR1=0x3cb0;
   //TMR1=0x3623;
	pic_init();
	lcd_init();
	while(1)
	{  
		if(RC3==1)
		{
		 TMR1ON=1;
		 if(RC4==1)
		 {
			TMR1ON=0;
		 }	
	}
   display();
  while(1);
 }		
	
}
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;
    print_number(v);
//    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 print_number(unsigned int i)
  {
	char u = 0;
	unsigned char lcd_buf[16];
	while(i) {
		lcd_buf[u++] = (i % 10 + '0');
		i /= 10;
	}
	while(u) {
                send_data(lcd_buf[--u]);
}
}
void delay(int d)
{
   int i;
   for(i=0;i<=d;i++);
}
 

Code:
Just check out ur main program:

while(1)
	{  
		if(RC3==1)
		{
		     TMR1ON=1;
		     if(RC4==1)
		     {
			TMR1ON=0;
		     }	
	       }
   display();
   while(1);
 }

Just think what will happen in this code.... And what you want actually?
 

i want to count the difference between two square signal , so that i am going for timer1 to count the difference . when i give 1st square wave to RC3 pin i.e making RC3 high timer gets on and another square wave i gave to RC4 pin while making high timer gets off .i chose prescaler 1:2 and and initial TMR1 value should to 15536 to get 50Hz frequency . i chose 50 hz for easy calculation . for that i made that code when that pin goes high timer gets on and another pin goes high timer gets off. i find a way to convert hex to dec now i cant find the difference . i think this term is not needed after display cal fun.
Code:
while(1);
my square wave also 50 Hz .
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top