pc duino 编程
14547862272014/10/23软件综合 IP:河北
     pcduino是一款高性价比的开发板 可以安装ubuntu 及Linux系统 这对于对于C语言的童鞋们既是好事也是坏事 因为arduino的语言基于c又简于c,虽然内容较简单 但是其编程语序和c比较来说简直是天差地别

Arduino语言注解

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

数据类型:
    字节                 范围
boolean  布尔类型                         1                   False or ture
char  字符类型                   1           -128-127
byte  字节类型                   1                0-255
int  整数类型                             2                   -32768-32768
unsigned int  无符号整数类型               2                        0-65535
long  长整数类型                4                        
unsigned long  无符号长整数类型            4
float  浮点类型                    4
double  双精度浮点类型      4
string  字符数组型
array  数组类型
void   无类型

数据类型转换:

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

常量:

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表示为013 mode表示INPUTOUTPUT
digitalWrite(pin, value)  
数字IO口输出电平定义函数,pin表示为013value表示为HIGHLOW
必须先定义为输入或输出模式digitalWrite才能生效。
int digitalRead(pin)    
数字IO口读输入电平函数,pin表示为013value表示为HIGHLOW。比如可以读数
字传感器。

模拟 I/O

int analogRead(pin)    
模拟IO口读函数,pin表示为05Arduino Diecimila05Arduino nano07)。比
如可以读模拟传感器(10AD05V表示为01023)。
analogWrite(pin, value) - PWM    
数字IOPWM输出函数,Arduino数字IO口标注了PWMIO口可使用该函数,pin表示3, 5, 6, 9, 10, 11value表示为0255。比如可用于电机PWM调速或音乐播放。

扩展 I/O

shiftOut(dataPin, clockPin, bitOrder, value)    
SPI外部IO扩展函数,通常使用带SPI接口的74HC5958IO扩展,dataPin为数据口,clockPin为时钟口,bitOrder为数据传输方向(MSBFIRST高位在前,LSBFIRST低位在前),value表示所要传送的数据(0255),另外还需要一个IO口做74HC595的使能控制。
unsigned long pulseIn(pin, value)    脉冲长度记录函数,返回时间参数(us),pin表示为013valueHIGHLOW。比如valueHIGH,那么当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,上限bx必须在ab之间才能返回。
map(value, fromLow, fromHigh, toLow, toHigh)    约束函数,value必须在fromLowtoLow之间和fromHightoHigh之间。
pow(base, exponent)  开方函数,baseexponent次方。
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)     外部中断只能用到数字IO23interrupt表示中断口初始01,表示一个功能函数,modeLOW低电平中断,CHANGE有变化就中断,RISING上升沿中断,FALLING 下降沿中断。
detachInterrupt(interrupt)    中断开关,interrupt=1 开,interrupt=0 关。

中断使能函数

interrupts()  使能中断
noInterrupts()  禁止中断

串口收发函数

XXXXXXXXXgin(speed)  串口定义波特率函数,speed表示波特率(串行每秒传输数据的速率),如960019200等。
int Serial.available()  判断缓冲器状态。
int XXXXXXXXXad()   读串口并返回收到参数。
Serial.flush()    清空缓冲器。
XXXXXXXXXint(data)  串口输出数据。XXXXXXXXXint(数据,数据的进制) 默认为十进制(DEC
XXXXXXXXXintln(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 "lower bounds" of either range may be larger or smaller than the "upper bounds" so 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.

也有效和正确


The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.

map()函数使用整型,所以不会产生分数,分数将会被截去,并不是全面的或平均值(?)

Parameters 参数

value: the number to map

map的值


fromLow: the lower bound of the value's current range

值现在的下界


fromHigh: the upper bound of the value's current range

值现在的上界


toLow: the lower bound of the value's target range

值目标范围的下界


toHigh: the upper bound of the value's target range

值目标范围的上界

Returns 返回值

The mapped value.

映射的值

Example


void setup() {}

void loop()
{
int val = analogRead(0); //读取0口的值
val = map(val, 0, 1023, 0, 255);//0-1023映射到0-255
analogWrite(9, val);//把映射后的值写给9
}

attachInterrupt(中断通道, 中断函数, 触发方式);
中断函数就是你要去执行的函数,这个函数不能带任何参数,且没有返回类型
中断模式就是你的中断触发方式。在大多数arduino上有以下四种触发方式:
LOW                  低电平触发
CHANGE            电平变化,高电平变低电平、低电平变高电平
RISING              上升沿触发
FALLING            下降沿触发
HIGH                 高电平触发(该中断模式仅适用于Arduino due)
各种型号Arduino外部中断引脚

外部中断在不同的Arduino型号上位置也不同,只有外部中断发生在以下端口,Arduino才能捕获到,以下例举了常见的几种型号的外部中断引脚标号。
型号         int.0  int.1  int.2  int.3  int.4  int.5
UNO\Ethernet    2    3     
Mega2560        2    3      21      20     19     18
Leonardo        3    2       0      1          
Due                   所有IO口均可
来自:计算机科学 / 软件综合
6
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
499382569
9年7个月前 IP:河北
727570
话说科创里好像没有多少玩pc duino的。。。话说这东西好像功能很多,应用范围广,性价比高,详细还真没怎么了解过
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
sinrave
9年7个月前 IP:山东
727611
druino这东西上手确实不错,小学生都能看得懂
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
amo
9年7个月前 IP:广东
727618
学生建议不要玩这个,只是个玩具而已,限制也很多,工作这么多年还没看到哪个产品上敢用(mega只有一家生产,曾经被厂家和代理联手玩缺货把价格炒高几十倍,并且破解是立等可取的那种)……
而直接用C的话,只要不是4位机都可以,能做到你能想象的任何事,移植也方便,关键是C精通了工作不用愁
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
.........
9年7个月前 IP:江西
727714
引用 amo:
学生建议不要玩这个,只是个玩具而已,限制也很多,工作这么多年还没看到哪个产品上敢用(mega只有一家生产,曾经被厂家和代理联手玩缺货把价格炒高几十倍,并且破解是立等可取的那种)……
而直接用C的话,只要不是4位机都可以,能做到你能想象的任何...
pcduino 用的好像是珠海全志的芯片。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
chenhello
9年7个月前 IP:湖南
727909
谢谢楼主
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
上级专业
同级专业
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}