0% found this document useful (0 votes)
58 views7 pages

Spark V Exp 2

This document is a lab manual that provides instructions for an experiment on programming a SPARK-V robot using ATMEL Studio. The objectives are to understand the ATMEL Studio programming environment, internal registers of the ATMEGA16 microcontroller, and write a sample program in C. The experiment covers input/output operations on the robot using various ports and registers of the microcontroller. Pin configurations and functions of the ATMEGA16 chip used in the SPARK-V robot are also detailed.

Uploaded by

yogesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

Spark V Exp 2

This document is a lab manual that provides instructions for an experiment on programming a SPARK-V robot using ATMEL Studio. The objectives are to understand the ATMEL Studio programming environment, internal registers of the ATMEGA16 microcontroller, and write a sample program in C. The experiment covers input/output operations on the robot using various ports and registers of the microcontroller. Pin configurations and functions of the ATMEGA16 chip used in the SPARK-V robot are also detailed.

Uploaded by

yogesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Manual on Embedded System in Robotics 1-1

SHRI VAISHNAV VIDYAPEETH VISHVAVIDYALAYA, INDORE


Shri Vaishnav Institute of Technology & Science
Discipline: Electronics & Communication Engineering No. of Pages: 07
Name: Roll Number: Batch:
Subject Code: BTEC-714 Subject Name: Embedded System in Robotics
Details of Submissions & Performance
Performing on First Submission Second submission
Regular turn Extra turn

Remarks and Grade by tutor:

Cross the Grade Exc VG G Fr M F Signature

Experiment No. 2

2.1. AIM

To study about programming of SPARK-V using ATMEL Studio.

2.2. OBJECTIVES

2.2.1. To understand the programming environment of ATMEL Studio


2.2.2. To understand the various internal registers of ATMEGA16 used in programming
2.2.3. Write a sample program in C and program SPARK-V through AVR Bootloader.

2.3. PRE-REQUISITES

2.3.1. Register organization and architecture of the ATMEGA16A.


2.3.2. Knowledge of ATMEL studio & AVR Bootloader
2.3.3. Basics of embedded C

2.4. APPARATUS REQUIRED

2.4.1. ATMEL Studio, AVR Bootloader


2.4.2. SPARK-V
2.4.3. AC adaptor with exact 12VDC with 1Amp. current rating for battery charging.
2.4.4. 6 NiMH rechargeable batteries.
2.4.5. USB Cable

Department of Electrical & Electronics Engineering


Lab Manual on Embedded System in Robotics 1-2

2.5. THEORY

2.5.1. Input / Output Operations on the Robot


ATMEGA16 microcontroller has four 8 bit ports from PORT A to PORT D. Input/output
operations are the most essential, basic and easy operations.
We will need frequent I/O operations mainly to do tasks:

Table 2.1
Function Pins Input / Output Recommended
Initial State
Robot Direction control PB0 to PB3 Output Logic 0
LCD display control PC0 to PC2 Output Logic 0
PC4 to PC7
Boot / I/O switch PD6 Input Pulled up*
Buzzer PC3 Output Logic 0

Note:
* In the AVR microcontrollers while pin is used as input it can be pulled up internally by
using software enabled internal pull-up resistor. This internal pull-up as name indicates pulls
up the floating pin towards Vcc. This makes input pin less susceptible to noise.

2.5.2. Registers for using I/O PORTs of the ATMEGA16 Microcontroller


Each pin of the port can be addressed individually and can be configured as input or output.
While pin is input it can be kept floating or pulled up by using internal pull-up. While pin is
in the output mode it can be logic 0 or logic 1. To configure these ports as input or output
each of the port has three associated I/O registers. These are
1. Data Direction Register (DDRx)
2. Port Drive Register (PORTx) and
3. Port pins register (PINx)
where ‘x’ is A to D indicating particular port name.

Data Direction Register (DDRx)


Data direction register is used to set which bits of the port are used for input and which bits
are used for output. If logic one is written to the pin location in the DDRx, then
corresponding port pin is configured as an output pin. If logic zero is written to the pin
location in the DDRx, then corresponding port pin is configured as an input pin.

DDRA = 0xF0; //sets the 4 MSB bits of PORTA as output port and 4 LSB bits
as input port

Port Drive Register (PORTx)


If the port is configured as output port, then the PORTx register drives the corresponding
value on output pins of the port.

Department of Electrical & Electronics Engineering


