本帖最后由 灯灯灯 于 2017-5-5 14:08 编辑
功能概览
相比Arduino Nano,CurieNano的优点有:运算速度快、更大的存储空间、运动传感器、电子罗盘、蓝牙4.0、硬件神经元、I2S接口、实时时钟、板载2MB SPIFlash空间、更高速的ADC、双串口、中断引脚多达20个。
型号 | ArduinoNano | CurieNano | 微控制器 | ATmega328P | Intel Curie | 时钟频率 | 16MHz | 32MHz | 工作电压 | 5V | 3.3V | 数字引脚个数 | 14 | 14 | PWN引脚个数 | 6 | 4 | 模拟输入引脚个数 | 6 | 6 | 中断引脚个数 | 2 | 20 | 串口数量 | 1 | 2 | Flash空间 | 32KB | 196KB | SRAM空间 | 2KB | 24KB | 独有特点 | | IMU,BLE,PME,I2S,RTC,SPIFlash |
一些例程
以下我给出一些独有特性的测试例程(其实例程在Arduino IDE官方示例里面都有,只不过我写的更加精简)
1、运动传感器IMU
效果:在串口打印加速度计读值,即101三个方向上的加速度值(单位m/s^2) - #include <CurieIMU.h>
- void setup() {
- Serial.begin(9600);
- CurieIMU.begin();
- CurieIMU.setAccelerometerRange(2);
- }
- void loop() {
- int ax, ay, az;
- CurieIMU.readAccelerometer(ax, ay, az);
- Serial.print((float)ax/16384);
- Serial.print("\t");
- Serial.print((float)ay/16384);
- Serial.print("\t");
- Serial.println((float)az/16384);
- }
复制代码2、蓝牙BLE
效果:下载手机端的NRF toobox以后,选择HRM(HeartRateMonitor),连接Arduino101,可以看到读值从0~255循环变化,每1秒变一次 - #include <CurieBLE.h>
- BLEPeripheral blePeripheral;
- BLEService S("180D");
- BLECharacteristic C("2A37", BLENotify, 2);
- void setup() {
- blePeripheral.setLocalName("Arduino 101");
- blePeripheral.setAdvertisedServiceUuid(S.uuid());
- blePeripheral.addAttribute(S);
- blePeripheral.addAttribute(C);
- blePeripheral.begin();
- }
- uint8_t data[] = {0,0};
- void loop() {
- data[1] ++;
- C.setValue(data,2);
- delay(1000);
- }
复制代码3、实时时钟
效果:在串口打印实时时钟时间。注意:101每次断电重启时,时钟都会恢复最初值 - #include <CurieTime.h>
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- Serial.print(hour());Serial.write(':');
- Serial.print(minute());Serial.write(':');
- Serial.print(second());Serial.print(" ");
- Serial.print(day());Serial.write('/');
- Serial.print(month());Serial.write('/');
- Serial.println(year());
- delay(1000);
- }
复制代码4、板载Flash
板载Flash大小为2MB,有一个固定文件大小的文件系统,每个文件在建立时就确定大小,之后不能改动,需要用到的库是SerialFlash,它是第三方的,需要去github下载github.com/PaulStoffregen/SerialFlash。以下是写入一个64B的文件的例程:
- #include <SerialFlash.h>
- #include <SPI.h>
- #define File_SIZE 64
- #define filename "myfile.txt"
- char contents[] = "Arduino 101 serial flash file system test";
- // digital pin for flash chip CS pin
- #define g_FlashChipSelect 21
- void setup() {
- Serial.begin(9600);
- while(!Serial);
-
- if ( !SerialFlash.begin(g_FlashChipSelect) ){
- Serial.println("Unable to access SPI Flash chip");
- }
- SerialFlashFile file;
- if ( !SerialFlash.exists(filename)){
- if(! SerialFlash.create(filename, File_SIZE)){
- Serial.println("Not enough space to create file " filename);
- return;
- }
- Serial.println("Creating file " filename);
- }else{
- Serial.println("File " filename " already exists");
- }
-
- file = SerialFlash.open(filename);
- file.write(contents, sizeof(contents));
- Serial.println("Write Done");
- }
- void loop() {
- SerialFlashFile file = SerialFlash.open(filename);
- if(file){
- char buf[sizeof(contents)];
- Serial.println(filename ":");
- file.read(buf,sizeof(contents));
- Serial.println(buf);
- }else{
- Serial.println("File " filename " not exists");
- }
- delay(3000);
- }
复制代码
文件写入后,可以运行下面的程序,读出这个文件,显示在串口:
- #include <SerialFlash.h>
- #include <SPI.h>
- #define filename "myfile.txt"
- #define g_FlashChipSelect 21
- void setup() {
- Serial.begin(9600);
- if ( !SerialFlash.begin(g_FlashChipSelect) ){
- Serial.println("Unable to access SPI Flash chip");
- }
- }
- void loop(){
- SerialFlashFile file = SerialFlash.open(filename);
- if(file){
- char buf[64] = {0};
- Serial.println(filename ":");
- file.read(buf,64);
- Serial.println(buf);
- }else{
- Serial.println("File " filename " not exists");
- }
- delay(3000);
- }
复制代码
5、外部中断
CurieNano的所有IO引脚都可用于外部中断。本程序监听14~17号引脚是否发生下降沿中断,发生时打印中断引脚号。
- void setup() {
- attachInterrupt(14,intf14,FALLING);
- attachInterrupt(15,intf15,FALLING);
- attachInterrupt(16,intf16,FALLING);
- attachInterrupt(17,intf17,FALLING);
- interrupts();
- Serial.begin(9600);
- }
- void intf14(){Serial.print("14 ");}
- void intf15(){Serial.print("15 ");}
- void intf16(){Serial.print("16 ");}
- void intf17(){Serial.print("17 ");}
- void loop() {}
复制代码
6、定时器中断
本程序每1秒触发一次定时器中断,将13号引脚电平反转,板载LED灯出现闪烁效果。 - #include <CurieTimerOne.h>
- void timeInterrupt(){
- static uint8_t toggle = 0;
- digitalWrite(13, toggle);
- toggle = !toggle;
- }
- void setup() {
- pinMode(13, OUTPUT);
- CurieTimerOne.start(100000, &timeInterrupt);
- }
- void loop() {}
复制代码 7、分类算法
这就用到了Curie芯片的硬件神经元,需要在github下载第三方库CuriePME:github.com/01org/Intel-Pattern-Matching-Technology。以下的代码调用PME引擎,实现数据分类:- #include <CuriePME.h>
- void setup(){
- Serial.begin(9600);
- Serial.setTimeout(0xffffffff);
- while (!Serial);
- CuriePME.begin();
- trainNeuronsWithData();
- }
- void loop(){
- uint8_t vector[2];
- vector[0] = (uint8_t) Serial.parseInt();
- vector[1] = (uint8_t) Serial.parseInt();
- int answer = CuriePME.classify(vector, 2);
- if( answer == CuriePME.noMatch ){
- Serial.println("Didn't match any categories.");
- }else{
- Serial.print("The closest matching is category: ");
- Serial.println(answer);
- }
- }
- void trainNeuronsWithData(){
- commitSamples(1, 11, 24);
- commitSamples(2, 38, 75);
- commitSamples(3, 2, 56);
- commitSamples(4, 111, 224);
- commitSamples(5, 128, 200);
- commitSamples(6, 99, 180);
- Serial.println("Now input 2 numbers, between 0 and 255, Like 30 24, then press enter, make sure a \\n or \\r sended after that.");
- }
- void commitSamples (int category, uint8_t s1, uint8_t s2){
- uint8_t vector[2] = {s1,s2};
- CuriePME.learn(vector, 2, category);
- Serial.print("Category ");Serial.print( category );
- Serial.print(" trained with: (");Serial.print(s1);Serial.print(",");Serial.print(s2);Serial.println(")");
- }
复制代码 |