Getting (Re)started with AVR - Initializing Peripherals

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Old dog learning new tricks here, and I need a pointer in the right direction...

 

Back in the Stone Age, I programmed AVRs in assembler, and a big part of the work with a new MCU was poring through the datasheet to understand all the peripherals and make sure they were initialized or disabled properly, lest the multifunction pins bite you in the rear later.

 

I'm re-entering the game and assuming that GCC is the better approach for most cases, but I'm puzzled to see almost no Google hits for initializing peripherals, best-practices for a given MCU startup, etc.

 

What's the current state-of-the-art here?  Should I be looking for a wizard in AS7 that generates this code from pull-downs?  Or is the practice still to pore through the datasheet or header file and code the init by hand, just in C?

 

FWIW, I'm targeting ATtiny25 at the moment.

 

Cheers,

Richard

 

Last Edited: Sat. Nov 4, 2017 - 06:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Last Edited: Sat. Nov 4, 2017 - 06:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, Richard -

 

There is an AVR "wizard" app that simplifies register settings, though I do not remember what the precise name is. Perhaps AVR Calc though it appears that there may be several.

 

That kind of tool works well for things like UART setup, with complex baud rate settings and such. But, timers are particularly vexing as there are so many combinations and YOU have to figure out which mode fits how your application. The wizard cannot do that. 

 

Personally, I choose to do it "manually" because I tend to turn peripherals on and off, or change their function during operation. When this is the case, you cannot just initialize and let it go. 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the correction on my 'pore spelling'. ;-)

 

http://start.atmel.com is both a very interesting and useless tool, it seems.  It appears to have loads of potential, but the choice of MCUs is far too limited.  Only one 8-pin model is even available.  A shame.

 

But yes, that's along the lines of what I was hoping to find.  Not that I mind doing the hard work, but I've come to appreciate that life is too short (and my list of projects too long) to reinvent the wheel unnecessarily.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Network wrote:
Should I be looking for a wizard in AS7 that generates this code from pull-downs?
ASF Wizard in AS7 though there's not much in ASF3 for tinyAVR.

If willing to move to tinyAVR 1-series there's a lot for that in ASF4 (Atmel START)

Network wrote:
FWIW, I'm targeting ATtiny25 at the moment.
tiny85 is popular via the Arduino IDE.

tiny25 apparently has an Arduino framework.

 


Atmel Studio

Starting a New GCC Project for AVR Device

http://www.atmel.com/webdoc/GUID-ECD8A826-B1DA-44FC-BE0B-5A53418A47BD/index.html?GUID-C32ADD88-CB66-4995-A6F7-ABB19006CBAF

(step 6 for ASF Wizard)

http://asf.atmel.com/docs/latest/search.html?device=tiny

http://start.atmel.com/#examples/attiny416

http://www.microchip.com/wwwproducts/en/attiny416

https://create.arduino.cc/projecthub/arjun/programming-attiny85-with-arduino-uno-afb829#toc-adding-attiny85-support-to-arduino-ide-1

http://platformio.org/boards (in MCU enter ATtiny25)

https://www.arduino.cc/en/Reference/HomePage

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Back in the Stone Age, I programmed AVRs in assembler

Hey! Watch it ...... wink

 

You can download a demo version of Codevision that will help you setting things up, then if you are happy you can purchase the full copy.

 

Or, if you are ultra modern (vs Stone age guy) and don't care about learning or thinking you can use the Arduino system, everything is done for you.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most of the AVR peripherals will remain nicely off if you don't do anything to them, and don't need to use them.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed, to get things started my default has been to assume the peripherals default off, though we all know about assuming. ;-)

 

CKDIV8 fuse is a great example of the tidbits I was hoping to uncover.  This is, of course, all covered in the datasheet; I was just hoping to jumpstart things - it seems that a nice setup wizard does exist, but only for newer parts.

 

Cheers,

Richard

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Likewise ICCv8 has a demo and a wizard as well!

 

jim

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Network wrote:
but I'm puzzled to see almost no Google hits for initializing peripherals
How is it possible NOT to find such things via Google? There are hundreds of thousands if not millions of pages about this?!?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A T25 only has two timers (plus watchdog), USI, ADC and AC, plus one port w/5 bits

 

void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0x00;
}

//Watchdog initialize
// prescale: 16K 
void watchdog_init(void)
{
 WDR(); //this prevents a timout on enabling
 WDTCR = 0x00; //WATCHDOG ENABLED - dont forget to issue WDRs
}


//TIMER0 initialize - prescale:8
// desired value: 1KHz
// actual value:  1.000KHz (0.0%)
void timer0_init(void)
{
 TCCR0A = 0x00; //stop
 TCNT0  = 0x83; //set count
 TCCR0A = 0x00; //start timer
 TCCR0B = 0x00; //start timer
}

//TIMER1 initialize - prescale:8
// desired value: 1KHz
// actual value:  1.000KHz (0.0%)
void timer1_init(void)
{
 TCCR1A = 0x00; //stop
 TCNT1 = 0x83
 OCR1A = 0x7D;
 OCR1B = 0x7D;
 OCR1C = 0x80;
 TCCR1A = 0x00;
}

//USI initialize
void usi_init(void)
{
 USICR= 0x10;
}

//ADC initialize
// Conversion time: 104uS
void adc_init(void)
{
 ADCSRA= 0x00; //disable adc
 ADMUX = 0x00; //select adc input 0
 ACSR  = 0x80;
 ADCSRA= 0x00;
}

Replace magic numbers with values you prefer...

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

THere may be "hundreds of thousands if not millions of pages about this" BUT you have to use sensible searches and THAT can be a problem. It is especially a problem if you are used to the old "one key word" searches. Google and the other search engines have gotten a lot better in just the last couple of years. For example, try

Mega328 UART initialization

Or

ATtiny25 timer initialization 

Those both work quite well. Or, even more "natural language":

How do you initialize ATtiny25 watchdog?

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Mon. Nov 6, 2017 - 05:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Network wrote:

Indeed, to get things started my default has been to assume the peripherals default off, though we all know about assuming. ;-)

 

Any reset and the reset at power-up will set peripherals to default, if you go re-setting all of them again you may accidentally enable some of them.