Lab Manual on Embedded System in Robotics 1-3

DDRA = 0xFF; //set all 8 bits of PORTA as output


PORTA = 0xF0; //output logic high on 4 MSB pins and logic low on 4 LSB pins

For pins configured as input, we can instruct the microcontroller to apply a pull up register by
writing logic 1 to the corresponding bit of the port driver register.

DDRA = 0x00; //set all 8 bits of PORTA as input


PORTA = 0xF0; //pull-up registers are connected on 4 MSB pins and 4 LSB pins
are floating.

Port pins register (PINx)


Reading from the input bits of port is done by reading port pin register

x = PINA; //read all 8 pins of port A

Table 2.2
DDRx PORTx I/O Pull-up Comments
0 0 Input No floating input
0 1 Input Yes will source current if
Externally pulled low
1 0 Output No Output Low (Sink)
1 1 Output No Output High (source)
Note:
• ‘X’ represents port name – A, B, C, D
• Tri-State is the floating pin condition.

Example:
Make PORTA 0-3 bits as output and PORTA 4-7 bits input.
Add pull-up to pins PORTA 4 and PORTA 5.
Output of PORTA0 and PORTA2 = 1; PORT A1 and PORT A3 = 0;

Table 2.3
Pin PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0
DDRA 0(i/p) 0(i/p) 0(i/p) 0(i/p) 1(o/p) 1(o/p) 1(o/p) 1(o/p)
PORTA 0 0 1 (↑) 1 (↑) 0 1 0 1
Status Floating Floating Pull-up Pull-up Low High Low High

{
unsigned char k;
DDRA = 0x0F; //Make PA4 to PA7 pins input and PA0 to PA3 pins output
PORTA = 0x35; //Make PA7, PA6 floating; PA5, PA4 pulled-up; PA3, PA1 logic 1;
PA3, PA1 //logic 0;
k = PINA; //Reads all the data from PORTA
while (1);
}

Department of Electrical & Electronics Engineering


Lab Manual on Embedded System in Robotics 1-4

Figure 2.1: I/O pin equivalent schematic.

All port pins have individually selectable pull-up resistors with a supply-voltage invariant
resistance. All I/O pins have protection diodes to both VCC and Ground as indicated in
Figure 2.1.

To disable pull-ups of all the ports we need to set Bit 2 of SFIOR to logic one.
Special Function I/O register – SFIOR
Table 2.4
Pin TSM - - - ACME PUD PSR0 PSR321
Read/ Write R/W R R R R/W R/W R/W R/W
Initial Val 0 0 0 0 0 0 0 0

Bit 2-PUD: Pull-Up Disable


When this bit is written to one, the pull-ups in all the I/O ports are disabled even if the
DDRxn and PORTxn Registers are configured to enable the pull-ups ({DDRxn, PORTxn} =
0b01).

Toggling the Pin


Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of
DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port. Where ‘x’
is the port name and ‘n’ is the bit number.

2.5.3. ATMEGA16 microcontroller pin configuration


Table 2.5: ATMEGA16 microcontroller pin configuration
PINNO Pin name USED FOR Status
1 (XCO/T0) PB0 Logic output 1 for Left motor (Left back) Output
2 (T1) PB1 Logic output 2 for Left motor (Left back) Output
3 (INT2/AIN0) PB2 Logic output 1 for Right motor (Right back) Output
4 (OC0/AIN1) PB3 Logic output 2 for Right motor (Right back) Output
5 (SS) PB4 Output
6 (MOSI) PB5 Output
7 (MISO) PB6 Input
8 (SCK) PB7 ISP (In System Programming) Output
9 RESET Microcontroller reset Default

Department of Electrical & Electronics Engineering


Lab Manual on Embedded System in Robotics 1-5

