Arduino作为一款为创客而造的智能硬件,以其简单,灵活,开源等特点,吸引了大批爱好者。只要稍微有一点c语言基础和初高中物理知识,就能利用arduino创造出许多好玩的智能小物件。
代码如下,使用arm交叉编译运行即可。
#include
#include
#include
#include
#include
#include
#include
#include
int fd;
int uart_device_open()
{
struct termios options;
if((fd=open(“/dev/ttyUSB0”,O_RDWR|O_NOCTTY|O_NDELAY))<0)
{
perror(“open failed”);
return -1;
}
tcgetattr(fd, &options);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= CS8;
options.c_cflag &= ~CSTOPB;
options.c_iflag |= IGNPAR;
options.c_iflag &= ~(BRKINT | INPCK | ISTRIP | ICRNL | IXON);
options.c_cc[VMIN] = 12;
options.c_oflag = 0;
options.c_lflag = 0;
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(fd,TCSANOW,&options);
printf(“serial ok!n”);
return 0;
}
int my_write(char command)
{
write(fd,&command,sizeof(command));
printf(“write %c successn”,command);
}
int main(int argc, const char *argv[])
{
uart_device_open();
char command;
while(1)
{
command = getchar();
if((command-‘1’)==0)
{
my_write(‘1’);
printf(“LED ONn”);
}
if((command-‘2’)==0)
{
my_write(‘2’);
printf(“LED OFFn”);
}
if((command-‘3’)==0)
{
my_write(‘3’);
printf(“bling blingn”);
}
}
return 0;
}
五、小结
逐步通过这个三个小实验,我们可以简单对arduino实现代码控制,串口控制,甚至于使用开发板控制。最终实现了上位机与下位机的简单通信。
在以后的博文中我们将采用arduino实现arm开发板对电机的控制,从而实现一个简单的智能小车。
文章源自华清远见嵌入式学院:http://www.embedu.org/
>>>更多优秀技术博文每日更新
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!