Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfacing of Input/Output devices with AVR Microcontroller Enrolment No. : 130200111097 Name of the student: SOMAIYA ISHA.

Similar presentations


Presentation on theme: "Interfacing of Input/Output devices with AVR Microcontroller Enrolment No. : 130200111097 Name of the student: SOMAIYA ISHA."— Presentation transcript:

1 Interfacing of Input/Output devices with AVR Microcontroller Enrolment No. : 130200111097 Name of the student: SOMAIYA ISHA

2 21 June 2016Dr. C.H. Vithalani2 Interfacing of push-button switches Write a program to generate square wave at port pin PC5 if switch connected at Port pin PB3 is pressed.

3 21 June 2016Dr. C.H. Vithalani3 Interfacing of push-button switches CBI DDRB,3 ;Clear DDR bit for PB3 to make it input SBI DDRC,5 ;Set DDR bit for PC5 to make it output HERE: SBIC PINB,3 ;Skip next instruction if PB3 is LOW RJMP HERE SBI PORTC,5 ;Set PC5 CBI PORTC,5 ;Clear PC5 RJMP HERE Assembly Language

4 21 June 2016Dr. C.H. Vithalani4 De-bouncing of push button switch De-bouncing can be done by Hardware or by software

5 21 June 2016Dr. C.H. Vithalani5 Exercise: Write program to Read status of Switch connected at Port Pin PC0, Make LED ON if switch is pressed and Make LED OFF if switch is released …. CBI DDRC,0 ;Set PC0 pin as an input pin SBI DDRB,0 ; Set PB0 pin as an output Port AGAIN: SBIS PINC,0 ; SKIP next instruction if PC0 is high RJMP DEVICE_ON SBI PORTB,0 RJMP AGAIN DEVICE_ON: CBI PORTB,0 RJMP AGAIN

6 21 June 2016Dr. C.H. Vithalani6 Interfacing of push-button switch Programming in C #include #define SW 3 //Bit position 3 #define SQR 5 //Bit position 5 int main(void) { DDRB=DDRB & ~(1<<SW); //Set PB3 pin input DDRC=DDRC | (1<<SQR);; //Set PC5 pin output while(1) { if(PINB & (1<<SW)) { PORTC=PORTC & ~(1<<SQR); } else { PORTC=PORTC | (1<<SQR); }

7 21 June 2016Dr. C.H. Vithalani7 LED Flashing Program in C # include void delay_ms(int count) { int i,j; for(int i=0;i<=count;i++){ for(int j=0;j<=100;j++); } int main(void) { DDRD=0xFF; //Port D Output Port PORTD=0xFF; while(1) { PORTD=0x55; delay_ms(1000); PORTD=0xAA; delay_ms(1000); } return 0; }

8 21 June 2016Dr. C.H. Vithalani8 LED Flashing Program in Assembly LDIR16, 0xFF ; Set Port B as an output Port OUT DDRB, R16 ; LDI R16, 0x08 ; Define Stack pointer OUT SPH,R16 LDI R16, 0x5F OUT SPL,R16 AGAIN: LDI R16, 0x55 OUT PORTB, R16 RCALL DELAY LDI R16, 0xaa OUT PORTB, R16 RCALL DELAY RJMP AGAIN DELAY: LDI R20,0x05 L3:LDI R21,0xFF L2:LDI R22,0xFF L1:DEC R22 BRNE L1 DEC R21 BRNE L2 DEC R20 BRNE L3 RET

9 21 June 2016Dr. C.H. Vithalani9 Interfacing of DIP switch & Seven Segment Display

10 Interfacing of SS Display.. 21 June 2016Dr. C.H. Vithalani10

