[TUT] How to get started with microcontrollers - Part 2

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

This is the second part of our microcontroller tutorial series. This time we cover PWM, ADC, control some motors and servos etc.


How to get started with microcontrollers - Part 2

Hope you like it. :)

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

Great stuff! Thanks! Definitely useful AND helpful!

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

Hehe, thanks. I was afraid that nobody even noticed this. :)

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

Yup - luverly stuff, thanks.

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

Great stuff. I've a question, in the project "Fading led" you use for the variable step type uint8_t which mean unsigned integer and in the while loop you have a state step *= -1;, is this ok?

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

Nice.
But it would be a good idea to put the link for part II at the end of the part I article (and maybe tell in the beginning of part I that there's a part II too). Otherwise you can read part I without realizing there's a part II.

Btw. great idea with the animations in the articles so people can see how the LEDs should blink etc.

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

It is a rather odd way of doing it, but it does actually work. Step ends up being either 1 or 255. Adding 255 to an 8 bit number will overflow. e.g. 5 + 255 = 260, which in hex is 0x0104. Truncating to 8 bits gives you 4, which is what we want. To make things clearer I would make step unsigned and use step = -1.

Regards,
Steve A.

The Board helps those that help themselves.

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

I'm not professional programmer, but in books for c programming language is written that unsigned can have value from 0 to 255 and you put -1 into it, is this some programming trick?

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

In a signed byte (decimal) -1 has a specific binary representation: 11111111 . In an unsigned byte the same bit pattern represents the decimal value 255. More on this and related here: http://en.wikipedia.org/wiki/Two... .

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Ok, you are right, I was a little confused with statement step = -1 or step *= -1, since it was defined as unsigned, but now I get it, thanks.

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

awesome... cant wait to get my hands on a servo motor!! :D

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

Great! Thank you, its a very good tutorial, I like it and learn lots from it.

I have a question about the PWM, fading LED example.

In the code, PWM was set to phase correct mode and the PWM frequency was calculated as below

// Set prescaler to 8
	// 1 MHz / 8*256 = ~490 Hz PWM frequency
	TCCR0B |= (1<<CS01);

However from the attiny 45 datasheet, it states that

PWM frequency = fclk_io/ N*510
The N variable represents the prescale factor (1, 8, 64, 256, or 1024).

taken from pg 78

So which is correct?

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

The tutorial is incorrect. You use 256 when using Fast PWM.

Regards,
Steve A.

The Board helps those that help themselves.

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

ok thank you.