BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 8:25 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Real time clk (RTC)
PostPosted: Wed Sep 14, 2011 9:21 pm 
Offline
Citizen

Joined: Wed Jun 29, 2011 2:29 am
Posts: 21
Hi..
how to use real time clk in basic atom pro 40..
can anyone please explain with example..?



Thanks


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Thu Sep 15, 2011 6:41 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 528
Nathan is the best person to answer it. But it has been my impression that in order for the RTC to work on the H8/3687 chip you need to have an external 32.xxxkhz external clock hooked up to chip and I don't think that either the BAP40 and the ARC32 has it. I hope I am wrong, but... I believe you can use the RTC without it as a simple 8 bit counter...

Kurt


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Thu Sep 15, 2011 10:14 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
The RTC hardware can be used as a regular timer(not an RTC) even whithout the 32khz crystal installed.

Currently only the MadHatter boards have the 32khz crystal. You can not easily modify any of the other boards to add the 32khz crystal.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Sun Sep 18, 2011 11:32 pm 
Offline
Citizen

Joined: Wed Jun 29, 2011 2:29 am
Posts: 21
Thanks Nathan.
What is the difference between real time clock time base function in TimerA and realtime clock(RTC)?


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Mon Sep 19, 2011 4:52 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
The RTC hardware in the H8-3687(eg AtomPro40,MadHatter,ARC32) isa full real time clock. It has second,minutes,hours,etc). The TimerA in the H8-3694 can use an external 32khz clock so you can implement your own RTC in software, but it does not have the extra hardware to handle calculating minutes,hours etc...

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Mon Sep 19, 2011 9:40 pm 
Offline
Citizen

Joined: Wed Jun 29, 2011 2:29 am
Posts: 21
how to set the RTC? Can you give example?


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Tue Sep 20, 2011 10:01 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
Click the help menu and open the H8-3687 hardware manual. Go to the RTC section. It describes how to setup the registers. All the registers are already defined in basic so you can access any of them as if they were byte sized variables.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Mon Sep 26, 2011 4:03 am 
Offline
Citizen

Joined: Wed Jun 29, 2011 2:29 am
Posts: 21
I read the manual but not understood.I will tell you my problem clearly.Here is my code contains kp,ki,kd as inputs. Error is calculated by using set point and current rpm value. I should know the dt value to calculate the duty cycle. I tried to understand RTC function but i dont know how to apply that function in my program. Could you please help me..

Code:
loop
    serin s_in,i9600,[dec kp]
    serin s_in,i9600,[dec ki]
    serin s_in,i9600,[dec kd]
    serout s_out,i9600,[dec kp," ",dec ki," ",dec kd,13]
    serin s_in,i9600,[dec sp]
    serout,s_out,i9600,[dec sp]
    err=sp-avgrpm
    intg=intg+err*dt
    deri=(err-err0)*dt
    dutycycle=kp*err+ki*intg+kd*deri
    err0=err
goto loop

Thanks alot


Last edited by KurtEck on Mon Sep 26, 2011 6:36 am, edited 1 time in total.
Code is easier to read in a code block


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Mon Sep 26, 2011 7:26 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 528
As we mentioned before the RTC will not work for you with the BAP40 as it does not have a 32khz crystal. But as I mentioned earlier either here or on the Lynxmotion thread, you can use the standard timers on the processor to time things... We do this all the time with several of our robots, like the Lynxmotion Phoenix...

The question is which timer to use. That often depends on other things like, what other things are you using in your program, duration of what you are trying to time and how high a resolution you need, it is all a set of trade offs.

The best timer to use for many things is TimerZ (TimerW on Bap24/28) as this is the only 16 bit timer. Which at it's lowest resolution (clock/8) will overflow about 38 times per second. But, TimerZ is used for HSERVO and/or HPWM or... So if you use any of these hardware features you probably can not use this timer. But if you are not using these other features and what you are timing is of a short enough duration that the clock does not overflow, than you can simply use this timer with no overhead. Simply initialize the timer and either grab or reset the TCNT (0 or 1 depending on how set up) at the start of what you are timing and then grab TCNT at the end and if you reset this is your DT or simply subtract off the value you grabbed at the start...

