Author Topic: Yet another AVR beginner  (Read 1908 times)

0 Members and 1 Guest are viewing this topic.

Offline StefflusTopic starter

  • Newbie
  • Posts: 5
  • Country: no
Yet another AVR beginner
« on: October 27, 2017, 08:31:39 pm »
Hi, I'm not completely sure on etiquette here, I was going to post in another topic called "AVR beginner", but the forum engine shouted at me with red letters asking if I was really sure I wanted to post there.

For quite some time I've been wanting to move into the world of the smaller MCUs, PIC12F and Attiny13s or 85s, and moving my Arduino projects onto small homemade PCBs.
But as with so much in electronics I'm finding the transition from "Easy" to skip "Medium" and go straight to "Hard".
It might be best if I mention the simple project at hand as it is for Arduino.
It's a simple PWM dimmer for my chicken coop based on time from a RTC chip via I2C.

Code: [Select]
#include <Wire.h>
#include "DS3231.h"

RTClib RTC;

const int ledPin = 9;     // LED connected to digital pin 9
int hourValue;             // Timeverdi
int minuteValue;           // Minuttverdi
int ledValue;             // PWM duty cycle 0-255, 0%-100%


void setup() {
    Serial.begin(57600);
    Wire.begin();         
}


void loop() {
 
    delay(10000);
 
    DateTime now = RTC.now();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    hourValue = (now.hour());
    minuteValue = (now.minute());

    if (hourValue >= 22 && hourValue < 6)
    {
      ledValue = 0;   // nighttime
    }
    else if (hourValue == 6)
    {
      ledValue = minuteValue * 4.2 ;    // dims up during 6. hour
    }
    else if (hourValue >= 7 && hourValue < 11)
    {
      ledValue = 255 ;    //  from 7-11 the light is fully on
    }
    else if (hourValue >= 11 && hourValue < 18)
    {
      ledValue = 0 ;      // daylight, light redundant
    }
    else if (hourValue >= 18 && hourValue < 21)
    {
      ledValue = 255 ;    // daylight fades, light takes over
    }
    else if (hourValue == 21)
    {
      ledValue = 255 - (minuteValue * 4.2) ; // dim down during 21. hour
    }

    analogWrite(ledPin, ledValue);
}

My main problem is with Atmel Studio 7.
I don't understand how to find what libraries, if any, it has?
From what I gather, TWI is something that needs adding so I found a library for Attiny and I think I got it where it needs to go. https://www.eevblog.com/forum/microcontrollers/good-usi-i2c-master-slave-attiny85-library-(optionally-master-arbitration-)/

After reading the documentation for the Attiny85 and getting nothing but red eyes I decided I was missing the bigger picture and most of all the order of things.
I suppose including libraries and defining pins comes first.
"io.h" is added automatically, so I guess AS7 has that.
But in this tutorial even "delay.h" has to be included: http://www.avr-tutorials.com/interfacing/interfacing-leds-avr-microcontroller -So that is built into AS7?
I don't even know what to ask right now, my head is fuzzy. Is Atmel Studio even usable as-is or is it just a hollow shell that needs filling?
 

Offline odessa

  • Regular Contributor
  • *
  • Posts: 113
  • Country: gb
Re: Yet another AVR beginner
« Reply #1 on: October 27, 2017, 08:35:41 pm »
I've not used Atmel Studio myself .... but, that code looks 100% Arduino to me ?
When  I die I want to die peacefully in my sleep like my Grandad ... Not all shouting and screaming like the passengers on his bus.
 

Offline StefflusTopic starter

  • Newbie
  • Posts: 5
  • Country: no
Re: Yet another AVR beginner
« Reply #2 on: October 27, 2017, 08:43:32 pm »
Yes it is.
The issue is rewriting it for an Attiny.
If Atmel Studio has no libraries and I have to type every bit to the RTC, then maybe I'm better off learning assembly?
 

Offline StefflusTopic starter

  • Newbie
  • Posts: 5
  • Country: no
Re: Yet another AVR beginner
« Reply #3 on: October 27, 2017, 08:53:30 pm »
Incredible.
I had to google "Atmel Studio delay.h" to find reference: http://www.atmel.com/webdoc/avrlibcreferencemanual/group__util__delay.html
"Begin making AVR projects in a few minutes, they said.."
 

Offline janoc

  • Super Contributor
  • ***
  • Posts: 3785
  • Country: de
Re: Yet another AVR beginner
« Reply #4 on: October 27, 2017, 08:53:59 pm »
Yes it is.
The issue is rewriting it for an Attiny.
If Atmel Studio has no libraries and I have to type every bit to the RTC, then maybe I'm better off learning assembly?

First, Atmel Studio doesn't work like Arduino. The "batteries" are not included. Atmel provides some libraries for some things but you need to download those yourself and they are fairly different from the Arduino code. I believe they call it the Atmel Software Framework (ASF):
http://www.atmel.com/tools/avrsoftwareframework.aspx

I have never used it, the 8bit AVR chips are very simple and quite easy to program without this.

Second, you can't compile Arduino code in Atmel Studio by default, because Atmel Studio won't have the libraries and the setup needed to deal with some of the Arduino hacks. It is possible to set it up for Arduino code but make sure  you follow the instructions on the Arduino site carefully.

Finally, you can't compile Arduino code for something like ATTiny without changes. If the libraries you are trying to use are not available for ATTiny (which isn't an officially supported Arduino platform), you are out of luck and will need to rewrite the code.

That doesn't mean you need to resort to assembler - C or C++ is perfectly fine but you will need to take the datasheet for your chip and learn which register does what (which is what Arduino hides from you).

You may want to look first at some tutorials to get an idea what you need to look for. AVR C/C++ is more or less the same as any other embedded chip but there is quite a bit more to learn if you were used to Arduino (which hides the dirty bits and does a lot of stuff behind the scenes for you).

