Time on microcontroller (PIC16f877a)

Status
Not open for further replies.

chanidu

Newbie level 4
Joined
Mar 21, 2016
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
75
I'm new to micro c . Need to turn Timer1 on pic16F877A after pressed a switch 1 and then turn it off when pressed switch 2, then displaying the period of the time on LCD. Plz help me someone...it is urgent...

I attached proteus simulation picture as well as micro c code that i tried... but i don't no whether it is correct...







Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// LCD module connections
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
 
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections
 
unsigned cnt;              // This is the counter variable which will extend desired period
char abc[7];
int clk2;
 
void InitTimer0(){
  OPTION_REG = 0x84;       // Prescaler 256
  TMR0 = 100;
  INTCON = 0xA0;
}
 
void Interrupt(){          // Occurs every 1ms
  if (TMR0IF_bit){
        cnt++;                 // Increment counter, this is the tweak we added in original Timer Calculator code
        TMR0IF_bit = 0;        // Clear TMR0IF
        TMR0       = 100;       // Prepare for next interrupt
  }
}
 
void display(){
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  IntToStr(clk2, abc);
  Lcd_Out(1,1,abc);      // Write text'Hello World' in first row
  }
 
void main() {
  InitTimer0();            // Initialize Timer0
 
 
 TRISD.F1 = 0; //Configure 1st bit of PORTD as output
  PORTD.F1 = 0; //LED OFF
  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
  TRISD.F2 = 1; //Configure 1st bit of PORTD as input
  
 
  cnt = 0;                 // Initialize cnt
 
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,"Hello");      // Write text'Hello World' in first row
 
 
  do {
        if(PORTD.F0 == 0)   //If the switch is pressed
        {
        Delay_ms(100);    //Switch Debounce
       if(PORTD.F0 == 0)//If the switch is still pressed
       {
         cnt = 0;
 
         PORTD.F1 = 1;
 
          }
 
        }
 
      if(PORTD.F2 == 0)   //If the switch is pressed
        {
        Delay_ms(100);    //Switch Debounce
       if(PORTD.F2 == 0)//If the switch is still pressed
          PORTD.F1 = 1;
        clk2 = cnt;
       Display();
       }
  } while(1);
}

 
Last edited by a moderator:

What is the problem with your setup? if I will suggest implement an interrupt timer that Blick the led at a fixed time interval. Then the other part can follow.
 

Please zip and post the complete mikroC project files and Proteus file.
 
Hi,

In question you have asked for timer 1 and in code you have written for timer0. Please check.

Also, interrupt function should be defined as follows:

void interrupt name()
{
}

Amit
 
No, in PIC all interrupts have single vector. So, the routine is common. Source is determined by checked corresponding flags.
 

Thank you very mush for responses...I want to swtich on timer when pressed push button 1 and stop the timer when presse push button 2.then period of time display on lcd...PLZ help me...

I want to swtich on timer when pressed push button 1 and stop when pressed push button 2 and display time period one diplay...thanks for responses...

- - - Updated - - -

Plz help me to do that....
 

Attachments

  • Program.rar
    89.7 KB · Views: 84

You can use single timer for 100ms. Use interrupt for checking buttons and increment timer. Simple task.
 

Hi,

What resolution in time do you want?
What max. time between START and STOP?
What accuracy do you need?

Klaus
 

Hi,

Klaus, do you really want to write code for him?
You caught me ;-)

No. But those informations he needs to provide to everybody who wants to write code for him.
And even he needs those informations if he wants to wrtie it on his own.

***
You said "a 100ms timer". Maybe this is the easiest solution. But who knows?.
If he wants to build a stop watch, then maybe he wants a better resolution.

But maybe he wants to measure the time from winter to summer....

***
Maybe he wants to measure the reaction time of a relay..
For a measurement tool the measured value needs to be reliable and accurate..

so much "maybe"...

There is so much I don´t know. :-(

Klaus
 

KlausST, i want 10ms resolution and max time between start and stop will be 10s. i need 1ms accuracy...
plz help me..i'm new tó pic programming...
 

Easyrider83, thanks for response to my problem. but i didn't understand that what u said...plz help me...i'm new to micro C...PLZ help me...
 

Hi,

if you want a 10ms resolution (then a 1ms accuracy is not possible) then:
* set up timer for interrupt 10ms.

Code:
declare run as bool = 0

on every timer tick:
* if START = pressed then  RUN = 1
* if STOP = pressed then 
    if RUN = 1 then
      RUN = 0
      display COUNTER
      COUNTER = 0
* if RUN = 1 then counter = counter +1
end of interrupt

main loop: nothing to do.

Try to program this.
Test it.
Then give us your program and the results of our test run.

Good luck

Klaus
 

Sir i don't understand that what u said...plz help me soon..this is my project work and have to submit tomorrow....
 

Hi,

I can't write the program for you, because i'm not familiar with PICs.

Klaus
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…