Arduino Programming Guide PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an overview of Arduino programming, including its structure, coding, and functions. It covers fundamental concepts like variables, constants, and input/output operations.
Full Transcript
# Arduino - An open-source electronic platform based on easy-to-use hardware and software. - It can be interfaced with a lot of extended modules of different functionality. ## Functions: - Can Read Input - Process Data - Produce Output ## Arduino Coding Structure - **Global:** Includes important...
# Arduino - An open-source electronic platform based on easy-to-use hardware and software. - It can be interfaced with a lot of extended modules of different functionality. ## Functions: - Can Read Input - Process Data - Produce Output ## Arduino Coding Structure - **Global:** Includes important variables, constants, and imported libraries - **Setup:** - Activates the pins and sensors used - It only runs once - **Loop:** This part of the code runs continuously (reading sensors and turning pins HIGH or LOW) ## Rules and Building Blocks - **General:** - Every line must end with a semicolon unless it's a conditional, loop, function or imported library. - Comments start with a '//' - Comments are text that the program ignores - It is also used to label and explain the code ## Constant and Variables: - **Variables:** - It stores different types of data - It change what data they hold through mathematical operations - **Constants:** - It cannot change their value after it has been assigned ### To assign a constant variable: - "constant int x" - Arduino Output Programming - Individual Pin Addressing: - Using pinMode() - Syntax: pinMode(pinNo, INPUT/OUTPUT) - Port Addressing: - Using DDRx (Data Direction Register) - Syntax: DDRB = Values ### Note: - Values that are in output pins are 1 and the values for input pins are 0 ## Parts of Arduino - USB Port - Barrel Jack - Ground/Grnd - 5v Source - 3.3v Source - Analog Port/Pins (Port C) - Digital I/O Ports/Pins (Port B) - Digital I/O Ports/Pins (Port D) - Aref - Reset - Power ON LED - RX/TX LED - ATMEGA Microcontroller - 5v Voltage Regulator - 7/8 - 000000 - 21098 76 - ARDUINO - 0 - 0 - 12 - IOREF - RESET - - - DIGIT - U - 00 - WWW.ARDU - POWER O - 000 - 543 - 1 - 14 - 2> - 501L - 10 - 9 - Resel - 0 - SPK16.000G - 57 - 346 - ZW - 0 - AREF - GND - TX - RX - 34 ## Example: - Output Pins: 0,2,4,6,8,10,12 - Input Pins: 1,3,5,7,9,11 - DDRB = B00010101; - DDRD = B01010101; ## Arduino Digital Pin Port Pin Port - 0-7 PDO - PD7 D - 8-13 PBO - PB5 B - A0 - A5 PCO - PC5 C ## Digital Output and Delay ### Syntax: - digitalWrite(pin, value) ### Parameters: - Pin: pin number - Value: HIGH or LOW ### Returns: - none ## Port Addressing: ### Syntax: - PORTD = B00000000; ### Parameters: - PORTD: Variable - B00000000: Pin Value ### Returns: - none # ATmega328 - Also known as AVR CPU - Uses Harvard Architecture ## Characteristics: - **Flash:** Program Memory (Read-only) - 32K - 15-bit address - Non-volatile - Allocates data to Flash using PROGMEM keyword - **SRAM:** Data Memory - 2K - For temporary values, stack, etc. (data) - Volatile - Limited Space - **EEPROM:** - For long-term data - On I/O data bus ## AVR CPU - Function: Instruction Fetch and Decode ## AVR Register File - 32 8-bit GP registers - Part of SRAM memory space ## Special Addressing Registers - X, Y and Z registers - 16-bit registers made using registers 26-31 - Supports Indirect Addressing ## AVR Memory - Program Memory - Flash - Data Memory - SRAM ## Stack Pointer Register - Special register in I/O space [3E, 3D] - Enough bits to address data space - Initialized to RAMEND (address of highest memory address) - Program Status Register (PSR) - Status bits set by instructions/Checked by Branch/Skip Instructions - Register Symbols: (?) - I-Global Interrupt Enable - T - Flag Bit - H - Half Carry (BCD arithmetic) - S - Sign - V- Overflow - N- Negative - Z-Zero - C - Carry ## AVR Architecture - **Characteristics:** - Have Three timers - It is very flexible - Choose clock rate - Choose "roll-over" value - Generate Interrupts - Generate PWM (Pulse Width Modulation) Signals - Represent 8-bit value using a clock signal - It interfaces to pins - Each pin is directly programmable - Program direction - Program value - Program pull-ups ### Additional Notes: - Use ATmega328 if you are going to incorporate many components. - Remove the pins on 0 and 1 to upload because it might cause errors due to the presence of the bootloader on the board. - **EEPROM:** It saves data to avoid loss during power outage - **Voltage Regulator:** Regulates the voltage input ## Microcontrollers pt.2 - **Instruction Set Architecture (ISA):** - Describes the aspect of a computer architecture visible to programmer. - Specification of the set of all binary codes, OP codes that are the native form of commands implemented by a particular CPU design. ### Two Common Types of ISA: - Complex Instruction Set Computer (CISC) - Reduced Instruction Set Computer (RISC) ### Microcontroller: - Has CPU inside with a fixed amount of RAM, ROM, and other peripherals all embedded on a single chip. ### Microprocessor: - Has only the CPU inside them i.e. the processing powers. They don't have RAM, ROM, and other peripherals on the chip. ### Advantages of Microcontroller: - Inexpensive (student projects) - Easily replaceable - Development tools are typically free - I/O ports are easily accessible - Easily interface to other electronic circuits or devices ### History of Microcontrollers: - **1970-1971:** Gary Boone, a Texas Instruments Engineer, created the first microcontroller (TMS1802NC). - **TMS1802NC:** A single-chip microcontroller with 5,000 transistors, 3,000 bits of program memory, and 128 bits of random access memory. - **8048-49 Microcontroller:** - Produced by Intel. - Its onboard memory consisted of 1 kbyte of ROM and 64 bytes of RAM, which was quickly doubled to 2kbytes and 128 bytes on a new version, 8049. - It could also access 4 kbytes of external ROM. - It had two counters/timers that could be used for counting incoming pulses as well as providing time delays. - **Z8:** - Produced by Zilog. - It had two counters/timers that could be used for counting incoming pulses as well as providing time delays. - It had 2 kbytes of ROM and 128 bytes of RAM that it called general purpose registers. - It had the ability to access 64 kbytes of external ROM and 64 kbytes of external RAM. - **MC6801:** - Produced by Motorola - It has 2kbytes of ROM and 128 kbytes of RAM together with a timer and UART. - Its instruction set was compatible with the MC6800 with a glimmer of 16-bit arithmetic creeping in. - **R6500:** - Launched by Rockwell - A 6502 microprocessor with the addition of 2 kbytes of ROM and 64 kbytes of RAM, - The internal RAM had a separate power supply in the form of a battery so that the data wasn't lost during a power failure. - It also included a universal asynchronous receiver/transmitter (UART) ### Hierarchy of Memory - **Extended Memory Hierarchy:** - CPU - Registers - Cache - RAM - Hard Disk - Off-Line Storage - **Typical Computer Block Diagram:** - Address Bus - CPU - Control Bus - Memory - Data Bus ### Von Neumann vs. Harvard Architecture - **Von Neumann Architecture:** - Main Memory - CU - ALU - **Harvard Architecture:** - Instruction and data are in same memory: One shared memory for instructions and data with one data bus and one address bus between processor and memory. - Slower but more flexible ## Microcontrollers pt.1 - **Microcontroller:** - It is a single chip microcomputer made through VLSI fabrication. - It is also called as Embedded Controller because the microcontroller and its support circuits are often built into, or embedded in, the devices they control. - It is available in different word lengths like microprocessors. - An integrated system that is designed to operate as an embedded computing system. - 4-bit, 8-bit, 16-bit, 32-bit, 64-bit, and 128-bit microcontrollers are available today. - A computing system is a computer which is a part of a larger system. ### Parts of a Microcontroller: - **Microprocessor (CPU):** - Central Processing Unit of the system - **Read Only Memory (ROM; for the program):** - For the program - **Random Access Memory (RAM; for the data):** - For the data - **I/O Ports:** - To communicate/interface with external resources - **Peripheral Devices:** - To make easier the interfacing and implementation of the desired functionalities ### Common Applications: - **Consumer:** - Washing Machine - Remote Control - Clocks and Watches - Games and Toys - Audio/Video - **Communications:** - Telephone Systems - Answering Machine - Cell phones and pagers - Networking (ATM, Credit Cards, Ethernet) - **Automotive:** - Safety Devices (Automatic Braking System, Airbag) - Motor Control (Ignition, Exhausts) - Power Windows and Seats - Instrumentation - **Military:** - Guidance System - Global Positioning System (GPS) - Target Recognition Systems - **Industrial:** - Traffic Control - Robotics - Production Plants - Inventory and Stock Management - **Medical:** - Cardiac Monitors - Renal Monitors - Pacemakers - Dialysis Machines ### Microcontroller Functional Units: - **Central Processing Unit (CPU):** - Exists in 4,8,16,32 bit data bus - **Read Only Memory (ROM):** - Also called as Firmware ### Microcontroller Organization Diagram: - crystal - vec - GND - CPU - RAM - timing& contrl - MEMORY - ROM/EPROM/ special functioning block - ocscillato r circuit - ADC - timers/counters interrupt ### Parts of Microcontroller Organization Diagram: - **Central Processing Unit (CPU):** - The brain of the microcontroller. - It is responsible for fetching, decoding, and executing instructions. It also connects every part of the microcontroller in a single unit. - 4,8,16,32 bit data bus - **Memory:** - It is used to store data and program. - **Types:** - Read Only Memory (ROM) - Firmware - Random Access Memory (RAM) - **Applications:** Register File, Processor Stack, Temporary Data - **Parallel I/O (PIO):** - These are used to drive/interface various devices such as printers, memories, etc. to a microcontroller. - Examples: Relays, Sensors - **Serial Ports:** - These ports provide various serial interfaces between other peripherals like parallel ports. - **Timers/Counters:** - These provide all timing and counting functions inside the microcontroller. - The major operations of this section are performed such as modulations, pulse generations, frequency measurements, etc. - They can also be used for counting external pulses. - **Analog to Digital Converter (ADC):** - It is used for converting the analog signals to digital format. - The input signal in this converter should be in analog form (voltage or current) and the output from this is in digital form. - The digital output can be used for various digital applications such as measurement devices. - **Digital to Analog Converter (DAC):** - Performs the reversal operation of ADC conversion. It converts the digital signal into analog format. ### Additional Information about Microcontroller Organization Diagram parts: - **Random Access Memory (RAM):** - Register File, Processor Stack, and Temporary Data - **Parallel I/O:** - Examples of this are relays and sensors - **Interrupt Inputs:** - External/Internal Sources - **Universal Asynchronous Receiver Transmitter:** - Example: RS232 - **General Purpose Timer:** - Optional Event Counter - **Pulse Width Modulator:** - Motor Controller - **Watch Dog Timer:** - For automatic reset - **Analog I/O:** - Examples are ADC and DAC - **Interrupt Control:** - It is used for providing interrupt or delay for a working process. - The interrupt may be external (activated by using interrupts) or internal (by using interrupt instructions during programming). ### Additional Notes: - A microcontroller has a certain amount of RAM and ROM (EEPROM memories for storing program source codes. - A microcontroller may have more than one timer and counters. # History - **1969:** Team of Japanese Engineers from the BUSICOM company arrived to United States with a request that a few integrated circuits for calculators be made using their projects. - **Intel:** The company which handled the project. - **Marcian Hoff:** - Responsible for the project. - Suggested a fundamentally different solution instead of the traditional way of building calculators which used special, integrated circuits, but instead, had the program stored in it that leads to the development of the 1st microprocessor - **Frederico Faggin:** - Helped in transforming the idea into a ready-made product in few months, in making a product from its first conception. - Works in INTEL. - Soon leaves Intel and started his own Zilog Inc. - **1971:** Intel bought the license from the BUSICOM Company. Intel obtained the rights to sell this integral block and it appeared on the market as a microprocessor called 4004. - **April of 1972:** 8008 Microprocessor appeared on the market. - **Intel 4004:** - First microprocessor - 4-bit microprocessor - 6,000 operations/s - **American Company CTC:** Requested INTEL and Texas instruments to make an 8-bit microprocessor. - **8008 Microprocessor:** - It can address 16KB of memory. - It has 45 instructions and a speed of 300,000 operations/s. - **April of 1974:** 8080 Microprocessor appeared on the market with a starting price of $360. - **8080 Microprocessor:** - Can address 64 KB of memory. - It has 75 instructions. - **Motorola:** Put out an 8-bit microprocessor known as 6800 and along with that the company started making other peripherals such as 6820 and 6821 etc. - **Chuck Peddle:** - Chief constructor of 6800. - Leaved Motorola to join MOS Technology and kept working in the development of microprocessors. - **1975:** A critical event took place in the history of Microprocessors in WESCON exhibit in US. - **MOS Technology:** Announced it was marketing microprocessors 6501 and 6502. This announcement made the companies to immediately purchase them for $25. - **6502:** - 8 - bit microprocessor with 56 instructions. - Can address 64KB of memory. - It was installed into computers such as: - KIM-1 - Apple I - Apple II - Atari - Comodore - Acorn - Oric ### Note: - Several makers of 6502 soon appeared: - Rockwell - Sznertek - GTE - NCR - Ricoh - **1975:** Zilog announced the Z80. - **Z80:** - Compatible with 8080. - Can address directly 64 KB of memory. - 176 instructions. - Large number of registers. - Built in option for refreshing the dynamic RAM Memory. - Single-supply. - Have greater speed of work. ### Note: - Z80 was the heart of many computers: - Spectrum - Partner - TRS703 - Z-3 - Other new manufacturers: - Mostek - NEC - SHARP - SGS - **1976:** Intel came up with an improved version of 8-bit microprocessor named 8085. However, Z80 was so much better that Intel soon lost the battle. ## Microprocessor Timeline # Introduction to Microprocessor ## Microprocessor - It implements the function of Arithmetic Logic Unit(ALU) and Control Unit (CU) in a single package. ## Functions: - Data Processing - Control System ## Major Parts of Microprocessor - **Arithmetic Logic Unit:** - Performs mathematical and logical operation. - **Register:** - Internal memory having enough bits to store one data word. - Also called as small processor - Helps in keeping the other parts to work together and in the right time sequence. - **Control Logic:** - Keeps track the address of the next instruction to be executed. ## Six Basic Register - **Program Counter:** - Keeps track the address of the next instruction to be executed. - **Accumulator:** - Buffer Register - Stores the intermediate answers during the execution of the program. - **Memory Address Register:** - Holds the address of the instruction that is being executed. - **Status Register:** - Stores the results of a certain test performed during the execution of the program. - Keeps track of the certain facts about the outcome of the operations (arithmetic or logical) in the system. - **Temporary Register:** - Called as ALU's accumulator latch. - **General Purpose Register:** - Storage area of data ## Block Diagram of Microprocessor - ALU - REGISTERS - CU ## Microprocessor Architecture: - MEMORY - STATUS REGISTER - ALU - TEMPORARY IN REGISTER - CU - REGISTER - INSTRUCTION REGISTER - REGISTER - REGISTER - REGISTER - OUT - IN - TEMPORARY REGISTER - INSTRUCTION DECODER - PROGRAM COUNTER - CONTROL LOGIC - ACCUMULATOR ## Microprocessor-Based vs. PC-Based - **Microprocessor-Based:** - MEMORY - MORV - Y - MICROPROCESSOR - **PC-Based:** - I - P - M - E - M - O - R - Y - P - R - O - C - E - S - S - O - R - I ## Additional Notes: - **Programmable Peripheral Interface (PPI):** - It is a general purpose programmable I/O device with a purpose of connecting the CPU with external devices such as ADC, DAC, keyboard and others. - It is also compatible with almost any microprocessors. - The Programmable Peripheral Interface is connected to the Motherboard. - When considering using a microprocessor in research, use mini pc or Jetson Nano for its efficiency in detection. - Diagram: - POWER - SUPPLIES - +5V - GND - GROUP A - CONTROL - GROUP A - PORT A - (8) - 1/0 - PA7-PAO - 0 - GROUP A - BI-DIRECTIONAL - DATA BUS - UPPER (4) - D7-D0 - DATA BUS - BUFFER - 8-BIT - GROUP B - INTERNAL - DATA BUS - PORT C - 1/0 - PC7-PC4 - LOWER (4) - READ - RD - WR - WRITE - A1 - GROUP B - CONTROL - CONTROL - LOGIC - RESET - R - GROUP B - PORT B - 1/0 - PB7-PBO - (8) - CS - C - M - U - T - E - R - INTERFACE - PERIPHERA] - EQUIPMENT - PC-BASED DIAGRAM The document also includes several diagrams and images, but these cannot be included in the markdown. This information is either in the text or could be inferred from the text to be included in the formatted markdown response.