嵌入式系统设计技术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, i = 10, wr_static;
    char *uart3 = "/dev/ttySAC3";
    char *buffer = "hello world!\n";
    printf("\r\nitop4412 uart3 writetest start\r\n");
    if ((fd = open(uart3, O_RDWR | O_CREAT, 0777)) < 0) {
        printf("open %s failed!\n", uart3);
    } else {
        printf("open %s is success!\n", uart3);
        set_opt(fd, 115200, 8, 'N', 1);
        while (i--) {
            wr_static = write(fd, buffer, strlen(buffer));
            if (wr_static < 0) {
                printf("write failed\n");
            } else {
                printf("wr_static is %d\n", wr_static);
            }
            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;
}

实验9:Linux 下 ADC程序设计
源码忘记保存,只有报告里的截图
在这里插入图片描述
在这里插入图片描述
Makefile文件
在这里插入图片描述

生成.ko文件
在这里插入图片描述

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值