June 2012 | EMBEDDED AND ROBOTICS

Introducing DELAY


INTRODUCES TIME DELAY IN PARTICULAR STEP.


header file required : #include<delay.h>
syntax: delay_ms(200);               // where delay is 200 milli seconds 

Interfacing an LCD

Lcd

a 16 pin lcd have following pin connection
16*2 lcd

pin diagram and explanation of 16*2 lcd

Printing Functions
Now once the connections have been made, we are ready to display something on our screenSome of the general LCD functions which you must know are:

lcd_clear()
Clears the lcd. Remember! Call this function before the while(1) loop, otherwise you won’t be able to see anything!

lcd_gotoxy(x,y)
Place the cursor at coordinates (x,y) and start writing from there. The first coordinate is (0,0). Hence, x ranges from 0 to 15 and y from 0 to 1 in our LCD.
Example: you want to display something starting from the 5th character in second line, then the function would be
lcd_gotoxy(5,1);

lcd_putchar(char c)
To display a single character.
E.g.,
lcd_putchar(‘H’);

lcd_putsf(constant string)
To display a constant string.
Eg,
lcd_putsf(“IIT Kanpur”);

lcd_puts(char arr)
To display a variable string, ie an array of characters (data type char) in C language .
 e.g., You have an array char c[10] which keeps on changing. Then to display it, the function would be called as
lcd_puts(c);


Now we have seen that only characters or strings (constant or variable) can be displayed on the LCD. But quite often we have to display values of numeric variables, which is not possible directly. Hence we need to first convert that numeric value to a string and then display it. For e.g., if we have a variable of type integer, say int k, and we need to display the value of k (which changes every now and then, 200 now and 250 after a second... and so on). For this, we use the C functions itoa() and ftoa(), but remember to include the header file <stdlib.h> to use these C functions.





 itoa(int val, char arr[])
It stores the value of integer val in the character array arr. E.g., we have already defined int i and char c[20], then
itoa(i,c);
lcd_puts(c);
Similarly we have

ftoa(float val, char decimal_places, char arr[])
It stores the value of floating variable f in the character array arr with the number of decimal places as specified by second parameter. E.g., we have already defined float f and char c[20], then
ftoa(f,4,c); // till 4 decimal places
lcd_puts(c);

Now we are ready to display anything we want on our LCD. Just try out something which you would like to see glowing on it!





header file required : #include<lcd.h>   //but it is not required as it is already applied by cvavr by selecting lcd

HOW TO SELECT LCD:
get started with lcd on cvavr

adding lcd on cvavr

Registers - DDR , PORT , PIN

All the configurations in microcontroller is set through 8 bit (1 byte) locations in RAM (RAM is a bank of memory bytes) of the microcontroller called as Registers.

Input Output functions are set by Three Registers for each PORT.
• DDRX (DATA DIRECTION REGISTER)‐‐‐‐> Sets whether a pin is Input or Output of PORTX.
• PORTX ‐‐‐> Sets the Output Value of PORTX.
• PINX ‐‐‐‐‐> Reads the Value of PORTX.

DDR:  

First of all we need to set whether we want a pin to act as output or input. DDRX register sets this.

• To make Input set bit 0
• To make Output set bit 1

example  DDRB=11010001
(PORT‐B)
PB7   Output     1
PB6   Output     1
PB5   Input       0
PB4   Output     1
PB3   Input       0
PB2   Input       0
PB1   Input       0
PB0   Output     1


to make a whole 8 bit port as input or output:
DDRA=255   (FOR OUTPUT PORT: 255 IS USED AS IT IS 8PIN ie  8 BIT PORT{2^8})
DDRA=0       (FOR MAKING INPUT PORT)

To make an individual pin of port as input or output:
syntax    DDRA.X=0;  (INPUT)               // WHERE X IS THE PIN NO. starting from 0 to 7
             DDRA.X=1;  (OUTPUT)

example: DDRC.5=1;                //IT MAKES 5TH PIN OF PORT C AS OUTPUT PIN
-----------------------------------------------------------------------------------------------------------------------

PORT:

  This register sets the value to the corresponding PORT. Now a pin can be Output or Input. So let’s discuss both the cases.

