Arduino 平台与C语言程序设计-week2-C Programming-Lesson1

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 2, C Programming, lessen 1.

week2 - C Programming

1. Lesson 1

1.1 Lecture1-1: Setting Up Your Environment

So this entire module is about the C programming language. C, C++. The reason for this is because Arduino is programmed in C++. Really, C but a little bit of C++.

1.1.1 Getting Started

I'm already assuming that you're using a desktop or a laptop. So I just want to describe for you how you would set up your desktop, laptop environment in order to run these programs to work with the programs that we're going to talk about. So we've already talked about the Arduino IDE, but that won't be useful for instance this program, where it prints hello, world, because that print doesn't work. The printf statement doesn't work properly on an Arduino because there's no screen.

<div style="text-align: justify;"><span style="font-family: Arial, Helvetica, sans-serif;">#include <stdio.h></span></div>Main(){
<div style="text-align: justify;"><span style="font-family: Arial, Helvetica, sans-serif;">	printf(“hello, world\n”);</span></div><div style="text-align: justify;"><span style="font-family: Arial, Helvetica, sans-serif;">}</span></div>

  • Print “hello, world” to the screen
  • Type this in with a text edit or and save it as hello.c

1.1.2 Running a Program

Youwill need a text editor and a compiler

  • Debugger will be needed later

Iuse GNU tools (free, open source)

Canrun on Windows but MacOS and Linux are easier

Eclipse Integrated Development Environment (IDE)

  • Puts all tools together in anice graphic user interface
  • Need Java Runtime Environment (JRE) to run it
  • http://tinyurl.com/12uo3g
  • can also use Microsoft Visual Studio (not free)

Eclipse is a common one you can use anyone you want but you can use eclipse its a common one open source now the difference is see the way I write my code I do everything with the command line, so I'll go to my text editor, type in the code, save the file. Then I'll go to the command line and say type in the command, the compile command directly. Gcc, give the name of my file. And I'll show you that in the next slide. So that's a way I do it, but a lot of people like to use these IDE's. Like the Arduino IDE that packages everything in one tool. So you type the codeinto the IDE and in the same IDE you click a button that compiles the code orin Eclipse it's called building the code. So people like to use IDEs like Eclipse and you can download that and use that too. That's fine. It puts all the tools together in one nice GUI, nice graphic user interface, just like Arduino on IDE. You need to run Java Virtual Environments. So, Java Runtime Environment, you have to have Java installed on your machine, because the Eclipse IDE is written in Java But, that's also true of the Arduino IDE, so shouldn't  be a problem.

1.2 Lecture1-2: Hello World

1.2.1 Breaking Down Hello.c

(1) #include <stdio.h>

  • Tells the complier to use the library function describe instdio.h
  • pritntf (the print function) is inside stdio

So to look at hello world. It starts off at the top with an include statement. Hashtag include standard IO, stdio.h. So hashtag include. Anything with a hashtag in front of it is what's called acompiler directive. It's not really a piece of code. It tells a compiler to do something special, and include tells the compiler to include whatever the file is to the right. So hashtag includes to stdio.h says take this file stdio.h and includeit right here in the code. So it just basically, it cuts and pastes. It takes that stdio.h and pastes it to the beginning of this file.

stdio.h is a library, standard library. Inf act, it stands for standard IO. And standard IO library, it has all the standard IO, input/output functions, like printf for printing. So if you ever wanna do printf you've going to include the standard IO library.

The point is that if you want to include a library, use alibrary, then you have to include it at the top, include at least it's header fileat the top of your code. So, stdio, if you want to use printer function for printing, you've got to include stdio.h. So, you do that and that's at the top. And you can include any number of libraries depending on what type of coding you're doing, but we'll stick with the standard library. So standard io.h, you include that at the top.

(2) .h

Now, the .h file, what that does is it just takes all the library functions and defines them. It doesn't define all the library functions; it defines their inputs and outputs. It doesn't have all the code for the, but it says each function, here's what it takes. One input, one output, something like that. It lists that. Gives what are called function prototypes.

(3) main()

Now inside the program, after you've done the“include”, you have themain function. So there's a function defined called main. And we'll talk in more detail about functions a little bit. But the main function, it starts out with this main, open parenthesis, closed parenthesis.This is where all the execution starts. So when it sees that main it says okay, this is where I want to start running my code. So every C program, or C++ program, has to have a main in it.

(4) { }

  • Curly brackets group lines of code
  • All functions start and end with curly brackets

After the main, there's an open curly bracket, then there's a little code, and then there's a closed curly bracket. So curly brackets are used to groups statements together into what are called scopes. So, but the idea here is that if there's a function, so the main, it's a list of instructions. In our case, it's one instruction. But you could have many instructions inside your main function. All of them have to be grouped together in curly brackets. So you have an open curly bracket, then you have all your instructions, and then you have a close curly bracket. And then all those instructions in between the curly brackets are part of main.

(5) printf(…);

  • Prints to the screen
  • The argument is in parenthesis
  • The argument is what is printed
  • Note the semicolon at the end

Now inside main function, we have a printf, print statement. And that just basically prints to the screen by default. Inside the parenthesis, you put inquotes whatever you want to print. So in our case, we want to print the word, the phrase, hello world. So you say printf and then in parenthesis, you have quotes Hello world, closed quotes. And there's a semicolon at the end of the print statement, after the print statement, that's standard in C. These traditional statements, assignment statements and so for the function calls, you put a semicolon at the end after, at the end of your statement so that C knows that the line has ended.

So say you got a program that is 20 lines long. You can put them on the same physical line in your text editor. It couldbe never hitting Return. You could just have one long line with 20 statements. As long as you put semicolons between them, C will execute it just fine.

(6) “\n”

So, in quotes in the original main, we said, "hello, world\n". So there's, if you'll notice the \n it's a special character. So hello world is clear. It prints the word hello world. Then the \n is a carriage return, a new line. So whenever it sees a \n, it goes to the next line

With Arduinos, you can but there's generally no need since you're not printing anything, right. You're not having a screen generally, so it's not something we worry about. But \n is useful, because carriage return is something you often want to print. And even in the Arduino, when we start using a serial interface, we'll be printing stuff to the screen for diagnostics and debugging, and stuff like that. And we'll want to put carriage returns in there so that we can read the text.

1.3 Lecture1-3: Variables

In this lecture, we'll talk a little bitmore about variables. Variables are key, variables just like in algebra. Youneed variables to write code and we'll talk about variables and how they can bedefined and what they mean.

1.3.1 Variables

  • Names that represent values in the program
  • Similar to algebraic variables
  • All variables have a type which must be declared

int x;

float y;

  • Type determines how arithmeticis performed, how much memory space is required

(1) Type

All variables have a type, which must be declared. So type is an important concept in C and in most languages. The type of a variable tells you, basically, it intuitively tells you what kind of datait is going to hold.

The type is important, because it determines how arithmetic is performed and it determines how much memory is going to be used to store the variable. Because remember, all of these variables have to be stored in memory somewhere. And so the question is with a variable like x, how many bytes of space do we needto store that information? And with y, how many bytes of information? Because the machine when it's running, it has to store it, it has to know how big it's going to be. So that's what the declarations are for.

If you say, int x, x is going to be the size of an integer, whatever that is on your machine. A standard 32 bit machinethe integer is going to be 4 bytes. Depends on the processor, but 4 bytes is standard for a 32-bit machine. Floating point might be eight bites something like that or I can't remember. Maybe eight, maybe four, maybe eight, but there are fixed sizes depending on the machine for each type. So one thing that typel ets you know is how much memory is going to be needed? But also the type tells you, how arithmetic will be performed? So arithmetic is performed differently depending on the type of data.

So, integer. If you have an integer type and you want to do addition, everybody knows how to do integer addition, but floating point addition is a much more complicated thing. So if you want to do floating point addition, actually what happens inside the hardware, inside the microprocessor is there's often a floating point unit, a dedicated piece of hardware, just there to do floating point operations. And it is separate than the integer operators, so you'll have an integer ALU, arithmetic logic unit, which performs integer addition and then you'll have some floating point unit that does floating point operations. And so depending on the type, you're going to have to send that data to a different piece of hardware to do the operations that need to be performed.

(2) Declare

You need to know what type and all the variables are going to be and so in C, you have to declare the type. So before you use a variable you have to declare it. So before you can use x in your program, you have to say int x; somewhere or float y; something like that. So that's what variables are, you've going to declare them in C.

1.3.2 Types and Type Qualifiers

  • Several built-in types, different sizes

  • Type qualifiers exist: short, long
  • Char is 8 bits on all platform

There are several built in types, here are the main  ones we're going to look at. Char, int, float and double. Really, we're going  to focus on probably char and int. When it comes  to embedded systems, we try to  avoid floats and doubles, because floating point arithmetic and double worth or  double is double precision point. It's slower and bigger, so we try to avoid  them in general. Sometimes you need it, but try to avoid them. So an int is typically whatever the word size is, but it could be down to 16. So for instance with an Arduino, I believe an int is 16 bits, but a char. No matter what platform you're using, a char is only 1 byte. That's just a standard size. So actually, it's very useful. It's nice. When I'm coding, you use a char and you always know what you're going to get. So, I often use a char by default. But floating point and double, they can be 64 bits, 128. They can be much longer.

1.3.3 Variable Names

  • A sequence of visible characters
  • Must start with a non-numerical character

int testvar1

× int 1testvar

  • No C language keywords

× if, else, while

Variables all have names, they have to have names associated with them. It's just a sequence of visible characters, there aresome limits. For instance, it has to start with a non-numerical character. So you can have int testvar1, that's legal, because it starts with a t. But you can't have int of 1testvar, because it starts with a 1. So that's not legal. You can't have any variable names that are C keywords. If, else, while, that sort of thing. So there are so limits on variable names, but you have wide discretion in what your names are going to be.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值