Featured post

Objectives and content

Monday 29 August 2016

Servo Motor

Servo Motor

Servo motors functions as electromechanical actuators that do not rotate continuously like DC/AC motors. Application of servo found in positioning or holding some object. Applying pulses at an equal interval servo motor rotates to a particular angular position and then stopped. Direction of rotation depends on the width of the pulse as given in the datasheet of the model. Most common use is to position the rudder of aircrafts and boats etc. Servos can be used effectively here because the rudders do not need to move full 360 degrees nor they require continuous rotation like a wheel. Servos are DC motors with built in gearing and feedback control loop circuitry.

Design steps:

1. Observe following pinout.
2. pulse width of 1ms to 2ms is used to set angular position of servo motor.
3. pulse rate in this 5 Volt servo is 50 Hz.
4. System clock taken 16 MHz and reduced to 2 MHz using prescaler.
5. Count taken to the register ICR1 is 2000000/50 = 40000 to generate 50 Hz rectangular pulse.
6. OCR1B register is loaded with ICR1-4499 to produce 2.5 ms pulse width.
7. delay for 2 seconds.
8. OCR1B register is loaded with ICR1-1299 to produce 0.65 ms pulse width.
9. delay for 2 seconds.
10. COM1B1 and COM1B0 are set so that on compare match of OCR1B and ICR1 rising edge will appear at pin 18 i.e. OC1B till ICR1 reaches its final count.

Circuit:



Code:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
TCCR1A |= 1<<WGM11 | 1<<COM1B1| 1<<COM1B0;
TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS11;
// initialize timer
DDRD |= 0xFF;
ICR1 = 39999;
while (1)
{
OCR1B = ICR1-4799;// 2.4 ms for 0 deg
_delay_ms(2000);
OCR1B = ICR1-3049;// 1.525 ms for 90 deg
_delay_ms(2000);
OCR1B = ICR1-1299;// .65 ms for 180 deg
_delay_ms(2000);
OCR1B = ICR1-3049;// 1.525 ms for 90 deg
_delay_ms(2000);
}
}


Thursday 11 August 2016

Keyboard

Expt-1: Interface keyboard with ATMEGA 32

Components required:

Sl. No.
Category
Item name/value
Quantity
1
IC
ATMEGA32
1
2

7805
1
3
Resistor
10K        
1
4

330R
1
5
Capacitor
0.1uf
2
6

22pf
2
7

10uf, 24Volt
1
8
Crystal Oscillator
16MHz
1
9
LCD
16X2
1
10
Matrix Keyboard
4X4
1

Model:

