module 1 - 202.62.95.70:8080
TRANSCRIPT
![Page 1: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/1.jpg)
Module 1
• Introduction to Embedded Systems- Philosophy, Embedded Systems, Embedded Design and Development Process – Applications –Microcontroller - Microprocessor - Von-Neumann and Harvard Architecture – RISC & CISC - 8051 Block diagram-Pin Diagram- Internal Data Memory -Addressing Modes- External Memory Access.
![Page 2: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/2.jpg)
Microprocessor Vs Microcontroller
![Page 3: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/3.jpg)
![Page 4: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/4.jpg)
Von-Neumann Architecture
![Page 5: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/5.jpg)
Harvard Architecture
![Page 6: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/6.jpg)
Von-Neumann Vs Harvard Architecture
![Page 7: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/7.jpg)
CISC Vs RISC
![Page 8: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/8.jpg)
Block Diagram 8051
![Page 9: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/9.jpg)
Simplified Block Diagram
![Page 10: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/10.jpg)
Pin Diagram
![Page 11: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/11.jpg)
The 8051 oscillator and clock
Time required to execute one instruction is called machine cycle
Crystal Frequency 11.0592 MHz
Baud rate: The bits per
second
![Page 12: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/12.jpg)
The 8051 oscillator and clock
Time required to execute one instruction is called machine cycle
Crystal Frequency 11.0592 MHz
Baud rate: The bits per
second
![Page 13: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/13.jpg)
Program Counter
• 16 bit register
• Holds the code memory address
• Tells the microcontroller where the next instruction to be executed is found in memory
• Increments by 1
• Does not have internal address01
0A
05
FA
BC
10
A8
90
![Page 14: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/14.jpg)
Data pointer
• DPTR (does not has direct address)
• Made up of two registers• DPH
• DPL
• External code and data memory access
• Under the control of the program
Has direct address
01
0A
05
FA
BC
10
A8
90
![Page 15: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/15.jpg)
A & B Registers
• Used to hold results of many instructions
• Used for many data transfer
• B register is used with the A reg for multiplication and division
![Page 16: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/16.jpg)
A & B registers
![Page 17: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/17.jpg)
8051 Memory organization
• Internal RAM• 128 byte
• Three areas• Byte addressable
• Bit addressable
• General purpose area
![Page 18: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/18.jpg)
Internal RAM
![Page 19: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/19.jpg)
Internal ROM
• Internally 0000h – 0FFFh
• Externally 0000h – FFFFh
![Page 20: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/20.jpg)
Flags
• 1 bit registers• 0 – reset
• 1 - set
• To store the results of certain instructions
• To indicate status of execution
• Decision can be made based on flag states
![Page 21: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/21.jpg)
Program Status Word (PSW)
![Page 22: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/22.jpg)
Special Function Registers
![Page 23: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/23.jpg)
Addressing modes and operations
![Page 24: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/24.jpg)
Programming the 8051
Label : instruction ; comment(s)
must begin with an alphabet
Reserved keywords should not be used
Keep the labels as short as possible
Use semicolon to specify a location
![Page 25: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/25.jpg)
Instruction
Label : instruction ; comment(s)
Mnemonic destination , source
![Page 26: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/26.jpg)
Mnemonic
• a system such as a pattern of letters, ideas, or associations which assists in remembering something.
![Page 27: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/27.jpg)
Comments
; this is a comment line ; can be used anywhere in the ; program by a programmer ;for reference / adding ;notes
![Page 28: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/28.jpg)
Addressing Modes
• Ways of specifying addresses
• Four types
Immediate Addressing
Register Addressing
Direct Addressing
Indirect addressing
![Page 29: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/29.jpg)
Immediate Addressing Mode
• Data is immediately available as part of instruction
• # represents immediate data
• Syntax
Immediate number cannot be a used as a destination
All numbers must start with a decimal number
![Page 30: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/30.jpg)
Register Addressing Mode
• Registers A and R0 to R7 can be used as source/destination
• Register bank can be selected by using RS0 and RS1 in the PSW
• Syntax
Register to Register moves should be done through A Register
![Page 31: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/31.jpg)
• Write an ALP to Load R1=33H and R2=44H and swap its contents.• Temporary register can be used
• Load R1 and R2
• Copy R1 into A
• Copy A into temp reg
• Copy R2 into A reg
• Copy A into R1
• Copy temp reg to A
• Copy A to R2
![Page 32: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/32.jpg)
Direct Addressing Mode• Direct address / names of Internal RAM locations and
the SFRs can be used as a source / destination
![Page 33: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/33.jpg)
Syntax
Only valid addresses should be used
Using of same address at both source and destination could trigger errors
![Page 34: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/34.jpg)
Indirect Addressing Modes
• Register is used to hold the address
• Pointing registers• R0 and R1
• @ is used to represent pointing registers
• Syntax
The number in the pointing register must be a RAM address
![Page 35: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/35.jpg)
Stack and Stack Pointer
• Stack • Area of an internal RAM
• Stack pointer• 8 bit register
• To hold an internal RAM address
• Indicates the recently accessed memory location using stack operation
• Default value is 07h
• Can be modified by the program
![Page 36: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/36.jpg)
Stack Operation
![Page 37: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/37.jpg)
Push and Pop instructions
Pushes above 7FH result in errors
Source Address
Destination Address
![Page 38: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/38.jpg)
Data Exchanges
• Moves data in two directions
• Except immediate all other addressing modes can be used
All changes are internal to the 8051All exchanges use register A
![Page 39: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/39.jpg)
External memory interfacing
![Page 40: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/40.jpg)
Timing Diagram
![Page 41: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/41.jpg)
External Data Moves
![Page 42: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/42.jpg)
Syntax
All external moves must involve the A register
![Page 43: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/43.jpg)
Module 2
• Introduction to 8051 assembly programming, Instruction set: Data Transfer, Arithmetic and Logical Instructions, Branching and Looping Instructions-Programming
![Page 44: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/44.jpg)
Byte Level Logical Operations
• Result affects the entire byte
• AND operation
Used to mask (set to 0) certain bits of an operand
![Page 45: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/45.jpg)
OR Operation
Used to set certain bits of operand to 1
![Page 46: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/46.jpg)
XOR Operation
Used to check if two registers have the same valueUsed to toggle the bits of an operand
![Page 47: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/47.jpg)
Clear and Complement operations
![Page 48: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/48.jpg)
Bit Level Logical Operations
![Page 49: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/49.jpg)
Rotate and Swap Operations
Used to check the bits of a byte
![Page 50: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/50.jpg)
Incrementing and Decrementing
![Page 51: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/51.jpg)
Addition
![Page 52: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/52.jpg)
Subtraction
Clear Carry flag for subtraction without borrow
![Page 53: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/53.jpg)
Multiplication
Division
![Page 54: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/54.jpg)
The Jump and Call
• Jump • Jumps to different locations of code memory
• Does not return a value
• Looping
• Call • Jumps to different locations of code memory
• Returns a value
• executing subroutine
![Page 55: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/55.jpg)
The Jump and Call Range
![Page 56: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/56.jpg)
Ranges
• Relative Address
• Uses Memory addresses• PC + 127
• PC-128
![Page 57: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/57.jpg)
Short Absolute Range
• Use the concept of dividing memory into logical divisions called pages
![Page 58: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/58.jpg)
Long Absolute Jump Range
• Jumps to any location of a program memory
![Page 59: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/59.jpg)
Conditional Jumps
• Bit Jumps
![Page 60: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/60.jpg)
Conditional Jumps• Byte Jumps
![Page 61: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/61.jpg)
Byte Jumps
![Page 62: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/62.jpg)
Unconditional Jumps
![Page 63: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/63.jpg)
Calls and Subroutines
• Calls• Control transfer instruction
• Calls a subroutine
• Subroutines• Sub program
• a set of instructions designed toperform a frequently usedoperation within a program.
![Page 64: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/64.jpg)
Calls and the stack
1. A call opcode occurs in the program software, or an interrupt is generated in thehardware circuitry.
2. The return address of the next instruction after the call instruction or interrupt isfound in the program counter.
3. The return address bytes are pushed on the stack, low byte first
4. The stack pointer is incremented for each push on the stack
5. The subroutine address is placed in the program counter.
6. The subroutine is executed.
7. A RET (return) opcode is encountered at the end of the subroutine.
8. Two pop operations restore the return address to the PC from the stack area ininternal RAM.
9. The stack pointer is decremented for each address byte pop.
![Page 65: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/65.jpg)
Storing and retrieving the return address
![Page 66: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/66.jpg)
Calls and Returns
![Page 67: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/67.jpg)
BCD Number System
• 0 to 9
• BCD numbers • Unpacked BCD
• Packed BCD
X X X X 1 0 0 1
1 0 0 0 1 0 0 1
![Page 68: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/68.jpg)
Problem with BCD Addition
Problem - 1
MOV A,#17H
ADD A,#28H0 0 0 1 0 1 1 1
0 0 1 0 1 0 0 0
0 0 1 1 1 1 1 1
3 F
Not a Valid BCD No
Solution
0 0 1 1 1 1 1 1
0 0 0 0 0 1 1 0
0 1 0 0 0 1 0 1
4 5
Add 6 to the lower
nibble
![Page 69: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/69.jpg)
Problem with BCD Addition
Problem - 2
MOV A,#52H
ADD A,#87H
0 1 0 1 0 0 1 0
1 0 0 0 0 1 1 1
1 1 0 1 1 0 0 1
D 9
Not a Valid BCD No
• Solution
1 1 0 1 1 0 0 1
0 1 1 0 0 0 0 0
0 0 1 1 1 0 0 1
1 3 9
Add 6 to the Upper
nibble
![Page 70: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/70.jpg)
DA Instruction
• Adds 06 to the sum of the addition
or
• Adds 60 to the sum of the addition
DA A instruction should be used after an ADD or ADDC instruction
![Page 71: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/71.jpg)
Module 3
• Introduction to Embedded C Programming –Timer/Counter
• Registers-Modes of operation-Timer/Counter Programming-
• Basics of serial communication- Serial Communication Registers-
• Programming-Types of Interrupts - Programming
![Page 72: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/72.jpg)
Why C?
• The reasons for writing programs in C
• It is easier and less time consuming to
• write in C than Assembly
• C is easier to modify and update
• You can use code available in function libraries
• C code is portable to other microcontroller with little or no modification
![Page 73: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/73.jpg)
C data types
• Unsigned char
• Signed char
• Unsigned int
• Signed int
• Sbit (single bit)
• Bit and sfr
![Page 74: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/74.jpg)
Unsigned Char
• The character data type is the most natural choice • 8051 is an 8-bit microcontroller
• Unsigned char is an 8-bit data type in the range of 0 – 255 (00 – FFH)
• C compilers use the signed char as the default if we do not put the keyword unsigned
![Page 75: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/75.jpg)
Write an 8051 C program to send values 00 –FF to port P1.#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
![Page 76: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/76.jpg)
Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C, and D to port P1.#include <reg51.h>
void main(void)
{
unsigned char mynum[]=“012345ABCD”;
unsigned char z;
for (z=0;z<=10;z++)
P1=mynum[z];
}
![Page 77: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/77.jpg)
Write an 8051 C program to toggle all the bits of P1 continuously//Toggle P1 forever#include <reg51.h>void main(void){for (;;){p1=0x55;p1=0xAA;}}
![Page 78: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/78.jpg)
Signed char
• The signed char is an 8-bit data type• Use the MSB D7 to represent – or +
• Give us values from –128 to +127
![Page 79: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/79.jpg)
Write an 8051 C program to send values of –4 to +4 to port P1.//Singed numbers
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=mynum[z];
}
![Page 80: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/80.jpg)
Unsigned int
• The unsigned int is a 16-bit data type• Takes a value in the range of 0 to 65535 (0000 – FFFFH)
• Define 16-bit variables such as memory addresses
• Set counter values of more than 256
• Since registers and memory accesses are in 8-bit chunks, the misuse of int variables will result in a larger hex file
![Page 81: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/81.jpg)
Signed int
• Signed int is a 16-bit data type• Use the MSB D15 to represent – or +
• We have 15 bits for the magnitude of the number from –32768 to +32767
![Page 82: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/82.jpg)
Single bit
Write an 8051 C program to toggle bit D0 of the port P1 (P1.0)50,000 times.
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
![Page 83: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/83.jpg)
Bit and sfr
• The bit data type allows access to• single bits of bit-addressable memory spaces 20 – 2FH
• To access the byte-size SFR registers, we use the sfr data type
![Page 84: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/84.jpg)
Summary
![Page 85: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/85.jpg)
Write an 8051 C program to toggle all the bits of port P1 continuouslywith some delay in between. Use Timer 0, 16-bit mode togenerate the delay.
#include <reg51.h>void T0Delay(void);void main(void) {while (1) {P1=0x55;T0Delay();P1=0xAA;T0Delay();}}
void T0Delay() {TMOD=0x01;TL0=0x00;TH0=0x35;TR0=1;while (TF0==0);TR0=0;TF0=0;}
![Page 86: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/86.jpg)
Write an 8051 C program to toggle only bit P1.5 continuously every 50 ms. Use Timer 0, mode 1 (16-bit) to create the delay#include <reg51.h>
void T0M1Delay(void);
sbit mybit=P1^5;
void main(void){
while (1) {
mybit=~mybit;
T0M1Delay();
}
}
void T0M1Delay(void){
TMOD=0x01;
TL0=0xFD;
TH0=0x4B;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
![Page 87: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/87.jpg)
Programs
• Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use Timer 1, mode 1 to create the delay.
• A switch is connected to pin P1.2. Write an 8051 C program to monitor SW and create the following frequencies on pin P1.7:
SW=0: 500Hz
SW=1: 750Hz, use Timer 0, mode 1 for both of them.
• Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to create delay.
• Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create the delay.
![Page 88: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/88.jpg)
Timers/Counters
• The 8051 has two timers/counters, they can be used either as• Timers to generate a time delay
• Event counters to count events happening outside the microcontroller
![Page 89: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/89.jpg)
SFR- Timers / Counters- TMOD
![Page 90: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/90.jpg)
Timer modes of operation Mode 0 – (00)
![Page 91: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/91.jpg)
Mode 1 (01)
![Page 92: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/92.jpg)
Mode 2 (10)
![Page 93: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/93.jpg)
Mode 3 (11)
![Page 94: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/94.jpg)
Timers-TCON
![Page 95: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/95.jpg)
Mode 1- Characteristics
• It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the timer’s register TL and TH
• After TH and TL are loaded with a 16-bit initial value, the timer must be started
• After the timer is started, it starts to count up
• After the timer reaches its limit and rolls over, in order to repeat the process
![Page 96: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/96.jpg)
Mode 1 internal setup
![Page 97: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/97.jpg)
Mode 1 – Steps in Delay generation
1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which timer mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB TFx,targetinstruction to see if it is raised
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
![Page 98: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/98.jpg)
Indicate which mode and which timer are selected for each of the following.(a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H
• We convert the value from hex to binary
• TMOD = 00000001, mode 1 of timer 0 is selected.
• TMOD = 00100000, mode 2 of timer 1 is selected.
• TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
![Page 99: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/99.jpg)
Find the timer’s clock frequency and its period for various 8051-based system, with the crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
![Page 100: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/100.jpg)
Analyze the program and find the delay
MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE
DELAY: SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0 ;until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
![Page 101: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/101.jpg)
Steps to analyze
• Timer clock period of T = 1/921.6kHz = 1.085us
• The number of counts for the roll over is FFFFH – FFF2H = 0DH (13 decimal).
• However, we add one to 13 because of the extra clock needed when it rolls over from FFFF to 0 and raise the TF flag
• This gives 14 × 1.085µs = 15.19µs
![Page 102: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/102.jpg)
To calculate the values to be loadedinto the TL and TH registers• Assume XTAL = 11.0592 MHz, we can use the following steps for
finding the TH, TL registers’ values
1. Divide the desired time delay by 1.085 us
2. Perform 65536 – n, where n is the decimal value we got in Step1
3. Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded into the timer’s register
4. Set TL = xx and TH = yy
![Page 103: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/103.jpg)
Assume that XTAL = 11.0592 MHz. What value do we need to load the timer’s register if we want to have a time delay of 5 ms(milliseconds)• EE0H
CLR P2.3 ;Clear P2.3MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#0 ;TL0=0, the low byteMOV TH0,#0EEH ;TH0=EE, the high byteSETB P2.3 ;SET high P2.3SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0CLR TR0 ;Stop the timer 0CLR TF0 ;Clear timer 0 flag
![Page 104: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/104.jpg)
• Assume that XTAL = 11.0592 MHz, write a program to generate asquare wave of 2 kHz frequency on pin P1.5.
• Assume XTAL = 11.0592 MHz, write a program to generate a squarewave of 50 kHz frequency on pin P2.3.
• Assume that XTAL = 11.0592 MHz. What value do we need to load thetimer’s register if we want to have a time delay of 15 s(milliseconds)
![Page 105: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/105.jpg)
Mode 2- Characteristics
• It is an 8-bit timer; therefore, it allows only values of 00 to FFH to beloaded into the timer’s register TH
• After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL
• After the timer is started, it starts to count up by incrementing the TLregister.
• When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloadedautomatically with the original value kept by the TH register
![Page 106: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/106.jpg)
Mode 2 internal setup
![Page 107: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/107.jpg)
Mode 2 – Steps in Delay generation
1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used, and the timer mode (mode 2) is selected
2. Load the TH registers with the initial count value
3. Start timer
4. Keep monitoring the timer flag (TF) with the JNB TFx,targetinstruction to see whether it is raised
5. Clear the TF flag
6. Go back to Step4, since mode 2 is auto reload
![Page 108: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/108.jpg)
Assume XTAL = 11.0592 MHz, find the frequency of the squarewave generated on pin P1.0 in the following program
MOV TMOD,#20H ;T1/8-bit/auto reload
MOV TH1,#5 ;TH1 = 5
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CPL P1.0 ;P1.0 to hi, lo
CLR TF1 ;clear Timer 1 flag
SJMP BACK ;mode 2 is auto-reload
![Page 109: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/109.jpg)
Programs
• Assume XTAL = 11.0592 MHz, write a program to generate a squarewave of frequency 1.83597 kHz on pin P2.3.
• Assume XTAL = 11.0592 MHz, write a program to generate a squarewave of frequency 72 Hz on pin P2.3.
![Page 110: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/110.jpg)
Module 48051 Microcontroller Interfacing
![Page 111: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/111.jpg)
KEYBOARD INTERFACING
• Input device to microcontroller
• keyboards are organized in a matrix of rows and columns.
![Page 112: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/112.jpg)
Scanning and identifying the key
• Rows == >Output
• Columns== > Input
• No key press• Columns = 1
• All rows are grounded and key pressed• Column (x) = 0
• Detect the key press
![Page 113: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/113.jpg)
Grounding rows and reading columns
• Grounding rows• 0s into the output latch
• No key press• C(D3 – D0) = 1111
• Key press • C(Dx) = 0
• Key identification• R(D3-D0) = 1110 = Reads columns C(D0) =0 /
C(D1) =0 / C(D2) =0 / C(D3) =0 == > key = 0/1/2/3• R(D3-D0) = 1101 = Reads columns C(D0) =0 /
C(D1) =0 / C(D2) =0 / C(D3) =0 == > key = 4/5/6/7• R(D3-D0) = 1011 = Reads columns C(D0) =0 /
C(D1) =0 / C(D2) =0 / C(D3) =0 == > key = 8/9/A/B• R(D3-D0) = 0111 = Reads columns C(D0) =0 /
C(D1) =0 / C(D2) =0 / C(D3) =0 == > key = C/D/E/F
![Page 114: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/114.jpg)
Flow Chart for Keyboard Subroutine
![Page 115: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/115.jpg)
Write ALP to detect the key press and identify the key
MOV P2,#0FFH
K1: MOV P1,#0
MOV A,P2
ANL A,00001111B
CJNE A,#00001111B, K1
K2: ACALL DELAY
MOV A,P2
ANL A,00001111B
CJNE A,#00001111B,OVER
SJMP K2
OVER: ACALL DELAY
MOV A,P2
ANL A,00001111B
CJNE A,#00001111B, OVER1
SJMP K2
OVER1: MOV P1, #11111110B
MOV A,P2
ANL A,#00001111B
CJNE A,#00001111B,ROW_0
MOV P1,#11111101B
MOV A,P2
Checks for key release
Detects key
press
Deb
ou
nce
Iden
tifying th
e row
![Page 116: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/116.jpg)
ANL A,#00001111B
CJNE A,#00001111B,ROW_1
MOV P1,#11111011B
MOV A,P2
ANL A,#00001111B
CJNE A,#00001111B,ROW_2
MOV P1,#11110111B
MOV A,P2
ANL A,#00001111B
CJNE A,#00001111B,ROW_3
LJMP K2
ROW_0: MOV DPTR,#KCODE0
SJMP FIND
ROW_1: MOV DPTR,#KCODE1
SJMP FIND
ROW_2: MOV DPTR,#KCODE2
SJMP FIND
ROW_3: MOV DPTR,#KCODE3
FIND: RRC A
JNC MATCH
INC DPTR
SJMP FIND
Iden
tifying th
e Ro
w
Identifying the Memory location
![Page 117: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/117.jpg)
MATCH: CLR A
MOVC A,@A+DPTR
MOV P0,A
LJMP K1
;ASCII LOOK-UP TABLE FOR EACH ROW
ORG 300H
KCODE0: DB ‘0’,’1’,’2’,’3’ ;ROW 0
KCODE1: DB ‘4’,’5’,’6’,’7’ ;ROW 1
KCODE2: DB ‘8’,’9’,’A’,’B’ ;ROW 2
KCODE3: DB ‘C’,’D’,’E’,’F’ ;ROW 3
END
Identifying the key
LUT
![Page 118: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/118.jpg)
Flow Chart for Keyboard Subroutine
![Page 119: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/119.jpg)
Write a C program to read the keypad and send the result to the serial port.P1.O-P1.3 connected to rowsP2.0-P2.3 connected to columnsConfigure the serial port for 9600 baud, 8-bit, and I stop bit.
#include <reg51.h>
#define COL P2
#define ROW P1
void MSDelay (unsigned int value);
void SerTX (unsigned char);
unsigned char keypad [4][4] =
{‘0’, ‘1’, ‘2’, ‘3’,
‘4’, ‘5’, ‘6’, ‘7’,
‘8’, ‘9’, ‘A’, ‘B’,
‘C’, ‘D’, ‘E’, ‘F’};
void main ()
{
unsigned char colloc, rowloc;
TMOD = 0X20;
TH1 = -3;
SCON = 0X50;
TR1 = 1;
![Page 120: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/120.jpg)
COL = 0xFF;
while(1)
{
do
{
ROW = OxOO.
colloc = COL;
colloc &= OXOF;
} while(colloc != OxOF);
do
{
do
{
MSDelay(20);
colloc = COL;
colloc &= 0x0F;
} while(colloc == 0x0F);
MSDelay(20);
colloc = COL;
colloc &= 0x0F;
} while(colloc == 0x0F);
![Page 121: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/121.jpg)
while(1)
{
ROW = 0xFE;
colloc = COL;
colloc &= 0x0F;
if(colloc != 0x0F) {
rowloc = 0;
break; }
ROW = 0xFD;
colloc = COL;
colloc &= 0x0F;
if(colloc != 0x0F) {
rowloc = 1;
break; }
ROW = 0xFB;
colloc = COL;
colloc &= 0x0F;
if(colloc != 0x0F)
{
rowloc = 2;
break; }
ROW = 0xF7;
colloc = COL;
colloc &= 0x0F;
rowloc = 3;
break;
}
![Page 122: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/122.jpg)
if(colloc == 0x0E)
SerTX(keypad[rowloc][0]);
else if(colloc == 0x0D)
SerTX(keypad[rowloc][1]);
else if(colloc == 0x0B)
SerTX(keypad[rowloc][2]);
else
SerTX(keypad[rowloc][3]);
}
}
void SerTX(unsigned char x)
{
SBUF = x;
while(TI==0);
TI = 0;
}
void MSDelay(unsigned int value)
{
unsigned int x, y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}
![Page 123: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/123.jpg)
Interfacing Relay with 8051
• It allows the isolation of twoseparate sections of a systemwith two different voltagesources.
• electromechanical (or electromagnetic) relay (EMR)
![Page 124: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/124.jpg)
Driving a relay
• Relay coil need 10 mA to energize
• ULN 2803 or Power transistor
ORG OH
MAIN:
SETB PI.O
MOV R5, #55
ACALL DELAY
CLR Pl.0
MOV R5, #55
ACALL DELAY
SJMP MAIN
DELAY:
HI: MOV R4,#IOO
H2: MOV R3 , # 253
H3: DJNZ R3 , H3
DJNZ R4, H2
DJNZ R5, HI
RET
END
![Page 125: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/125.jpg)
Solid-state relay
• No coil, spring, or mechanical contact switch,• Extremely low input current
![Page 126: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/126.jpg)
Optoisolator
• Optocoupler
• isolate two parts of a system
• An optoisolator has an LED (light-emitting diode) transmitter and a photosensor receiver,
![Page 127: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/127.jpg)
Stepper Motor
• Used to translate electricalpulses into discrete mechanicalmovement
![Page 128: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/128.jpg)
Step angle
• The step angle is the minimumdegree of rotation associatedwith a single step
• Steps per revolution is the totalnumber of steps needed torotate one complete rotation or360 degrees.
Step Angles
Steps per revolution
0.72 500
1.8 200
2.0 180
2.5 144
5.0 72
7.5 48
15 24
![Page 129: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/129.jpg)
Normal 4-step sequence
![Page 130: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/130.jpg)
Half step 8-step sequence
![Page 131: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/131.jpg)
8051 connection with stepper motor
![Page 132: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/132.jpg)
A switch is connected to pin P2.7, Write an ALP to monitor the status of SW and perform the following:(a) If SW = 0, the stepper motor moves clockwise,(b) If SW = 1, the stepper motor moves counterclockwise,
ORG 0H MAIN: SETB P2.7
MOV A, #66H MOV P1, A
TURN: JNB P2.7, CW RL A ACALL DELAY MOV P1,A SJMP TURN
CW: RR A ACALL DELAY MOV P1,A SJMP TURN
DELAY: MOV R2, #100H1: MOV R3, #255H2: DJNZ R3, H2
DJNZ R2,H1RETEND
![Page 133: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/133.jpg)
A switch is connected to pin P2.7, Write a 8051 C to monitor the status of SW and perform the following:(a) If SW = 0, the stepper motor moves clockwise,(b) If SW = 1, the stepper motor moves counterclockwise,#include <reg.h>
sbit SW=P2^7
void main()
{
SW = 1
while(1)
{
if(SW == 1)
{
P1 = 0x66
MSDelay(100)
P1 = 0xCC
MSDelay(100)
P1 = 0x99
MSDelay(100)
P1 = 0x33
MSDelay(100)
}
else
{
P1 = 0x66
MSDelay(100)
P1 = 0x33
MSDelay(100)
P1 = 0x99
MSDelay(100)
P1 = 0xCC
MSDelay(100)
}
}
}
void MSDelay(unsigned int value)
{
unsigned int x, y
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}
![Page 134: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/134.jpg)
Interfacing DC motor with 8051
![Page 135: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/135.jpg)
H- Bridge
• An H bridge is an electroniccircuit that switches the polarityof a voltage applied to a load.
• These circuits are often used inrobotics and other applicationsto allow DC motors to runforwards or backwards.
![Page 136: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/136.jpg)
H-Bridge Configurations
![Page 137: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/137.jpg)
H-Bridge Invalid configurations
![Page 138: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/138.jpg)
A switch is connected to pin P2.7, Using a simulator, write a program to monitor thestatus of SW and perform the following:(a) If SW = 0, the DC motor moves clockwise,(b) If SW = I, the DC motor moves counterclockwise.
ORG 0H
MAIN:
CLR P1.0
CLR P1.1
CLR P1.2
CLR P1.3
SETB P2.7
MONITOR:
JNB P2.7, CLOCKWISE
SETB P1.0
CLR P1.1
CLR P1.2
SETB P1.3
SJMP MONITOR
CLOCKWISE:
CLR P1.0
SETB P1.1
SETB P1.2
CLR P1.3
SJMP MONITOR
END
![Page 139: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/139.jpg)
DC motor control using L293
ORG 0H
MAIN:
CLR P1.0
CLR P1.1
CLR P1.2
SETB P2.7
MONITOR:
SETB P1.0
JNB P2.7, CLOCKWISE
CLR P1.1
SETB P1.2
SJMP MONITOR
CLOCKWISE:
SETB P1.1
CLR P1.2
SJMP MONITOR
END
#include <reg51.h>
sbit SW = P2^7;
sbit ENABLE = P1^0;
sbit MTR_1 = P1^1;
sbit MTR_2 = P1^2;
void main()
{
SW = 1;
ENABLE = 0;
MTR_1 = 0;
MTR_2 = 0;
while(1)
{
ENABLE = 1;
if(SW == 1)
{
MTR_0 = 0;
MTR_1 = 1;
}
else
{
MTR_0 = 1;
MTR_1 = 0;
}
}
}
![Page 140: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/140.jpg)
Enabling DC motor speed control using PWM interfacing with 8051
![Page 141: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/141.jpg)
PWM
![Page 142: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/142.jpg)
Write a program to monitor the status of the switch and perform the following (a) IfP2.7 = I, the DC motor move with 25% duty cycle pulse ,(b) If P2.7 = 0, the DC motor moves with 50% duty cycle pulse.
ORG 0H
MAIN:
CLR P1.0
SETB P2.7
MONITOR:
JNB P2.7, FIFTYPERCENT
SETB P1.0
MOV R5, #25
ACALL DELAY
CLR P1.0
MOV R5, #75
ACALL DELAY
SJMP MONITOR
FIFTYPERCENT:
SETB P1.0
MOV R5, #50
ACALL DELAY
CLR P1.0
MOV R5, #50
ACALL DELAY
SJMP MONITOR
DELAY:
H1: MOV R2, #100
H2: MOV R3, #255
H3: DJNZ R3, H3
DJNZ R2, H2
DJNZ R5, H1
RET
END
![Page 143: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/143.jpg)
Enabling DC motor speed control using PWM using 8051 C
![Page 144: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/144.jpg)
Write a C program to monitor the status of the switch and perform the following (a) IfP2.7 = I, the DC motor move with 25% duty cycle pulse ,(b) If P2.7 = 0, the DC motor moves with 50% duty cycle pulse.
#include <reg51.h>
sbit SW = P2^7
sbit MTR = P1^0
void MSDelay(unsigned int value)
void main()
{
SW = 1
MTR = 0
while(1)
{
if(SW == 1)
{
MTR = 1
MSDelay(25)
MTR = 0
MSDelay(75)
}
else
{
MTR = 1
MSDelay(50)
MTR = 0
MSDelay(50)
}
}
}
void MSDelay(unsigned int value)
{
unsigned char x, y
for(x=O; x<1275; x++) .
for(y=O; y<value; y++); }
![Page 145: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/145.jpg)
Two switches are connected to pins P2.0 and P2.1. Write a C program to monitor the status of both switches and perform the following:SW2 SW 10 0 (slow speed)0 1 (Moderate speed)1 0 (fast)1 1 (Very fast)
#include <reg51.h>
sbit MTR = P1^0
void MSDelay(unsigned int value);
void main()
{
unsigned char z;
P2 = 0xFF;
z = P2;
z = z & 0x03;
MTR = 0;
case(1):
{
MTR = 1;
MSDelay(50);
MTR = 0;
MSDelay(50);
break;
}
while(1)
{
switch(z)
{
case(0):
{
MTR = 1;
MSDelay(25);
MTR = 0;
MSDelay(75);
break;
}
case(2):
{
MTR = 1;
MSDelay(75);
MTR = 0;
MSDelay(25);
break;
}
default:
MTR = 1;
}
}
![Page 146: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/146.jpg)
LCD interfacing with 8051
![Page 147: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/147.jpg)
Pin details
![Page 148: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/148.jpg)
LCD Command codes
![Page 149: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/149.jpg)
LCD interfacing ;calls a time delay before sending next.data command. ; P1.0-P1.7 are connected to LCD data pins 00-07. P2.0 is connected to RS pln of LCD. P2.1 is connected to R/W pin of LCD. P2.2 is connected to E pin of LCD
ORG 0H
MOV A,#38H
ACALL COMNWRT
ACALL DELAY
MOV A,#0EH
ACALL COMNWRT
ACALL DELAY
MOV A,#01
ACALL COMNWRT
ACALL DELAY
MOV A,#06H
ACALL COMNWRT
ACALL DELAY
MOV A,#84H
ACALL COMNWRT
ACALL DELAY
MOV A,#'N'
ACALL DATAWRT
ACALL DELAY
MOV A,#'O'
ACALL DATAWRT
AGAIN: SJMP AGAIN
COMNWRT:
MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLR P2.2
RET
DATAWRT:
MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLR P2.2
RET
![Page 150: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/150.jpg)
Write an 8051 C program to send letters ‘E', ‘E', and 'E' to the LCD using delays.
#include <reg51.h>
sfr ldata = 0x90;
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
void main()
{
lcdcmd(0x38);
MSDelay(250);
lcdcmd(0x0E);
MSDelay(250);
lcdcmd(0x01);
MSDelay(250);
lcdcmd(0x06);
MSDelay(250);
lcdcmd(0x86);
MSDelay(250);
lcddata(‘E');
MSDelay(250);
lcddata(‘E');
MSDelay(250);
lcddata(‘E');
}
void lcdcmd(unsigned char value)
{
ldata = value;
rs = 0;
rw = 0;
en = 1;
MSDelay(1);
en = 0;
return;
}
void lcddata(unsigned char value)
{
ldata = value;
rs = 1;
rw = 0;
en = 1;
MSDelay(1);
en = 0;
return;
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
![Page 151: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/151.jpg)
![Page 152: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/152.jpg)
Interfacing 8255 with 8051
• To expand 8051 ports
• Programmable Peripheral Interface • Three 8 bit ports (A, B and C)• PAO-PA7• PBO·PB7• PCO-PC7• RDandWR• DO to D7 data pin• RESET• AO,A1, and CS
Remember External Memory
interfacing
![Page 153: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/153.jpg)
8255 Port Selection
![Page 154: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/154.jpg)
Mode selection of the 8255
• Mode 0, simple I/O• Any of the ports A, B, CL, and CU can be
programmed as input out output• All bits are out or all are in
• Mode 1• Port A and B can be used as input or output
ports with handshaking capabilities
• Mode 2• Port A can be used as a bidirectional I/O
portwith handshaking capabilities providedby port C
• Port B can be used either in mode 0 ormode 1
• BSR (bit set/reset) mode• Only the individual bits of port C can be
programmed
![Page 155: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/155.jpg)
Simple I/O programming
• Find the control word ofthe 8255 for the followingconfigurations:• All the ports of A, B and C
are output ports (mode 0)• 1000 0000 = 80H
• PA = in, PB = out, PCL = out,and PCH = out• 10010000 = 90H
![Page 156: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/156.jpg)
Connecting 8031/51 to 8255
• Send a byte to the controlregister of 8255
• Find the port address assignedto each of ports A, B ,C and thecontrol register
![Page 157: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/157.jpg)
Example
• Find the I/O port addressesassigned to ports A, B, C, and thecontrol register.
• Program the 8255 for ports A, B,and C to be output ports.• 1000 0000 = 80H
![Page 158: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/158.jpg)
8255 programmin
g in Assembly
and C
![Page 159: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/159.jpg)
Write a program to send 55H and AAH to all portscontinuously. Ports are configured as outputs
MOV A,#80H
MOV DPTR,#4003H
MOVX @DPTR,A
MOV A,#55H
AGAIN: MOV DPTR,#4000H
MOVX @DPTR,A
INC DPTR
MOVX @DPTR,A
INC DPTR
MOVX @DPTR,A
CPL A
ACALL DELAY
SJMP AGAIN
![Page 160: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/160.jpg)
• Find the I/O port addresses assigned to ports A, B, C, and the control register.• 1000H PA
• 1001H PB
• 1002H PC
• 1003H Control register
• Find the control byte for PA = in, PB = out, PC = out.• 10010000, or 90H.
A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
![Page 161: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/161.jpg)
Write a program to get data from PA and send it to both B and C.MOV A,#90H
MOV DPTR,#1003H
MOVX @DPTR,A
MOV DPTR,#1000H
MOVX A,@DPTR
INC DPTR
MOVX @DPTR, A
INC DPTR
MOVX @DPTR, A
![Page 162: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/162.jpg)
Write a C program to send 55H and AAH to all ports of th 8255’ , continuously, Assume the base address of the 8255 iS 4000H,
#include <reg51.h>
#include <absacc.h>
void MSDelay(unsigned int itime);
void main()
{
unsigned char value;
XBYTE[0x4003]=0x80;
while(1)
{
value = 0x55;
XBYTE[0x4000]=value;
XBYTE[0x4001]=value;
XBYTE[0x4002]=value;
MSDelay(100);
value = 0xAA;
XBYTE[0x4000]=value;
XBYTE[0x4001]=value;
XBYTE[0x4002]=value;
MSDelay(100);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0; i<itime; i++)
for(j=0; j<1275; j++);
}
![Page 163: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/163.jpg)
Write a C program to get data from PA and send it toboth ports B and C. Use a base address of 4000H for the8255.#include <reg51.h>
#include <absacc.h>
void main()
{
unsigned char value;
XDATA[0x4003]=0x90;
while(1)
{
value = XDATA[0x4000];
XDATA[0x4001]=value;
XDATA[0x4002]=value;
}
}
![Page 164: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/164.jpg)
ATMEGA - 2560Module 5
![Page 165: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/165.jpg)
Features
• High Performance, Low Power Atmel® AVR® 8-Bit Microcontroller
• Advanced RISC Architecture• 135 Powerful Instructions – Most Single Clock Cycle Execution
• 32 × 8 General Purpose Working Registers
• On-Chip 2-cycle Multiplier
• High Endurance Non-volatile Memory Segments• 64K/128K/256KBytes of In-System Self-Programmable Flash
• 4Kbytes EEPROM
• 8Kbytes Internal SRAM
![Page 166: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/166.jpg)
Peripheral Features
• Two 8-bit Timer/Counters with Separate Prescaler and Compare Mode
• Four 16-bit Timer/Counter with Separate Prescaler, Compare- and Capture Mode
• Real Time Counter with Separate Oscillator
• Four 8-bit PWM Channels
• Programmable Watchdog Timer with Separate On-chip Oscillator
• On-chip Analog Comparator
• Interrupt and Wake-up on Pin Change
![Page 167: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/167.jpg)
Special Microcontroller Features
• External and Internal Interrupt Sources
• Power-on Reset and Programmable Brown-out Detection
• Internal Calibrated Oscillator
• Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, Standby, and Extended Standby
![Page 168: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/168.jpg)
Block Diagram
![Page 169: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/169.jpg)
Pin diagram
![Page 170: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/170.jpg)
Ports
• Port A (PA7..PA0)• 8-bit bi-directional I/O port with internal
pull-up resistors
• Port B (PB7..PB0)• Port B is an 8-bit bi-directional I/O port
with internal pull-up resistors
• Port C (PC7..PC0)• Port C is an 8-bit bi-directional I/O port
with internal pull-up resistors
• Port D (PD7..PD0)• Port D is an 8-bit bi-directional I/O port
with internal pull-up resistors
• Port E (PE7..PE0)• Port E is an 8-bit bi-directional I/O port
with internal pull-up resistors
• Port F (PF7..PF0)• serves as analog inputs to the A/D
Converter.• serves as an 8-bit bi-directional I/O port,
if the A/D Converter is not used• can provide internal pull-up resistors
• Port G (PG5..PG0)• 6-bit I/O port with internal pull-up
resistors
• Port H, J, K and L
![Page 171: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/171.jpg)
Port A (78- 71)
Port Pin Alternate Function
PA7 AD7 (External memory interface address and data bit 7)
PA6 AD6 (External memory interface address and data bit 6)
PA5 AD5 (External memory interface address and data bit 5)
PA4 AD4 (External memory interface address and data bit 4)
PA3 AD3 (External memory interface address and data bit 3)
PA2 AD2 (External memory interface address and data bit 2)
PA1 AD1 (External memory interface address and data bit 1)
PA0 AD0 (External memory interface address and data bit 0)
![Page 172: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/172.jpg)
Port B (19-26)
Port Pin Alternate Function
PB7
OC0A/OC1C/PCINT7 (Output Compare and PWM Output A for Timer/Counter0, Output Compare and PWM Output C for Timer/Counter1 or Pin Change Interrupt 7)
PB6OC1B/PCINT6 (Output Compare and PWM Output B for Timer/Counter1 or Pin Change Interrupt 6)
PB5OC1A/PCINT5 (Output Compare and PWM Output A for Timer/Counter1 or Pin Change Interrupt 5)
PB4OC2A/PCINT4 (Output Compare and PWM Output A for Timer/Counter2 or Pin Change Interrupt 4)
PB3 MISO/PCINT3 (SPI Bus Master Input/Slave Output or Pin Change Interrupt 3)
PB2 MOSI/PCINT2 (SPI Bus Master Output/Slave Input or Pin Change Interrupt 2)
PB1 SCK/PCINT1 (SPI Bus Serial Clock or Pin Change Interrupt 1)
PB0 SS/PCINT0 (SPI Slave Select input or Pin Change Interrupt 0)
![Page 173: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/173.jpg)
Port C (53-60)
Port Pin Alternate Function
PC7 A15 (External Memory interface address bit 15)
PC6 A14 (External Memory interface address bit 14)
PC5 A13 (External Memory interface address bit 13)
PC4 A12 (External Memory interface address bit 12)
PC3 A11 (External Memory interface address bit 11)
PC2 A10 (External Memory interface address bit 10)
PC1 A9 (External Memory interface address bit 9)
PC0 A8 (External Memory interface address bit 8)
![Page 174: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/174.jpg)
Port D (43-50)
Port Pin Alternate Function
PD7 T0 (Timer/Counter0 Clock Input)
PD6 T1 (Timer/Counter1 Clock Input)
PD5 XCK1 (USART1 External Clock Input/Output)
PD4 ICP1 (Timer/Counter1 Input Capture Trigger)
PD3 INT3/TXD1 (External Interrupt3 Input or USART1 Transmit Pin)
PD2 INT2/RXD1 (External Interrupt2 Input or USART1 Receive Pin)
PD1 INT1/SDA (External Interrupt1 Input or TWI Serial DAta)
PD0 INT0/SCL (External Interrupt0 Input or TWI Serial CLock)
![Page 175: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/175.jpg)
Port E (2-9)Port Pin Alternate Function
PE7INT7/ICP3/CLK0(External Interrupt 7 Input, Timer/Counter3 Input Capture Trigger or Divided System Clock)
PE6INT6/ T3(External Interrupt 6 Input or Timer/Counter3 Clock Input)
PE5INT5/OC3C(External Interrupt 5 Input or Output Compare and PWM Output C for Timer/Counter3)
PE4INT4/OC3B(External Interrupt4 Input or Output Compare and PWM Output B for Timer/Counter3)
PE3
AIN1/OC3A(Analog Comparator Negative Input or Output Compare and PWM Output A for Timer/Counter3)
PE2AIN0/XCK0(Analog Comparator Positive Input or USART0 external clock input/output)
PE1PDO(1)/TXD0(Programming Data Output or USART0 Transmit Pin)
PE0PDI(1)/RXD0/PCINT8(Programming Data Input, USART0 Receive Pin or Pin Change Interrupt 8)
![Page 176: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/176.jpg)
Port F (97-90)Port Pin Alternate Function
PF7 ADC7/TDI (ADC input channel 7 or JTAG Test Data Input)
PF6 ADC6/TDO (ADC input channel 6 or JTAG Test Data Output)
PF5 ADC5/TMS (ADC input channel 5 or JTAG Test Mode Select)
PF4 ADC4/TCK (ADC input channel 4 or JTAG Test ClocK)
PF3 ADC3 (ADC input channel 3)
PF2 ADC2 (ADC input channel 2)
PF1 ADC1 (ADC input channel 1)
PF0 ADC0 (ADC input channel 0)
![Page 177: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/177.jpg)
Port G (52-51, 70, 28-29)
Port Pin Alternate Function
PG5 OC0B (Output Compare and PWM Output B for Timer/Counter0)
PG4 TOSC1 (RTC Oscillator Timer/Counter2)
PG3 TOSC2 (RTC Oscillator Timer/Counter2)
PG2 ALE (Address Latch Enable to external memory)
PG1 RD (Read strobe to external memory)
PG0 WR (Write strobe to external memory)
![Page 178: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/178.jpg)
Port H (12-18 & 27)
Port Pin Alternate Function
PH7 T4 (Timer/Counter4 Clock Input)
PH6 OC2B (Output Compare and PWM Output B for Timer/Counter2)
PH5 OC4C (Output Compare and PWM Output C for Timer/Counter4)
PH4 OC4B (Output Compare and PWM Output B for Timer/Counter4)
PH3 OC4A (Output Compare and PWM Output A for Timer/Counter4)
PH2 XCK2 (USART2 External Clock)
PH1 TXD2 (USART2 Transmit Pin)
PH0 RXD2 (USART2 Receive Pin)
![Page 179: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/179.jpg)
Port J (63-69 & 79)
Port Pin Alternate Function
PJ7 –
PJ6 PCINT15 (Pin Change Interrupt 15)
PJ5 PCINT14 (Pin Change Interrupt 14)
PJ4 PCINT13 (Pin Change Interrupt 13)
PJ3 PCINT12 (Pin Change Interrupt 12)
PJ2 XCK3/PCINT11 (USART3 External Clock or Pin Change Interrupt 11)
PJ1 TXD3/PCINT10 (USART3 Transmit Pin or Pin Change Interrupt 10)
PJ0 RXD3/PCINT9 (USART3 Receive Pin or Pin Change Interrupt 9)
![Page 180: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/180.jpg)
Port K (89-82)
Port Pin Alternate Function
PK7 ADC15/PCINT23 (ADC Input Channel 15 or Pin Change Interrupt 23)
PK6 ADC14/PCINT22 (ADC Input Channel 14 or Pin Change Interrupt 22)
PK5 ADC13/PCINT21 (ADC Input Channel 13 or Pin Change Interrupt 21)
PK4 ADC12/PCINT20 (ADC Input Channel 12 or Pin Change Interrupt 20)
PK3 ADC11/PCINT19 (ADC Input Channel 11 or Pin Change Interrupt 19)
PK2 ADC10/PCINT18 (ADC Input Channel 10 or Pin Change Interrupt 18)
PK1 ADC9/PCINT17 (ADC Input Channel 9 or Pin Change Interrupt 17)
PK0 ADC8 /PCINT16 (ADC Input Channel 8 or Pin Change Interrupt 16)
![Page 181: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/181.jpg)
Port L (35-42)
Port Pin Alternate Function
PL7 –
PL6 –
PL5 OC5C (Output Compare and PWM Output C for Timer/Counter5)
PL4 OC5B (Output Compare and PWM Output B for Timer/Counter5)
PL3 OC5A (Output Compare and PWM Output A for Timer/Counter5)
PL2 T5 (Timer/Counter5 Clock Input)
PL1 ICP5 (Timer/Counter5 Input Capture Trigger)
PL0 ICP4 (Timer/Counter4 Input Capture Trigger)
![Page 182: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/182.jpg)
AVR Memories
• Memory spaces • the Data Memory
• the Program Memory
• EEPROM Memory
• In-System Reprogrammable Flash Program Memory• The ATmega640/1280/1281/2560/2561 contains 64K/128K/256K bytes On-
chip In-System Reprogrammable Flash memory for program storage
• Flash Program memory space is divided into two sections• Boot Program section and Application Program section.
![Page 183: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/183.jpg)
AVR Memories
![Page 184: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/184.jpg)
Clock distribution
![Page 185: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/185.jpg)
Peripherals
• 57 Interrputs
• 8-bit Timer/Counter0 with PWM• Timer/Counter0 is a general purpose 8-bit Timer/Counter module, with two
independent Output Compare Units, and with PWM support. It allowsaccurate program execution timing (event management) and wave generation
• 16-bit Timer/Counter (Timer/Counter 1, 3, 4, and 5)• The 16-bit Timer/Counter unit allows accurate program execution timing
(event management), wave generation, and signal timing measurement.
• Output Compare Modulator (OCM1C0A)• The Output Compare Modulator (OCM) allows generation of waveforms
modulated with a carrier frequency
![Page 186: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/186.jpg)
• 8-bit Timer/Counter2 with PWM and Asynchronous Operation
• SPI – Serial Peripheral Interface• The Serial Peripheral Interface (SPI) allows high-speed synchronous data transfer
between the ATmega640/1280/1281/2560/2561 and peripheral devices or between several AVR devices
• USART• The Universal Synchronous and Asynchronous serial Receiver and Transmitter
(USART) is a highly flexible serial communication device.
• AC – Analog Comparator• The Analog Comparator compares the input values on the positive pin AIN0 and
negative pin AIN1
• ADC – Analog to Digital Converter• The ATmega640/1280/1281/2560/2561 features a 10-bit successive approximation
ADC• The ADC is connected to an 8/16-channel Analog Multiplexer which allows
eight/sixteen single-ended voltage inputs constructed from the pins of Port F and Port K.
![Page 187: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/187.jpg)
AVR Programming
![Page 188: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/188.jpg)
Arduino Atmega 2560
![Page 189: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/189.jpg)
![Page 190: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/190.jpg)
![Page 191: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/191.jpg)
![Page 192: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/192.jpg)
Arduino/Genuino Uno Board Anatomy
1. Digital pins Use these pins with digitalRead(), digitalWrite(), and analogWrite(). analogWrite() works only on the pins with the PWM symbol.
2. Pin 13 LED The only actuator built-in to your board. Besides being a handy target for your first blink sketch, this LED is very useful for debugging.
3. Power LED Indicates that your Genuino is receiving power. Useful for debugging.
4. ATmega microcontroller The heart of your board.
5. Analog in Use these pins with analogRead().
6. GND and 5V pins Use these pins to provide +5V power and ground to your circuits.
7. Power connector This is how you power your Genuino when it’s not plugged into a USB port for power. Can accept voltages between 7-12V.
8. TX and RX LEDs These LEDs indicate communication between your Genuino and your computer. Expect them to flicker rapidly during sketch upload as well as during serial communication. Useful for debugging.
9. USB port Used for powering your Genuino Uno, uploading your sketches to your Genuino, and for communicating with your Genuino sketch (via Serial. println() etc.).
10. Reset button Resets the ATmega microcontroller.
![Page 193: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/193.jpg)
Arduino Progrmming
• Arduino programming language can be divided into three main parts:• functions,
• values (variables and constants)
• structure.
![Page 194: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/194.jpg)
values (variables and constants)Constants- data types and constants• Floating Point Constants
• floating point constants are used to make code more readable
• n = 0.005; // 0.005 is a floating point constant
• Floating point constants can also be expressed in a variety of scientific notation. 'E' and 'e' are both accepted as valid exponent indicators.
FLOATING-POINT CONSTANT EVALUATES TO: ALSO EVALUATES TO:
10.0 10
2.34E5 2.34 * 10^5 234000
67e-12 67.0 * 10^-12 0.000000000067
![Page 195: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/195.jpg)
Integer Constants
BASE EXAMPLE FORMATTER COMMENT
10 (decimal) n = 101; none
2 (binary) n = B101; leading 'B'only works with 8 bit values (0 to 255) characters 0&1 valid
8 (octal) n = 0101; leading "0" characters 0-7 valid
16 (hexadecimal) n = 0x101; leading "0x"characters 0-9, A-F, a-f valid
![Page 196: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/196.jpg)
Constants
• Constants are predefined expressions in the Arduino language• Defining Logical Levels (True and False)
• Defining Pin Levels ( HIGH and LOW )
• Defining Digital Pins modes (INPUT, INPUT_PULLUP, and OUTPUT)
• Defining built-ins (LED_BUILTIN)
![Page 197: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/197.jpg)
Conversion
• (unsigned int) • Converts a value to the unsigned int
data type• (unsigned int)x
• (unsigned long)• Converts a value to the unsigned long
data type.• (unsigned long)x
• byte()• byte(x)• (byte)x (C-style type conversion)
• char()• Converts a value to the char data type
• char(x)• (char)x (C-style type conversion)
• Float (), int (), long() • Converts a value to the float data
type• float(x)• (float)x (C-style type conversion)
• word()• word(x)• word(h, l)• (word)x (C-style type conversion)
![Page 198: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/198.jpg)
Data Types
• String()
• array
• bool
• boolean
• byte
• char
• double
• float
• int
• long
• short
• size_t
• string
• unsigned char
• unsigned int
• unsigned long
• void
• word
![Page 199: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/199.jpg)
Variable Scope & Qualifiers & Utilities
• const• It is a variable qualifier that modifies
the behavior of the variable, making avariable "read-only“
• Static• Gives the same value across the
program
• Volatile• to modify the way in which the
compiler and subsequent programtreat the variable.
• PROGMEM• It tells the compiler "put this
information into flash memory",instead of into SRAM
• sizeof()• The sizeof operator returns the
number of bytes in a variable type, orthe number of bytes occupied by anarray.
![Page 200: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/200.jpg)
Sketches
Setup ()
• The setup() function is called whena sketch starts.
• Use it to initialize variables, pinmodes, start using libraries, etc.
• The setup() function will only runonce, after each powerup or resetof the Arduino board.
Loop ()
• After creating a setup() function,which initializes and sets the initialvalues,
• the loop() function does preciselywhat its name suggests, and loopsconsecutively, allowing yourprogram to change and respond.
• Use it to actively control theArduino board.
![Page 201: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/201.jpg)
FUNCTIONS - For controlling the Arduino board and performing computations. • Digital I/O
• Reads the value from a specified digital pin, either HIGH or LOW.• digitalRead(pin), digitalWrite(pin) and pinMode()
int ledPin = 13; // LED connected to digital pin 13int inPin = 7; // pushbutton connected to digital pin 7int val = 0; // variable to store the read value
void setup() {pinMode(ledPin, OUTPUT); // sets the digital pin 13 as outputpinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop() {val = digitalRead(inPin); // read the input pindigitalWrite(ledPin, val); // sets the LED to the button's value
}
![Page 202: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/202.jpg)
pinMode(pin, mode)
• pin: the Arduino pin number to set the mode of.
• mode: INPUT, OUTPUT, or INPUT_PULLUP.
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
![Page 203: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/203.jpg)
Analog I/O
• Reads the value from the specified analog pin. Arduino boardscontain a multichannel, 10-bit analog to digital converter.
• Maps input voltages between 0 and the operating voltage(5V or 3.3V)into integer values between 0 and 1023.
• On an Arduino UNO, for example, this yields a resolution betweenreadings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit.• analogRead()
• analogReference()
• analogWrite()
![Page 204: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/204.jpg)
analogRead()
int analogPin = A3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
![Page 205: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/205.jpg)
analogWrite()• Writes an analog value (PWM wave) to a pin.
int ledPin = 9; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
![Page 206: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/206.jpg)
Delay()
int ledPin = 13; // LED connected to digital pin 13
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
![Page 207: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/207.jpg)
Time
delayMicrosecondsint outPin = 8; // digital pin 8
void setup() {
pinMode(outPin, OUTPUT); // sets the digital pin as output
}
void loop() {
digitalWrite(outPin, HIGH); // sets the pin on
delayMicroseconds(50); // pauses for 50 microseconds
digitalWrite(outPin, LOW); // sets the pin off
delayMicroseconds(50); // pauses for 50 microseconds
}
micros();unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Time: ");
time = micros();
Serial.println(time); //prints time since program started
delay(1000); // wait a second so as not to send massive amounts of data
}
![Page 208: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/208.jpg)
Maths
• abs()
• constrain()
• map()
• max()
• min()
• pow()
• sq()
• sqrt()
Characters
• isAlpha()
• isAlphaNumeric()
• isAscii()
• isControl()
• isDigit()
• isGraph()
• isHexadecimalDigit()
• isLowerCase()
• isPrintable()
• isPunct()
• isSpace()
• isUpperCase()
• isWhitespace()
![Page 209: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/209.jpg)
Random numbers
random()
• The random function generatespseudo-random numbers.• random(max)
• random(min, max)
randomSeed()
• initializes the pseudo-randomnumber generator, causing it tostart at an arbitrary point in itsrandom sequence.
• This sequence, while very long,and random, is always the same.
![Page 210: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/210.jpg)
Example
long randNumber;
void setup() {
Serial.begin(9600);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
}
void loop() {
// print a random number from 0 to 299
randNumber = random(300);
Serial.println(randNumber);
// print a random number from 10 to 19
randNumber = random(10, 20);
Serial.println(randNumber);
delay(50);
}
![Page 211: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/211.jpg)
Bits and Bytes
• bit() • Computes the value of the
specified bit (bit 0 is 1, bit 1 is 2, bit 2 is 4, etc.).
• bit(n)
• bitClear()• Clears (writes a 0 to) a bit of a
numeric variable.
• bitClear(x, n)
• bitRead()• Reads a bit of a number.
• bitRead(x, n)
• bitSet()• Sets (writes a 1 to) a bit of a
numeric variable.
• bitSet(x, n)
• bitWrite()• Writes a bit of a numeric variable
• bitWrite(x, n, b)
![Page 212: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/212.jpg)
Example - bitwrite
void setup() {Serial.begin(9600);while (!Serial) {} // wait for serial port to connect. Needed for native USB
port onlybyte x = 0b10000000; // the 0b prefix indicates a binary constantSerial.println(x, BIN); // 10000000bitWrite(x, 0, 1); // write 1 to the least significant bit of xSerial.println(x, BIN); // 10000001
}
void loop() {}
![Page 213: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/213.jpg)
Bits and Bytes
• highByte()• Extracts the high-order (leftmost) byte of a word (or the second lowest byte
of a larger data type).
• highByte(x)
• lowByte()• Extracts the low-order (rightmost) byte of a variable (e.g. a word).
• lowByte(x)
![Page 214: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/214.jpg)
External Interrupts
• attachInterrupt()
• attachInterrupt(digitalPinToInterrupt(pin),ISR, mode)
• Pins for interrupts are 2, 3, 18, 19, 20, 21
• Typically global variables are used to pass databetween an ISR and the main program. To make surevariables shared between an ISR and the main programare updated correctly, declare them as volatile.
• pin: the Arduino pin number.
• ISR: the ISR to call when the interruptoccurs; this function must take noparameters and return nothing. Thisfunction is sometimes referred to asan interrupt service routine.
• mode: defines when the interruptshould be triggered. Four constantsare predefined as valid values:• LOW to trigger the interrupt whenever
the pin is low,• CHANGE to trigger the interrupt
whenever the pin changes value• RISING to trigger when the pin goes from
low to high,• FALLING for when the pin goes from high
to low.• HIGH to trigger the interrupt whenever
the pin is high.
![Page 215: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/215.jpg)
Interrupt - Example
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
![Page 216: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/216.jpg)
Interrupt Numbers
BOARD INT.0 INT.1 INT.2 INT.3 INT.4 INT.5
Uno, Ethernet
2 3
Mega2560
2 3 21 20 19 18
![Page 217: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/217.jpg)
detachInterrupt()
• Turns off the given interrupt.
• detachInterrupt(digitalPinToInterrupt(pin))
![Page 218: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/218.jpg)
Interrupts and no interrupts
• interrupts()• Re-enables interrupts (after they’ve been disabled by nointerrupts(). Interrupts allow
certain important tasks to happen in the background and are enabled by default.
void setup() {}
void loop() {noInterrupts();// critical, time-sensitive code hereinterrupts();// other code here
}
![Page 219: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/219.jpg)
Serial
• Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART), and some have several.
BOARDUSB CDC NAME
SERIAL PINS
SERIAL1 PINS
SERIAL2 PINS
SERIAL3 PINS
Uno, Nano, Mini
0(RX), 1(TX)
Mega0(RX), 1(TX)
19(RX), 18(TX)
17(RX), 16(TX)
15(RX), 14(TX)
![Page 220: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/220.jpg)
STRUCTURE
• Control Structure• break • continue • do...while • else • for • goto• if • return • switch...case • while
• Arithmetic Operators• % (remainder)
• * (multiplication)
• + (addition)
• - (subtraction)
• / (division)
• = (assignment operator)
![Page 221: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/221.jpg)
• Comparison Operators• != (not equal to)
• < (less than)
• <= (less than or equal to)
• == (equal to)
• > (greater than)
• >= (greater than or equal to)
• Boolean Operators• ! (logical not)
• && (logical and)
• || (logical or)
• Compound Operators
• %= (compound remainder)
• &= (compound bitwise and)
• *= (compound multiplication)
• ++ (increment)
• += (compound addition)
• -- (decrement)
• -= (compound subtraction)
• /= (compound division)
• ^= (compound bitwise xor)
• |= (compound bitwise or)
![Page 222: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/222.jpg)
• Bitwise operators • & (bitwise and) • << (bitshift left) • >> (bitshift right) • ^ (bitwise xor) • | (bitwise or) • ~ (bitwise not)
• Pointer Access Operators• & (reference operator) • * (dereference operator)
• Further Syntax• #define (define) • #include (include) • /* */ (block comment) • // (single line comment) • ; (semicolon) • {} (curly braces)
![Page 223: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/223.jpg)
PWM (Pulse Width Modulation)
• used to create a square wavewith different ON and OFF time
• A call to analogWrite() is on ascale of 0 – 255
• analogWrite(255) requests a100% duty cycle (always on)
• analogWrite(127) is a 50% dutycycle (on half the time)
![Page 224: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/224.jpg)
Analog write
int ledPin = 9; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
BOARD PWM PINS PWM FREQUENCY
Uno, Nano, Mini 3, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)
Mega 2 - 13, 44 - 46490 Hz (pins 4 and 13: 980 Hz)
![Page 225: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/225.jpg)
Arduino Software (IDE)
• Sketch • Arduino program
![Page 226: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/226.jpg)
Libraries
• The Arduino environment can be extended through the use oflibraries, just like most programming platforms. Libraries provideextra functionality for use in sketches
• A number of libraries come installed with the IDE, but you can alsodownload or create your own.• LiquidCrystal
• Servo
• Stepper
• WiFi
![Page 227: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/227.jpg)
LiquidCrystal Library (#include <LiquidCrystal.h>)
• LiquidCrystal()• begin()• clear()• home()• setCursor()• write()• print()• cursor()• noCursor()• blink()
• noBlink()• display()• noDisplay()• scrollDisplayLeft()• scrollDisplayRight()• autoscroll()• noAutoscroll()• leftToRight()• rightToLeft()• createChar()
![Page 228: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/228.jpg)
LCD- Example
• LiquidCrystal (rs, enable, d4, d5, d6, d7)
• LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
• LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5,d6, d7)
• LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4,d5, d6, d7)
#include <LiquidCrystal.h>
LiquidCrystallcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
lcd.begin(16,1);
lcd.print("hello, world!");
}
void loop() {}
![Page 229: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/229.jpg)
Stepper – ( #include <Stepper.h>)
• This library allows you to control unipolar or bipolar stepper motors.
• To use it you will need a stepper motor, and the appropriate hardware to control it
• Stepper(steps, pin1, pin2)
• Stepper(steps, pin1, pin2, pin3, pin4)
• setSpeed(rpm)
• step(steps)
![Page 230: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/230.jpg)
Analog Read Serial
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
https://www.tinkercad.com/things/9Sdyv0cYKen-terrific-fyyran-fulffy/editel?tenant=circuits
10 bit A2D 0 - 1023
![Page 231: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/231.jpg)
BLINK
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
![Page 232: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/232.jpg)
Digital Read Serial
int pushButton = 2;void setup() {Serial.begin(9600);pinMode(pushButton, INPUT);}void loop() {int buttonState = digitalRead(pushButton);Serial.println(buttonState);delay(1);}
![Page 233: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/233.jpg)
FADEint brightness = 0;
void setup()
{
pinMode(9, OUTPUT);
}
void loop()
{
for (brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(9, brightness);
delay(30); // Wait for 30 millisecond(s)
}
for (brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(9, brightness);
delay(30); // Wait for 30 millisecond(s)
}
}
Generates PWM Wave with different duty cycle based on the brightness
value (0- 255)
![Page 234: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/234.jpg)
Read Analog Voltage
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.print (“ADC output :”);
Serial.println (sensorValue);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.print(“scaled down value :”);
Serial.println(voltage);
}
A2D generates integers 0 – 1023. Hence it is scaled
down to 0- 5
![Page 235: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/235.jpg)
Controlling LED using switch
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup()
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}}
![Page 236: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/236.jpg)
Debounce
const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
} } }
![Page 237: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/237.jpg)
BLINK
/* This program blinks pin 13 of the Arduino (the built-in LED) */
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
// turn the LED on (HIGH is the voltage level)
digitalWrite(13, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
// turn the LED off by making the voltage LOW
digitalWrite(13, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
![Page 238: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/238.jpg)
Pulsating LED
int ledPin = 11;
float sinVal;
int ledVal;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
for (int x=0; x<180; x++) {
// convert degrees to radians
// then obtain sin value
sinVal = (sin(x*(3.1412/180)));
//Serial.println(sinVal);
ledVal = int(sinVal*255);
Serial.println(ledVal);
analogWrite(ledPin, ledVal);
delay(25);
}
}
![Page 239: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/239.jpg)
Tone Keyboard
• to use the tone() command to generate different pitches depending on which sensor is pressed.
• tone(pin, frequency)
• tone(pin, frequency, duration)
void setup()
{
pinMode(A0, INPUT);
pinMode(8, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}
void loop()
{
if (digitalRead(A0) == HIGH) {
tone(8, 440, 100);
}
if (digitalRead(A1) == HIGH) {
tone(8, 494, 100);
if (digitalRead(A2) == HIGH) {
tone(8, 523, 100);
}
delay(10);
}tone (pin, frequency, duration)
![Page 240: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/240.jpg)
Pitches.h
![Page 241: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/241.jpg)
Ultrasonic Sensor
![Page 242: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/242.jpg)
Ultrasonic sensor
int inches = 0;
int cm = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn (echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
cm = 0.01723 * readUltrasonicDistance(7, 7);
inches = (cm / 2.54);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.println("cm");
delay(100);
}
![Page 243: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/243.jpg)
LCD
#include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 5, 4, 3, 2);void setup() {lcd.begin(16, 2);lcd.print("hello, world!");}void loop() {lcd.setCursor(0, 1);lcd.print(millis() / 1000);}
![Page 244: Module 1 - 202.62.95.70:8080](https://reader031.vdocuments.net/reader031/viewer/2022012012/61da20cc964b7c4f965ffd53/html5/thumbnails/244.jpg)
For More Details
https://www.arduino.cc/en/tutorial/foundations