Spark V Exp 2
Spark V Exp 2
Experiment No. 2
2.1. AIM
2.2. OBJECTIVES
2.3. PRE-REQUISITES
2.5. THEORY
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.
DDRA = 0xF0; //sets the 4 MSB bits of PORTA as output port and 4 LSB bits
as input port
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.
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);
}
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
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
{
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)
REFERENCES