Timer in 8051

Thread Starter

mayank rob

Joined Sep 18, 2012
17
Hi there
I am new to embedded,i have programmed my boards using assembly language but now m trying to learn it using embedded C.
I am using keil, controller-89V51RD2
I want to use timer without using interrupt.
my code is:


#include<reg51.h>
sbit LED1=P3^0;

//Timer 0 is used
void main()
{

while(1)
{
unsigned int x;
//Mode 1 is used
TMOD=0x01;
TH0=0xFF;
TL0=0xFF;
LED1=1;
TF0=0; //clearing the overflow flag
TR0=1; //START TIMER

while(TF0==0)
{
//waiting till the timer flag overflows
}
x=x+1;
if(x%99==0)
{
LED1=!LED1;
}
TF0=0;
TR0=0;

}
}


the code shows 2 warning when i execute it.
I am wondering that if something is wrong with my code or logic...not able to figure it out.
thanks
 

embpic

Joined May 29, 2013
189
as you have given TH0 and TL0 as 0xFF so at every time it get overflow when it get initialize. so no need to use of timer then.
and if you are going to use it without interrupt then no use of timer then use delay instead.
 

Thread Starter

mayank rob

Joined Sep 18, 2012
17
embpic: thank you for realizing me for such a such a silly mistake,it was blunder i realized.
And i didnt use interrupt because am a beginner,so just a thought came into mind of using it without interrupt but then its not working and now am wondering whats wrong.
and i tested it again changing the values to 0x00,but no effect.
The warning it showed is:
*** WARNING L1: UNRESOLVED EXTERNAL SYMBOL
SYMBOL: ?C_START
MODULE: STARTUP.obj (?C_STARTUP)
*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL
SYMBOL: ?C_START
MODULE: STARTUP.obj (?C_STARTUP)
ADDRESS: 080AH

And if i compile it and burn it anyway then i see no response in LED.
LED is in active low configuration
the timers and circuit is working well since i have tested the default codes by the board provider n they run smoothly. Its something wrong with my logic.
 

JohnInTX

Joined Jun 26, 2012
4,787
The warning it showed is:
*** WARNING L1: UNRESOLVED EXTERNAL SYMBOL
SYMBOL: ?C_START
MODULE: STARTUP.obj (?C_STARTUP)
*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL
SYMBOL: ?C_START
MODULE: STARTUP.obj (?C_STARTUP)
ADDRESS: 080AH
This error is issued by the linker and means that there is no object file containing a routine labeled ?C_START.

?C_STARTUP (in STARTUP.A51) LJMPs to ?C_START which has been declared EXTERN (in another file - to be resolved by the linker).

In my older version of Keil C51, ?C_START is in INIT.A51 which must be assembled and linked. ?C_START is declared PUBLIC.

Attempting to run with this error means you have a big hole in your code. Any indication of the code working is pure happenstance.

EDIT:
It looks like ?C_START usage is controlled by PPAGEENABLE which in turn is controlled by the memory model used. Using the COMPACT model should remove the LJMP to ?C_START (which looks like it initializes variables in paged memory). Read the memory model documentation for your chip.

From STARTUP.A51:
Rich (BB code):
;------------------------------------------------------------------------------
;
;  Page Definition for Using the Compact Model with 64 KByte xdata RAM
;
;  The following EQU statements define the xdata page used for pdata
;  variables. The EQU PPAGE must conform with the PPAGE control used
;  in the linker invocation.
;
PPAGEENABLE    EQU    0    ; set to 1 if pdata object are used.
PPAGE        EQU    0    ; define PPAGE number.
;
;------------------------------------------------------------------------------
and later - ?C_START is used only when PPAGEENABLE is <>0 i.e. a non-compact memory model.
Rich (BB code):
IF PPAGEENABLE <> 0
        MOV    P2,#PPAGE
ENDIF

        MOV    SP,#?STACK-1
        LJMP    ?C_START
Finally, there are various versions of STARTUP.A51 tailored to different 8051 variants. Presumably, the IDE project configuration selects the proper one to fit the chip/memory model. You'll have to figure out why the disagreement.
Whew. Have fun!
 
Last edited:

Thread Starter

mayank rob

Joined Sep 18, 2012
17
Thank you
I found the solution.
I just removed the startup.a51 file and the program is running smoothly and behaving as expected.
 

JohnInTX

Joined Jun 26, 2012
4,787
Thank you
I found the solution.
I just removed the startup.a51 file and the program is running smoothly and behaving as expected.
I'm not sure that is a good idea. startup.a51 initializes the C environment including setting variables to 0, loading initialized variables and initializing the 8051 stack pointer.

The startup code runs from reset then jumps to main() to start the user program. Without it or something that does the same thing, you may be in for trouble.

As I said, your problem could be one of confusion between modules about the memory model used. You should resolve that problem instead of removing the startup code.

Look at the link map and see where main() is. If its at the reset vector i.e. no startup code, you have problems.

OTOH, you might find another, correct version of startup.A51 is now in use. In that case you are good to go.
 
Top