: ПОДЕРГАТЬ 1 PORTA ;
roboforum.ruТехнический форум по робототехнике. |
|
|
> .res
free FLASH cells 7240
free RAM cells 1734
used EEPROM cells 64
used data stack cells 0
used return stack 10
free return stack 70
ok
> amforth 4.0 ATmega32
>
\ #include bitnames.frt
PORTC 4 portpin: green
PORTC 3 portpin: red
PORTC 2 portpin: blue
PORTD 7 portpin: white \ OC2 выход компаратора таймера2
: inileds
green pin_output
red pin_output
blue pin_output
white pin_output
;
: blink ( n n -- )
0 ?do
blue over pulse dup ms
red over pulse dup ms
loop drop
;
\ стописят раз моргнуть синий-красный, импульсы и паузы 75 миллисекунд
\ 75 150 blink
: inilight
[ 1 6 lshift \ wgm00
1 5 lshift or ] \ com01
TCCR2 c!
;
hex
\ выключить свет ( остановить таймер CS2[2..0] = 000 )
: -light TCCR2 dup c@ F8 and swap c! white low ;
\ включить
: +light TCCR2 dup c@ 1 or swap c! ; \ ( CS2[2..0] = 001 )
\ яркость
: bright! ( n -- ) FF and OCR2 c! ;
\ motor1:
\ разрешение/скорость(ШИМ)
PORTD 5 portpin: en1 en1 pin_output
\ направление:
PORTA 6 portpin: m10 m10 pin_output
PORTA 7 portpin: m11 m11 pin_output
\ motor2:
\ разрешение/скорость(ШИМ)
PORTD 4 portpin: en2 en2 pin_output
\ направление:
PORTA 4 portpin: m20 m20 pin_output
PORTA 5 portpin: m21 m21 pin_output
marker stepper
base @ hex
PORTA F bitmask: stepper1 stepper1 pin_output
create phase
5 , \ 0101
1 , \ 0001
9 , \ 1001
8 , \ 1000
A , \ 1010
2 , \ 0010
6 , \ 0110
4 , \ 0100
base !
\ это константы, которые прибавляются к индексу( смещение в таблице phase)
1 constant half-step
2 constant wave
2 constant normal
variable sequence
variable idx \ текущий индекс в таблице
variable direction
variable dly \ пауза между последовательными шагами
ATmega32 memory use summary [bytes]:
Segment Begin End Code Data Used Size Use%
---------------------------------------------------------------
[.cseg] 0x000000 0x0077b0 2152 9510 11662 32768 35.6%
[.dseg] 0x000060 0x000060 0 0 0 2048 0.0%
[.eseg] 0x000000 0x000040 0 64 64 1024 6.3%
Assembly complete, 0 errors, 0 warnings
amforth 4.2 ATmega32
> .res
amforth 4.2 ATmega32 running at 8000 kHz
free FLASH cells 6211
free RAM bytes 1744
used EEPROM bytes 68
used data stack cells 0
used return stack cells 5
free return stack cells 35
ok
>
\
\ stepper.frt
\ chu 16:09 29.09.2010
\
\ ==============================================================
\
\ Stepper motor
\
\ The half-step sequence
\ 0 1 0 1 \ 0x05
\ 0 0 0 1 \ 0x01
\ 1 0 0 1 \ 0x09
\ 1 0 0 0 \ 0x08
\ 1 0 1 0 \ 0x0A
\ 0 0 1 0 \ 0x02
\ 0 1 1 0 \ 0x06
\ 0 1 0 0 \ 0x04
\
\ ==============================================================
\
marker -stepper
base @ hex
create phase
55 ,
11 ,
99 ,
88 ,
AA ,
22 ,
66 ,
44 ,
base !
1 constant half-step
2 constant wave
2 constant normal
\
\ ==============================================================
\
: -- create over , + does> i@ + ;
0
1 -- idx \ displacement in phase table
1 -- sequence \ value to change idx
1 -- shft \ reserved***
1 -- ststate \ reserved***
1 cells -- dly \ before next step
1 cells -- direction \ boolean value
1 cells -- steps \ to rotate
1 cells -- total \ steps, signed
1 cells -- res1 \ reserved***
1 cells -- motor \ pinmask:portadr
constant /stepper
\
\ ==============================================================
\ example:create new stepper motor structure with name 'altitude'
\ PORTA F stepper: altitude
: stepper: ( portaddr pinmask -- )
here /stepper allot dup constant
motor swap over c! 1+ c!
;
: motor@ ( stm_addr -- pinmask portaddr )
motor dup c@ swap 1+ c@
;
\
\ example:
\ 2 half-step altitude init_stm!
: init_stm! ( dly sequence stm_addr -- )
swap over sequence c!
swap over dly !
0 over idx c!
0 over direction !
0 over steps !
0 over total !
motor@ pin_output
;
: idx+ ( stm_addr -- )
dup idx c@
over sequence c@ + 7 and
swap idx c!
;
: idx- ( stm_addr -- )
dup idx c@
over sequence c@ - 7 and
swap idx c!
;
: step ( stm_addr -- )
dup direction @
if dup idx- dup total @ over sequence c@ - over total !
else dup idx+ dup total @ over sequence c@ + over total !
then phase over idx c@ + i@ swap motor@ pin!
;
: rotate ( stm_addr n -- )
0 ?do dup step dup dly @ ms loop drop
;
: reverse ( stm_addr -- )
direction dup @ invert swap !
;
: half! ( stm_addr -- )
sequence half-step swap !
;
: normal! ( stm_addr -- )
dup half! dup idx c@ 1 and
if dup step then sequence normal swap !
;
: wave! ( stm_addr -- )
dup half! dup idx c@ 1 and 0=
if dup step then sequence wave swap !
;
\ снять напряжение с обмоток
: stopoff ( stm_addr -- )
0 swap motor@ pin!
;
\ провернуть двигатель в "нулевую" позицию
: park ( stm_addr -- )
dup total @ dup 0< if
over direction 0 swap ! else
over direction -1 swap ! then
abs rotate
;
\
: run ( stm_addr -- )
dup steps @ 0= if drop exit then
dup steps @ 0< if
dup sequence @ over steps +!
dup direction -1 swap ! else
dup sequence @ negate over steps +!
dup direction 0 swap ! then
dup step dly @ ms
;
\
\ ==============================================================
\
: stm? ( stm_addr -- )
cr
base @ >r
decimal
." idx.............." dup idx c@ u. cr
." sequence........." dup sequence c@ u. cr
." delay............" dup dly @ u. cr
." direction........" dup direction @ . cr
." steps to rotate.." dup steps @ . cr
." total steps......" dup total @ . cr
hex
." port............." motor@ u. cr
." mask............." u. cr
r> base !
;
\
\ ==============================================================
\
amforth 4.2 ATmega32
> .res
amforth 4.2 ATmega32 running at 8000 kHz
free FLASH cells 5485
free RAM bytes 1712
used EEPROM bytes 68
used data stack cells 0
used return stack cells 5
free return stack cells 35
ok
> hex
ok
> PORTA F stepper: stm1
ok
> PORTA F0 stepper: stm2
ok
> 2 half-step stm1 init_stm!
ok
> 5 half-step stm2 init_stm!
ok
> stm1 stm? stm2 stm?
idx..............0
sequence.........1
delay............2
direction........0
steps to rotate..0
total steps......0
port.............3B
mask.............F
idx..............0
sequence.........1
delay............5
direction........0
steps to rotate..0
total steps......0
port.............3B
mask.............F0
ok
> stm2 100 rotate stm1 55 rotate
ok
> stm1 stm? stm2 stm?
idx..............5
sequence.........1
delay............2
direction........0
steps to rotate..0
total steps......85
port.............3B
mask.............F
idx..............0
sequence.........1
delay............5
direction........0
steps to rotate..0
total steps......256
port.............3B
mask.............F0
ok
> decimal
ok
> stm2 reverse stm2 200 rotate
ok
> stm2 100 rotate
ok
> stm2 reverse stm2 80 rotate
ok
> stm1 reverse stm1 150 rotate
ok
> stm1 stm? stm2 stm?
idx..............7
sequence.........1
delay............2
direction........-1
steps to rotate..0
total steps......-65
port.............3B
mask.............F
idx..............4
sequence.........1
delay............5
direction........0
steps to rotate..0
total steps......36
port.............3B
mask.............F0
ok
> stm1 park
ok
> stm2 park
ok
> stm1 stm? stm2 stm?
idx..............0
sequence.........1
delay............2
direction........0
steps to rotate..0
total steps......0
port.............3B
mask.............F
idx..............0
sequence.........1
delay............5
direction........-1
steps to rotate..0
total steps......0
port.............3B
mask.............F0
ok
> stm1 stopoff stm2 stopoff
ok
>
amforth 4.2 ATmega32
> decimal
ok
> : t2> 0 TCCR2 c! ; \ остановить таймер
ok
> : <t2 2 TCCR2 c! ; \ запустить в вышеобозначенном режиме
ok
> \ собственно сам тест
ok
> : <t2> t2> 0 TCNT2 c! <t2 t2> cr TCNT2 c@ dup .x bl emit . ;
ok
> <t2>
16 22 ok
> <t2>
15 21 ok
> <t2>
15 21 ok
>
> only forth also assembler
ok
> hex 25 constant tccr2
ok
> code t2> tccr2 R2 out, end-code
ok
> code <t2 R17 2 ldi, tccr2 R17 out, end-code
ok
> : <t2> t2> 0 TCNT2 c! <t2 t2> cr TCNT2 c@ dup .x bl emit . ;
ok
> only forth
ok
> decimal
ok
> <t2>
3 3 ok
> <t2>
3 3 ok
> <t2>
3 3 ok
>
> : <<t2>> t2> 0 TCNT2 c! <t2 noop t2> cr TCNT2 c@ dup .x bl enmit . ;
ok
> <<t2>>
7 7 ok
>
Eruman писал(а):Вымораживает только постфиксная запись. Мозги ломаются. Хотя, это дело привычки, ИМХО
Eruman писал(а):Возможна ли реализация прямой записи, а-ля бейсик?
Eruman писал(а): Чу, а можно прикрутить Форт к Меге8?
Вернуться в МиниБот — национальный класс роботов
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 3