[p=26, null, left][color=#333333][font=Arial]入手pcDuino有一段时间了,鼓起勇气刷了个官方最新lubuntu系统,开始我的arduino之旅。刚刚调通了第一个基于arduino语言的led闪烁程序,与大家分享。Arduino就这么简单,很好上手。[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]目标:使接在pcduino上的led灯闪烁[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]硬件连接:一个led灯+限流电阻,一端接5V,另一端接pcduino上的任一GPIO(0-13)[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]一、搭建环境[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]安装官方文档刷最新ubuntu系统,很详细不再细说。其实刷机时接上debug,会有提示安装信息,不用一直盯着TX RX led。下载[url]https://github.com/pcduino/c_enviroment[/url]上面的arduino例程。[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]二、编写arduino代码[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]我是参考arduino给的例子写的,觉得arduino语言太好上手,没有什么硬件基础都可以玩。下面编写一个led闪烁程序blink[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]在arduino-master\test增加blank.c文件:arduino-master\test\blink.c[/font][/color][/p][code]/*
* 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 <core.h>
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
} [/code][color=#333333][font=Arial]修改arduino-master\test\Makefile:[/font][/color][code]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) [ DISCUZ_CODE_3 ]lt; -o $@
blink: blink.c
$(CC) $(LIBS) $(INCS) [ DISCUZ_CODE_3 ]lt; -o $@
adc_test: adc_test.c
$(CC) $(LIBS) $(INCS) [ DISCUZ_CODE_3 ]lt; -o $@
pwm_test: pwm_test.c
$(CC) $(LIBS) $(INCS) [ DISCUZ_CODE_3 ]lt; -o $@
spi_test: spi_test.c
$(CC) $(LIBS) $(INCS) [ DISCUZ_CODE_3 ]lt; -o $@
adxl345_test: adxl345_test.c
$(CC) [ DISCUZ_CODE_3 ]lt; -o $@
clean:
@for i in $(OBJS); do rm -f $(TARGET)/$i; done [/code][color=#333333][font=Arial]其中blink: blink.c是新加的led闪烁程序。就这样,代码就编写完了[/font][/color]
[p=26, null, left][color=#333333][font=Arial]三、编译[/font][/color][/p][p=26, null, left][color=#333333][font=Arial][b]ubuntu@ubuntu:/$[/b] cd home/ubuntu/work/arduino-master/test/
[b]ubuntu@ubuntu:~/work/arduino-master/test$ [/b]ls
Makefile adc_test.c adxl345_test.c blink.c io_test.c pwm_test.c spi_test.c
[b]ubuntu@ubuntu:~/work/arduino-master/test$ [/b]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
[b]ubuntu@ubuntu:~/work/arduino-master/test$ [/b]cd ../../
[b]ubuntu@ubuntu:~/work$ [/b]cd sample/
[b]ubuntu@ubuntu:~/work/sample$[/b] ls
core test
[b]ubuntu@ubuntu:~/work/sample$ [/b]cd test/
[b]ubuntu@ubuntu:~/work/sample/test$ [/b]ls
adc_test adxl345_test blink io_test pwm_test spi_test
[b]ubuntu@ubuntu:~/work/sample/test$ [/b][/font][/color][/p][p=26, null, left][color=#333333][font=Arial]分析Makefile可知,最终编译生成的扩展性文件在/home/ubuntu/work/sample目录下[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]四、测试[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]开始测试写的blink.c led闪烁程序,我的led接在gpio5上,传入参数5[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]ubuntu@ubuntu:~/work/sample/test$ ./blink 5
blink LED (connected to pin 5)
[/font][/color][/p][p=26, null, left][color=#333333][font=Arial]现在就可以看到接在GPIO5上的LED灯开始闪烁了。Arduino程序就这么简单,但又很灵活,不信你也可以试试。[/font][/color][/p]
|