查看: 4136|回复: 3

【CurieNano上手2】快速上手

[复制链接]
  • TA的每日心情
    开心
    2017-5-15 14:59
  • 签到天数: 8 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2017-5-3 15:35:43 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 灯灯灯 于 2017-5-5 14:08 编辑


    功能概览
    相比Arduino Nano,CurieNano的优点有:运算速度快、更大的存储空间、运动传感器、电子罗盘、蓝牙4.0、硬件神经元、I2S接口、实时时钟、板载2MB SPIFlash空间、更高速的ADC、双串口、中断引脚多达20个。

    型号 ArduinoNano CurieNano
    微控制器 ATmega328P Intel Curie
    时钟频率 16MHz32MHz
    工作电压 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)
    1. #include <CurieIMU.h>

    2. void setup() {
    3.   Serial.begin(9600);
    4.   CurieIMU.begin();
    5.   CurieIMU.setAccelerometerRange(2);
    6. }

    7. void loop() {
    8.   int ax, ay, az;
    9.   CurieIMU.readAccelerometer(ax, ay, az);
    10.   Serial.print((float)ax/16384);
    11.   Serial.print("\t");
    12.   Serial.print((float)ay/16384);
    13.   Serial.print("\t");
    14.   Serial.println((float)az/16384);
    15. }
    复制代码
    2、蓝牙BLE

    效果:下载手机端的NRF toobox以后,选择HRM(HeartRateMonitor),连接Arduino101,可以看到读值从0~255循环变化,每1秒变一次
    1. #include <CurieBLE.h>

    2. BLEPeripheral blePeripheral;
    3. BLEService S("180D");
    4. BLECharacteristic C("2A37", BLENotify, 2);

    5. void setup() {
    6.   blePeripheral.setLocalName("Arduino 101");
    7.   blePeripheral.setAdvertisedServiceUuid(S.uuid());
    8.   blePeripheral.addAttribute(S);
    9.   blePeripheral.addAttribute(C);
    10.   blePeripheral.begin();
    11. }

    12. uint8_t data[] = {0,0};

    13. void loop() {
    14.   data[1] ++;
    15.   C.setValue(data,2);
    16.   delay(1000);
    17. }
    复制代码
    3、实时时钟

    效果:在串口打印实时时钟时间。注意:101每次断电重启时,时钟都会恢复最初值
    1. #include <CurieTime.h>

    2. void setup() {
    3.   Serial.begin(9600);
    4. }

    5. void loop() {
    6.   Serial.print(hour());Serial.write(':');
    7.   Serial.print(minute());Serial.write(':');
    8.   Serial.print(second());Serial.print("   ");
    9.   Serial.print(day());Serial.write('/');
    10.   Serial.print(month());Serial.write('/');
    11.   Serial.println(year());
    12.   delay(1000);
    13. }
    复制代码
    4、板载Flash

    板载Flash大小为2MB,有一个固定文件大小的文件系统,每个文件在建立时就确定大小,之后不能改动,需要用到的库是SerialFlash,它是第三方的,需要去github下载github.com/PaulStoffregen/SerialFlash。以下是写入一个64B的文件的例程:

    1. #include <SerialFlash.h>
    2. #include <SPI.h>

    3. #define File_SIZE 64

    4. #define filename  "myfile.txt"

    5. char contents[] = "Arduino 101 serial flash file system test";

    6. // digital pin for flash chip CS pin
    7. #define g_FlashChipSelect 21

    8. void setup() {
    9.   Serial.begin(9600);
    10.   while(!Serial);
    11.   
    12.   if ( !SerialFlash.begin(g_FlashChipSelect) ){
    13.     Serial.println("Unable to access SPI Flash chip");
    14.   }

    15.   SerialFlashFile file;

    16.   if ( !SerialFlash.exists(filename)){
    17.     if(! SerialFlash.create(filename, File_SIZE)){
    18.       Serial.println("Not enough space to create file " filename);
    19.       return;
    20.     }
    21.     Serial.println("Creating file " filename);
    22.   }else{
    23.     Serial.println("File " filename " already exists");
    24.   }
    25.   
    26.   file = SerialFlash.open(filename);
    27.   file.write(contents, sizeof(contents));
    28.   Serial.println("Write Done");
    29. }

    30. void loop() {
    31.   SerialFlashFile file = SerialFlash.open(filename);
    32.   if(file){
    33.     char buf[sizeof(contents)];
    34.     Serial.println(filename ":");
    35.     file.read(buf,sizeof(contents));
    36.     Serial.println(buf);
    37.   }else{
    38.     Serial.println("File " filename " not exists");
    39.   }
    40.   delay(3000);
    41. }
    复制代码

    文件写入后,可以运行下面的程序,读出这个文件,显示在串口:

    1. #include <SerialFlash.h>
    2. #include <SPI.h>

    3. #define filename  "myfile.txt"

    4. #define g_FlashChipSelect 21

    5. void setup() {
    6.   Serial.begin(9600);
    7.   if ( !SerialFlash.begin(g_FlashChipSelect) ){
    8.     Serial.println("Unable to access SPI Flash chip");
    9.   }
    10. }

    11. void loop(){
    12.   SerialFlashFile file = SerialFlash.open(filename);
    13.   if(file){
    14.     char buf[64] = {0};
    15.     Serial.println(filename ":");
    16.     file.read(buf,64);
    17.     Serial.println(buf);
    18.   }else{
    19.     Serial.println("File " filename " not exists");
    20.   }
    21.   delay(3000);
    22. }
    复制代码


    5、外部中断

    CurieNano的所有IO引脚都可用于外部中断。本程序监听14~17号引脚是否发生下降沿中断,发生时打印中断引脚号。

    1. void setup() {
    2.   attachInterrupt(14,intf14,FALLING);
    3.   attachInterrupt(15,intf15,FALLING);
    4.   attachInterrupt(16,intf16,FALLING);
    5.   attachInterrupt(17,intf17,FALLING);
    6.   interrupts();
    7.   Serial.begin(9600);
    8. }

    9. void intf14(){Serial.print("14 ");}
    10. void intf15(){Serial.print("15 ");}
    11. void intf16(){Serial.print("16 ");}
    12. void intf17(){Serial.print("17 ");}

    13. void loop() {}
    复制代码


    6、定时器中断

    本程序每1秒触发一次定时器中断,将13号引脚电平反转,板载LED灯出现闪烁效果。
    1. #include <CurieTimerOne.h>

    2. void timeInterrupt(){
    3.   static uint8_t toggle = 0;
    4.   digitalWrite(13, toggle);
    5.   toggle = !toggle;
    6. }

    7. void setup() {
    8.   pinMode(13, OUTPUT);
    9.   CurieTimerOne.start(100000, &timeInterrupt);
    10. }

    11. void loop() {}
    复制代码
    7、分类算法
    这就用到了Curie芯片的硬件神经元,需要在github下载第三方库CuriePME:github.com/01org/Intel-Pattern-Matching-Technology。以下的代码调用PME引擎,实现数据分类:
    1. #include <CuriePME.h>

    2. void setup(){
    3.   Serial.begin(9600);
    4.   Serial.setTimeout(0xffffffff);
    5.   while (!Serial);
    6.   CuriePME.begin();
    7.   trainNeuronsWithData();
    8. }

    9. void loop(){
    10.   uint8_t vector[2];
    11.   vector[0] = (uint8_t) Serial.parseInt();
    12.   vector[1] = (uint8_t) Serial.parseInt();
    13.   int answer = CuriePME.classify(vector, 2);
    14.   if( answer == CuriePME.noMatch ){
    15.     Serial.println("Didn't match any categories.");
    16.   }else{
    17.     Serial.print("The closest matching is category: ");
    18.     Serial.println(answer);
    19.   }
    20. }

    21. void trainNeuronsWithData(){
    22.   commitSamples(1, 11, 24);
    23.   commitSamples(2, 38, 75);
    24.   commitSamples(3, 2, 56);
    25.   commitSamples(4, 111, 224);
    26.   commitSamples(5, 128, 200);
    27.   commitSamples(6, 99, 180);
    28.   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.");
    29. }

    30. void commitSamples (int category, uint8_t s1, uint8_t s2){
    31.   uint8_t vector[2] = {s1,s2};
    32.   CuriePME.learn(vector, 2, category);
    33.   Serial.print("Category ");Serial.print( category );
    34.   Serial.print(" trained with: (");Serial.print(s1);Serial.print(",");Serial.print(s2);Serial.println(")");
    35. }
    复制代码
    回复

    使用道具 举报

  • TA的每日心情

    2021-12-7 12:35
  • 签到天数: 1354 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2017-5-4 09:59:57 | 显示全部楼层
    参考和学习,很适用
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-12-10 15:56
  • 签到天数: 2675 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2017-5-4 10:06:33 | 显示全部楼层
    感谢分享,找块板子来试试了。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2021-2-27 22:16
  • 签到天数: 1568 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2017-12-22 10:24:52 | 显示全部楼层
    这些例程有详细的么
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-29 20:07 , Processed in 0.136825 second(s), 21 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.