夜owl

困到睡不着
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

arduino学习笔记1-初

Posted on 2020-04-13 23:57  夜owl  阅读(495)  评论(0编辑  收藏  举报

arduino小白入门

玩过很多微控制器,所以编程有一定基础,对于arduino,只闻其名,不曾接触,在别人买了一个二手的arduino板子,一直吃灰,疫情期间在家时间闲的无聊,把这个门给入了.

2020-4-13

文件资源

官网

https://www.arduino.cc/

中文视频 - 网易云课堂

http://study.163.com/find.htm#/find/search?p=arduino

微信公众号

Python与Arduino

arduino各芯片引脚图

https://www.geek-workshop.com/forum.php?mod=viewthread&tid=3499

连接准备

首先先下载IDE,在官网下载页面可以找到,

image

然后连接usb,安装驱动,我直接完成了,驱动显示未知可以在设备管理器手动安装,选择上图的第一个drives的驱动,安装完成后会显示COM口,

另外可以考虑官网线上编译器,可以从多端登入参考另一篇学习日记

https://www.cnblogs.com/nightowl/p/12695068.html

编程烧录

第一次使用需要配置,在工具tag下选择com和board。

测试板子可以使用软件自动example的点灯程序blink。

一步到位,没问题。

实验例程

接下来就可以直接上手例程了,编程语言已经比较熟悉,特有的函数和语法可以不懂的再查阅,这样效率更高。下面列下功能的语言

/*************Arduino 语言*************/

结构

  • void setup()   初始化变量,管脚模式,调用库函数等
  • void loop() 连续执行函数内的语句

功能

数字 I/O

  • pinMode(pin, mode)    数字IO口输入输出模式定义函数,pin表示为0~13, mode表示为INPUT或OUTPUT。
  • digitalWrite(pin, value)   数字IO口输出电平定义函数,pin表示为0~13,value表示为HIGH或LOW。比如定义HIGH可以驱动LED。
  • 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)   开根号

三角函数

随机数函数

  • 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 关。

中断使能函数

串口收发函数

/**********************************/

/************Arduino语言库文件*************/

官方库文件
非官方库文件
  • 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 入门到精通 例程1

1Hello World

  1 int val;//定义变量val
  2 int ledpin=13;//定义数字接口13
  3 void setup()
  4 {
  5 Serial.begin(9600);//设置波特率为9600,这里要跟软件设置相一致。当接入特定设备(如:蓝牙)时,我们也要跟其他设备的波特率达到一致。
  6 pinMode(ledpin,OUTPUT);//设置数字13 口为输出接口,Arduino 上我们用到的I/O 口都要进行类似这样的定义。
  7 }
  8 void loop()
  9 {
 10 val=Serial.read();//读取PC 机发送给Arduino 的指令或字符,并将该指令或字符赋给val
 11 if(val=='R')//判断接收到的指令或字符是否是“R”。
 12 {//如果接收到的是“R”字符
 13 digitalWrite(ledpin,HIGH);//点亮数字13 口LED。
 14 delay(500);
 15 digitalWrite(ledpin,LOW);//熄灭数字13 口LED
 16 delay(500);
 17 Serial.println("Hello World!");//显示“Hello World!”字符串
 18 }
 19 }


打开tool下的串口监视器(c'+s'+m

发送之后没有问题,程序很简单,开始修改,发现读取只能是一个字节,想想读一个字符串,自己瞎改没有成功,还是找教程吧(微信公众号精品很多啊)

https://mp.weixin.qq.com/s?__biz=MzIyMjg3Njg4Nw==&mid=2247483809&idx=1&sn=b9a6062ad1057df75e71fc27302dc3c2&scene=19#wechat_redirect

通过Serial.read()函数得到的是对应字符的ASCII码值