But if TimerZ does not work for you than there is the two 8 bit timers. TimerB0 and TimerV. Each with their own pluses and minuses. Unless you are timing something with a really short duration, you will probably need to setup an interrupt handler, which is not hard, but there are issues. That is if an interrupt occurs while you are doing some bitbang type operations, like SERIN/SEROUT... It is very possible the interrupt code will corrupt the data you see from those bitbang functions. But in many cases you can work to avoid this. On Bap40 you can use HSERIAL to talk to the debug terminal and another device as well, which avoids bit bang. Also you can work to turn off interrupts while the bit bang is happening. I do this with phoenix code, knowing that I will reenable them quick enough not to lose any... I will try to show some extracts of the code that shows an example of using TimerB1...

Code:
;[TIMING]
lTimerCnt         var   LONG   ; used now also in timing of how long since we received a message
lCurrentTime      var long   
lTimerStart         var long   ;Start time of the calculation cycles
lTimerEnd         var long    ;End time of the calculation cycles
CycleTime         var word   ;Total Cycle time
...
TIMERINT   con   TIMERB1INT
ONASMINTERRUPT TIMERB1INT, HANDLE_TIMERB1_ASM
...
WTIMERTICSPERMSMUL con 256   ; Arc32 is 20mhz need a multiplyer and divider to make the conversion with /8192
WTIMERTICSPERMSDIV con 625  ;
TMB1 = 0   ; clock / 8192               ; Low resolution clock - used for timeouts...
...
ENABLE TIMERINT
ENABLE
...
  'Start time
  GOSUB GetCurrentTime[], lTimerStart
...
do whatever it is you want timed...
...
       ;Get endtime and calculate wait time
      GOSUB GetCurrentTime[], lTimerEnd   
     CycleTime = ((lTimerEnd-lTimerStart) * WTIMERTICSPERMSMUL) / WTIMERTICSPERMSDIV
...
   BEGINASMSUB
HANDLE_TIMERB1_ASM
ASM {
   push.l    er1                  ; first save away ER1 as we will mess with it.
   bclr    #5,@IRR2:8               ; clear the corresponding bit in the interrupt pending mask
   mov.l    @LTIMERCNT:16,er1      ; Add 256 to our counter
   add.l   #256,er1
   mov.l    er1, @LTIMERCNT:16
   pop.l    er1
   rte
}
   ENDASMSUB
;==============================================================================
;[GetCurrentTime] - Gets the Timer value from our overflow counter as well as the TCB1 counter.  It
;                makes sure of consistency. That is it is very possible that
;                after we grabbed the timers value it overflows, before we grab the other part
;                so we check to make sure it is correct and if necessary regrab things.
;==============================================================================
GetCurrentTime:
   lCurrentTime = lTimerCnt + TCB1
   ; handle wrap
   if lTimerCnt <> (lCurrentTime & 0xffffff00) then
         lCurrentTime = lTimerCnt + TCB1
   endif

   return lCurrentTime



Note: I simply extracted code fragments from an older version of phoenix code as the current code had Bap40 support removed... So I did not try to compile or the like, but this should give you an idea.

For areas that you do bit bang output, you may need/want to add so disable/enable timer around the call. Something like:
Code:
disable Timerint
serout ...
enable TimerInt


This is fine for short durations as the TimerB1 with a clock divide of 8192 will only overflow about 9.5 times per second so as long as your disable is less than 1/9.5 seconds you should not lose an interrupt.
But if resolution of clock/8192 is not sufficient for you, than you need to update the init code plus time conversion constants, which will cause more interrupts per second...

Hope that helps
Kurt


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Tue Sep 27, 2011 2:49 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
It's been done. Sorta.
A loooong time ago, before BMicro introduced the Pro40, I got the Renasa chip they used in the design eventually and had them program it with the bootloader. I kludged a proto board for the first one and wrote some code, one piece being for the built-in RTC. Since the chip has the RTC registers, keeping time became a snap. The long winding road to this is in this post:
http://forums.basicmicro.net/post39690.html#p39690
The actual code is at the end of the second page. Your mileage may vary by now due to possible changes in Studio. Take it all with a grain of salt...
Have fun.

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Real time clk (RTC)
PostPosted: Tue Oct 04, 2011 4:53 am 
Offline
Citizen

Joined: Wed Jun 29, 2011 2:29 am
Posts: 21
Thank you so much to both of you kurt and kenji. Your information is very useful and it helped a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO