arduino 编程语言

2020-03-29 12:42

Arduino语言注解

Arduino语言是建立在C/C++基础上的,其实也就是基础的C语言,Arduino语言只不过把AVR单片机(微控制器)相关的一些参数设置都函数化,不用我们去了解他的底层,让我们不了解AVR单片机(微控制器)的朋友也能轻松上手。 关键字: if if...else for switch case while

do... while break continue return goto

语法符号: ; {} //

数据类型: boolean 布尔类型 char 字符类型 byte 字节类型 int 整数类型 unsigned int 无符号整数类型 long 长整数类型 unsigned long 无符号长整数类型 float 浮点类型

double 双精度浮点类型 string 字符数组型 array 数组类型 void 无类型

数据类型转换:

char() byte() int() long() float()

字节 1 1 1 2 2 4 4 4 4

范围

False or ture -128-127 0-255 -32768-32768 0-65535

常量:

HIGH | LOW 表示数字IO口的电平,HIGH 表示高电平(1),LOW 表示低电平(0)。 INPUT | OUTPUT 表示数字IO口的方向,INPUT 表示输入(高阻态),OUTPUT 表示输出(AVR能提供5V电压 40mA电流)。 true | false true 表示真(1),false表示假(0)。

程序结构

声明变量及接口名称(例如:int val;int ledPin=13;)。

void setup() 函数在程序开始时使用,初始化变量,管脚模式,调用库函数等(例如:pinMode(ledPin,OUTUPT);)。

void loop() 在setup()函数之后,即初始化之后,loop() 让你的程序循环地被执行。使用它来运转Arduino。

数字 I/O

pinMode(pin, mode)

数字IO口输入输出模式定义函数,pin表示为0~13, mode表示INPUT或OUTPUT。 digitalWrite(pin, value)

数字IO口输出电平定义函数,pin表示为0~13,value表示为HIGH或LOW。 必须先定义为输入或输出模式digitalWrite才能生效。

int digitalRead(pin)

数字IO口读输入电平函数,pin表示为0~13,value表示为HIGH或LOW。比如可以读数 字传感器。

模拟 I/O

int analogRead(pin)

模拟IO口读函数,pin表示为0~5(Arduino Diecimila为0~5,Arduino nano为0~7)。比 如可以读模拟传感器(10位AD,0~5V表示为0~1023)。

analogWrite(pin, value) - PWM

数字IO口PWM输出函数,Arduino数字IO口标注了PWM的IO口可使用该函数,pin表示3, 5, 6, 9, 10, 11,value表示为0~255。比如可用于电机PWM调速或音乐播放。

扩展 I/O

shiftOut(dataPin, clockPin, bitOrder, value)

SPI外部IO扩展函数,通常使用带SPI接口的74HC595做8个IO扩展,dataPin为数据口,clockPin为时钟口,bitOrder为数据传输方向(MSBFIRST高位在前,LSBFIRST低位在前),value表示所要传送的数据(0~255),另外还需要一个IO口做74HC595的使能控制。 unsigned long pulseIn(pin, value) 脉冲长度记录函数,返回时间参数(us),pin表示为0~13,value为HIGH或LOW。比如value为HIGH,那么当pin输入为高电平时,开始计时,当pin输入为低电平时,停止计时,然后返回该时间。

时间函数

unsigned long millis() 返回时间函数(单位ms),该函数是指,当程序运行就开始计时并返回记录的参数,该参数溢出大概需要50天时间。 delay(ms) 延时函数(单位ms)。

delayMicroseconds(us) 延时函数(单位us)。

数学函数

min(x, y) 求最小值 max(x, y) 求最大值

abs(x) 计算绝对值

constrain(x, a, b) 约束函数,下限a,上限b,x必须在ab之间才能返回。

map(value, fromLow, fromHigh, toLow, toHigh) 约束函数,value必须在fromLow与toLow之间和fromHigh与toHigh之间。

pow(base, exponent) 开方函数,base的exponent次方。 sq(x) 平方 sqrt(x) 开根号

三角函数

sin(rad) cos(rad) tan(rad)

随机数函数

randomSeed(seed) 随机数端口定义函数,seed表示读模拟口analogRead(pin)函数 。 long random(max) 随机数函数,返回数据大于等于0,小于max。 long random(min, max) 随机数函数,返回数据大于等于min,小于max。

外部中断函数

attachInterrupt(interrupt, , mode) 外部中断只能用到数字IO口2和3,interrupt表示中断口初始0或1,表示一个功能函数,mode:LOW低电平中断,CHANGE有变化就中断,RISING上升沿中断,FALLING 下降沿中断。

detachInterrupt(interrupt) 中断开关,interrupt=1 开,interrupt=0 关。

中断使能函数

interrupts() 使能中断

noInterrupts() 禁止中断

串口收发函数

Serial.begin(speed) 串口定义波特率函数,speed表示波特率(串行每秒传输数据的速率),如9600,19200等。

int Serial.available() 判断缓冲器状态。 int Serial.read() 读串口并返回收到参数。 Serial.flush() 清空缓冲器。

Serial.print(data) 串口输出数据。Serial.print(数据,数据的进制) 默认为十进制(DEC) Serial.println(data) 串口输出数据并带回车符。

官方库文件

EEPROM - EEPROM读写程序库 Ethernet - 以太网控制器程序库

LiquidCrystal - LCD控制程序库 Servo - 舵机控制程序库

SoftwareSerial - 任何数字IO口模拟串口程序库 Stepper - 步进电机控制程序库 Wire - TWI/I2C总线程序库

Matrix - LED矩阵控制程序库

Sprite - LED矩阵图象处理控制程序库 非官方库文件

DateTime - a library for keeping track of the current date and time in software.

Debounce - for reading noisy digital inputs (e.g. from buttons)

Firmata - for communicating with applications on the computer using a standard serial protocol. GLCD - graphics routines for LCD based on the KS0108 or equivalent chipset. LCD - control LCDs (using 8 data lines)

LCD 4 Bit - control LCDs (using 4 data lines)

LedControl - for controlling LED matrices or seven-segment displays with a MAX7221 or MAX7219.

LedControl - an alternative to the Matrix library for driving multiple LEDs with Maxim chips. Messenger - for processing text-based messages from the computer

Metro - help you time actions at regular intervals

MsTimer2 - uses the timer 2 interrupt to trigger an action every N milliseconds. OneWire - control devices (from Dallas Semiconductor) that use the One Wire protocol. PS2Keyboard - read characters from a PS2 keyboard.

Servo - provides software support for Servo motors on any pins.

Servotimer1 - provides hardware support for Servo motors on pins 9 and 10 Simple Message System - send messages between Arduino and the computer

SSerial2Mobile - send text messages or emails using a cell phone (via AT commands over software serial)

TextString - handle strings

TLC5940 - 16 channel 12 bit PWM controller.

X10 - Sending X10 signals over AC power lines

以上库文件都需要下载到编译环境(如下目录:arduino-0011\\hardware\\libraries)中才能使用。

map();函数 把一个大范围数,变小范围数的函数 map(value, fromLow, fromHigh, toLow, toHigh) Description

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

把一个数从一个范围变换到另一个范围。

Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired.

不会把值强制限制在范围之内,因为超范围的值经常也是有用的。如果需要的范围做一限制。可以在这个函数之前或之后使用constrain() 函数。

Note that the \the map() function may be used to reverse a range of numbers, for example

注意,两个范围中的“下界”要比“上界”大或下,这样map()可以用来反转一个范围,例如

y = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so that this example

函数也可以处理负数,例如

y = map(x, 1, 50, 50, -100);

is also valid and works well.


arduino 编程语言.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:电大作业-现代科技基础知识

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: