嵌入式系统设计技术ARM实验 综合设计
Posted Wang_rush
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌入式系统设计技术ARM实验 综合设计相关的知识,希望对你有一定的参考价值。
编写综合的c程序,能够实现对led灯、蜂鸣器设备、adc设备和串口通信设备的调用
这给出含注释的代码,希望我的代码可以给大家一点帮助。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <termios.h>
#include <errno.h>
#define BUZZER_C 2 //宏定义
#define LED_NUM 2
#define LED_C 2
int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop); //定义所需函数
void led (int argv1 , int argv2); //控制led灯函数
void buzzer (int argv1); //控制蜂鸣器函数
int adc(); //有返回值
void control (int argv1);
int uart_init();
int read_uart(int fd);
int write_uart(int i,int fd);
int r=0;
int q=0;
int main(int argc, char const *argv[])
int fd; //句柄
fd = uart_init();
while(1)
read_uart(fd);
if(q==1) //退出函数
return 0;
return 0;
void control(int argv)
if(argv==1)
led(1,0);
printf("led2 is open\\n");
else if(argv==2)
led(0,0);
printf("led2 is close\\n");
else if(argv==3)
led(1,1);
printf("led3 is open\\n");
else if(argv==4)
led(0,1);
printf("led3 is close\\n");
else if(argv==5)
buzzer(1);
printf("buzzer is open\\n");
else if(argv==6)
buzzer(0);
printf("buzzer is close\\n");
else if(argv==7)
adc();
//LED灯
void led (int argv1 , int argv2) //argv1 是状态 argv2 是位置
int fd, led_num, led_c;
char *leds = "/dev/leds";
led_num = LED_NUM;
led_c = LED_C;
printf("argvl is cmd; argv2 is io\\n"); //判断输入指令是否正确
if(argv1 >= led_c)
printf("argv1 is 0 or 1\\n");
exit(1);
if(argv2 >= led_num)
printf("argv2 is 0 or 1\\n");
exit(1);
if ((fd = open(leds, O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
printf("open %s failed\\n", leds);
else
ioctl(fd, argv1, argv2);
printf("ioctl %s success\\n", leds);
close(fd);//关闭这个设备节点
//蜂鸣器
void buzzer (int argv1)
char *buzzer_ctl = "/dev/buzzer_ctl";
int fd, ret, buzzer_c;
buzzer_c = BUZZER_C;
if(argv1 >= buzzer_c)
printf("argv[1] is 0 or 1\\n");
exit(1);
if((fd = open(buzzer_ctl, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) //以可读可写非阻塞方式打开
printf("open %s failed\\n", buzzer_c);
exit(1);
ret = ioctl(fd, argv1, 1); //第二个参数固定
close(fd); //关闭这个设备节点
//ADC
int adc()
int fd;
char *adc = "/dev/adc";
char buffer[512];
int len = 0; //初始化
memset(buffer, 0, sizeof(buffer)); //清空buffer
printf("adc ready!\\n");
if((fd = open(adc, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) //以可读可写非阻塞方式打开
printf("open %s err\\n", adc);
else
printf("open adc success!\\n");
len = read(fd, buffer, 10); //读取电阻值
if(len == 0)
printf("return null\\n");
else
r = atoi(buffer); //把char变成int
r = (int)(r * 10000 / 4095); //转换成对应值
printf("res value is %d\\n", r);
close(fd);
return r; //在主函数返回当前电阻值 int
//UART初始化
int uart_init()
int fd;
char buffer[10]; //缓存指令
memset(buffer, 0, sizeof(buffer));
int nByte;
char *uart3 = "/dev/ttySAC3"; //打开串口通信的设备节点
char *uart_out = "请输入指令\\r\\n";
char *uart_out1="输入*查看指令\\r\\n"; // /r/n回车换行
char *uart_out2="输入#退出\\r\\n";
if ((fd = open(uart3, O_RDWR | O_CREAT, 0777)) < 0)
printf("open %s failed!\\n", uart3);
else
set_opt(fd, 115200, 8, 'N', 1); //设置串口参数
write(fd,uart_out,strlen(uart_out)); //掺入提示
write(fd,uart_out1,strlen(uart_out1)); //指令提示
write(fd,uart_out2,strlen(uart_out2)); //退出提示
return fd;
int read_uart(int fd)
char buffer[10];
memset(buffer, 0, sizeof(buffer));
int nByte;
while (1)
while ((nByte = read(fd, buffer, 3)) > 0)
buffer[nByte + 1] = '\\0'; //转换成char
int argv;
// printf("open %s failed!\\n", buffer);
argv = buffer[0]-'0'; //转换为int
control(argv); //读取指令
if(buffer[0]=='*')
char *uart_out3 = "命令 1 :开启led2\\r\\n";
char *uart_out4 = "命令 2 :关闭led2\\r\\n";
char *uart_out5 = "命令 3 :开启led3\\r\\n";
char *uart_out6 = "命令 4 :关闭led4\\r\\n";
char *uart_out7 = "命令 5 :开启蜂鸣器\\r\\n";
char *uart_out8 = "命令 6 :关闭蜂鸣器\\r\\n";
char *uart_out9 = "命令 7 :获取电阻值\\r\\n";
set_opt(fd, 115200, 8, 'N', 1);
write(fd,uart_out3,strlen(uart_out3));
write(fd,uart_out4,strlen(uart_out4));
write(fd,uart_out5,strlen(uart_out5));
write(fd,uart_out6,strlen(uart_out6));
write(fd,uart_out7,strlen(uart_out7));
write(fd,uart_out8,strlen(uart_out8));
write(fd,uart_out9,strlen(uart_out9));
if(buffer[0] == '#')
close(fd);
q = 1;
return 0;
if (r != 0)
write_uart(r,fd);
printf("open %s failed!\\n", buffer);
r = 0;
write(fd, buffer, strlen(buffer));
memset(buffer, 0, strlen(buffer));
nByte = 0;
int write_uart(int i,int fd)
char buffer[10], tmp[10]; //nuffbur int转换为char 把电阻值在串口助手中打印出来
int cnt = 0;
while(i)
tmp[cnt++] = i % 10 + '0';
i /= 10;
int j;
for(j = cnt - 1; j >= 0; j --)
buffer[j] = tmp[cnt - j - 1];
buffer[cnt] = '\\n';
write(fd, buffer, cnt+1);
return 0;
//串口初始化
int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
struct termios newtio, oldtio;
if (tcgetattr(fd, &oldtio) != 0)
perror("SetupSerial 1");
return -1;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch (nBits)
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
switch (nEvent)
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
switch (nSpeed)
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
case 921600:
printf("B921600\\n");
cfsetispeed(&newtio, B921600);
cfsetospeed(&newtio, B921600);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
if (nStop == 1)
newtio.c_cflag &= ~CSTOPB;
else if (nStop == 2)
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0)
perror("com set error");
return -1;
printf("set done!\\n\\r");
return 0;
以上是关于嵌入式系统设计技术ARM实验 综合设计的主要内容,如果未能解决你的问题,请参考以下文章