Author Topic: Want to program Atmel AVR MCUs directly, where do I start?  (Read 11282 times)

0 Members and 1 Guest are viewing this topic.

Offline amyk

  • Super Contributor
  • ***
  • Posts: 8240
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #25 on: June 18, 2015, 05:34:16 am »
You could start with this: :)
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4196
  • Country: us
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #26 on: June 18, 2015, 06:57:36 pm »
Printed Books seem to have trouble keeping up with the rate of change that happens in the Arduino Community.
Ordinarily, I'd recommend a book like https://store-jwm4nhbo.mybigcommerce.com/an-arduino-workshop-book/ based largely on the author's contributions over at AVRFreaks, but... it seems to be based on pre-Uno Arduinos and pre-1.0 IDE versions :-(  Uno is 5y old at this point, but even the last six months have seen substantial changes, and print is slow.   I guess this is a general problem with high tech, and even more so with open source designs. :-(
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #27 on: June 20, 2015, 09:35:41 pm »
Arduino is no issue there's tons of resources online on how to do stuff.  Was talking more about if I do move on to coding them directly.  Learning all the functions, built in variables etc...  Most tutorials I found just touch on very basics and that's it. 

Though using arduino but with the programmer seems to be the best of both worlds, as I'm still putting a program on the chip directly without needing an arduino board  but with access to all the arduino code base which has more online documentation available.
 

Offline Quarlo Klobrigney

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #28 on: August 20, 2015, 11:39:30 pm »
http://www.avrfreaks.net/forums/tutorials Be warned the search box is a bit wonky.

Install Studio 4.1x not 6 and AVRGCC
You can program and simulate in Studio in either assembly or C

Also youtube is your friend
Voltage does not flow, nor does voltage go.
 

Offline bufo333

  • Newbie
  • Posts: 2
  • Country: us
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #29 on: August 21, 2015, 10:22:17 am »
I recently wanted to learn to program AVR MCU's using the data sheet directly and not use any Arduino code. I purchases the following book which has been excellent http://www.amazon.com/Make-Programming-Learning-Software-Hardware/dp/1449355781/ref=sr_1_5?ie=UTF8&qid=1440151544&sr=8-5&keywords=avr. This book takes you through the process of setting up the gcc toolchain and your c Makefile manually on Windows, Linux, and Mac. It takes your through the atmegaxx8p hardware one peripheral at a time and shows you how to manipulate registers directly.

Oh and did I mention you can use notepad/VI/Emacs/textedit/.... plain old text editors to write your c code and run make install from the command line to read your make file, compile and link the code and upload to the Arduino using an ICSP programmer

Some of the benefits to this are:
  • More efficient code (setting a pin output high takes 40 cycles in Arduino vs 1 cycle setting the register directly)
  • The ability to read and set multiple pins at once
  • More readable code once you get used to the register names / shorter code
  • The ability to configure counters/timers how you want and not how Arduino wants
  • Better ability to control the peripherals directly such as (using interrupts for ADC results, doing hardware serial/SPI/I2c (reduced code and cycles * for example: lots of Arduino code especially Adafruit modules states they don't support hardware serial yet*
  • More complete understanding of the code, and improved programming skill as everything is a matter of bit shifting or masking


And finally I will leave you with blink code for an attiny85 that uses the watchdog timer to blink an led every second and sleep %99 of the time. this chip uses microamps of power and has a battery life of years compared to most Arduino projects. And you can look up what is actually happening by reading the data sheet as all  the registers are defined there.

Also note most boot loaders such as the one used on the Adafruit Gemma/Trinket and other Arduino boot loaders do not support the use of this watchdog timer.

Code: [Select]
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

volatile int f_wdt=1;

ISR(WDT_vect)
{
if (f_wdt == 0)
    {
f_wdt=1;
    }
}


void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_disable();
power_all_enable();
}


int main(void)
{
//Disable interrupts
cli();

// Configure ports 0 & 1 for output
DDRB = ((1<< DDB1) | (1<< DDB0));

// Configure initial state of pin 1 to HIGH
PORTB= 1 << DDB1;

// Set Watchdog Reset flag to 0
MCUSR &= ~(1<< WDRF);
WDTCR = 0x00;

// set watchdog enable and change enable bits to HIGH
WDTCR |= (1<< WDCE) | (1<< WDE);

// Enable Watchdog timer interrupt
WDTCR |= 1<<  WDIE;

// set watchdog prescaler values to 1.0 seconds
WDTCR |= (1<< WDP2) | (1<< WDP1);

// Enable Interrupts
sei();

for(;;) {
  // put your main code here, to run repeatedly:
if (f_wdt==1)
{
PORTB ^= ( (1 << PORTB1) | (1 << PORTB0));
f_wdt = 0;
enterSleep();
 }
}
return 0;
}


« Last Edit: August 21, 2015, 10:24:58 am by bufo333 »
 

Online bingo600

  • Super Contributor
  • ***
  • Posts: 1976
  • Country: dk
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #30 on: August 21, 2015, 01:25:36 pm »
For avrdude and fuses , i usually use this site

http://www.engbedded.com/fusecalc

It also gives the avrdude "hex fuse values"


/Bingo
 

Offline Quarlo Klobrigney

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #31 on: August 22, 2015, 01:33:09 pm »
Programmers Notepad is a good editor to use. It recognizes the file format and is also able to color the text accordingly. If printing it will output formatted color text. Try that in notepad. If coupled with tools it can make as well. Google is your friend.
Voltage does not flow, nor does voltage go.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf