GPIO & PWM Part 1 PDF

Document Details

Uploaded by Deleted User

Oakland University

O. Rawashdeh

Tags

arduino programming electronics computer science

Summary

This presentation (likely part of a course) explores GPIOs and PWM, concepts important for beginners in Arduino programming. It covers topics like using external LEDs, PWM as an analog output, and bouncing switches.

Full Transcript

GPIO & PWM Part 1 O. Rawashdeh, ECE Dept. Oakland University Exploring Arduino by J. Blum, Part 1 (Pp. 23-34) Overview GPIO = General Purpose Input/Output PWM – Pulse-Width Modulation RGB = Red-Green-Blue Topics – Using External LEDs – PWM as an analog output...

GPIO & PWM Part 1 O. Rawashdeh, ECE Dept. Oakland University Exploring Arduino by J. Blum, Part 1 (Pp. 23-34) Overview GPIO = General Purpose Input/Output PWM – Pulse-Width Modulation RGB = Red-Green-Blue Topics – Using External LEDs – PWM as an analog output – Bouncing switches 2 The Solderless Breadboard 3 Good Coding Practice: GPIOs - Avoid “magic numbers” in code - Capitalize names of constants - Add comments The “reset-state” of GPIO ports on microcontrollers is typically input mode (why?) GPIO pin modes are typically set in the setup() portion of a sketch. Example for LED on previous slide: const int LED=9; // define LED for Pin 9. Also #define LED 9 would work void setup() { pinMode (LED, OUTPUT); // Set the LED pin as an output } Adding to digitalWirte(LED, HIGH); to setup() or loop() will turn it on (and leave it on) 4 C Primer before getting into LEDs and Buttons 5 Commenting Two methods of Commenting Code // Comments are not instructions and thus do not get compiled or uploaded. The comments are to aid you and others in following the reasoning and function of you code. Always comment your code. A little investment in time will save a lot of time when troubleshooting errors. 6 Markers ; the semicolon is essential in our coding. They let the editor know where the instructions stop. They mark the next line. We use braces, { }, to encapsulate code belonging to conditional statements, loops, and procedures. If ( a == 3) { pinMode (LED, OUTPUT); // Set the LED pin as an output } Variable Declarations and Types Different variable types have different properties and memory requirements. We make declarations of variable types. In this process we assign names to point to allocations of memory. Some types of variables are not compatible with different portions of your code. Use types that agree with your instructions/functions and do not require more memory than necessary. Variable Declarations and Types - continued boolean byte char int unsigned int long unsigned long float double boolean and byte A boolean holds one of two values, true or false. Each boolean variable occupies one byte of memory. This is not ANSI/Standard/ISO C. boolean running = false; // declare a variable, one byte allocation of memory, 1 or 0 running = !running; // take the inverse or toggle the state byte LEDpin = 5; // make a one byte allocation of memory to hold a pin number. digitalWrite(LEDpin, running) // write the running Boolean to pin 5 char A data type that takes up 1 byte of memory that is often used to store a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters (i.e., strings) use double quotes: "ABC“, which can’t be used with individual chars). char myChar = 'A'; // myChar holds the ASCII code of the capital letter A char myChar = 65; // is equivalent char myChar = 0x41; // is equivalent char myChar = 0b01000001; // is equivalent int 16-bit or 2-byte variable. Ranges -32,768 to 32,767 (-2^15 to (2^15) - 1). The Arduino Duo’s int stores a 32-bit or 4-byte variables. Range: -2,147,483,648 to 2,147,483,647 (-2^31 to (2^31) - 1). Arduino, like most common processors, creates negative numbers using 2's complement math. The MSB doubles as a sign bit. This identifies if the variable is a negative number. The rest of the bits are inverted and 1 is added to the LSB. unsigned int unsigned ints are the same as ints in that they store a 2 byte value. We just get more positive values and no negative numbers. Range: 0 to 65,535 or 0 to 2^16 - 1. The Due stores a 4 byte or 32-bit variable. Range: 0 to 4,294,967,295 or 0 to 2^32 - 1. long and unsigned long Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647 Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1). float Datatype for floating-point numbers, or a number that has a decimal point. Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information. Floats have only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point. Unlike other platforms, where you can get more precision by using a double (e.g. up to 15 digits), on the Arduino, double is the same size as float. Floating point numbers are not exact, and may yield strange results when compared. For example 6.0 / 3.0 may not equal 2.0. You should instead check that the absolute value of the difference between the numbers is less than some small number. Floating point math is also much slower than integer math in performing calculations, so should be avoided if, for example, a loop has to run at top speed for a critical timing function. Programmers often go to some lengths to convert floating point calculations to integer math to increase speed. double This is a floating point number. The Uno and ATMEGA boards use 4 bytes to hold these values. Which is the same as the float. On the Arduino Duo, doubles have 8-byte (64 bit) precision. for Loops and Incremental Operations The for loop is an operation of looping governed by a counting process. A variable type is defined with a start value and a test. Finally a math operation is assigned such as add 1. We could state i++ to count up, or i-- to count down. In any iterative process we may need to have a variable hold a count. This can be performed with the following. To increment I, we could state i++, or we could state i = i + 1 To decrement I we will state I-- or alternatively we could state i = i -1 For Loop Example 18 Blinking LED using for Loop (Listing 2-2) const int LED=9; //define LED for Pin 9 void setup() { pinMode (LED, OUTPUT); //Set the LED pin as an output } void loop() { for (int i=100; i

Use Quizgecko on...
Browser
Browser