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.

Blinking LED with Timer 1 of PIC16F877A

Status
Not open for further replies.

jean12

Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
Hello,I am trying to use the timer 1 in Timer mode for timing a determined time for blinking a LED ,but I am not coming out with this,could anyone help me to sort this out?
I am uisng MicroC
the the code below I was trying to use:
PHP:
//#include <16f877a.h>
int count=0;
void main(void)
 {
  T1CON=0x01;
  TRISC.F7=0X00;
  TRISD.F1=0X00;
  while(1)
  {
  while(TMR1IF==1) ;
  {
    PIR1.TMR1IF=0;
      count++;
      if(count==15)
      {
      count=0;
      PORTC.F7=1;
       delay_ms(1000);
       PORTC.F7=0;
      }
  }
  }
  }

Please help,thank you!!
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
You are in a wrong category, should be in Microcontrollers

Code:
  while(TMR1IF==1) [SIZE=4][COLOR="#FF0000"];[/COLOR][/SIZE]
  {
    PIR1.TMR1IF=0;
  }

I have not pay attention in the logic of your code at all, but as you can see there is a while() and a block following that are not connected. Was that your intension?

- - - Updated - - -

Do try some corrections.
I do not like MicroC and so I am not using it. The code bellow may need some variable names corrected to MicroC syntax:

Code:
#include <16f877a.h> 

void main(void) 
{ 
  int count=0; 
  T1CON=1; 

  TRISC.F7=0X00; 
  TRISD.F1=0X00; 
  while(1) 
  { 
          //wait till tmr1 overflows
          while(PIR1.TMR1IF==0) ; 

          //tmr1 overflows
          PIR1.TMR1IF=0; 
          count++; 
          if(count==15) 
          { 
                count=0; 
                PORTC.F7=!PORTC.F7; 
          } 
  } 
}

if you have an 4MHz oscillator, the led should stay on for about a second and off for the same time. This will last till powered off.
 

jean12

Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
Hello,Xenos;your codes are very interesting and running as you stated,what if I want a LED to be on a different time to off time of the same.
Assume ON time=1s
OFF Time=5s
I would like to use also for loop for helping me for example to implement the following:
1.OFF TIME=5s
2.OFF TIME=5s
3.OFF TIME=5S
4.ON TIME=2S
5.OFF TIME=5s

Please help,
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,707
Helped
4,340
Reputation
8,689
Reaction score
4,303
Trophy points
1,393
Activity points
130,467
Hi,

1.OFF TIME=5s
2.OFF TIME=5s
3.OFF TIME=5S
4.ON TIME=2S
5.OFF TIME=5s

Simplified this says: 20s OFF / 2s ON

Klaus
 

jean12

Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
no no,I was meaning
1.OFF TIME=5s
2.ON TIME=2s
3.OFF TIME=5s
4.ON TIME=2s
5.OFF TIME=3s
6.ON TIME=2s
7.OFF TIME=5s
8.ON TIME=2s
9.OFF TIME=5s


Thank you please help!!
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,707
Helped
4,340
Reputation
8,689
Reaction score
4,303
Trophy points
1,393
Activity points
130,467
Hi,

Should it now 5s OFF / 2s ON in a loop,

Or do you really want the 3s of your line 5?

If your table starts with OFF, then it should end with ON.

Klaus
 

jean12

Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
that`s it use microc for showing it.

Thx
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top