Hello Jan,
Jan Kromhout writes:
> Hi,
>
> I have this simple Arduino program thats put a PWM signal to port 9 of the
> Arduino.
> Checked with my RIGOL and the PWM is changed when I change the value of ppm.
>
> #define PWM_A 9 /* Pin-9 on Arduino Board */
>
> void setup() {
> Serial.begin(115200);
>
> int pwm = 200; /* duty 50% */
> pinMode(PWM_A, OUTPUT);
> /* PWM speed is at 20 kHz */
> /*Set the timers */
> TCCR1A = 0b10100000;
> TCCR1B = 0b00010001;
> ICR1 = 400;
>
> /* value of 400 = 100 % ppm */
>
> OCR1A = pwm;
> }
>
> void loop() {
> }
>
>
> Convert it to amForth.
>
> \ Address of Timer/Counter Arduino UNO
> \ Adresses are taken from device.py
> \ Partname ATmega328P
>
> $80 constant TCCR1A \ Timer/Counter1 Control Register A
> $81 constant TCCR1B \ Timer/Counter1 Control Register B
> $86 constant ICR1 \ Timer/Counter1 Input Capture Register
> $88 constant OCR1A \ Timer/Counter1 Output Compare Register A
> $8a constant OCR1B \ Timer/Counter1 Output Compare Register B
>
> PORTB 1 portpin: PWM_A \ alias for digital pin 9 (PB1)
>
> : PWM_init
> PWM_A pin_output \ Set pin 9 (PB1) to output
> %10100000 TCCR1A c! \ Store constant
> %00010001 TCCR1B c! \ Store constant
> &400 OCR1A c! \ Store constant
> ;
400 is larger than 255, so "c!" will not do, what you think.
I have this snippet in my code (check amforth commented projects
clockworks) for your inspiration.
\ enable ticks
\ crystal: 11059200 /sec
\ prescaler: 256
\ 43200 /sec
\ TOP+1: 1350
\ 32 /sec
: +ticks
0 ct.ticks !
0 ct.ticks.follow !
0 last.tick[4] !
0 last.tick[5] !
\ --- timer1 ! ---
[ %00000000 \ WGM1[10] CTC mode
%01000000 or \ COM1A[10] toggle OC1A on compare match
] literal TCCR1A c!
#1350 1- OCR1A ! \ TOP or compare match value rather
DDRD c@ $80 or DDRD c! \ pin OC2A output
[ %00000100 \ CS1[210] clock_ts2/256
%00001000 or \ WGM1[32] CTC mode
] literal TCCR1B c!
\ register isr
['] tick_isr TIMER1_COMPAAddr int!
TIMSK1 c@ $02 or TIMSK1 c! \ enable OCIE1A interupt
;
Cheers, Erich
>
> : PWM_set ( value -- ) \ PWM is between 0..400
> OCR1A c! \ Store into OCR1A
> ;
>
>
>> PWM_init
>> 200 PWM_set
>
>
>
> It will not work. What do I wrong.
> Thanks for any help
>
> Cheers,
>
> Jan
>
> _______________________________________________
> Amforth-devel mailing list for http://amforth.sf.net/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/amforth-devel
--
May the Forth be with you ...
_______________________________________________
Amforth-devel mailing list for http://amforth.sf.net/
[email protected]
https://lists.sourceforge.net/lists/listinfo/amforth-devel