11 Program in C … 21 June 2016Dr. C.H. Vithalani11 int main(void) { DDRD=0xFF; //Port D Output Port DDRA=0x00; //Port A Input PORTD=0xFF; unsigned char ssdata[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92, 0x82,0xF8, 0x80,0x90,0xC8,0x83,0xC6,0xA1,0x86, 0x8E,0x00}; unsigned char switchdata; while(1) { switchdata=PINA; switchdata=switchdata&0x0F; PORTD=ssdata[switchdata]; }

12 21 June 2016Dr. C.H. Vithalani12 Program: DIP SW and SS Display LDI R16,0x00 OUT DDRC,R16 ;Port C Input to read DIP Switch LDI R16,0xFF OUT DDRB,R16 ;Port B Output to Display on SS LDI ZH,HIGH(SSDATA<<1) ;Memory pointer NEXT: IN R16,PINC ANDI R16,0b00001111 ;Mask Upper Nibble LDI ZL,LOW(SSDATA<<1) ADD ZL,R16 LPM R17,Z OUT PORTB,R17 RJMP NEXT SSDATA:;Look Up Table for SS Code.DB 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xC8,0x83.DB 0xC6,0xA1, 0x86, 0x8E,0x00

13 21 June 2016Dr. C.H. Vithalani13 Program: DIP SW and SS Display int main(void) { unsigned char ssdata[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,0xC8,0x83,0xC6,0xA1,0x86, 0x8E,0x00}; unsigned char switchdata; DDRC=0x00; //Port C Input DDRB=0xFF; //Port B Output while(1) { switchdata=PINC; switchdata=switchdata&0x0F; PORTB=ssdata[switchdata]; }

14 21 June 2016Dr. C.H. Vithalani14 Interfacing of thumb wheel SW Thumb wheel switch is widely used in the industry for setting various parameters like temperature at various points in furnace, pressure, fluid flow etc. It is widely used in code lock system to set password and then open lock with that password. It is used when we want to operate equipment by unskilled operator. Operator has to rotate the wheel (or in some switches, push the switch to increment the digit) to change parameters. Thumb wheel switch comes in two configuration: Decimal switch which provides BCD output and Hexadecimal switch which provides binary output corresponding to hexadecimal number. BCD thumbwheel switch is more popular because unskilled operator can use it to set the parameters.

15 21 June 2016Dr. C.H. Vithalani15 Interfacing of thumb wheel SW

16 21 June 2016Dr. C.H. Vithalani16 Program #include int main(void) { unsigned char ssdata[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,0xC8,0x83,0xC6,0xA1,0x86, 0x8E,0x00}; unsigned char switchdata; DDRA=0x00; //Port A Input DDRD=0xFF; //Port D Output while(1) { switchdata=PINA; switchdata=switchdata&0x0F; PORTD=ssdata[switchdata]; } }

17 21 June 2016Dr. C.H. Vithalani17 LCD Basic Principle LCD is constructed by sandwiching a thin layer of liquid crystal fluid between two glass plates. A transparent electrically conductive film (electrodes) or back plane is kept on rear glass sheet. Each pixel of an LCD typically consists of a layer of molecules aligned between two transparent electrodes and two polarizing filters, the axes of transmission are perpendicular (cross) to each other With no liquid crystal between the polarizing filters, light passing through the first filter would be blocked by the second (crossed) polarizer.

18 21 June 2016Dr. C.H. Vithalani18 Basic Principle When a voltage is applied across the electrodes (between backplane and segments), electric field created to align the liquid crystal molecules parallel which will cause transmission of light for particular segment.

19 21 June 2016Dr. C.H. Vithalani19 Basic Principle We can not apply steady dc voltage between back plane and segment. LCD segment drive signals must be square wave with a frequency 30 Hz to 150 Hz. Microcontroller application designer should not take care of applying square waves by programming 8051 because this care is already taken inside LCD module by dedicated microcontroller. The HD44780U is dot-matrix liquid crystal display controller and driver. It can be configured to drive a dot-matrix liquid crystal display under the control of a 4 or 8 bit microcontroller.

20 21 June 2016Dr. C.H. Vithalani20 Basic Principle LCD controller HD44780 supports: 16  1 (16 characters, single row), 16  2 (16 characters, two row) 20  2 (20 characters, two row), 24  2 (24 characters, two row) 40  2 (40 characters, two row)

21 21 June 2016Dr. C.H. Vithalani21 Alphanumeric LCD Interfacing Pinout 8 data pins D7:D0 RS: Data or Command Register Select R/W: Read or Write E: Enable (Latch data) RS – Register Select RS = 0  Command Register RS = 1  Data Register R/W = 0  Write, R/W = 1  Read E – Enable Used to latch the data present on the data pins. D0 – D7 Bi-directional data/command pins. Alphanumeric characters are sent in ASCII format. E R/W RS DB7–DB0 LCD controller communications bus 8 LCD Module Microcontroller

22 21 June 2016Dr. C.H. Vithalani22 LCD Commands The LCD’s internal controller can accept several commands and modify the display accordingly. Such as: Clear screen Return home Decrement/Increment cursor After writing to the LCD, it takes some time for it to complete its internal operations. During this time, it will not accept any new commands or data. We need to insert time delay between any two commands or data sent to LCD

23 21 June 2016Dr. C.H. Vithalani23 Interfacing diagram PORTD : Data bus D0-D7 of LCD PB0: Register select pin RS of LCD PB2: Enable pin of LCD R/W pin of LCD Grounded

24 21 June 2016Dr. C.H. Vithalani24 Programming steps …. Initialize the LCD. For example send command word #38h to initialize LCD for 5  7 dots/character and 2 rows. send command word #3Ch for 5  10 dots/character and two rows. Write separate routine to send command. Make RS=0 and after transferring command word to port Port D, enable LCD by sending pulse at port pin PB0. Make PB1 pin low because it is write operation. Write separate routine to send data to the LCD in which Make RS=1 and after transferring data to port Port D, we will enable LCD to display data. Make pin Pb1 low because it is write operation.

25 21 June 2016Dr. C.H. Vithalani25 LCD Assembly Language program.EQU LCD_DATA=PORTD ;Port D drives data lines.EQU LCD_DDR =DDRD.EQU LCD_CMD = PORTB ;Port B drives control signals RS,E and RW.EQU LCD_CDDR = DDRB.EQU RS=0.EQU RW=1.EQU EN=2 LDI R16, 0xFF ; Set Port D as an output Port OUT LCD_DDR, R16 ; we can also write OUT DDRD,R16 OUT LCD_CDDR,R16 ; We can also write OUT DDRB,R16 LDI R16, 0x08 ; Define Stack pointer OUT SPH,R16 ;Stack pointer initialisation LDI R16, 0x5F OUT SPL,R16

26 21 June 2016Dr. C.H. Vithalani26 LCD Assembly Language program CALL LCD_INIT LDI R16,'G' CALL LCDData LDI R16,'E' CALL LCDData LDI R16,'C' CALL LCDData LOOP:RJMP LOOP

27 21 June 2016Dr. C.H. Vithalani27 LCD Assembly Language program LCD_INIT: LDI R16,0x38 ;Initialise LCD for 5x7 dots/char, two lines CALL LCDCommand LDI R16,0x0E ;Display ON, Cursor ON CALL LCDCommand LDI R16,0x01 ; Clear LCD CALL LCDCommand LDI R16,0x06 ; Shift cursor right CALL LCDCommand LDI R16,0x80 ; Move cursor to first line, first column CALL LCDCommand RET

28 21 June 2016Dr. C.H. Vithalani28 LCD Assembly Language program LCDCommand: OUT LCD_DATA,R16 CBI LCD_CMD,RS ;RS=0 to select command register CBI LCD_CMD,RW ;RW=0 for write operation SBI LCD_CMD,EN ;EN=1 to enable LCD NOP ;Short delay with NOP NOP CBI LCD_CMD,EN ; EN=0 CALL DELAY ; Call delay RET

29 21 June 2016Dr. C.H. Vithalani29 LCD Assembly Language program LCDData: OUT LCD_DATA,R16 SBI LCD_CMD,RS ;RS=1 to select command register CBI LCD_CMD,RW ;RW=0 for write operation SBI LCD_CMD,EN ;EN=1 to enable LCD NOP ;Short delay with NOP NOP CBI LCD_CMD,EN ; EN=0 CALL DELAY ; Call delay RET

30 21 June 2016Dr. C.H. Vithalani30 LCD Assembly Language program DELAY: LDI R21,0x05 L2: LDI R22,0xFF L1: DEC R22 BRNE L1 DEC R21 BRNE L2 RET

31 21 June 2016Dr. C.H. Vithalani31 Command for position of cursor

32 21 June 2016Dr. C.H. Vithalani32 Display String CALL LCD_INIT LDI R31,HIGH(msg<<1) ;Pointer for LCD Message LDI R30,LOW(msg<<1) LOOP: LPM R16,Z+ ;Get data from location defined by memory pointer ; and increment memory pointer CPI R16,0 BREQ EXIT CALL LCDData RJMP LOOP EXIT: RJMP EXIT msg:.db “Welcome to GEC Rajkot",0

33 21 June 2016Dr. C.H. Vithalani33 LCD Program in C # include # include //To use in-built delay routine #define LCD_DATA PORTD #define LCD_DDR DDRD #define LCD_CMD PORTB #define LCD_CDDR DDRB #define RS 0 #define RW 1 #define EN 2 void mydelay(int count) { int i,j; for(int i=0;i<=count;i++) { for(int j=0;j<=100;j++); } }

34 21 June 2016Dr. C.H. Vithalani34 LCD Program in C void lcdCommand(unsigned char); void lcdData(unsigned char); void lcdMessage(char *); void lcdInit(); int main(void) { lcdInit(); lcdCommand(0x80); lcdMessage("GEC RAJKOT"); lcdCommand(0xC0); lcdMessage("EC Department"); while(1); return 0; }

35 21 June 2016Dr. C.H. Vithalani35 LCD Program in C void lcdInit() { LCD_DDR=0xFF; LCD_CDDR=0xFF; lcdCommand(0x38); //Initialise LCD for 5x7 dots/char, 2 lines lcdCommand(0x0E); //Display ON and Cursor ON lcdCommand(0x01); //Clear LCD mydelay(10); lcdCommand(0x06); //Shift cursorn right }

36 21 June 2016Dr. C.H. Vithalani36 LCD Program in C void lcdCommand(unsigned char cmd) { LCD_DATA=cmd; LCD_CMD=LCD_CMD & ~(1<<RS); //Make RS=0 LCD_CMD=LCD_CMD & ~(1<<RW); //Make RW=0 LCD_CMD=LCD_CMD | (1<<EN); //Make EN=1 mydelay(1); LCD_CMD=LCD_CMD & ~(1<<EN); //Make EN=0 mydelay(10); }

37 21 June 2016Dr. C.H. Vithalani37 LCD Program in C void lcdData(unsigned char mydata) { LCD_DATA=mydata; LCD_CMD=LCD_CMD | (1<<RS); //Make RS=1 LCD_CMD=LCD_CMD & ~(1<<RW); //Make RW=0 LCD_CMD=LCD_CMD | (1<<EN); //Make EN=1 mydelay(1); LCD_CMD=LCD_CMD & ~(1<<EN); //Make EN=0 mydelay(10); }

38 21 June 2016Dr. C.H. Vithalani38 LCD Program in C void lcdMessage(char *msg) { unsigned char i=0; while(msg[i]!=0) { lcdData(msg[i]); i++; } }

39 21 June 2016Dr. C.H. Vithalani39 Some Commands

40 21 June 2016Dr. C.H. Vithalani40 THANK YOU


Download ppt "Interfacing of Input/Output devices with AVR Microcontroller Enrolment No. : 130200111097 Name of the student: SOMAIYA ISHA."

Similar presentations


Ads by Google