plz any one tell me how to work with timer.
i m working with p89v51rd2. in that i m trying to a simple program which showing the numbers on led as counter.
logic of my program is when one cycle is complete of timer than number have to change.
but i m not able to do this.
TMOD=0x20; that is for timer for 2 mode which 16 bit TH1=0xf3; that is for 2400 bugd rate but convert to 9600
any one help me plz.....
Hi Ambuj,
Why not try a versatile software timer in your project?
members.iinet.net.au/.../Timers_0.3.php
Regards, Murray R. Van Luyn.
#include <P89V51Rx2.H> #define char unsigned char #define int unsigned int char timecount,move; /********************************************************* The main function **********************************************************/
void main() { P2=0xff; //Commissioner of all the lights TMOD=0x01; //Timing / Counter 0 work in a way TH0=0x4C; TL0=0x00; //50ms time constants EA=1; //A total interruption ET0=1; //Allow time / Counter 0 interrupted TR0=1; //Start time / Counter 0 interrupted move=0xfe; while(1); } /********************************************************* Interrupt service function **********************************************************/ void Time0(void) interrupt 1 // using 0 { TH0=0x4c; //50ms timing TL0=0x00; timecount++; if(timecount==10) { timecount=0; P2=move; move=(move <<1)|0x01; if(move==0xff) move=0xfe; } }
in that program led was blink but i m not able to interface with 7 segment display. who could do this plz tell me
Did you miss this: www.danlhenry.com/.../keil_code.png ?
that was my mistake
#include <P89V51Rx2.H> #define char unsigned char #define int unsigned int char timecount,move; /********************************************************* The main function **********************************************************/ void main() { P2=0xff; //Commissioner of all the lights TMOD=0x01; //Timing / Counter 0 work in a way TH0=0x4C; TL0=0x00; //50ms time constants EA=1; //A total interruption ET0=1; //Allow time / Counter 0 interrupted TR0=1; //Start time / Counter 0 interrupted move=0xfe; while(1); } /********************************************************* Interrupt service function **********************************************************/ void Time0(void) interrupt 1 // using 0 { TH0=0x4c; //50ms timing TL0=0x00; timecount++; if(timecount==10) { timecount=0; P2=move; move=(move <<1)|0x01; if(move==0xff) move=0xfe; } }
.
That's still a bit of a mess!
Your code would be a whole lot easier for you & others to read if you take time to lay it out carefully; eg,
void main() { P2 = 0xff; //Commissioner of all the lights TMOD= 0x01; //Timer/Counter 0 work in a way TH0 = 0x4C; TL0 = 0x00; //50ms time constants EA = 1; //A total interruption ET0 = 1; //Allow time / Counter 0 interrupted TR0 = 1; //Start time / Counter 0 interrupted move = 0xfe; while(1); }
Any particular reason why you use #define instead of typedef?
Are you sure that those comments will really be meaningful when you look at this again in 6 months...?
Avoid the use of "Magic Numbers" - define constants with meaningful names.
That's a totally different question - so start a new thread.
But before you start a new thread, have you actually done any research into this?
Both the 8051 and 7-segment displays have been around for over a quarter of a century - surely you must be able to find something helpful that's been written down in all that time...?
If you have a specific question, then take time to explain what you've done so far, and where you're stuck...
...
#define char unsigned char #define int unsigned int char timecount,move;
This "clever" use of #define will be problematic when you use it with pointers.
Jon
Jon;
This "clever" use of #define will be problematic when you use it with pointers. Why? Bradford
Jon was thinking about defining pointer types with #define instead of a real typedef:
#define uchar unsigned char #define ucharp unsigned char* uchar a,b; ucharp ap,bp;
expands to:
unsigned char a,b; unsigned char* ap,bp;
And suddenly bp isn't a pointer anymore.
Not even to mention that it'll f*ck up perfectly fine source code using things like "signed char" or "unsigned int".
If #define had been working for defining new data types, the C standard would not have contained any dedicated keyword - the language is minimalistic.
Duh! I failed to look carefully at the original post. My bad! Thanks guys. Bradford