-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial_Communication_ZigBee_wireless.c
127 lines (91 loc) · 3.8 KB
/
Serial_Communication_ZigBee_wireless.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/********************************************************************************
Application example: XBee wireless communication module verification
(located on the ATMEGA260 microcontroller adaptor board)
Concepts covered: serial communication
Serial Port used: UART0, UART1
Serial Communication: PORTD 2 --> RXD1 UART1 receive for RS232 serial communication
PORTD 3 --> TXD1 UART1 transmit for RS232 serial communication
PORTH 0 --> RXD2 UART 2 receive for USB - RS232 communication
PORTH 1 --> TXD2 UART 2 transmit for USB - RS232 communication
PORTE 0 --> RXD0 UART0 receive for ZigBee wireless communication
PORTE 1 --> TXD0 UART0 transmit for ZigBee wireless communication
PORTJ 0 --> RXD3 UART3 receive available on microcontroller expansion socket
PORTJ 1 --> TXD3 UART3 transmit available on microcontroller expansion socket
Serial communication baud rate: 9600bps
Note:
1. Make sure that in the configuration options following settings are
done for proper operation of the code
Microcontroller: atmega2560
Frequency: 14745600
Optimization: -O0 (For more information read section: Selecting proper optimization
options below figure 2.22 in the Software Manual)
2. Difference between the codes for RS232 serial, USB and wireless communication is only in the serial port number.
Rest of the things are the same.
3. For USB communication check the Jumper 1 position on the ATMEGA2560 microcontroller adaptor board
4. Auxiliary power can supply current up to 1 Ampere while Battery can supply current up to
2 Ampere. When both motors of the robot changes direction suddenly without stopping,
it produces large current surge. When robot is powered by Auxiliary power which can supply
only 1 Ampere of current, sudden direction change in both the motors will cause current
surge which can reset the microcontroller because of sudden fall in voltage.
It is a good practice to stop the motors for at least 0.5seconds before changing
the direction. This will also increase the useable time of the fully charged battery.
the life of the motor.
*********************************************************************************/
/********************************************************************************
********************************************************************************/
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
unsigned char data; //to store received data from UDR1
//Function To Initialize UART0
// desired baud rate:9600
// actual baud rate:9600 (error 0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x5F; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98;
}
//Function To Initialize UART1
// desired baud rate:9600
// actual baud rate:9600 (error 0.0%)
// char size: 8 bit
// parity: Disabled
void uart2_init(void)
{
UCSR2B = 0x00; //disable while setting baud rate
UCSR2A = 0x00;
UCSR2C = 0x06;
UBRR2L = 0x5F; //set baud rate lo
UBRR2H = 0x00; //set baud rate hi
UCSR2B = 0x98;
}
SIGNAL(SIG_USART0_RECV) // ISR for receive complete interrupt
{
data = UDR0; //making copy of data from UDR0 in 'data' variable
UDR2 = data; //echo data back to PC
}
SIGNAL(SIG_USART2_RECV) // ISR for receive complete interrupt
{
data = UDR2; //making copy of data from UDR0 in 'data' variable
UDR0 = data; //echo data back to PC
}
//Function To Initialize all The Devices
void init_devices()
{
cli(); //Clears the global interrupts
uart0_init(); //Initailize UART0 for serial communiaction
uart2_init(); //Initailize UART1 for serial communiaction
sei(); //Enables the global interrupts
}
//Main Function
int main(void)
{
init_devices();
while(1);
}