E.g. these:
https://exploreembedded.com/wiki/Basics_of_AVR_%27C%27
« Last Edit: October 27, 2017, 09:00:21 pm by janoc »
 

Offline odessa

  • Regular Contributor
  • *
  • Posts: 113
  • Country: gb
Re: Yet another AVR beginner
« Reply #5 on: October 27, 2017, 08:55:24 pm »
Or if you want to keep it easy, make PCB's with the same chip you used with Arduino and program them with your arduino code ?

If you have the Atmel programmer you could burn the code with no bootloader included so it runs instantly, I've done this with a lot of my arduino projects.

I mainly write for PIC's , so when I use things like the RTC etc I have to write my own libraries.

When  I die I want to die peacefully in my sleep like my Grandad ... Not all shouting and screaming like the passengers on his bus.
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Yet another AVR beginner
« Reply #6 on: October 28, 2017, 01:39:49 am »
The issue is rewriting it for an Attiny.

I appreciate you might want to, but just in case you don't realise, you don't actually need to.

You can use the Arduino environment to target various ATTiny if you use an appropriate core:

https://github.com/sleemanj/optiboot/blob/master/dists/README.md

your code compiles fine in the IDE for a Tiny84 no problem with plenty of flash space to spare. 

Not actually tested of course, but it compiles.  Tiny85 should be OK too if you change the LED pin, since it doesn't have that many :-)

~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline frog

  • Contributor
  • Posts: 42
  • Country: nz
Re: Yet another AVR beginner
« Reply #7 on: October 28, 2017, 02:01:21 am »
Hi Stefflus,
  I build code from scratch for ATtiny and the like all the time.
Typically you can start with an ASF example (although I think the ATtiny examples are fairly sparse) - find something that almost does what you want, step through to see how it works and then customise it to your requirements.  I've often (and this may not be the best way) built a few different examples to get the individual features working and then manually merged the code.

Be aware that ASF is designed to support multiple devices, so there are quite a few layers of abstraction.  Sometimes I've found it easiest to step through the code with the debugger until you get to the instruction(s) that access the hardware and copy/paste these directly into main().  Crude but effective - if your program is a few dozen lines then there's little point in having layers of abstraction.

One thing I have used to very good effect is Pavel Haiduc's CVAVR.  This used to be a completely standalone IDE but I think it's now using either Visual Studio or completely merged into AS7, although I haven't updated for years.  Either way, it has a very useful program generator that will configure hardware and provide some elementary drivers, so you'll need to implement little more than the business logic.  Last I looked I could build a CVAVR program and copy it into AS7 with minimal modifications.  I believe there's a free version of CVAVR which should give you everything you need.

Something perhaps to consider is that the more powerful devices (AVR XMEGA SAMD SAM3 SAM4) aren't expensive and are well supported in the code examples.  You may find that these are easier to get code running on, and it does mean that when you come to move on to more advanced projects you won't need to change architectures.  I first picked up a SAM3 a number of years ago and have used the same core code for every SAM3/SAM4 project I've done since, which gets the rubber onto the road very rapidly.

The main difference you'll find between Arduino and bare-metal is that Arduino provides all the device drivers for you.  This is good in that it means you can concentrate on the business logic, but probably not ideal from a performance point of view.  If you have a trivial app like yours then this doesn't matter, but for performance-critical designs you'll want to be working on the bare metal.

Hope that helps a little.
 

Offline janoc

  • Super Contributor
  • ***
  • Posts: 3785
  • Country: de
Re: Yet another AVR beginner
« Reply #8 on: October 28, 2017, 09:40:38 am »
The issue is rewriting it for an Attiny.

I appreciate you might want to, but just in case you don't realise, you don't actually need to.

You can use the Arduino environment to target various ATTiny if you use an appropriate core:

https://github.com/sleemanj/optiboot/blob/master/dists/README.md

your code compiles fine in the IDE for a Tiny84 no problem with plenty of flash space to spare. 

Not actually tested of course, but it compiles.  Tiny85 should be OK too if you change the LED pin, since it doesn't have that many :-)

This is sorta-kinda thing that will likely give the OP more trouble than it is worth.

Many Arduino libraries aren't supported on the Tiny or assume things like peripheral availability that the Tiny series doesn't have (e.g. normal UART or some timers), thus they won't work without porting. And trying to get it to work on anything else than ATTiny85 is mostly hopeless because there isn't enough flash.

So there is a difference between works in theory and practically usable. (I did actually try this).
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Yet another AVR beginner
« Reply #9 on: October 28, 2017, 11:43:10 am »
Many Arduino libraries aren't supported on the Tiny or assume things like peripheral availability that the Tiny series doesn't have (e.g. normal UART or some timers), thus they won't work without porting. And trying to get it to work on anything else than ATTiny85 is mostly hopeless because there isn't enough flash.

So there is a difference between works in theory and practically usable. (I did actually try this).

I don't want to derail the thread, but you'll note the link is to one of my own repos ;-)

My repo is highly optimised (in addition to LTO), to such an extent that, for an example, even an ATtiny13 can be usefully put to task, including such treats as Serial, Print, tone and SPI support (but admittedly, spi is barely useful, and Wire (i2c) is not practical to support in 1k). 

Libraries which use/build upon the arduino abstractions (eg use SPI or Wire libraries, Serial objects, the usual pinMode, digitalWrite etc functions (in my fork  highly optimisable for constants) should generally work (within flash and ram constraints).  Of course there are plenty that are not well written (imo) and descend too close to the bare metal unnecessarily, but digging in and fixing such things is still quicker than starting from scratch, usually.
« Last Edit: October 28, 2017, 12:44:25 pm by sleemanj »
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf