Labels

Showing posts with label Atmega. Show all posts
Showing posts with label Atmega. Show all posts

Thursday, April 10, 2014

Using the XBee Library with Atmel Studio 6

Now that the library is coming together, I thought it would be useful to write up a quick tutorial on how to use the library, and elaborate a little more on the structure.

The test platform I initially used when developing this code is shown below:



Although an arduino board was used, the arduino bootloader was not. However, since the arduino compiler uses avr-gcc, the library can still be included with arduino code. The ISP programming pins are available on the arduino, and any ISP programmer can be used to upload c code to the Atmega328p microcontroller. I used the AVRISP MKII, which can be purchased from mouser for around 40 bucks.


I prefer the AVRISP MKII, although it is more expensive than 3rd party programmers, it is almost always guaranteed to work with Atmel studio software.

Now, I will create a new project in Atmel Studio 6:


Remember to select the proper AVR if you are using something other than an arduino uno. Since I'm using the uno, I've selected the Atmega328p. After the new project is created, I will copy the XbeeS2.c, XbeeS2.h, and XbeeHAL.h files into the same directory as the .c file with main(). After the files have been added, be sure to add the files to the project:



Once the files are added, be sure to #include "XbeeS2.h". Before compiling, the only file the user needs to edit to use the library is XbeeHAL.h. XbeeHAL contains all the hardware specific abstractions. At the time of writing this tutorial, the Atmega328p and Atmega2560 is supported. If you are also using the Atmega328p, you just have to make sure that "ATmega328P" is #defined. If you are using a different micro, there are a few functions to define that are necessary for the library to function properly.

  • XBEE_UDR - Xbee USART Data Register; This is macro'd to the Data Register Buffer of the USART that the microcontroller is using for interfacing with the Xbee.
  • TX_BUFFER_IS_FULL() - Normally macro'd to ((UCSR0A & (1 << RXC0)) == 0) for the ATmega328P, this is a simple function that is used to check if the UDR is ready to be written. Most USART modules on microcontroller include a bit in a configuration register that indicates when the register is clear, so just change it such that it is appropriate for your micro.
  • RX_BUFFER_IS_FULL() - Macro'd to ((UCSR0A & (1 << UDRE0)) == 0) for the ATmega328P, this is the same thing as TX_BUFFER_IS_FULL(), but indicates when new data is received on UDR rather than when UDR is available to be written.
  • TOGGLE_LED()/LED_OUTPUT() - A test led. This can be changed to whatever is appropriate for your setup, and may be removed from the library later. Not actually needed for proper functionality.
This covers the macros needed, but there are also 2 functions that need to be set in XbeeHAL.h.
  • ISR(USART_RX_vect) { rxISR(); } - This is strictly for AVR's, but the HAL needs to have access to your micro's USART ISR. All the ISR needs to do is call the rxISR() function from the Xbee.c file.
  • void USART_INIT(void) - This is a function just to set up the USART at whatever baud rate you want to specify. Just make sure it matches your Xbee's baud rate! ;)
That covers the hairy details. If you are using the Arduino UNO or ATmega328P, then all of those points can be ignored.


To use the library, the user must call XbeeUSART_init() first. Then, the user may call any function from the library, such at Zigbee_Transmit_Request() for sending messages. Also, a callback feature is available for when the user wants to perform some task when a new packet arrives. To do this, the user calls setNewPacketCB(), passing it the name of the function to be called when the new packet arrives. 




I hope this helps anyone who is looking to try out the library! I will post new revisions here as they come out! Remember, you should only have to modify the HAL, and all the functions available can be found in Xbee.h.

Thanks for reading!

XBee S2 HAL
XBeeS2.c
XBeeS2.h

UPDATE:
I have added more features and restructured some of the library's files. For the time being I will post a link here to the newest version, and later provide a tutorial and github link.
https://dl.dropboxusercontent.com/u/47687797/XBee%20Library.zip

Tuesday, July 16, 2013

The Origins of an Atmega Based Sensor Network

I've decided to make this blog to periodically post my progress on various projects and contribute back to the resource I use most - looking at people's code on other blogs that I find through Google searches. It might also help to keep me keep focus if I continuously write down what I've done.

So, I've been working on this project for nearly a year now, and there's much to be said about all that I've discovered, but I'm just going to start from where I am now, since I've practically had to start from square one again. The project is designing a sensor network that uses xbees and atmega328 microcontrollers. I'm not the first one to try to do this by far, but there's a few new changes that I believe are important enough to warrant the creation of a new wireless sensor network (WSN) development platform.

Most WSN dev boards are difficult to use because of proprietary software or a very small support community for a complex system. Also, most research in sensor networks is focused on routing protocols and reducing energy consumption. I'm mostly interested in the applications of WSNs, particularly with robotics. So the board being designed here will use Xbees because of the common interface they use, and if I want to change the radio frequency the project is using or the routing protocol, I can change Xbees and only apply minor changes to the code. Xbees are also good because they come in varieties that already run the Zigbee stack which makes multihop applications easier to develope and they also have modules that only run 802.15.4, so if you want to work on your own custom routing protocol you can. We're also using atmegas because of the low power consumption of the chips and because I just like them.

We are currently working on developing an Xbee library for the atmega328p that will support interfaces for using Xbee series 1 and 2, and eventually adding a HAL (hardware abstraction layer) for using the library with other atmega chips and even other brands. Now, an obvious thing people will like to point out is that there is an Xbee library for arduino already which also uses the atmega328. Arduino doesn't provide the fine control we will be looking for in this project, so we are coding in C and using AVR studio. The library will hopefully be able to be added to any arduino project when we are finished however.

For more information on Xbees, check out the links below:
https://www.sparkfun.com/pages/xbee_guide
http://www.digi.com/xbee/