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.

stopwatch using PIC?

Status
Not open for further replies.
thank you very much @internetuser2k12 .. It really helped me. i know for 1ms increment, counter variable will be incrementing very fast and could not see the increment value in LCD and looks like blur. But the problem is, i am not sure whether i'm calculating the timer value to correct. Please check it once,
using internal oscillator, 4Mhz
so f = 4Mhz/4 => 1Mhz
using prescaler 1:2, so dividing by 2, => 1Mhz/2 => 0.5Mhz
t = 1/0.5Mhz =>2us

to increment for 1msec, 1msec/2us => 500 which is 1F4(in Hex)

this is the value I have loaded in timer = FFFF-1F4 => FE0B

Is it correct??? Please help

finally my code looks like this.
HTML:
#include "18F2520.h"
#include <f2520_registers.h>
#fuses INTRC_IO
#use delay(clock=4000000)

#define RS PIN_A2 
#define EN PIN_A1 

void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void T0_init();
void lcd_init();

unsigned int16 temp;
unsigned char intr_flag = 0;

#INT_TIMER0
void Interrupt()
{
  if (TMR0IF)
	{ 
    TMR0IF = 0;
    TMR0H  = 0xFE;
    TMR0L  = 0x0B;
    temp++;
    intr_flag = 1;
	}
}

void main()
{
TRISA = 0x00;
LATA  = 0x00;
TRISB = 0x00;
LATB  = 0x00;
lcd_init();
T0_init();
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(1)
	{
   lcd_cmd(0x80);
   if(intr_flag == 1) {
   	printf(lcd_data,"%LU",temp);
	intr_flag = 0;
   }
}
}

void T0_init()
{
   T0CON = 0x80;
   TMR0H = 0xFE;
   TMR0L = 0x0B;
   GIE	 = 1;
   TMR0IE = 1;               // Clear Timer0 interrupt flag
}

void lcd_init()
{
   lcd_cmd(0x30);      // Configure the LCD in 8-bit mode, 1 line and 5x7 font
   lcd_cmd(0x0C);      // Display On and Cursor Off
   lcd_cmd(0x01);      // Clear display screen
   lcd_cmd(0x06);      // Increment cursor
   lcd_cmd(0x80);      // Set cursor position to 1st line, 1st column
}

void lcd_cmd(unsigned char c) 
{
   output_b (c);
   output_low(RS);
   output_high (EN);
   delay_ms (10);
   output_low (EN);
}

void lcd_data(unsigned char z)
{
   output_b(z);
   output_high(RS);
   output_high(EN);
   delay_ms(10);
   output_low(EN);
}
 

Last edited:

yeah okey i have removed those lines. But still the problem is not solved. Please can you simulate in proteus and try it? It is incrementing but not as quick which i expect. for 10 sec, in LCD it must show 10000. but during simulation, it shows nearly 8000 . Y this is happening? as you said timer value is correct. It must work right???
 

it might be a problem of proteus. zip and post your proteus file and hex file.

Try to add this line TMR0IF = 0; after intr_flag = 0; and see if there will be any change.
 
Last edited:

I'm sorry. i dono how to attach files. I have tried attachment options but it shows error. Please can you tel how to attach? or try to compile the code which i posted above and simulate in your proteus :)
 

First zip your files then click "reply" and then "manage attachments" and then "Add files" then click "Select files" browse for the file in the dialog box and click open. then click "Upload files" after that the uploaded file thumbnail appears in the list. then post the message.
 
thanks ..I have attached. Please look at it.
 

Attachments

  • lcd.rar
    7.6 KB · Views: 46
  • main.rar
    931 bytes · Views: 52

Time has a small variation. see the attached image. LCD is showing 9476 and in the bottom of the image the time since simulation started is 10.25 seconds.
Atleast it takes some time for PIC to start and for Lcd initialization etc.,
 

Attachments

  • time.jpg
    time.jpg
    227 KB · Views: 54

Thanks for your information. But can you do one thing. Please start the stopwatch from your mobile or any other source and compare with the animating time on the proteus. Proteus is running slow as i seen. I have switch on my stopwatch when animating time reaches 5sec since i couldn't start both at same time. animating time must be 5 sec ahead of my stopwatch. But while i was comparing, when animating time reaches 25secs, suprisingly my stopwatch also reaches 25secs. Still running more time, my stopwatch advances the animating time of the proteus. can you know the reason???
 

It is the problem of real time simulation of proteus. It depends on system it is running. proteus has to do many things in 1 sec. like if you clock frequency of PIC is 20 MHz then it should run PIC at 20 MHz at the same time it has to animate LCD etc., so proteus timing is not correct as you said. But you can test on real hardware and see.
 

Apart from possible Proteus inaccuraries, the suggested timer usage involves systematical errors because interrupt latency is added to the actual period. An exact timer routine would e.g. use Timer2 and it's period register. CCS C has built-in functions to setup the timer, by the way.
Code:
void Interrupt()
{
  if (TMR0IF)
  { 
    TMR0IF = 0;
    TMR0H  = 0xFE;
    TMR0L  = 0x0B;
    temp++;
    intr_flag = 1;
  }
}
 
  • Like
Reactions: tpetar

    tpetar

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top