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.

1 second delay using TIMER1 of PIC16f877a

Status
Not open for further replies.
i am doing a simple project of generating 1 second time delay and i choose timer1 of pic micro controller (PIC16f877a)
the formula i have chosen to compute the time delay is like
TIMER1_count.jpg


so for 1 sec the count value comes out to be 15

register bit selection is as follows

T1CON_REGISTER.jpg



  1. TMR1ON=1; // the timer is enable
  2. TMR1CS=0; // internal clock source
  3. T1CKPS0=0; // Prescaler value set to “00”
  4. T1CKPS1=0; // which means 1:1 (no division)

and according to the calculation above my code is for 1 sec as follows( iam using mikroc compiler and proteus for simulation) :


int count=0;
void main() {

TRISC=0; // configuring output port
PORTC.F0=1; // led connected to 1st pin of portC
TMR1H=0x00; // initial count values in timer1 register
TMR1L=0x00;
T1CON=0x01; // explained above in the discription
while(1)
{
while(TMR1IF==0); // monitor the timer1 flag for overflow
count=count+1;

if(count==15)
{
PORTC.F0=~PORTC.F0;
count=0;
}
PIR1.F0=0; //contaning Timer flag register at bit 0

}
}

i think iam doing right but the code does not work
anyone please tell me what wrong with it
i think iam doing right but the code does not work
anyone please tell me what wrong with it
 
Last edited:
You're not clearing the timer1 interrupt flag.

Whoops, yes you are but in a confusing way. Why PIR1.F0 instead of TMR1IF?

Otherwise, it looks correct.

What is it doing besides "not working"?

Edit, is your config right as you didn't include it. And please, not a hex value.

Mike.
 
You're not clearing the timer1 interrupt flag.

Whoops, yes you are but in a confusing way. Why PIR1.F0 instead of TMR1IF?

Otherwise, it looks correct.

What is it doing besides "not working"?

Edit, is your config right as you didn't include it. And please, not a hex value.

Mike.

if i use TMR1IF=0 instead of PIR1.F0=0
yhen the mikro c compiler gives me error that " Assigning to non-lvalue 'TMR1IF' timer1.c"

and what is ur point at "is your config right as you didn't include it. And please, not a hex value."
sorry, i did not understand
 
If you can't set TMR1IF=0 then the compiler is crap. Put in a bug report. As to the config, If you've stated RC oscillator and used a crystal then that will also stop it working. I'm just asking questions, just trying to help.

You didn't answer my question, what is it doing beside not working! It doesn't work doesn't tell us/me much.

I'm now a bit drunk but trying to help.

Mike.
 
If you can do if(TMR1IF==0) then why not TMR1IF=0? Did I miss something?

Still going crap if the above is correct.

Mike.
 
If you can do if(TMR1IF==0) then why not TMR1IF=0? Did I miss something?

Still going crap if the above is correct.

Mike.

You can't, which is probably why the code doesn't work as he expects

TMR1IF is defined as 0

so this
Code:
while(TMR1IF==0); // monitor the timer1 flag for overflow

is always true

What he needs is
Code:
while(TMR1IF_bit ==0); // monitor the timer1 flag for overflow
which accesses bit 0 of PIR1

or this does the same thing.
Code:
while (PIR1.TMR1IF == 0) ;
 
Last edited:
MikroC is weird around bit definitions... This is the part where I have to re-think things.... Everyone else's compiler uses the definitions out of the datasheet.. But as they have adopted the "sbit" variable type, we'll carry on.

As Pete said... you can still use the long method... PIR1.TMR1IF
 
MikroC is weird around bit definitions... This is the part where I have to re-think things.... Everyone else's compiler uses the definitions out of the datasheet.. But as they have adopted the "sbit" variable type, we'll carry on.

As Pete said... you can still use the long method... PIR1.TMR1IF
hi there, is there any link u send to me that explains how to do single bit manipulation and in special purpose registers
using mikroc compiler
 
These are the things I find discouraging to attempt learning C for these micros. So similar, so different thus so confusing. :wideyed: The struggle to learn it right, scares me. :nailbiting: And I suspect, not getting much benefit of it.

Of course, always from my point of view, an (at all) non-conversant.

(Hundreds of millions people participated in holy wars along History. Do not count on me). :happy:
 
Deleted text.
 
hi there, is there any link u send to me that explains how to do single bit manipulation and in special purpose registers
using mikroc compiler

Only the help file..... If you need to know any identifiers that have been defined.. Start typing the statement and hit "Ctrl+Space" and the code explorer will show you whats available...
 
Only the help file..... If you need to know any identifiers that have been defined.. Start typing the statement and hit "Ctrl+Space" and the code explorer will show you whats available...

I was going to suggest looking at the MikroC Help but having looked at it earlier it seems to be badly worded and confusing...... is it just me?

If you look in the installation directory for MikroC and find the directory named Defs (Mikroelektronika\mikroC PRO for PIC\Defs) then find the file named 16F877a.c and look in there you will see all the declarations for the registers and bit fields.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top