pcDuino上跑第一个Arduino程序_blink

pc朵拉 发布于 2013/11/30 00:01
阅读 507
收藏 0

入手pcDuino有一段时间了,鼓起勇气刷了个官方最新lubuntu系统,开始我的arduino之旅。刚刚调通了第一个基于arduino语言的led闪烁程序,与大家分享。Arduino就这么简单,很好上手。

目标:使接在pcduino上的led灯闪烁

硬件连接:一个led灯+限流电阻,一端接5V,另一端接pcduino上的任一GPIO(0-13)

  • 搭建环境

安装官方文档刷最新ubuntu系统,很详细不再细说。其实刷机时接上debug,会有提示安装信息,不用一直盯着TX RX led。下载https://github.com/pcduino/c_enviroment上面的arduino例程。

  • 编写Arduino代码

我是参考arduino给的例子写的,觉得arduino语言太好上手,没有什么硬件基础都可以玩。下面编写一个led闪烁程序blink

在arduino-master\test增加blank.c文件:arduino-master\test\blink.c

/*
* I/O test program for a10
* Blink
* Turns on an LED on for one second, then off for one second, repeatedly.
* This example code is in the public domain.
*/
 
#include
int led_pin = 5;
 
void setup()
{
if ( argc !=2 )
{
printf("Usage %s  LED_PIN_NUM(0-13)\n", argv[0]);
exit(-1);
}
 
led_pin = atoi(argv[1]);
 
printf("blink  LED (connected to pin %d)\n",led_pin);
 
// initialize the digital pin as an output.
// led_pin has an LED connected on most Arduino boards:
pinMode(led_pin, OUTPUT);
}
 
void loop() {
digitalWrite(led_pin, HIGH);   // set the LED on
delay(1000);              // wait for a second
digitalWrite(led_pin, LOW);    // set the LED off
delay(1000);              // wait for a second
}

修改arduino-master\test\Makefile:

LIBS=-L../../sample/core -larduino -lspi
INCS=-I../../sample/core/include
TARGET=../../sample/test
 
OBJS = io_test adc_test pwm_test spi_test adxl345_test blink
 
all: $(OBJS)
     @mkdir -p $(TARGET)
     @mv $(OBJS) $(TARGET)
 
io_test: io_test.c
    $(CC) $(LIBS) $(INCS) $< -o $@
 
blink: blink.c
    $(CC) $(LIBS) $(INCS) $< -o $@
 
adc_test: adc_test.c
    $(CC) $(LIBS) $(INCS) $< -o $@
 
pwm_test: pwm_test.c
    $(CC) $(LIBS) $(INCS) $< -o $@
 
spi_test: spi_test.c
    $(CC) $(LIBS) $(INCS) $< -o $@
 
adxl345_test: adxl345_test.c
    $(CC) $< -o $@
clean:
     @for i in $(OBJS); do rm -f $(TARGET)/$$i; done

其中blink: blink.c是新加的led闪烁程序。就这样,代码就编写完了

  • 编译

ubuntu@ubuntu :/$ cd home/ubuntu/work/arduino-master/test/
ubuntu@ubuntu :~/work/arduino-master/test$ ls
Makefile  adc_test.c  adxl345_test.c  blink.c  io_test.c  pwm_test.c  spi_test.c
ubuntu@ubuntu :~/work/arduino-master/test$ make
cc -L../../sample/core -larduino -lspi -I../../sample/core/include io_test.c -o io_test
cc -L../../sample/core -larduino -lspi -I../../sample/core/include adc_test.c -o adc_test
cc -L../../sample/core -larduino -lspi -I../../sample/core/include pwm_test.c -o pwm_test
cc -L../../sample/core -larduino -lspi -I../../sample/core/include spi_test.c -o spi_test
cc adxl345_test.c -o adxl345_test
cc -L../../sample/core -larduino -lspi -I../../sample/core/include blink.c -o blink
ubuntu@ubuntu :~/work/arduino-master/test$ cd ../../
ubuntu@ubuntu :~/work$ cd sample/
ubuntu@ubuntu :~/work/sample$ ls
core  test
ubuntu@ubuntu :~/work/sample$ cd test/
ubuntu@ubuntu :~/work/sample/test$ ls
adc_test  adxl345_test  blink  io_test  pwm_test  spi_test
ubuntu@ubuntu :~/work/sample/test$ 

分析Makefile可知,最终编译生成的扩展性文件在/home/ubuntu/work/sample目录下

  • 测试

开始测试写的blink.c led闪烁程序,我的led接在gpio5上,传入参数5

ubuntu@ubuntu :~/work/sample/test$ ./blink 5
blink  LED (connected to pin 5)

现在就可以看到接在GPIO5上的LED灯开始闪烁了。Arduino程序就这么简单,但又很灵活,不信你也可以试试。

帖子出自: http://cnlearn.linksprite.com/?p=362

加载中
返回顶部
顶部