Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Wait execution takes too long- Clock Source ?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 62
Location: Aachen

germany.gif
PostPosted: Tue Apr 15, 2014 1:26 pm    Post subject: Wait execution takes too long- Clock Source ? Reply with quote

Dear All,
I am running a ATMEGA 168 20 PU with a crystal rated at 6 MHz.

Crytstral is connected via 2 capacitors 22pf each to GND and the other direction goes to the XTAL1 and XTAL2 pins of the CPU.

In the execution of wait commands the processor needs 4.5 more time than specified.
This means Wait 10 takes 45 seconds.
Fusebits are set accoring to $PROG &HFF,&HF7,&HDF,&HFF

So the external crystal is selected and clock division by 8 is not selected.

I have no idea why it goes wrong.


Please let me have your advice

best regards

Christian

(BASCOM-AVR version : 2.0.7.6 , Latest : 2.0.7.7 )
Back to top
View user's profile
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Tue Apr 15, 2014 4:47 pm    Post subject: Reply with quote

What programmer do you use ?

Form Helpfile :
Quote:
The $PROG setting is only supported by the AVRISP, STK200/300, Sample Electronics and Universal MCS Programmer Interface. The USB-ISP programmer also supports the $PROG directive.

_________________
/ Kim
Back to top
View user's profile Visit poster's website MSN Messenger
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5060
Location: Holland

blank.gif
PostPosted: Tue Apr 15, 2014 9:05 pm    Post subject: Reply with quote

- $crystal is not having the proper value that matches the oscillator value
- your oscillator is not working at the speed you think it is

best to use some simple code which i have shown here many times to check the speed.
$regfile = ...
$crystal=8000000
config portb=output
do
toggle portB 'check interval
waitms 1000
loop

_________________
Mark
Back to top
View user's profile Visit poster's website
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 62
Location: Aachen

germany.gif
PostPosted: Wed Apr 16, 2014 11:37 pm    Post subject: Reply with quote

Dear Kim, Dear Albert,
many thanks for answering my posting.

@ Kim
Iam using a Diamex DX ISP USB programmer. It runs with the STK500 protocol.
Transfer of data and also of fusebit settings runs fine.

@ Albert
I assume definetely a mismatch with the oscillator frequency.
For the execution of the wait command the compiler needs to know the speed of the oscillator.
During the execution of the wait command the CPU is cunting down some registers in several loops.

Which number is used by the compiler to determine how often these loops have to run ?
I understand the documentation that the value of $CRYSTAL is the important number.

Can it happen that a crystal rated at 6 MHz operates at 1.3 MHz ?

How can I measure the frequency of the oscillator ? By using an oscilloscope most probably ?

best regards

Christian
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1193
Location: SYDNEY

australia.gif
PostPosted: Wed Apr 16, 2014 11:58 pm    Post subject: Reply with quote

Hello Christian

The M168 has a clock output pin CLKO (pin 14 on 28pin dip) by setting the CKOUT fuse you can measure it there The output is buffered so it is able to drive other devices.
Note on the forum there is a frequency counter project using an AVR nice project to build if you do not have one.

Regards Paul
Back to top
View user's profile
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 62
Location: Aachen

germany.gif
PostPosted: Thu Apr 17, 2014 6:27 pm    Post subject: Reply with quote

Dear Paul,
I followed your advice and checked the clock output signal with an oscilloscope.
Result: The unit runs at 6 MHz. I got a wonderful powerful sinus wave with appr. 7V peak to peak.

So it is much more difficult to my to unserstand why the execution of the wait command is extended by factor 4.5


best regards

christian
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5060
Location: Holland

blank.gif
PostPosted: Thu Apr 17, 2014 9:55 pm    Post subject: Reply with quote

and you are sure you have $crystal=6000000 in your code?
just try this simple code :

$regfile="your chip."
$crystal=6000000
config portb=output

do
toggle portb 'connect a led or so
waitms 1000
loop


that should give 1 sec interval. if not, some fuse is really not set properly.

_________________
Mark
Back to top
View user's profile Visit poster's website
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 62
Location: Aachen

germany.gif
PostPosted: Sun Apr 20, 2014 7:30 pm    Post subject: Reply with quote

Dear Albert,

I think I am coming closer to the solution.
It seems that there is a conflict with INT1.
I am using INT 1 to generate a signal to start a measurement of temperature every 5 minutes.
As soon as I have the command Enable INT1 active in the code the execution of the wait command is extended.

I put the reload value into a constant Const Reload = 56594
With 6 Mhz clock it should generate every 10 s an interrupt. Prescaler = 1024
Execution of the ISR every 10 s does not influence a wait 5 instruction ( IMHO)

Every 10 s a variable named Lcd_counter is incremented. When it reaches the value of 30 = 5 Minutes then the temperature from a 1 wire device is requested. I am using the registers 30, 31 and 16

I


These are the setting for the timer:

Config Timer1 = Timer , Prescale = 1024 ' Generates Interrupt for Temperature measuring
Ocr1ah = High(reload)
Ocr1al = Low(reload)
Tccr1a = 0 ' <Separates Outputs from the timer
Set Tccr1b.3
On Compare1a Timer1_isr Nosave


This is the ISR:


Timer1_isr: ' Increments LCD_Counter
$asm

push r30 'Statusregister in r 30
in r30,sreg 'Statusregister to Stack
push r30 'X Register High to Stack
push r31
push r16
Loadadr Lcd_counter , Z
ld r16,Z
inc r16
st Z,r16
pop r16
pop r31
pop r30
!out sreg,r30
pop r30
$end Asm

Return

In the simulator I saw that the WAIT 5 instruction seems to use registers 30 and 31.
On the other hand the ISR is executed every 10s. My wait times are always shorter than 10s and delays by a few micro seconds.
Do you have a good idea for me ?


best regards
Christian
Back to top
View user's profile
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 62
Location: Aachen

germany.gif
PostPosted: Tue Apr 22, 2014 9:20 am    Post subject: Reply with quote

Dear All,
after further digging in the forum I followed a posting and removed the nosave directive at the declaration of the timer.
I did not change anything.
All WAIT commands are executed much longer that programmed.
I do not see where the intereference comes from.
The timer registers and the logic behind the timer runs independently from the execution of the code.
Is the WAIT command checking if interrupts are allowed?
In case this is true and the WAIT commands is stuck in a checking routine, then using of interrups and WAIT commands simultaneously is difficult.

Another possibilöity may be that the ISR is executed many times and slows down the execution of any other code.
To my opinion the ISR is executed every 10 seconds. execution thakews a few microseconds. I doubt that ths can be the reason for extending the wait period by factor 4.

I am lost in the moment.

best regards
Christian
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Tue Apr 22, 2014 1:28 pm    Post subject: Reply with quote

Hi,

So if you disable the ISR the wait command is correct.

Maybe just add abit of code to the ISR in question that increments a byte and display the byte on the LCD.

INT1 is an external hardware interrupt, that can cause an interrupt on Rising,Falling or while LOW. So if the pin is LOW the interrupt will fire continusly. Maybe thats your problem.

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 62
Location: Aachen

germany.gif
PostPosted: Tue Apr 22, 2014 11:04 pm    Post subject: Reply with quote

Dear Ian,

Thank you very much
In a former version of the program I needed INT1.
The directive enable INT1 is not necessary any more. I remove this remnant.

If I am correct then Pin 15 ( PCINT1) is responsible for launching Interrupt 1 is floating free because it is not connected to anything.

The wait command runs correct now.


best regards and many thanks

Christian
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum