-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPPFile1.cpp
More file actions
248 lines (191 loc) · 7.36 KB
/
CPPFile1.cpp
File metadata and controls
248 lines (191 loc) · 7.36 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* CPPFile1.cpp
*
* Created: 02-02-2017 20:53:41
* Author: shubh
*/
/********************************************************************************
Written by: Vinod Desai, NEX Robotics Pvt. Ltd.
Edited by: Sachitanand Malewar, NEX Robotics Pvt. Ltd.
AVR Studio Version 4.17, Build 666
Date: 26th December 2010
Application example: Robot control over serial port via XBee wireless communication module
(located on the ATMEGA260 microcontroller adaptor board)
Concepts covered: serial communication
Serial Port used: UART0
There are two components to the motion control:
1. Direction control using pins PORTA0 to PORTA3
2. Velocity control by PWM on pins PL3 and PL4 using OC5A and OC5B.
In this experiment for the simplicity PL3 and PL4 are kept at logic 1.
Pins for PWM are kept at logic 1.
Connection Details:
Motion control: L-1---->PA0; L-2---->PA1;
R-1---->PA2; R-2---->PA3;
PL3 (OC5A) ----> Logic 1; PL4 (OC5B) ----> Logic 1;
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
To control robot use number pad of the keyboard which is located on the right hand side of the keyboard.
Make sure that NUM lock is on.
Commands:
Keyboard Key ASCII value Action
8 0x38 Forward
2 0x32 Backward
4 0x34 Left
6 0x36 Right
5 0x35 Stop
7 0x37 Buzzer on
9 0x39 Buzzer off
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.
*********************************************************************************/
/********************************************************************************
Copyright (c) 2010, NEX Robotics Pvt. Ltd. -*- c -*-
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
* Source code can be used for academic purpose.
For commercial use permission form the author needs to be taken.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Software released under Creative Commence cc by-nc-sa licence.
For legal information refer to:
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
********************************************************************************/
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
unsigned char data; //to store received data from UDR1
void buzzer_pin_config (void)
{
DDRC = DDRC | 0x08; //Setting PORTC 3 as outpt
PORTC = PORTC & 0xF7; //Setting PORTC 3 logic low to turnoff buzzer
}
void motion_pin_config (void)
{
DDRA = DDRA | 0x0F;
PORTA = PORTA & 0xF0;
DDRL = DDRL | 0x18; //Setting PL3 and PL4 pins as output for PWM generation
PORTL = PORTL | 0x18; //PL3 and PL4 pins are for velocity control using PWM.
}
//Function to initialize ports
void port_init()
{
motion_pin_config();
buzzer_pin_config();
}
void buzzer_on (void)
{
unsigned char port_restore = 0;
port_restore = PINC;
port_restore = port_restore | 0x08;
PORTC = port_restore;
}
void buzzer_off (void)
{
unsigned char port_restore = 0;
port_restore = PINC;
port_restore = port_restore & 0xF7;
PORTC = port_restore;
}
//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;
}
ISR(USART0_RX_vect) // ISR for receive complete interrupt
{
data = UDR0; //making copy of data from UDR0 in 'data' variable
UDR0 = data; //echo data back to PC
if(data == 0x38) //ASCII value of 8
{
PORTA=0x06; //forward
}
if(data == 0x32) //ASCII value of 2
{
PORTA=0x09; //back
}
if(data == 0x34) //ASCII value of 4
{
PORTA=0x05; //left
}
if(data == 0x36) //ASCII value of 6
{
PORTA=0x0A; //right
}
if(data == 0x35) //ASCII value of 5
{
PORTA=0x00; //stop
}
if(data == 0x37) //ASCII value of 7
{
buzzer_on();
}
if(data == 0x39) //ASCII value of 9
{
buzzer_off();
}
}
//Function To Initialize all The Devices
void init_devices()
{
cli(); //Clears the global interrupts
port_init(); //Initializes all the ports
uart0_init(); //Initailize UART1 for serial communiaction
sei(); //Enables the global interrupts
}
//Main Function
int main(void)
{
init_devices();
while(1);
}