10 VCC 5V --
11 GND Ground --
12 XTAL1 Default
13 XTAL2 Crystal 7.3728 MHz Default
14 (RXD) PD0 UART Receive* Input
15 (TXD) PD1 UART Transmit* Output
16 (INT0) PD2 Position Encoder input for Left Motor,
TSOP1738 output** Input
17 (INT1) PD3 Position Encoder input for Right Motor Input
18 (OC1B) PD4 PWM output for Left Motor Output
19 (OC1A) PD5 PWM output for Right Motor Output
20 (ICP1) PD6 Ultrasonic Trigger Input Left (sensor no. 1)
Ultrasonic sensor*** Output
21 (OC2) PD7 Boot loader switch / Servo Pod output In/Out
22 (SCL) PC0 LCD control line RS (Register Select) Output
23 (SDA) PC1 LCD control line RW(Read/Write Select) Output
24 (TCK) PC2 LCD control line EN(Enable Signal) Output
25 (TMS) PC3 Buzzer Output
26 (TDO) PC4
27 (TDI) PC5
28 (TOSC1) PC6
29 (TOSC2) PC7 LCD data lines (4-bit mode) Output
30 AVCC 5V --
31 AGND Ground --
32 AREF ADC reference voltage pin (5V external) **** --
33 (ADC7) PA7 ADC input for External ultrasonic sensor Input*****
34 (ADC6) PA6 ADC input for battery voltage monitoring Input*****
35 (ADC5) PA5 ADC input for white line sensor Right Input*****
36 (ADC4) PA4 ADC input for white line sensor Center Input*****
37 (ADC3) PA3 ADC input for white line sensor Left Input*****
38 (ADC2) PA2 ADC input for right side analog IR proximity sensor or
ultrasonic range sensor Input*****
39 (ADC1) PA1 ADC input for center side analog IR proximity sensor
or Input*****ultrasonic range sensor
40 (ADC0) PA0 ADC input for left side analog IR proximity sensor or
ultrasonic range sensor Input*****
* UART can be connected between FT232 USB to Serial converter or XBee wireless
module using jumper J5.
** Output of the Left position encoder and TSOP1738 IR receiver are open collector and
both share the same 10K ohm pull-up resistor.
*** Ultrasonic sensors are connected in daisy chain for trigger synchronizing. For more
details refer to chapter 3.
**** AREF can be obtained from the 5V microcontroller
***** All the ADC pins must be configured as input and floating

Department of Electrical & Electronics Engineering


Lab Manual on Embedded System in Robotics 1-6

2.5.4. Application example Buzzer Beep


Microcontroller: atmega16
Frequency: 7372800
Optimization: -O0
//Buzzer is connected at the third pin of the PORTC
//To turn it on make PORTC 3rd pin logic 1
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
//Function to initialize Buzzer
void buzzer_pin_config (void)
{
DDRC = DDRC | 0x08; //Setting PORTC 3 as output
PORTC = PORTC & 0xF7; //Setting PORTC 3 logic low to turnoff buzzer
}
void port_init (void)
{
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;
}
void init_devices (void)
{
cli(); //Clears the global interrupts
port_init();
sei(); //Enables the global interrupts
}
//Main Function
int main(void)
{
init_devices();
while(1)

Department of Electrical & Electronics Engineering


Lab Manual on Embedded System in Robotics 1-7

{
buzzer_on();
_delay_ms(1000); //delay
buzzer_off();
_delay_ms(1000); //delay
}
}

In this code, first three lines represent the header file declaration. The # include directive is
used for including header files in the existing code. The syntax for writing header file is as
follows: #include <avr/io.h> This # include directive will add the already existing io.h header
file stored in avr folder under winavr folder. The same way other header files are also
included in the main program so that we can use various utilities defined in the header files.
In all the codes we will configure pins related to any particular module in the
xxxx_pin_config() functions. In this example code we have used the function
buzzer_pin_config(). Buzzer is connected to the PORTC 3 pin of the microcontroller.
PORTC 3 is configured as output with the initial value set to logic 0 to keep buzzer off at the
time of port initialization. All the xxxx_pin_config() functions will be initialized in the
port_init() function in all the codes as a convention. Function init_devices() will be used to
initialize all the peripherals of the microcontroller as a convention. In the above code buzzer
is turned on by calling function buzzer_on(). _delay_ms(1000) introduces delay of 1 second.
Buzzer is turned off by calling function buzzer_off(). Again _delay_ms(1000) introduces
delay of 1 second. All these statements are written in while(1) loop construct to make buzzer
on-off periodically.

2.6. Questions to be answered by the students after completion of this experiment (in
own handwriting)

Q1. What are the different registers used for IO operation?


Q2. How can you define the CPU Frequency?
Q3. What do you understand by data type in embedded C? Write their names along with
size.

2.7. Students should submit in their own handwriting.

2.7.1. Answers to the questionnaire.

REFERENCES

[1]. SPARK-V Software Manual, NEX Robotics PVT LTD India.

Department of Electrical & Electronics Engineering

You might also like