From: Robert E. <epp...@so...> - 2012-01-15 13:28:38
|
Does amforth set up a timer to measure time or do I have to do that by hand? I need a word similar to arduino micros() to keep track of time. Has somebody already written that? btw: What hw initialization (like timers, setting up i/o pins and the like) does amforth do, if any? Robert |
From: Matthias T. <mt...@we...> - 2012-01-15 14:46:34
|
Hi Robert, > Does amforth set up a timer to measure time or do I have to do that by > hand? I need a word similar to arduino micros() to keep track of time. > Has somebody already written that? The directory examples may be a starting point. More can be found in the subversion tree /applications (not included into the release files however). http://amforth.svn.sourceforge.net/viewvc/amforth/ > btw: What hw initialization (like timers, setting up i/o pins and the > like) does amforth do, if any? amforth initializes only what it needs for itself: the usart command prompt. Everything else comes from external sources. Matthias |
From: pito <pi...@vo...> - 2012-01-15 17:03:28
|
Robert, long time back I used these words for time measurements: \ 2007-12-26 EW w4_clock_tick.fs \ 2010-09-02 PITO - FLOAT TIME MEASUREMENT ATMEGA 32 \ +timer register and enable interupt \ -timer disable interupt \ this is for mega32 marker -mytimer \ TIMER_COUNTER_2 $42 constant ASSR \ Asynchronous Status Register $45 constant TCCR2 \ Timer/Counter2 Control Register $00A constant TIMER2_OVFAddr \ Timer/Counter2 Overflow $59 constant TIMSK \ Timer/Counter Interrupt Mask Register 2variable timer \ overflow2 interupt service routine \ increment tick : tick_isr 1. timer 2@ d+ timer 2! ; \ enable ticks \ overflow: f_cpu/256/64 : +timer \ $7 TCCR2 c! ( 111b = f_cpu clock/1024 ) $4 TCCR2 c! ( 100b = f_cpu clock/64 ) $0 ASSR c! ( source: internal clock f_cpu) ['] tick_isr TIMER2_OVFAddr int! ( register interrupt ) 0. timer 2! TIMSK c@ $40 or TIMSK c! ( enable timer2 interrupt ) ; \ disable ticks : -timer TIMSK c@ [ $40 invert $ff and ] literal and TIMSK c! ( clr timer2 interrupt ) ; \ MEASURE TIME IN SECONDS (FLOAT) decimal 2variable elapsed_ticks \ tck_ms = 1 / f_cpu / 256 / 64 f_cpu d>f 256 s>f f/ 64 s>f f/ 1 s>f fswap f/ fconstant tck_ms : timer-start ( -- ) timer 2@ elapsed_ticks 2! ; : timer-stop ( -- f ) timer 2@ d>f elapsed_ticks 2@ d>f f- tck_ms f* ; +timer Regards, Pito ----- PŮVODNÍ ZPRÁVA ----- Od: "Robert Epprecht" <epp...@so...> Komu: amf...@li... Předmět: [Amforth] measuring time Datum: 15.1.2012 - 14:28:01 > Does amforth set up a timer to measure time or do > I have to do that by > hand? I need a word similar to arduino micros() to > keep track of time. > Has somebody already written that? > > btw: What hw initialization (like timers, setting > up i/o pins and the > like) does amforth do, if any? > > Robert > > > ------------------------------------------------------------------------------ > > RSA(R) Conference 2012 > Mar 27 - Feb 2 > Save $400 by Jan. 27 > Register now! > http://p.sf.net/sfu/rsa-sfdev2dev2 > _______________________________________________ > Amforth-devel mailing list for > http://amforth.sf.net/ > Amf...@li... > https://lists.sourceforge.net/lists/listinfo/amforth-devel > -- - - Reklama - - - - - - - - - - - - - - Lyže, snowboardy, monoski, sáňky, brusle, sněžnice a další kluzadla oprášit, zimní radovánky jsou tu! Speciál Zima portálu VOLNÝ.cz najdete na http://bit.ly/ymcefA |
From: Marcin C. <sa...@sa...> - 2012-01-15 18:52:12
|
>> Robert Epprecht <epp...@so...> wrote: > Does amforth set up a timer to measure time or do I have to do that by > hand? I need a word similar to arduino micros() to keep track of time. > Has somebody already written that? 1ms ( is wait one microsecond ) n ms ( wait n microseconds) ) > btw: What hw initialization (like timers, setting up i/o pins and the > like) does amforth do, if any? Not much, it sets up interrupt handlers and uses serial port in a pretty simple way to communicate with the user. //Marcin |
From: Robert E. <epp...@so...> - 2012-01-16 05:54:03
|
Marcin Cieslak <sa...@sa...> writes: >>> Robert Epprecht <epp...@so...> wrote: >> I need a word similar to arduino micros() to keep track of time. > 1ms ( is wait one microsecond ) > n ms ( wait n microseconds) ) yes, but these are for waiting, not measuring time. Robert |
From: Robert E. <epp...@so...> - 2012-01-16 05:50:25
|
"pito" <pi...@vo...> writes: > long time back I used these words for time measurements: [...] Thanks, looks easier then i thought it would be. Will try to do something similar. Is there a special reason you selected timer 2? I was thinking about using timer 0. Robert |
From: Erich W. <ew....@na...> - 2012-01-17 21:05:24
|
Hello Robert, On 01/16/2012 06:49 AM, Robert Epprecht wrote: > "pito"<pi...@vo...> writes: > >> long time back I used these words for time measurements: > [...] > > Thanks, looks easier then i thought it would be. > Will try to do something similar. > > Is there a special reason you selected timer 2? > I was thinking about using timer 0. "Measuring time" is more diverse than it seems. * Do you want to know, how long something takes? Start and Stop condition are in the programm. Then use any timer, feed it any availabe clock_source/prescaler combination. Clear the timer register, start the timer, stop and read the timer ... * Do you want to measure the length of an external event? An external pulse length maybe? Then you can use a timer providing the Input-Capture feature. The rising edge of the external pulse triggers the start of the timer, the falling edge triggers the timer to stop. You need to wait or poll for the event to finish, but then you just read the timer register ... * Do you want to keep track of time? Like in a clock? And maybe periodically run some functions? Then you could use any timer as well. But you could also use timer2, which dann be connected to a clock crystal running at 32768 Hz. If the prescaler is off, this gives 128 timer overflows per second. I call them "ticks". The mainloop runs a routine, which will see that a tick has elapsed. This routine does all the bookkeeping of ticks, seconds, minutes, hours, day, month, year. If a second (or any defined intervall) is over, it will set a flag. The rest of the main loop checks these flags and if set, calls the work to be done. These are called job.tick, job.sec ... in my application. If tracking time like this is not accurate enough, add a phase shift register to the mix. It will basically say: after so many ticks, make some correction. * Do you want to measure a frequency of an external pulse signal? a. count N of them, measure the elapsed time t: f=N/t b. measure the time between consequtive rising edges: f=1/t c. measure some given time intervall and count the pulses: f=N/T * Do you want to produce a pulse train with specific timings? PWM might do the job. Waiting and bit banging might do the job. Extra periphery might do the job, e.g. a 1wire controller connected to twi. It all depends on the conditions. How long is the time, you want to measure? How accurate should it be? I can provide code for some of these, if you are interested. Cheers, Erich |
From: Christian K. <ck...@pe...> - 2012-01-17 21:54:30
|
* Erich Waelde <ew....@na...> [120117 22:05]: > * Do you want to produce a pulse train with specific timings? > > PWM might do the job. Waiting and bit banging might do the job. > Extra periphery might do the job, e.g. a 1wire controller connected > to twi. > > > It all depends on the conditions. How long is the time, you > want to measure? How accurate should it be? > > > I can provide code for some of these, if you are interested. I am interested in an example for bit banging, communicating with a serial device on a digital port. Do you have an example for such code? Kind regards, Christian -- Who can (make) the muddy water (clear)? Let it be still, and it will gradually become clear. Who can secure the condition of rest? Let movement go on, and the condition of rest will gradually arise. -- Lao Tse. |
From: Erich W. <ew....@na...> - 2012-01-18 15:44:34
|
Hello Christian, On 01/17/2012 10:15 PM, Christian Kellermann wrote: > > I am interested in an example for bit banging, communicating with > a serial device on a digital port. Do you have an example for such > code? yes and no. please find below the code I used to demonstrate a connection to a i2c (twi) device. The device was connected to PortB 3 and 4. This is a *very* simplistic approach, but it does work nicely for i2c devices. No error checking, whatsoever. You can see in the code, a few low level words are needed (sda0, sda1, scl0, scl1, wait) from which the pulse trains for one byte data exchange are constructed (bit>i2c, byte>i2c). Add generating start and stop conditions, and you are pretty much done. This does not implement clock slowing by the addressed device or multi master collision detection. But it's good for some scenarios. That is an example of bit-banging. --- No, I have not tried to build a soft uart or 1-wire in software. How would I go about it? Well, say I want a connection with 9600 baud. Then I need a feeling, of how many forth commands can I run within one Bit transfer time: 1/9600 * 1105920 (crystal) gives 1152 AVR cycles per bit. The idle command loop needs approx. 40 cycles for one round, so we are near 29 rounds per bit. This does not sound too bad. A few commands (assert the current bit, get the next bit, then wait some) seems possible to fit in. *However*, I would try to measure the resulting pulse times and fine tune the words sending one bit with with additional noop commands. IF that does not work, or if busy waiting is not an option, then the next idea would be to use a timer and put the handling of one Bit into the timer overflow isr. I have done this with receiving single bits from a rfm12 434MHz receiver and rotating them into a temporary variable. If you seach the web, I'm sure, something more sophisticated will come up. If you give it a try, be sure to share the code on the list. Cheers, Erich ------------------------------------------------------------------ \ 2011-01-23 EW fosdem i2c bitbang demo \ purely didactic thing. \ for production code use lib/twi.frt marker --start-- decimal PORTB 0 portpin: blue \ sda PORTB 1 portpin: green PORTB 2 portpin: red \ scl PORTB 3 portpin: sda PORTB 4 portpin: scl : sda0 sda low blue high ; : sda1 sda high blue low ; : scl0 scl low red high ; : scl1 scl high red low ; : wait-long &500 0 do 1ms loop ; : wait-short &50 0 do 1ms loop ; \ "function pointer" wait Rdefer wait : slow ['] wait-long is wait ; : fast ['] wait-short is wait ; slow : pulses ( n -- ) 0 ?do red low wait red high wait loop ; \ test "function pointer" : test-wait slow &5 pulses fast &25 pulses ; \ clock a given data bit out : bit>i2c ( bit -- ) if sda1 else sda0 then \ set data scl1 wait scl0 wait \ clock it out ; \ see if bit at pos is set : get.bit ( byte pos -- bit ) 1 swap lshift \ -- byte bitmask and \ -- bit ; \ clock one byte out, MSB first! : byte>i2c ( byte -- ) 8 0 do dup 7 i - \ 7 6 5 ... 0: MSB first! get.bit bit>i2c loop drop ; \ create start, stop conditions : i2c.start sda0 wait scl0 wait ; : i2c.stop scl1 wait sda1 wait ; \ read ack|nack from bus : ack<i2c ( -- t/f ) sda pin_input scl1 wait sda pin_low? sda pin_output scl0 wait ; \ make it really fast! ' noop is wait \ send a byte to pcf8574 : >8io ( x -- ) $40 \ addr i2c.start byte>i2c ack<i2c drop byte>i2c ack<i2c drop i2c.stop ; : i2c.scan $FF 0 do i2c.start i byte>i2c ack<i2c \ -- ack|nack i2c.stop if i . cr then 2 +loop ; : init blue pin_output blue high red pin_output red high sda pin_output sda high scl pin_output scl high \ alternatively \ $ff DDRB c! $ff PORTB c! ['] noop is wait ; : ms 0 ?do 1ms loop ; variable N 0 N ! : run init $00 >8io &1000 ms begin N @ invert >8io 1 N +! &1000 ms key? until key drop ; |
From: Robert E. <epp...@so...> - 2012-01-20 06:49:45
|
Exploring amforth on my arduino uno is a bit difficult without a working upload tool. I help myself with a simple bash script which just waits after characters and newlines and works often (but not always) for short single files and is s l o o o o w ... But here we go: Whats wrong with the following simple test code: \ ################################################################ marker _TIMER0_ variable ticks : tim0isr ( -- ) 1 ticks +! ; \ timer0 ISR : tim0ini ( -- ) \ initialize timer0 0 ticks ! 0 TCNT0 c! \ clear counter 0 TCCR0A c! \ normal mode 4 TCCR0B c! \ internal clock /256 \ hangs system, had to re-flash amforth \ some variants: \ 3 TCCR0B c! \ internal clock, prescaler set to 64 \ does NOT tick \ TCCR0B c@ &fff8 and 3 or TCCR0B c! \ internal clock /64 \ same \ 5 TCCR0B c! \ internal clock /1024 \ reset? ['] tim0isr TIMER0_OVFAddr int! \ ['] noop TIMER0_OVFAddr int! \ just a test ; : +tim0int ( -- ) 1 TIMSK0 c! ; \ enable interrupt : -tim0int ( -- ) 0 TIMSK0 c! ; \ disable interrupt : tim0start ( -- ) tim0ini +tim0int ; \ ################################################################ The result on my arduino uno is quite inconsistent, but mostly I see: 3 TCCR0B c! does not hang the system, but does not tick either. 4 TCCR0B c! hangs system, had to re-flash amforth often also takes my /dev/ttyACM0 down and I have to reboot linux (which looks to me like a linux kernel issue). 5 TCCR0B c! reset? The system starts to print the boot message interruptet by some bytes of garbage and endlessly repeats that. Confused, Robert |
From: Matthias T. <mt...@we...> - 2012-01-20 08:47:30
|
Hi, > But here we go: > Whats wrong with the following simple test code: looks innocent (haven't tried it however) > The result on my arduino uno is quite inconsistent, but mostly I see: > > 3 TCCR0B c! > does not hang the system, but does not tick either. > > 4 TCCR0B c! > hangs system, had to re-flash amforth > often also takes my /dev/ttyACM0 down and I have to reboot linux > (which looks to me like a linux kernel issue). > > 5 TCCR0B c! > reset? > The system starts to print the boot message interruptet by some > bytes of garbage and endlessly repeats that. Are the numbers behind the T-labels correct (memory mapped addresses, somewhere around $40ff) for the Atmega328p? I did something similiar for the atmega16 without any trouble. Otherwise it looks like you're programming the wrong controller. Loosing the ACM device is really strange, need-to-reflash by writing a single byte is very unusual as well. Matthias |
From: Robert E. <epp...@so...> - 2012-01-20 12:08:35
|
Matthias Trute <mt...@we...> writes: My chip is labelled ATMEGA-328-PU I have included atmega328p.frt Hope that's ok? > Are the numbers behind the T-labels correct (memory mapped addresses, > somewhere around $40ff) for the Atmega328p? hex TCCR0A . 44 > Otherwise it looks like you're programming > the wrong controller. ??? > Loosing the ACM device is really strange, yes > need-to-reflash by writing a single byte is > very unusual as well. well this single byte was enabling an interrupt, but strange anyway... Robert |
From: Mark M. <m.m...@gm...> - 2012-01-20 12:53:48
|
Working with the ATMega328, I found that I had to set the prescaler (TCCR0B) after I assigned the interrupt vector. So, you might try : tim0start ( -- ) tim0ini +tim0int 5 TCCR0B c! ; (remove the prescaler store from tim0ini ) It worked for me. BTW thanks to the list for help with the lfuse clock issue... obviously I got it solved! On Fri, Jan 20, 2012 at 7:07 AM, Robert Epprecht <epp...@so...> wrote: > Matthias Trute <mt...@we...> writes: > > My chip is labelled ATMEGA-328-PU > I have included atmega328p.frt > Hope that's ok? > > > Are the numbers behind the T-labels correct (memory mapped addresses, > > somewhere around $40ff) for the Atmega328p? > hex TCCR0A . > 44 > > > Otherwise it looks like you're programming > > the wrong controller. > ??? > > > Loosing the ACM device is really strange, > yes > > > need-to-reflash by writing a single byte is > > very unusual as well. > well this single byte was enabling an interrupt, > but strange anyway... > > Robert > > > ------------------------------------------------------------------------------ > Keep Your Developer Skills Current with LearnDevNow! > The most comprehensive online learning library for Microsoft developers > is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-d2d > _______________________________________________ > Amforth-devel mailing list for http://amforth.sf.net/ > Amf...@li... > https://lists.sourceforge.net/lists/listinfo/amforth-devel > |
From: pito <pi...@vo...> - 2012-01-20 15:25:22
|
..long time back.. maybe somebody finds it useful.. provided as-is.. Time measurement (with float results) 48bit timer (timer + epoch) for 1284p: \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \ 2007-12-26 EW w4_clock_tick.fs \ 2010-09-02 PITO - FLOAT TIME MEASUREMENT ATMEGA 1284P \ +timer register and enable interupt \ -timer disable interupt marker -mytimer 2variable timer variable epoch 1. 2constant _one 0. 2constant _zero 1 s>f fconstant f1.0 $B1 constant TCCR2B $B6 constant ASSR $70 constant TIMSK2 $016 constant TIMER2_OVFAddr \ overflow2 interupt service routine \ increment tick timer : tick_isr timer 2@ 2dup _one d+ timer 2! 0= if epoch @ 1+ epoch ! then ; \ enable ticks \ overflow fires each f_cpu/256/256 : +timer $6 TCCR2B c! ( prescaler 110b = f_cpu/256 ) $0 ASSR c! ( source: internal f_cpu) ['] tick_isr TIMER2_OVFAddr int! ( do register the interrupt ) 0 epoch ! _zero timer 2! ( clear the timer at start ) TIMSK2 c@ $01 or TIMSK2 c! ( do enable the timer2 interrupt ) ; \ disable ticks : -timer TIMSK2 c@ $fe and TIMSK2 c! ( disable - clr timer2 interrupt ) ; \ MEASURE TIME IN SECONDS (FLOAT) decimal 2variable 0_ticks \ tick_in_secs = 1 / f_cpu / 256 / 256 f_cpu d>f 256 s>f f/ 256 s>f f/ f1.0 fswap f/ fconstant tick_in_secs : timer-start ( -- ) timer 2@ 0_ticks 2! ; : .elapsed ( -- ) timer 2@ 0_ticks 2@ d- d>f tick_in_secs f* 3 fsn. ." secs " ; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ and "zero drift clock" - it increments in one second interval - when you know the exact crystal frequency (ie f_cpu = 12.487.388 Hz) then you get a precise second - none other tweaking required \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ marker _zerodriftclock_ \ ZERO DRIFT CLOCK \ IT USES F_CPU ONLY (XTAL FREQUENCY SHALL BE KNOWN AS PRECISE AS \ POSSIBLE) FOR PRECISE 1 SECOND INCREMENT \ IT WORKS WITH _ANY_ VALUE XTAL \ THE SECONDS ARE INCREMENTED IN 1 SEC INTERVAL \ USES TIMER2 ATMEGA32 \ Pito July2010 decimal 2variable ticker variable seconds 262144. 2constant clkpi ( clk per interrupt = 256*prescaler ) 1. ticker 2! 0 seconds ! \ overflow2 interupt service routine \ ! not optimised yet : tick_isr ticker 2@ clkpi d- ticker 2! ticker 2@ clkpi d< if f_cpu ticker 2@ d+ ticker 2! 1 seconds +! then ; hex \ enable ticks : +ticks 7 TCCR2 c! ( 111b = f_cpu clock/1024 ) 0 ASSR c! ( source: internal clock f_cpu) ['] tick_isr TIMER2_OVFAddr int! ( register interupt ) TIMSK c@ 40 or TIMSK c! ( enable timer2 interupt ) ; \ disable ticks : -ticks TIMSK c@ [ 40 invert ff and ] literal and TIMSK c! ( clr timer2 ) ; decimal : shwticker 1000 0 do cr ticker 2@ d. space seconds @ u. loop ; : shwsec seconds @ u. ; \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ p. -- - - Reklama - - - - - - - - - - - - - - Lyže, snowboardy, monoski, sáňky, brusle, sněžnice a další kluzadla oprášit, zimní radovánky jsou tu! Speciál Zima portálu VOLNÝ.cz najdete na http://bit.ly/ymcefA |
From: Erich W. <ew....@na...> - 2012-01-21 10:06:05
|
Hello Robert, On 01/20/2012 07:49 AM, Robert Epprecht wrote: > 3 TCCR0B c! > does not hang the system, but does not tick either. > > 4 TCCR0B c! > hangs system, had to re-flash amforth > often also takes my /dev/ttyACM0 down and I have to reboot linux > (which looks to me like a linux kernel issue). > > 5 TCCR0B c! > reset? > The system starts to print the boot message interruptet by some > bytes of garbage and endlessly repeats that. > > > Confused, > Robert You did check these hard coded numbers against the datasheet, did you? The atmega32 (where my code works) and the atmega328p (on the arduino board) are different in unexpected places. For example: sleep.asm The bits controlling sleep mode are in the register MCUCR on atmega32 and in the separate register SMCR on atmega328p. Cheers, Erich |
From: Robert E. <epp...@so...> - 2012-01-22 18:31:13
|
Erich Waelde <ew....@na...> writes: > On 01/20/2012 07:49 AM, Robert Epprecht wrote: >> 3 TCCR0B c! >> does not hang the system, but does not tick either. >> >> 4 TCCR0B c! >> hangs system, had to re-flash amforth >> often also takes my /dev/ttyACM0 down and I have to reboot linux >> (which looks to me like a linux kernel issue). >> >> 5 TCCR0B c! >> reset? >> The system starts to print the boot message interruptet by some >> bytes of garbage and endlessly repeats that. >> >> >> Confused, >> Robert > You did check these hard coded numbers against the > datasheet, did you? Double, and now triple checked. My code does not define any register address constants but uses atmega328p.frt. Next to check: hardware problems? Robert |
From: Robert E. <epp...@so...> - 2012-01-20 07:26:46
|
Erich Waelde <ew....@na...> writes: > "Measuring time" is more diverse than it seems. [ very good list skipped ] Yes, I know. I have experimented with many of these situations in arduino c dialect before. > [...] use any timer, feed it any availabe clock_source/prescaler > combination. Clear the timer register, start the timer, stop > and read the timer ... Thats what I try to do as a very first test. Looks like I don't get how to do it (see other post). > It all depends on the conditions. How long is the time, you > want to measure? How accurate should it be? I have different applications in mind with different requirements. I think I should be able to figure that part out once I'm over the very basic problems like uploading files and timer initialisation. > I can provide code for some of these, if you are interested. Despite the fact that I'm in a beginners exploring phase and absolutely want to reinvent the wheel myself and try to understand every single bit I am still very interested to look at your code to have some working examples to learn from. Robert |
From: Erich W. <ew....@na...> - 2012-01-21 10:19:33
|
Hello Robert, On 01/20/2012 08:25 AM, Robert Epprecht wrote: > Despite the fact that I'm in a beginners exploring phase and absolutely > want to reinvent the wheel myself and try to understand every single bit I'm all for it! > I am still very interested to look at your code to have some working > examples to learn from. Ok, I promise to release some bits onto the list. If I'm not fast enough, feel free to remind me. To all: The could be more code snippets circulating on the list ... Cheers, Erich |