User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Tue Oct 30, 2012 4:04 pm

wallarug wrote:

Code: Select all

timer0 = 46 875 Hz  //Fast PWM  && Prescaler of "1"
timer1 = 23 437.5 Hz  //Phase corrected PWM && Prescaler of "1"
timer2 = 23 437.5 Hz  //Phase corrected PWM && Prescaler of "1"
For what it's worth, those numbers look good to me. Don't forget to set the bits to enable Fast PWM in TCCR0A for Timer 0, as per instructions on the page you linked to.

Code: Select all

TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
Again, just assuming things, as I don't have an oscilloscope to check if that's correct (It's in the top 3 on my wish-list though).

At first, I thought that 'Fast PWM' was twice the speed of 'Phase Correct' Mode, but it seems 'Phase Correct' is half the speed of 'Fast PWM'. Both are still true of course (duh!), but the maximum frequency depends on what you consider the 'default' (don't know how to say this correctly in English). That seems to be 'Fast PWM', or max 46.875 KHz (@12MHz F_CPU) for Timer/Counter 0, as you say.
wallarug wrote:Also, I think that Fast PWM can only be achieved on timer0...I am right?
Both Timer/Counter 1 (16 bit) and Timer/Counter 2 (8 bit) can be set to Fast PWM Mode. See page 124 and page 148 of the 'complete' ATmega328P datasheet. I won't comment on what bits to set to enable that, I'll be poking in the dark without being able to check.

[edit to add quotes, we're on page 2]

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Tue Oct 30, 2012 10:45 pm

wallarug wrote:So the only frequencies achievable are with the set pre scaler values.

Code: Select all

62500 Hz
31 250 Hz
....
If someone knows different, please let me know.
Anything inbetween is possible.

There's a good article on AVR timers here: http://www.engblaze.com/microcontroller ... nterrupts/
http://www.engblaze.com/ wrote:It turns out there’s another mode of operation for AVR timers. This mode is called Clear Timer on Compare Match, or CTC. Instead of counting until an overflow occurs, the timer compares its count to a value that was previously stored in a register. When the count matches that value, the timer can either set a flag or trigger an interrupt, just like the overflow case.

JohnCrow
Posts: 25
Joined: Fri May 25, 2012 5:28 pm
Location: Lincolnshire

Re: Question about Atmel chip

Wed Nov 07, 2012 7:36 pm

Hi
I have noticed the 6pin header on the Gertboard for programming the AVR has the same pinouts as the AVR ISP mk II In circuit programmer.
Will this work with the Gert Board, because this means I can program the AVR with flowcode (a language I know) rather than having to learn the Arduino system.
As far as I can tell the ISP takes its power from the target board.

http://uk.farnell.com/jsp/displayProduc ... ctlistings

Thanks
1 in 10 people understand binary, the other one doesn't

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Question about Atmel chip

Wed Nov 07, 2012 7:50 pm

JohnCrow wrote:Hi
I have noticed the 6pin header on the Gertboard for programming the AVR has the same pinouts as the AVR ISP mk II In circuit programmer.
Will this work with the Gert Board, because this means I can program the AVR with flowcode (a language I know) rather than having to learn the Arduino system.
As far as I can tell the ISP takes its power from the target board.

http://uk.farnell.com/jsp/displayProduc ... ctlistings

Thanks
Yes, it's a standard ICSP programming header and you can program the ATmega externally - or directly from the Pi using it.

-Gordon
--
Gordons projects: https://projects.drogon.net/

JohnCrow
Posts: 25
Joined: Fri May 25, 2012 5:28 pm
Location: Lincolnshire

Re: Question about Atmel chip

Wed Nov 07, 2012 7:58 pm

Thanks Gordon
1 in 10 people understand binary, the other one doesn't

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Thu Nov 08, 2012 4:34 am

I was wondering if someone could post an example sketch to set the timers on the Gertboard to their default values or maximum values.
It would be great if this could happen soon as it would help a lot of Gertboard owners now and in the future.

I will experiment with arduino clocks and timers later. Thanks for the reply @panki

EDIT:
One more question, Can the Gertboard's ATMEGA chip be powered by 5 volts without exploding or being damaged?

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Thu Nov 08, 2012 12:24 pm

The ATmega328 can handle 5.5V (@20MHz), however on the Gertboard it's powered by 3.3V (@12MHz). Connecting 5V to its VCC pin will certainly damage your Raspberry Pi*. You don't want to try that.

If by 'sketch' you mean 'Arduino sketch', I can't help with that. I don't use the Arduino IDE myself, but anything Arduino related is really well documented on the interweb.

Just curious, what is it you want to do exactly, and why is achieving the highest frequencies so important to you? As Gert already said in this topic, the motor driver on the Gertboard is perfectly happy with 30KHz. I'm sure even lower frequencies will work as well.

[*] edit: On second thought, the ATmega328 is probably powered by the onboard 3.3V regulator. So maybe it doesn't damage the Raspberry Pi, but you still don't want to try. It'll kind of defeat the purpose of the Gertboard, with being able to directly connect ATmega pins to Raspberry Pi GPIO pins.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2866
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Question about Atmel chip

Thu Nov 08, 2012 1:08 pm

panik wrote: Just curious, what is it you want to do exactly, and why is achieving the highest frequencies so important to you? As Gert already said in this topic, the motor driver on the Gertboard is perfectly happy with 30KHz. I'm sure even lower frequencies will work as well.
They do. The software PWM in the RPi.GPIO Python version of the motor program defaults to 2kHz with a max of 3kHz and it works tolerably well.
Alex Eames RasPi.TV, RasP.iO

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Thu Nov 08, 2012 11:41 pm

I thought the higher the frequency would be better for the motors. I could be wrong.

To be more precise:
I would like a basic set-up script to set the timers on the Gertboard ATMEGA chip.

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Fri Nov 09, 2012 1:52 pm

wallarug wrote:I would like a basic set-up script to set the timers on the Gertboard ATMEGA chip.
I'm not sure I understand the question. You want to set the timers to do what? They're pretty versatile little buggers.

Gordon did a great job making sure the Arduino IDE plays nicely with the 12MHz crystal*. That is, I didn't hear anyone complaining. Didn't try it out myself. The analogWrite(pin, value) function should 'just work'. It only sets the duty cycle (the OCRxx bits) of the corresponding timer, when set up as PWM. It shouldn't be affected by the frequency.

If you want higher frequencies (on Timer0), add:

Code: Select all

TCCR0B = TCCR0B & 0b11111000 | 0x01;
to your setup() function. That sets the divisor to 1 (from 64, the default, or 0x03). In this case, just remember to multiply the amount of milliseconds by 64 if you're using millis() or delay(). This is straight from the page you linked to.

Or, change TCCR1B (timer1) or TCCR2B (timer2) as per instructions to not affect millis() and delay(). Messing with Timer1 affects the Arduino Servo library, and messing with Timer2 affects the Arduino Tone library. This is what I learned from: http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM (that's the first link on the page you linked to)

If you want to use the timers for other things (like triggering an interrupt every x amount of time) read the engblaze.com page I linked to earlier.

[*] Gordon really did the world a favor by patching avrdude to make sure we can program the ATmega328 on the Gertboard directly from the Raspberry Pi GPIO pins. That's off topic, but it can't be said often enough. :D

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2866
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Question about Atmel chip

Fri Nov 09, 2012 2:06 pm

panik wrote:Gordon did a great job making sure the Arduino IDE plays nicely with the 12MHz crystal*. That is, I didn't hear anyone complaining. Didn't try it out myself.
I have. It's great :)
panik wrote:The analogWrite(pin, value) function should 'just work'.
It does. I've had it doing variable brightness on leds.
Alex Eames RasPi.TV, RasP.iO

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Sat Nov 17, 2012 7:26 am

alexeames wrote:
panik wrote:Gordon did a great job making sure the Arduino IDE plays nicely with the 12MHz crystal*. That is, I didn't hear anyone complaining. Didn't try it out myself.
I have. It's great :)
panik wrote:The analogWrite(pin, value) function should 'just work'.
It does. I've had it doing variable brightness on leds.
So I don't need to set the 'default' timer values?

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Sat Nov 17, 2012 12:42 pm

wallarug wrote:So I don't need to set the 'default' timer values?
When using the Arduino environment: correct. You don't need to set the 'default' timer values. Setting the frequency is abstracted away from the user. You don't have to think about it, it's automatically done for you. Just supply an 8 bit value for the duty cycle on a PWM pin with analogWrite(pin, value). When the value is 0, the motor/led is off, and when the value is 255, the motor/led is fully on.

Your original question in this topic was how to change the frequency. There's no standard Arduino function to change the frequency, because it would mess up the delay() and millis() function, or the Servo library or Tone library, depending on which of the 3 timers' frequency you're changing. The authors of the Arduino library don't want you to change the frequency, because it's hairy. Earlier in this topic, we discussed what to do if you DO want to do just that: AND use the Arduino library, AND change the frequency.

Without using the Arduino library, the timers are off by default (prescaler=0). You'll have to turn them on anyway, so you can (must) choose the frequency you want.

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Sat Nov 17, 2012 9:55 pm

wallarug wrote:Would the following be the ONLY code I need to put in the setup():

Code: Select all

TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = TCCR0B & 0b11111000 | 0x02 ;  //timer0 to 5.7Khz
to set the frequencies I need with no modification to any files?

@panik, can you please confirm/add to/ dismiss whether this would get me up and running on the Gertboard ATMEGA chip.

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Sat Nov 17, 2012 11:01 pm

wallarug wrote:
wallarug wrote:Would the following be the ONLY code I need to put in the setup():

Code: Select all

TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = TCCR0B & 0b11111000 | 0x02 ;  //timer0 to 5.7Khz
to set the frequencies I need with no modification to any files?
@panik, can you please confirm/add to/ dismiss whether this would get me up and running on the Gertboard ATMEGA chip.
The first line of code seems to set up Timer 0 as Fast PWM (mode 3).
The second line sets the prescaler to 8. That's 12M / 8 / 256 = 5.859375KHz.

If that's what you want, then yes, that would get you up and running. Set OCR0A and/or OCR0B to some 8 bit values to change the duty cycles on pin D6 and/or pin D5 in your loop() function. Or use analogWrite(pin, value) where 'pin' is the Arduino equivalent of pin D6 and D5. Both should do the same thing.

Edit: pin D6 and D5 should both be set to outputs. Easy to forget. I believe analogWrite() does that automatically as well. Not sure though.

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Sun Nov 18, 2012 12:21 am

panik wrote:
Edit: pin D6 and D5 should both be set to outputs. Easy to forget. I believe analogWrite() does that automatically as well. Not sure though.
Not sure but I have sent all pins to output anyway.

Thanks for all the help, I will give this a go when I get a chance on my Arduino and then port the code to the Gertboard after it has been built

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Sun Nov 18, 2012 12:19 pm

2 small things.

Yes, analogWrite(pin, value) automatically sets the pin as an output. See line 107 in wiring_analog.c: https://github.com/arduino/Arduino/blob ... g_analog.c

Also, you can shorten the code in your CMD_ROBOT.py with:

Code: Select all

def invert_mota():
    motor_a = 255 - motor_a

def invert_motb():
    motor_b = 255 - motor_b
I hope that makes sense.

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Sun Nov 18, 2012 8:26 pm

panik wrote:2 small things.

Yes, analogWrite(pin, value) automatically sets the pin as an output. See line 107 in wiring_analog.c: https://github.com/arduino/Arduino/blob ... g_analog.c

Also, you can shorten the code in your CMD_ROBOT.py with:

Code: Select all

def invert_mota():
    motor_a = 255 - motor_a

def invert_motb():
    motor_b = 255 - motor_b

I hope that makes sense.
That makes sense.

the...

Code: Select all

>>> from CMDmodule import *
....command seems to work but nothing happens when I try and put stuff through the def statement. any ideas why?
http://www.element14.com/community/thre ... 1?tstart=0

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Wed Nov 28, 2012 9:01 am

How can I write to the ATMEGA chip using serial? What is it called? Do I need wiringpi or something?

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Question about Atmel chip

Thu Nov 29, 2012 11:59 am

wallarug wrote:How can I write to the ATMEGA chip using serial? What is it called? Do I need wiringpi or something?
You can read and write data through the serial port to the ATmega as if it were an Arduino. That could be with the Arduino IDE, minicom, screen, wiringPi, or any custom application written in any language that has a serial library, or code you've written yourself. It should all 'just work'.

User avatar
Gert van Loo
Posts: 2484
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Question about Atmel chip

Thu Nov 29, 2012 2:12 pm

wallarug wrote:How can I write to the ATMEGA chip using serial? What is it called? Do I need wiringpi or something?
Just a word of warning: If you use the 328 on the Gertboard you can connect the serial TX pin to the Raspberry-Pi serial RX pin.
In fact the Gertboards has pins where you just put a jumper on it to accomplish this.

If you use an external Arduino boiard you can NOT do that as it will output 5V which your Pi does not like.
In that case use a resistor divider between the two to get your 5V down to 3V3.

You can safely connect the Pi TX to the 328/Arduino RX.

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Fri Nov 30, 2012 1:13 am

Gert van Loo wrote:
wallarug wrote:How can I write to the ATMEGA chip using serial? What is it called? Do I need wiringpi or something?
Just a word of warning: If you use the 328 on the Gertboard you can connect the serial TX pin to the Raspberry-Pi serial RX pin.
In fact the Gertboards has pins where you just put a jumper on it to accomplish this.

If you use an external Arduino boiard you can NOT do that as it will output 5V which your Pi does not like.
In that case use a resistor divider between the two to get your 5V down to 3V3.

You can safely connect the Pi TX to the 328/Arduino RX.
What does the device recognise as? '/dev/??????'

Also: I can't jumper it on the gertboard...The pins are too far apart. (Builder's fault :D)

Wendo
Posts: 142
Joined: Sun Jun 10, 2012 8:27 pm

Re: Question about Atmel chip

Fri Nov 30, 2012 2:14 am

The serial port on the Pi is /dev/ttyAMA0

You'll need to disable the console output on it before you try to use it

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Question about Atmel chip

Fri Nov 30, 2012 2:18 am

Wendo wrote:The serial port on the Pi is /dev/ttyAMA0

You'll need to disable the console output on it before you try to use it
That is what my Arduino registers as...

What about the GPIO serial then?

Wendo
Posts: 142
Joined: Sun Jun 10, 2012 8:27 pm

Re: Question about Atmel chip

Fri Nov 30, 2012 2:29 am

Sorry, I don't understand. The Pi has 1 serial port in the GPIO header. What you connect to it is up to you

Return to “HATs and other add-ons”