Arduino 平台与C语言程序设计-week3-Arduino Programs-Lesson3
2015-12-06 23:03
453 查看
This series of articles are the study notes of "An Arduino platform and C Programming", by Prof. Harris, Department of Computer Science, University of California, Irvine. This article is the notes of week 3, Arduino Programs, lessen 3.
3.1 Lecture3-1:
We've been talking about pins and how they can be inputs and outputs, and be digital and analog. And so now, we're going to talk about how you do that from the code. So what code do you need to write, what function calls do you need to make in order to use
the pins as inputs and outputs, in order to use them as digital and analog.
the analog pins. Analog pins, they can be inputs but they can't be outputs, but the standard digital pins, they can be input and output. So, before you use the pins, you have to select if the pin is going to act as an input
or an output. And you should do this for all the pins that you're going to use. Before you try to read from it or write to it, you have to assign it to be an input or an output.
Now if you don't do this, something will happen. It will have a default value. So, by default, the pin will either be an input or output, and you should never rely on the default value.
Before you access the pins, you should call this function, this
pinMode function, in order to assign the pin to either an input or output. It's a library function. It takes two arguments, pin and mode. The job of this is to set a pin to be either an input
or an output.
Pin is the number of the pin
0 - 13 for the digital pins
A0 - A5 for the analog pins
Mode is the I/O mode the pin is set to
INPUT, OUTPUT or INPUT_PULLPUT
INPUT_PULLPUT acts as input with reversed polarity
Basically what that does, is it makes the pin act like an input, but it reverses the polarity. So, when they drive zero volts on it, when they drive it low, you read it as a high. Or when they drive a high, you read it as a low. So it's reverse polarity input.
Returns the state of an input pin
Returns either LOW (0 volts) or HIGH (5 volts)
Example:
int pinval;
pinval = digitalRead(3);
pinval is set to the state o digital pin 3.
Let's say we're talking about the digital pins, and we've assigned this pin to be an input. We used
pinMode, we made pin zero and input. Now, we wanna, in our code, actually read from the pin, read a value from the pin. So, for that, use this function called
digitalRead. And it takes an argument, one argument, the pin. And it just returns the state of the pin, which is either low or high. Now that note that it returns an int, so if you look at the example, I say I create an int
pinval. I say pinval = digitalRead(3). So what that does, is it reads the state of pin 3, assumes it's either 0 volts or 5 volts and returns low if it's 0 volts, high if it's 5 volts. Now notice that pinval is an int, so
you can interpret that as a 0 and a 1. 0 meaning low, 1 meaning high.
Assigns the state of an output pin
Assigns either LOW (0 volts) or HIGH (5 volts)
Example:
digitalWrite (3,HIGH);
Digital pin 3 is set HIGH (5 volts)
And then if you want to write to a pin, you call digitalWrite. That takes two arguments, the pin, and the value that you want to write to it. And the value has to be either low or high, which would be zero or one. Zero volts, five volts. So, it assigns state
of the pin. So if you say digitalWrite like I do here, digitalWrite (3, HIGH) it's going to set pin 3 to high, which would be 5 volts.
Returns the state of an analog input pin
Returns integer from 0 to 1023
0 for 0 volts, 1023 for 5 volts
Example:
int pinval;
pinval = analogRead (A3);
Pin must be an analog pin.
Now, for the analog pins, remember the analog, we can't drive analog output data. We can't take a pin and make it an analog output, but we can read analog input data of the analog pins in any rate. Now, what actually happens is that the analog value, remember
our processor, it is a digital processor. So, it really needs to see digital values. But we want to be able to read analog values, so the microprocessor inside it, has an analog to digital converter. Which, when you call this function
analogRead, it reads the analog value off of the pin and then converts it to digital using the analog to digital converter and then our code gets the result.
So, what happens is, analogRead returns the state of the analog input pin, but
it returns an integer from 0 to 1023. Now 0 is the bottom of the range, so if you've got 0 volts on this pin, it's going to return you a 0. And the top of the range is 5 volts. So if it sees 5 volts then it's going to return
you a 1023.
Pauses the program for milliseconds (ms)
Useful for human interaction
Example:
digitalWrite (3,HIGH);
delay (1000);
digitalWrite (3,LOW);
Pin 3 is HIGH for 1 second
And this user interface has to be slowed down, right? It has to be at the speed that a human can perceive it, right? So, you can't just output some data and expect the human to read it in one clock cycle and then change it. If you want the human to see something,
to hear something, it has to be present for a certain amount of time, or else the human can't even perceive it.
File > Examples > 01. Basics > Blink
Causes an LED to blink
LED is built-in, no wiring required
So what happens in the Arduino, this Blink example, it blinks and one of the LEDs built into the Arduino. And so there's an LED built into the Arduino uno that is connect to pin 13 and actually is right next to pin 13.
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
3. Lesson 3
3.1 Lecture3-1:
Input andOutpu
We've been talking about pins and how they can be inputs and outputs, and be digital and analog. And so now, we're going to talk about how you do that from the code. So what code do you need to write, what function calls do you need to make in order to usethe pins as inputs and outputs, in order to use them as digital and analog.
3.1.1 Input/Output (1/O)
(1) These functions allow access to the pins
void pinMdoe(pin,mode);(2) Sets a pin to act as either an input or an output
Some of the Pins can be inputs or outputs. Not all the pins, some of the pins can't be inputs, or can't be outputs. For instance,the analog pins. Analog pins, they can be inputs but they can't be outputs, but the standard digital pins, they can be input and output. So, before you use the pins, you have to select if the pin is going to act as an input
or an output. And you should do this for all the pins that you're going to use. Before you try to read from it or write to it, you have to assign it to be an input or an output.
Now if you don't do this, something will happen. It will have a default value. So, by default, the pin will either be an input or output, and you should never rely on the default value.
Before you access the pins, you should call this function, this
pinMode function, in order to assign the pin to either an input or output. It's a library function. It takes two arguments, pin and mode. The job of this is to set a pin to be either an input
or an output.
Pin is the number of the pin
0 - 13 for the digital pins
A0 - A5 for the analog pins
Mode is the I/O mode the pin is set to
INPUT, OUTPUT or INPUT_PULLPUT
INPUT_PULLPUT acts as input with reversed polarity
Basically what that does, is it makes the pin act like an input, but it reverses the polarity. So, when they drive zero volts on it, when they drive it low, you read it as a high. Or when they drive a high, you read it as a low. So it's reverse polarity input.
3.1.2 Digital Input
int digitalRead (Pin)Returns the state of an input pin
Returns either LOW (0 volts) or HIGH (5 volts)
Example:
int pinval;
pinval = digitalRead(3);
pinval is set to the state o digital pin 3.
Let's say we're talking about the digital pins, and we've assigned this pin to be an input. We used
pinMode, we made pin zero and input. Now, we wanna, in our code, actually read from the pin, read a value from the pin. So, for that, use this function called
digitalRead. And it takes an argument, one argument, the pin. And it just returns the state of the pin, which is either low or high. Now that note that it returns an int, so if you look at the example, I say I create an int
pinval. I say pinval = digitalRead(3). So what that does, is it reads the state of pin 3, assumes it's either 0 volts or 5 volts and returns low if it's 0 volts, high if it's 5 volts. Now notice that pinval is an int, so
you can interpret that as a 0 and a 1. 0 meaning low, 1 meaning high.
3.1.3 Digital Output
void digitalWrite(pin, value);Assigns the state of an output pin
Assigns either LOW (0 volts) or HIGH (5 volts)
Example:
digitalWrite (3,HIGH);
Digital pin 3 is set HIGH (5 volts)
And then if you want to write to a pin, you call digitalWrite. That takes two arguments, the pin, and the value that you want to write to it. And the value has to be either low or high, which would be zero or one. Zero volts, five volts. So, it assigns state
of the pin. So if you say digitalWrite like I do here, digitalWrite (3, HIGH) it's going to set pin 3 to high, which would be 5 volts.
3.1.4 Analog Input
int analogRead (Pin)Returns the state of an analog input pin
Returns integer from 0 to 1023
0 for 0 volts, 1023 for 5 volts
Example:
int pinval;
pinval = analogRead (A3);
Pin must be an analog pin.
Now, for the analog pins, remember the analog, we can't drive analog output data. We can't take a pin and make it an analog output, but we can read analog input data of the analog pins in any rate. Now, what actually happens is that the analog value, remember
our processor, it is a digital processor. So, it really needs to see digital values. But we want to be able to read analog values, so the microprocessor inside it, has an analog to digital converter. Which, when you call this function
analogRead, it reads the analog value off of the pin and then converts it to digital using the analog to digital converter and then our code gets the result.
So, what happens is, analogRead returns the state of the analog input pin, but
it returns an integer from 0 to 1023. Now 0 is the bottom of the range, so if you've got 0 volts on this pin, it's going to return you a 0. And the top of the range is 5 volts. So if it sees 5 volts then it's going to return
you a 1023.
3.2 Lecture3-2: Blink Example
in this set of slides, we'll talk about the blink example, a piece of code, a very simple piece of code that everybody needs to run on their Arduino to sanity check it, to make sure everything's working.3.2.1 Delay
void delay(msec);Pauses the program for milliseconds (ms)
Useful for human interaction
Example:
digitalWrite (3,HIGH);
delay (1000);
digitalWrite (3,LOW);
Pin 3 is HIGH for 1 second
And this user interface has to be slowed down, right? It has to be at the speed that a human can perceive it, right? So, you can't just output some data and expect the human to read it in one clock cycle and then change it. If you want the human to see something,
to hear something, it has to be present for a certain amount of time, or else the human can't even perceive it.
3.2.2 Blink Example
Blink is the generic simple example for embedded systems: Like “hello, world”File > Examples > 01. Basics > Blink
Causes an LED to blink
LED is built-in, no wiring required
(1) the LED connect to pin 13
So what happens in the Arduino, this Blink example, it blinks and one of the LEDs built into the Arduino. And so there's an LED built into the Arduino uno that is connect to pin 13 and actually is right next to pin 13.
(2) Blink Sketch
// the setup function runs once when you press reset or power the boardvoid setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
3.3 Video Demonstration: Arduino Blink Example
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 如何组织构建多文件 C 语言程序(二)
- 关于指针的一些事情
- 如何写好 C main 函数
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua和C语言的交互详解
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C++变位词问题分析