Some people came here from Google, looking for “avr example”. I’m playing with Atmega88 for a few days, because I’m thinking about simple motorcycle alarm.
Normally when I get new microcontroller I’m programming UART to get some readable debug output. It (probably) won’t be needed in final version of device – I don’t want MAX232 to consume power and I may have not enough memory to append extra debug code. (situation with not enough memory would be a nightmare, but it’s still possible – UART and Debug classes aren’t very big, but const strings with debug output may consume a lot) Anyway it’s good idea to get UART working at the very beginning.
Of course you know that you may set your house on fire or brake your leg because of my ideas or software I developed. I’ve got both legs straight and they seem to be all right, but it doesn’t prove anything. I’ve heard that probability of train appearing over your head (3..2..1..now) doesn’t equal zero. So, you’ve been warned.
I’ve written a post about avr and interrupts in c++ about three weeks ago. There was an example of defining interrupt routine as a friend of class. I’ve implemented C_UART class methods and added C_Debug class.
You can get sources here.
Directories and files:
- Devs
- debug.h, debug.cpp – debug device, defines operator <<
- devs.h, devs.cpp – devices namespace
- Periphs
- uart.h, uart.cpp – uart peripheral class and interrupt routines
- periphs.h, periphs.cpp – peripherals namespace
After including files with namespaces in main.cpp:
Debug<<"Hello World!\n"<<"uiTest = "<<123<<"\n";
Remember that there is no memory copying, so there is no hidden buffers – when you’re using this debug output microcontroller waits until all data is sent.
You have to change UBRR0H/L definitions in uart.h. They’re correct for baud rate 9600 when clock is ~8.8MHz. You may find proper values in ATmega88 datasheet.
Makefile is based on some example from AVRFreaks, I’ve just modified it to compile C++ code, added some dependencies and run statement. You may compile this project using WinAVR under Windows or anyhow under Linux.
After “$ make all” and “$ make run” you should get something like this (terminal – 9600/8/1):
Hello World!
uiTest = 123
That’s all folks :-).
Thanks to Patrys for hosting my files.
14-01-2008 at 8:28 |
Well, this code good for uart 0 only. That’s not really good. it’s better to do some virtual functions( Init, Send, Receive etc. ). and why you’ve made Rx and Tx none member? Are there any methods to include them in to class?
06-05-2008 at 3:53 |
hi
tnx for this .
be with God , hi is your guard
03-08-2008 at 3:19 |
Thanks for the posting. I spent the better part of Friday debugging my own C++ code (I’m very new to C++ on AVR). In the end, my constructors were not running reliably. I notice that you’ve used an Init() method on the Uart class instead of relying on a constructor. Is this a best C++ practice, best C++ practice for AVR, or is it incidental?
Thanks!
Gary
05-08-2008 at 21:31 |
Lets just say it’s incidental :-).
I’ve done it this way, because I wanted to put there also Shutdown method and put power management into Init/Shutdown methods. I planned to have peripherals as global objects in Periphs namespace and manage their power consumption.
As C++ practice separate Init method is bad idea, because when you create object you must remember to call some methods to initialize it. Lets just say that you have Init1, Init2 and Init3 methods and you have to call them all in proper order after object creation. It’s nightmare :-).
So if it’s possible you should initialize objects with their constructors. Created object is then ready to use without calling extra methods.
I’ve done this that way just because planned power management support and global scope of Uart instance.
11-02-2009 at 13:49 |
Hy Men!
I`m new in the avr bussines, also beginer to C++, but i only have one question to you:
How do I change the status(1/0) of the ports of the microcontroller when writing a program?where do I find the name of a certain port?what is the comand to set is 0 or 1? (i`m using ATmega32).
:D
13-02-2018 at 11:39 |
wow useless