EX: No: 11 IMPLEMENTING ZIGBEE PROTOCOL WITH ARM
AIM
To transmit the data via Xbee and view the output data in the PC.
APPARATUS & SOFTWARE REQUIRED
1. VSK-SCM4 Development board.
2. IAR IDE software.
3. Flash Loader Demonstrator.
4. Xbee modules 2 Nos.
THEORY
The X Bee/X Bee-PRO ZNet 2.5 (formerly known as Series 2 and Series 2 PRO)
RF Modules were directed to operate within the ZigBee protocol. The modules
provide reliable delivery of data between remote devices. Zigbee is the
communication protocol like wifi and Bluetooth. Xbee is the module using Zigbee
protocol.
Some of its features are:
ZigBee is targeted at radio-frequency (RF) applications.
Low data rate, long battery life, and secure networking.
Transmission range is between 10 and 75 meters (33~246 feet).
The addressing space allows of extreme node density—up to
18,450,000,000,000,000,000 devices (64 bit IEEE address).
using local addressing, simple networks of more than 65,000 nodes can be
configured, with reduced address overhead.
The radios use direct-sequence spread spectrum coding, which is managed
by the digital stream into the modulator.
To ensure reliable data transmission.
Binary phase shift keying (BPSK) in the 868/915 MHz.
Offset quadrature phase shift keying (O-QPSK) at 2.4 GHz.
Prepared By: [Link], AP/ECE Page 1
Fig (a): Xbee interfacing with PC
Fig (b): Xbee interfacing with Microcontroller
Prepared By: [Link], AP/ECE Page 2
PROCEDURE (GENERAL STEPS)
1. Double click IAR Embedded Workbench in the Desktop.
2. To create a new project, choose Project>Create New Project. In the
Dialog box, set the tool chain to ARM and select the project template as
empty project.
3. To type the program, select new from file menu and save it with the name
( anyname.c)
4. Add the necessary library files to the project.
5. Build the program. Hex file will be generated if the program has no errors.
STEPS: TO VIEW OUTPUT
Compile, Debug & Simulate the above code in IAR.
Connect the RS232 cable / USB cable provided to the PC / Laptop and the
VSK – STM4 Boards, and Power on the Board.
For seeing the output in real-time, select the PROG mode, reset the board
and download the code in the board using Flash Loader Demonstrator
through UART0.
Connect 1 Xbee module with VSK-STM4 board and the other module to the
PC.
Open Win X Talk with a baud rate of 115200.
Now change the Mode-Selection-Switch to EXEC position and reset the
board.
The data that is transmitted from the controller can be viewed in PC.
Prepared By: [Link], AP/ECE Page 3
PROGRAM
#include "stm32f4xx.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_gpio.h"
#include "misc.h"
#include <stdio.h>
unsigned char d1[] = {" WELCOME "};
unsigned char d2[] = {" Press any Key"};
int i,val,valsw;
enum { high =1,low = !high };
uint32_t tmp;
unsigned char r1_char[];
void USART2_config()
{
NVIC_InitTypeDef NVIC_InitStructure;
RCC->AHB1ENR |= high << 0;
RCC->APB1ENR |= high <<17;
GPIOA->MODER |= 0x000000A0;
GPIOA->AFR[0] |= 0x00007700;
//USART2->BRR = 0x16D; //115200
USART2->BRR = 0x1117; //9600 XBEE ONLY
USART2->CR3 = 0x0000;
USART2->CR2 = 0x000;
USART2->CR1 = 0x200C;
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // to configure
the USART3 interrupts
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // this sets the
priority group of the USART3 interrupts
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the
subpriority inside the group
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //the
USART3 interrupts are globally enabled
Prepared By: [Link], AP/ECE Page 4
NVIC_Init(&NVIC_InitStructure); // the properties are
passed to the NVIC_Init function which takes care of the low level stuff
}
void delay(int a)
{
int i,j;
for(i=0;i<a;i++)
for(j=0;j<a;j++);
}
void busy_check()
{
delay(200);
GPIOE->BSRRH = 0x0020; //low the RS pin
GPIOE->BSRRH = 0x0040; //low RW pin
void command_write(int comm)
{
busy_check();
GPIOE->BSRRH = 0x0020; //low RS pin
GPIOE->ODR = (comm<<8)|(0<<5); //write the data from 8th bit.(i.E) PG8 to
PG15
GPIOE->BSRRL = 0x0080; //enable pin high
delay(200);
GPIOE->BSRRH = 0x0080; //enable pin low
}
/*LCD initial commands */
void lcd_init()
{
command_write(0x38);
command_write(0x01);
command_write(0x0f);
command_write(0xC0);
command_write(0x06);
Prepared By: [Link], AP/ECE Page 5
}
void lcd_out(unsigned char a)
{
busy_check();
GPIOE->BSRRL = 0x0020; //high RS pin
GPIOE->ODR = (a<<8)|(1<<5); //write the data from 8th bit.(i.E) PG8 to PG15
GPIOE->BSRRL = 0x0080;//enable pin high
delay(200);
GPIOE->BSRRH = 0x0080;//enable pin low
}
void lcd_stringout(unsigned char *gk)
{
int i;
busy_check();
GPIOE->BSRRL = 0x0020; //high RS pin
for(i=0;gk[i] !='\0';i++)
{
GPIOE->ODR =( gk[i]<<8)|(1<<5); //write the data from 8th bit.(i.E) PG8 to
PG15
GPIOE->BSRRL = 0x0080;//enable pin high
delay(200);
GPIOE->BSRRH = 0x0080;//enable pin low
}
}
void mx_pinout_config(void)
{
/*Enable or disable the AHB1 peripheral clock */
RCC->AHB1ENR |= 1<<3; // enable the clock to port D
RCC->AHB1ENR |= 1<<4; //enable clock to port E
RCC->AHB1ENR |= 1<<2; //enable clock to port C
RCC->AHB1ENR |= 1<<1; //Enable clock to port B
GPIOD->MODER |=0x00550055; //set port D0-D3 as o/p mode,other pins are
input moode
Prepared By: [Link], AP/ECE Page 6
GPIOD->OTYPER =0x00000000;
GPIOD->PUPDR = 0x00000000;
GPIOD->OSPEEDR =0xAAAAAAAA;
GPIOB->MODER = 0x55555555;
GPIOB->OSPEEDR = 0xFFFFFFFF;
GPIOC->MODER = 0x55555555; //PC0 as o/p mode
/*Enable pin */
GPIOE->MODER = 0X55555445; // lcd_pins OUTPUT MODE SELECT
GPIOE->OTYPER = 0x00000000;
GPIOE->PUPDR = 0x00000880;
GPIOE->OSPEEDR = 0xAAAAAAAA;
// GPIOE->ODR = 0x00000000;
}
void xbee_tx()
{
/////////////////////////// 1st Row \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
GPIOD->BSRRH = 0x0001; //high PD0
GPIOD->BSRRL = 0x000E; //low PD1 to PD3
val = (GPIOD->IDR)&0xF0; //status of D4 to D7
valsw = (GPIOE->IDR)&0x14;
if(valsw == 0x0B)
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("PE2_SW12 PRESSED");
USART2->DR = 0x43;
delay(4000);
Prepared By: [Link], AP/ECE Page 7
while((USART2->SR & 0x40)==0);
}
else if(val== 0xE0) //2nd column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("PE4_SW11 PRESSED");
USART2->DR = 0x44;
while((USART2->SR & 0x40)==0);
}
if(val == 0xE0) //1st column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("C");
USART2->DR = 0x43;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xD0) //2nd column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("D");
USART2->DR = 0x44;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xB0) //3rd column
{
Prepared By: [Link], AP/ECE Page 8
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("E");
USART2->DR = 0x45;
while((USART2->SR & 0x40)==0);
}
else if(val== 0x70) //4th column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("F");
USART2->DR = 0x46;
while((USART2->SR & 0x40)==0);
}
/////////////////////////// 2nd Row \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
GPIOD->BSRRH = 0x0002; //low PD0
GPIOD->BSRRL = 0x000D; //low PD0
val = (GPIOD->IDR)&0xF0; //status of D4 to D7
if(val == 0xE0) //1st column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("8");
USART2->DR = 0x38;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xD0) //2nd column
Prepared By: [Link], AP/ECE Page 9
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("9");
USART2->DR = 0x39;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xB0) //3rd column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("A");
USART2->DR = 0x41;
while((USART2->SR & 0x40)==0);
}
else if(val== 0x70) //4th column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("B");
USART2->DR = 0x42;
while((USART2->SR & 0x40)==0);
}
/////////////////////////// 3rd Row \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
GPIOD->BSRRH = 0x0004; //low PD0
GPIOD->BSRRL = 0x000B; //low PD0
Prepared By: [Link], AP/ECE Page 10
val = (GPIOD->IDR)&0xF0; //status of D4 to D7
if(val == 0xE0) //1st column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("4");
USART2->DR = 0x34;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xD0) //2nd column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("5");
USART2->DR = 0x35;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xB0) //3rd column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("6");
USART2->DR = 0x36;
while((USART2->SR & 0x40)==0);
}
else if(val== 0x70) //4th column
{
Prepared By: [Link], AP/ECE Page 11
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("7");
USART2->DR = 0x37;
while((USART2->SR & 0x40)==0);
}
/////////////////////////// 4th Row \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
GPIOD->BSRRH = 0x0008; //low PD0
GPIOD->BSRRL = 0x0007; //low PD0
val = (GPIOD->IDR)&0xF0; //status of D4 to D7
if(val == 0xE0) //1st column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("0");
USART2->DR = 0x30;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xD0) //2nd column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("1");
USART2->DR = 0x31;
while((USART2->SR & 0x40)==0);
}
else if(val== 0xB0) //3rd column
Prepared By: [Link], AP/ECE Page 12
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("2");
USART2->DR = 0x32;
while((USART2->SR & 0x40)==0);
}
else if(val== 0x70) //4th column
{
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Transmit");
delay(4000);
command_write(0xc8);
lcd_stringout("3");
USART2->DR = 0x33;
while((USART2->SR & 0x40)==0);
}
}
////////////////////// MAIN \\\\\\\\\\\\\\\\\\\\\\\\\\
void main()
{
USART2_config();
mx_pinout_config();
lcd_init(); //send initial commands
command_write(0x80);
lcd_stringout(d1);
command_write(0xC0);
lcd_stringout(d2);
delay(100);
while(1)
{
Prepared By: [Link], AP/ECE Page 13
GPIOC->ODR = 0x0001;
xbee_tx();
GPIOD->ODR = 0x0<<8;
}
}
//////////////////////// Interrupt \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void USART2_IRQHandler(void)
{
/* receiver interrupt
check if the USART2 receive interrupt flag was set*/
if(USART_GetITStatus(USART2, USART_IT_RXNE) )
{
char k = USART2->DR; // the character from the USART2 data register is
saved in t
GPIOD->ODR = k<<8;
command_write(0x01);
command_write(0x80);
lcd_stringout("Xbee Reciver");
command_write(0xc8);
GPIOC->ODR = 0x0001;
if(k == 0x30)
{
command_write(0xc3);
lcd_stringout("Relay");
GPIOC->ODR = 0x0201; //relay on
delay(10000);
GPIOC->ODR = 0x0001; //relay off
delay(10000);
}
else if(k == 0x31)
{
Prepared By: [Link], AP/ECE Page 14
command_write(0xc0);
lcd_stringout("Steeper_M_ck");
for(i=0;i<56;i++)
{
GPIOB->ODR = 0x9000;
delay(500);
GPIOB->ODR = 0x5000;
delay(500);
GPIOB->ODR = 0x6000;
delay(500);
GPIOB->ODR = 0xA000;
delay(500);
}
}
else if(k == 0x32)
{
command_write(0xc0);
lcd_stringout("Steeper_M_A-ck");
for(i=0;i<56;i++)
{
GPIOB->ODR = 0xA000;
delay(500);
GPIOB->ODR = 0x6000;
delay(500);
GPIOB->ODR = 0x5000;
delay(500);
GPIOB->ODR = 0x9000;
delay(500);
}
}
else if(k == 0x33)
{
lcd_stringout("3");
}
else if(k == 0x34)
{
lcd_stringout("4");
}
else if(k == 0x35)
{
Prepared By: [Link], AP/ECE Page 15
command_write(0xc4);
lcd_stringout("BUZZER_ON");
GPIOC->ODR = 0x0000; //BUZ on
delay(10000);
GPIOC->ODR = 0x0001;//BUZ off
command_write(0xc4);
lcd_stringout("BUZZER_OFF");
delay(3000);
}
else if(k == 0x36)
{
lcd_stringout("6");
}
else if(k == 0x37)
{
lcd_stringout("7");
}
else if(k == 0x38)
{
lcd_stringout("8");
}
else if(k == 0x39)
{
lcd_stringout("9");
}
else if(k == 0x41)
{
lcd_stringout("A");
}
else if(k == 0x42)
{
lcd_stringout("B");
}
else if(k == 0x43)
{
lcd_stringout("C");
}
else if(k == 0x44)
{
Prepared By: [Link], AP/ECE Page 16
lcd_stringout("D");
}
else if(k == 0x45)
{
lcd_stringout("E");
}
else if(k == 0x46)
{
lcd_stringout("F");
}
delay(1000);
}
}
RESULT
Thus the output data is viewed in the Personal computer by transmitting data
via Xbee.
Prepared By: [Link], AP/ECE Page 17