MikroBasic for 8051 v2.20 timing problem using silabs 8051

General discussion on mikroBasic PRO for 8051.
Post Reply
Author
Message
rod27cn
Posts: 6
Joined: 05 Jul 2008 12:41

MikroBasic for 8051 v2.20 timing problem using silabs 8051

#1 Post by rod27cn » 29 Nov 2009 12:33

Hello I tried to convert a simple example for MikroBasic for 8051 v2.20 to have it working for Silabs C8051F020, actually it works but the problem is the timing the built in delay function below:

Delay_ms(1000) ' 1 second delay

gives me around 12seconds delay instead of 1 second delay.

Actually I already change the type of MCU and the Frequency in the project setting to the appropriate value which is used for my hardware which is C8051F020 and 22.1184MHz.

Please advise how to correct this problem, see my code below:

program LED_Blinking

main:
WDTCN = 0xDE ' Sequence for
WDTCN = 0xAD ' disabling the watchdog timer
P5 = 0x0F
P74OUT = 0x08

OSCXCN = 0xF7


while TRUE
P5 = 0x00 ' Turn ON diodes on PORT1
Delay_ms(1000) ' 1 second delay

P5 = 0xFF ' Turn OFF diodes on PORT1
Delay_ms(1000) ' 1 second delay
wend ' Endless loop
end.

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#2 Post by anikolic » 02 Dec 2009 15:30

Hi,
The problem here is that your microcontroller is set to use internal 2MHz oscillator by default, and this hasn't been changed in the course of initialization. If you take a look at c8051F020 datasheet: http://www.keil.com/dd/docs/datashts/si ... 51f02x.pdf in section 14 default on-reset value of OSCICN is 00010100 which implies that internal oscillator is default and set to ON when the chip is reset. This is why your Delay on ~24MHz frequency runs 12 times slower, because the frequency is 12 times lower then one used in delay calculations. So, to use the external oscillator, you must set OSCICN.CLKSL = 1. Also, for greater stability, you should consider the following procedure described in the datasheet:

Code: Select all

Step 1. Enable the external oscillator.
Step 2. Wait at least 1 ms.
Step 3. Poll for XTLVLD => ‘1’.
Step 4. Switch the system clock to the external oscillator.
Delay function calculates the right values according to the frequency you have set in the appropriate IDE oscillator option. So, whatever real frequency is set after changes to OSCXCN and OSCICN, you should set that frequency as the reference for compiler.

Best regards,
Aleksandar
Web Department Manager

Post Reply

Return to “mikroBasic PRO for 8051 General”