Procedure:
1.  Define F_CPU 16000000UL
2.  include <avr/io.h>, <compat/deprecated.h>, <util/delay.h> <avr/interrupt.h>
3.  define E 5 since pin 5 of PORTD is connected to LCD enable pin
4.  define RS 6 since 6 pin of PORTD is connected to LCD RS i.e. “register selection” pin
5.  declare functions
a.  void send_a_command(unsigned char command);
b.  void send_a_character(unsigned char character);
c.  void send_a_string(char *string_of_characters);
d.  declare uint8_t a,b,c,p,q,r,s,i,f,z; char buffer[16];
e.  int key=0; for allocating integer to reset the LCD once it reaches its display limit
f.  int keypressed=0; for integer for storing matrix value
6.  declare PORTB and PORTD as output pins
7.  initialize i=f=z=0;
8.  give delay of 50ms
9.  send_a_command(0x01); to Clear Screen 0x01 = 00000001
10. give delay of 50ms
11. send_a_command(0x38); to tell lcd we are using 8bit command /data mode
12. give delay of 50ms
13. send_a_command(0b00001111); makes LCD SCREEN ON and courser blinking
14. DDRA=0xF0; makes column pins as input and row pins as output
15. _delay_ms(1);
16. PORTA=0x0F;// to power the row ins
17. _delay_ms(1);
18. Infinite loop 1 starts
19. Infinite loop 2 starts
20. if (z>1) break;
21. else if (z>0)
a.  send_a_command(0x80 + 0x40 +0); to move courser to second line.
b.  _delay_ms(50);
c.  send_a_string("Stop "); to display a string
22. else
a.  send_a_command(0x01);//clear lcd
b.  _delay_ms(50);
c.  send_a_command(0x80 +0); to move courser to first line.
d.  _delay_ms(50);
e.  send_a_string("Start ");//display string
23. Infinite loop 3 starts
24. Check true for any of column pins goes low
a.  if PA0 is low assign a=1 else a=0;
b.  if PA1 is low assign b=2 else b=0;
c.  if PA2 is low assign c=4 else c=0;
d.  if a>0 assign keypressed=a;
e.  if b>0 assign keypressed=b;
f.  if c>0 assign keypressed=c;
g.  display keypressed to show column address
h.  break Infinite loop 3 and goto step 25
25. assign DDRA to DDRA bitwise XOR with 0b11111111 to make rows as inputs and columns as output
26. Delay for 10 ms
27. Power the columns
28. Delay for 50 ms
29. Infinite loop 4 starts
30. Check true for any of row pins goes low
a.  If PA4 is low assign p=16 else p=0;
b.  If PA5 is low assign q=32 else q=0;
c.  If PA6 is low assign r=64 else r=0;
d.  If PA7 is low assign s=16 else s=0;
e.  break Infinite loop 4 and goto step 31;
31. if (p>0) assign keypressed to keypressed bitwise OR with p;
a.  else if (q>0) assign keypressed to keypressed bitwise OR with q;
b.  else if (r>0) assign keypressed to keypressed bitwise OR with r;
c.  else if (s>0) assign keypressed to keypressed bitwise OR with s;
32. display keypressed to show row address
33. check for keypressed==0b00010001
a.  if yes display 3, increment key counter and assign initial and final position if z>0
34. check for keypressed==0b00010010
a.  if yes display 2, increment key counter and assign initial and final position if z>0
35. check for keypressed==0b00010100
a.  if yes display 1, increment key counter and assign initial and final position if z>0
36. check for keypressed==0b00011000
a.  if yes display “ ”, increment key counter and assign initial and final position if z>0
37. check for keypressed==0b00100001
a.  if yes display 6, increment key counter and assign initial and final position if z>0
38. check for keypressed==0b00100010
a.  if yes display 5, increment key counter and assign initial and final position if z>0
39. check for keypressed==0b00100100
a.  if yes display 4, increment key counter and assign initial and final position if z>0
40. check for keypressed==0b00101000
a.  if yes display “ ”, increment key counter and assign initial and final position if z>0
41. check for keypressed==0b01000001
a.  if yes display 9, increment key counter and assign initial and final position if z>0
42. check for keypressed==0b01000010
a.  if yes display 8, increment key counter and assign initial and final position if z>0
43. check for keypressed==0b01000100
a.  if yes display 7, increment key counter and assign initial and final position if z>0
44. check for keypressed==0b01001000
a.  if yes display “ ”, increment key counter and assign initial and final position if z>0
45. check for keypressed==0b10000001
a.  if yes display #, increment key counter and assign initial and final position if z>0
46. check for keypressed==0b10000010
a.  if yes display 0, increment key counter and assign initial and final position if z>0
47. check for keypressed==0b10000100
a.  if yes display *, increment key counter and assign initial and final position if z>0
48. check for keypressed==0b10001000
a.  if yes display “ ”, increment key counter and assign initial and final position if z>0
49. reset keypressed to 0;//after showing integer erasing the row column memory
50. assign DDRA to DDRA bitwise XOR with 0b11111111 to make columns as inputs and rows as output
51. Delay for 10 ms
52. Power the rows
53. Delay for 50 ms
54. Increment z++;
55. If (z>1) break Infinite loop 2 else goto step 19;
56. Reset z to 0;
57. Continue Infinite loop 1
58. return(0);
59. end


