嵌入式系统设计技术ARM实验(5~9)
Posted Wang_rush
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌入式系统设计技术ARM实验(5~9)相关的知识,希望对你有一定的参考价值。
实验5:Linux下LED 灯编程
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define LED_NUM 2 //LED_NUM=2
#define LED_C 2 //LED_C=2
//cmd为0灭,为1亮
//io为0靠近蜂鸣器,为1靠近独立按键
int main(int argc, char const *argv[]) //指向字符常量的指针
int fd, led_num, led_c;
char *leds = "/dev/leds";
led_c = LED_C; //led_c=2
led_num = LED_NUM; //led_num=2
printf("argvl is cmd; argv2 is io\\n");
if(atoi(argv[1]) >= led_c) //atoi函数是把字符转换成int型
printf("argv1 is 0 or 1\\n");
exit(1);
if(atoi(argv[2]) >= 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, atoi(argv[1]), atoi(argv[2]));
printf("ioctl %s success\\n", leds);
close(fd);
return 0;
实验5~8对文件的编译操作一样,只需要改一下文件名字就行
实验6:Linux下蜂鸣器编程
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define BUZZER_C 2
int main(int argc, char const *argv[])
char *buzzer_ctl = "/dev/buzzer_ctl";
int fd, ret, buzzer_c;
buzzer_c = BUZZER_C;
if(atoi(argv[1]) >= buzzer_c)
printf("argv[1] is 0 or 2\\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, atoi(argv[1]), atoi(argv[2]));
close(fd);
return 0;
实验7:Linux 下 ADC程序设计
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
main()
int fd;
char *adc = "/dev/adc";
char buffer[512];
int len = 0, r = 0;
memset(buffer, 0, sizeof(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);
r = (int)(r * 10000 / 4095);
printf("res value is %d\\n", r);
实验8:Linux 下串口通信程序设计
uart:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
int set_opt(int, int, int, char, int);
void leds_control(int);
void main(int argc, char *argv[])
int fd, read_num = 0, flag = 0, i = 10;
unsigned char buffer[1024] = "hello uart!";
if (argc < 2)
printf("usage: ./uarttest /dev/ttySAC3\\n");
return;
if ((fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
printf("open %s is failed\\n", argv[1]);
return;
else
set_opt(fd, 115200, 8, 'N', 1);
printf("open %s success!\\t\\n", argv[1]);
while (i--)
write(fd, buffer, strlen(buffer));
sleep(1);
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;
uartread:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
int set_opt(int, int, int, char, int);
void main()
int fd, nByte;
char *uart3 = "/dev/ttySAC3";
char buffer[512];
char *uart_out = "please input\\r\\n";
memset(buffer, 0, sizeof(buffer));
if ((fd = open(uart3, O_RDWR | O_CREAT, 0777)) < 0)
printf("open %s failed!\\n", uart3);
else
set_opt(fd, 115200, 8, 'N', 1);
while (1)
while ((nByte = read(fd, buffer, 512)) > 0)
buffer[nByte + 1] = '\\0';
write(fd, buffer, strlen(buffer));
memset(buffer, 0, strlen(buffer));
nByte = 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;
uartwrite:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
int set_opt(int, int, int, char, int);
void main()
int fd嵌入式系统设计技术ARM实验 综合设计