• Output Pin
If a pin is set to be output, then by setting bit 1 we make output High that is +5V and by setting bit 0 we make output Low that is 0V.

SYNTAX:
PORTX ‐‐‐‐> to set value of PORTX with a byte.
PORTX.y ‐‐> to set value of yth pin of PORTX with a bit (works only with CVAVR)

example: PORTA.4=0;                       //GIVE 0 VOLT AT OUTPUT OF PIN 4 OF PORT A
PORTB=255;                             //GIVE 5 VOLT AT OUTPUT OF ALL PINS OF PORT B


• Input Pin

If a pin is set to be input, then by setting its corresponding bit in PORTX register will make it as follows, Set bit 0 ‐‐‐> Tri‐Stated Set bit 1 ‐‐‐> Pull Up
Tristated means the input will hang (no specific value) if no input voltage is specified on that pin.
Pull Up means input will go to +5V if no input voltage is given on that pin. It is basically connecting
PIN to +5V through a 10K Ohm resistance.
----------------------------------------------------------------------------------------------------------------------

PIN register:



This register is used to read the value of a PORT. If a pin is set as input then corresponding bit on PIN register is,
• 0 for Low Input that is V < 2.5V
• 1 for High Input that is V > 2.5V (Ideally, but actually 0.8 V ‐ 2.8 V is error zone!)

SYNTAX:

• PINX ‐‐‐‐> Read complete value of PORTX as a byte.
• PINX.y ‐‐> Read yth pin of PORTX as a bit (works only with CVAVR).

example  if(PINA.1==1)
              {
             ___                      
             ___
              }
    // READ IF VALUE AT PIN 1 OF PORT A IS 1 OR NOT, IF IT IS 1 THEN IT EXECUTES INSTRUCTIONS UNDER if STATEMENT

-------------------------------------------------------------------------------------------------------------------------------------------




Atmega16 Pin Diagram And Explanation




Atmega16 Pin Diagram And Explanation

• MOSI (Master Out Slave In)
• MISO (Master In Slave Out)
• SCK (Serial Clock)
• RESET
• GND (Ground)
Now connect the power supplies that are Vcc and GND to the micro controller.
Vcc = +5V and GND = 0V
Do not forget to connect Reset to Vcc with a 1K/10K resistor for pulling up. That is it we are ready with the hardware.
ATmega16 has 16 KB programmable flash memory, static RAM of 1 KB and EEPROM of 512 Bytes. 



Pin No.
Pin name
Description
Alternate Function
1
(XCK/T0) PB0
I/O PORTB, Pin 0
T0: Timer0 External Counter Input.
XCK : USART External Clock I/O
2
(T1) PB1
I/O PORTB, Pin 1
T1:Timer1 External Counter Input
3
(INT2/AIN0) PB2
I/O PORTB, Pin 2
AIN0: Analog Comparator Positive I/P
INT2: External Interrupt 2 Input
4
(OC0/AIN1) PB3
I/O PORTB, Pin 3
AIN1: Analog Comparator Negative I/P
OC0 : Timer0 Output Compare Match Output
5
(SS) PB4
I/O PORTB, Pin 4
In System Programmer (ISP)
Serial Peripheral Interface (SPI)
6
(MOSI) PB5
I/O PORTB, Pin 5
7
(MISO) PB6
I/O PORTB, Pin 6
8
(SCK) PB7
I/O PORTB, Pin 7
9
RESET
Reset Pin, Active Low Reset

10
Vcc
Vcc = +5V