void send_a_command(unsigned char command)
{
PORTB = command;
PORTD &= ~ (1<<RS); //putting 0 in RS to tell lcd we are sending command
PORTD |= 1<<E; //telling lcd to receive command /data at the port
_delay_ms(50);
PORTD &= ~1<<E;//telling lcd we completed sending data
PORTB= 0;
}
void send_a_character(unsigned char character)
{
PORTB= character;
PORTD |= 1<<RS;//telling LCD we are sending data not commands
PORTD |= 1<<E;//telling LCD to start receiving command/data
_delay_ms(50);
PORTD &= ~1<<E;//telling lcd we completed sending data/command
PORTB = 0;
}
void send_a_string(char *string_of_characters)
{
while(*string_of_characters > 0)
{
send_a_character(*string_of_characters++);
}
}




Code:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <compat/deprecated.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define E 5 //giving name “enable” to 5 pin of PORTD, since it Is connected to LCD enable pin
#define RS 6 //giving name “register selection” to 6 pin of PORTD, since is connected to LCD RS pin
void send_a_command(unsigned char command);
void send_a_character(unsigned char character);
void send_a_string(char *string_of_characters);
int main()
{
uint8_t a,b,c,p,q,r,s,i,f,z;
char buffer[16];
int key=0;//allocating integer to reset the LCD once it reaches its display limit
int keypressed=0;//integer for storing matrix value
DDRB = 0xFF;
DDRD = 0xFF;
_delay_ms(50);
i=f=z=0;
_delay_ms(50);//giving delay of 50ms
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
//putting portB and portD as output pins DDRD = 0xFF;
_delay_ms(50);//giving delay of 50ms
send_a_command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(50);
send_a_command(0x38);//telling lcd we are using 8bit command /data mode
_delay_ms(50);
send_a_command(0b00001111);//LCD SCREEN ON and courser blinking
DDRA=0xF0;//taking column pins as input and row pins as output
_delay_ms(1);
PORTA=0x0F;// powering the row ins
_delay_ms(1);
while(1)// Infinite loop 1 starts
{
while(1)// Infinite loop 2 starts
       {
if (z>1)
{
       break;
}     
else if (z>0)
{
send_a_command(0x80 + 0x40 +0);//move courser to second line.
_delay_ms(50);
send_a_string("Stop ");//display string
}
else
{
send_a_command(0x01);//clear lcd
_delay_ms(50);
send_a_command(0x80 +0);//move courser to first line.
_delay_ms(50);
send_a_string("Start ");//display string
}
while (1)//   Infinite loop 3 starts
{
if (bit_is_clear(PINA,0)|bit_is_clear(PINA,1)|bit_is_clear(PINA,2))//in any of column pins goes low execute the loop
{
       if(bit_is_clear(PINA,0))
       a=1<<PA0;
       else a=0;
       if (bit_is_clear(PINA,1))
       b=1<<PA1;
       else b=0;
       if (bit_is_clear(PINA,2))
       c=1<<PA2;
       else c=0;
       if (a>0)
       keypressed=a;
       else if (b>0)
       keypressed=b;
       else if (c>0)
       keypressed=c;
itoa(keypressed,buffer,16);
send_a_string(buffer);
//_delay_ms(1);
send_a_string("-");
//_delay_ms(1);
break;
}
}//    Infinite loop 3 stops
PORTA=0xFF;
_delay_ms(50);
DDRA ^=0b11111111;//making rows as inputs and columns as output
_delay_ms(10);
PORTA = 0xF0;//powering columns
_delay_ms(50);
       while(1)//    Infinite loop 4 starts
       {
       if (bit_is_clear(PINA,4)|bit_is_clear(PINA,5)|bit_is_clear(PINA,6)|bit_is_clear(PINA,7))
       {
       if(bit_is_clear(PINA,4))
       p=1<<PA4;
       else p=0;
       if (bit_is_clear(PINA,5))
       q=1<<PA5;
       else q=0;
       if (bit_is_clear(PINA,6))
       r=1<<PA6;
       else r=0;
       if (bit_is_clear(PINA,7))
       s=1<<PA7;
       else s=0;
       break;
       }
       }             //     Infinite loop 4 stops
       if (p>0)
       keypressed|=p;
       else if (q>0)
       keypressed|=q;
       else if (r>0)
       keypressed|=r;
       else if (s>0)
       keypressed|=s;
itoa(keypressed,buffer,16);
send_a_string(buffer);
_delay_ms(1);
send_a_string("/");
_delay_ms(1);
if (keypressed==0b00010001)//
{
send_a_string("3");//if row1 and column1 is high show “1”
key++;
if(z>0)
f=3;
else i=3;
}
if (keypressed==0b00010010)
{
send_a_string("2");// if row1 and column2 is high show “4”
key++;
if(z>0)
f=2;
else i=2;
}
if (keypressed==0b00010100)
{
send_a_string("1");// if row1 and column3 is high show “7”
key++;
if(z>0)
f=1;
else i=1;
}
if (keypressed==0b00011000)
{
send_a_string(" ");//if row1 and column4 is high show “*”
key++;
}
if (keypressed==0b00100001)
{
send_a_string("6");// if row2 and column1 is high show “2”
key++;
if(z>0)
f=6;
else i=6;
}
if (keypressed==0b00100010)
{
send_a_string("5");// if row2 and column2 is high show “5”
key++;
if(z>0)
f=5;
else i=5;
}
if (keypressed==0b00100100)
{
send_a_string("4");// if row2 and column3 is high show “8”
key++;
if(z>0)
f=4;
else i=4;
}
if (keypressed==0b00101000)
{
send_a_string(" ");// if row2 and column4 is high show “0”
key++;
}
if (keypressed==0b01000001)
{
send_a_string("9");
key++;
if(z>0)
f=9;
else i=9;
}
if (keypressed==0b01000010)
{
send_a_string("8");
key++;
if(z>0)
f=8;
else i=8;
}
if (keypressed==0b01000100)
{
send_a_string("7");
key++;
if(z>0)
f=7;
else i=7;
}
if (keypressed==0b01001000)
{
send_a_string(" ");
key++;
}
if (keypressed==0b10000001)
{
send_a_string("#");
key++;
}
if (keypressed==0b10000010)
{
send_a_string("0");
key++;
}
if (keypressed==0b10000100)
{
send_a_string("*");
key++;
}
if (keypressed==0b10001000)
{
send_a_string(" ");
key++;
}
keypressed=0;//after showing integer erasing the row column memory
PORTA=0xFF;
_delay_ms(500);
DDRA ^=0b11111111;//shifting input and power port
_delay_ms(10);
PORTA = 0x0F;//powering row pins of keypad
_delay_ms(50);
z++;
if(z>1)
{
       break;
}
}//    Infinite loop 2 stops
z=0;
}      //     Infinite loop 1 stops
return(0);          
}
      
void send_a_command(unsigned char command)
{
PORTB = command;
PORTD &= ~ (1<<RS); //putting 0 in RS to tell lcd we are sending command
PORTD |= 1<<E; //telling lcd to receive command /data at the port
_delay_ms(50);
PORTD &= ~1<<E;//telling lcd we completed sending data
PORTB= 0;
}
void send_a_character(unsigned char character)
{
PORTB= character;
PORTD |= 1<<RS;//telling LCD we are sending data not commands
PORTD |= 1<<E;//telling LCD to start receiving command/data
_delay_ms(50);
PORTD &= ~1<<E;//telling lcd we completed sending data/command
PORTB = 0;
}
void send_a_string(char *string_of_characters)
{
while(*string_of_characters > 0)
{
send_a_character(*string_of_characters++);
}

}