11
GND
GROUND
12
XTAL2
Output to Inverting Oscillator Amplifier
13
XTAL1
Input to Inverting Oscillator Amplifier
14
(RXD) PD0
I/O PORTD, Pin 0
USART Serial Communication Interface
15
(TXD) PD1
I/O PORTD, Pin 1
16
(INT0) PD2
I/O PORTD, Pin 2
External Interrupt INT0
17
(INT1) PD3
I/O PORTD, Pin 3
External Interrupt INT1
18
(OC1B) PD4
I/O PORTD, Pin 4
PWM Channel Outputs
19
(OC1A) PD5
I/O PORTD, Pin 5
20
(ICP) PD6
I/O PORTD, Pin 6
Timer/Counter1 Input Capture Pin
21
PD7 (OC2)
I/O PORTD, Pin 7
Timer/Counter2 Output Compare Match Output
22
PC0 (SCL)
I/O PORTC, Pin 0
TWI Interface
23
PC1 (SDA)
I/O PORTC, Pin 1
24
PC2 (TCK)
I/O PORTC, Pin 2
JTAG Interface
25
PC3 (TMS)
I/O PORTC, Pin 3
26
PC4 (TDO)
I/O PORTC, Pin 4
27
PC5 (TDI)
I/O PORTC, Pin 5
28
PC6 (TOSC1)
I/O PORTC, Pin 6
Timer Oscillator Pin 1
29
PC7 (TOSC2)
I/O PORTC, Pin 7
Timer Oscillator Pin 2
30
AVcc
Voltage Supply = Vcc for ADC
31
GND
GROUND
32
AREF
Analog Reference Pin for ADC
33
PA7 (ADC7)
I/O PORTA, Pin 7
ADC Channel 7
34
PA6 (ADC6)
I/O PORTA, Pin 6
ADC Channel 6
35
PA5 (ADC5)
I/O PORTA, Pin 5
ADC Channel 5
36
PA4 (ADC4)
I/O PORTA, Pin 4
ADC Channel 4
37
PA3 (ADC3)
I/O PORTA, Pin 3
ADC Channel 3
38
PA2 (ADC2)
I/O PORTA, Pin 2
ADC Channel 2
39
PA1 (ADC1)
I/O PORTA, Pin 1
ADC Channel 1
40
PA0 (ADC0)
I/O PORTA, Pin 0
ADC Channel 0




Port A (PA7 ‐ PA0): Port A serves as the analog inputs to the A/D Converter. Port A also serves as an 8‐bit bi‐directional I/O port, if the A/D Converter is not used. When pins PA0 to PA7 are used as inputs and are externally pulled low, they will source current if the internal pull‐up resistors are activated. The Port A pins are tri‐stated when a reset condition becomes active, even if the clock is not running.
Port B (PB7 ‐ PB0): Port B is an 8‐bit bi‐directional I/O port with internal pull‐up resistors (selected for each bit). Port B also serves the functions of various special features of the ATmega16 as listed on page 58 of datasheet.
Port C (PC7 ‐ PC0): Port C is an 8‐bit bi‐directional I/O port with internal pull‐up resistors (selected for each bit). Port C also serves the functions of the JTAG interface and other special features of the ATmega16 as listed on page 61 of datasheet. If the JTAG interface is enabled, the pull‐up resistors on pins PC5(TDI), PC3(TMS) and PC2(TCK) will be activated even if a reset occurs.
Port D (PD7 ‐ PD0): Port D is an 8‐bit bi‐directional I/O port with internal pull‐up resistors (selected for each bit). Port D also serves the functions of various special features of the ATmega16 as listed on page 63 of datasheet.
RESET: Reset Input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running.
XTAL1: External oscillator pin 1
XTAL2: External oscillator pin 2
41
AVCC: AVCC is the supply voltage pin for Port A and the A/D Converter. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a low‐pass filter.
AREF: AREF is the analog reference pin for the A/D Converter.



You can see it has 32 I/O (Input/output) pins grouped as A, B, C & D with 8 pins in each group. This group is called as PORT.
• PA0 ‐ PA7 (PORTA)
• PB0 ‐ PB7 (PORTB)
• PC0 ‐ PC7 (PORTC)
• PD0 ‐ PD7 (PORTD)
Notice that all these pins have some function written in bracket. These are additional function that pin can perform other than I/O. Some of them are.
• ADC (ADC0 ‐ ADC7 on PORTA)
• UART (Rx,Tx on PORTD)
• TIMERS (OC0 ‐ OC2)
• SPI (MISO, MOSI, SCK on PORTB)
• External Interrupts (INT0 ‐ INT2)


Related Posts Plugin for WordPress, Blogger...

Popular Posts