有辦法透過C語言控制A80的紅綠LED嗎?
想請教各位有辦法透過C語言控制A80的紅綠LED嗎?
像是寫一個 變數 = 1時,紅燈亮
變數 = 0時,紅滅綠亮
因為現在好像CPU執行時紅綠LED是閃爍的
不曉得該從哪裡下手
謝謝各位。
可以啊,最基本的,你会linux 文件操作即可,给你一个思路:
int fd = open("pin_name", O_WR);
write(fd, 1,1);
close(fd);
注意使用root 权限,否则你应该是写不成功的。
本身这个问题,在2013年的时候,就已经玩过了,当时有的是使用了python库,有的是直接使用的 open() unix系列函数来操作的,分别对应写0和1就可以了。
你不妨试着在论坛搜一下。 http://forum.cubietech.com/forum.php?mod=viewthread&tid=456 #include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#define SW_PORTC_IO_BASE0x06000800
int main() {
unsigned int * pc;
int fd, i;
char * ptr;
unsigned int addr_start, addr_offset, PageSize, PageMask, data;
PageSize = sysconf(_SC_PAGESIZE);
PageMask = (~(PageSize-1));
addr_start = SW_PORTC_IO_BASE & PageMask;
addr_offset = SW_PORTC_IO_BASE & ~PageMask;
fd = open("/dev/mem", O_RDWR);
if(fd < 0) {
perror("Unable to open /dev/mem");
return(-1);
}
pc = mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start);
if(pc == MAP_FAILED) {
perror("Unable to mmap file");
printf("pc:%lx\n", (unsigned long)pc);
return(-1);
}
printf("PageSize:%8.8x\tPageMask:%8.8x\naddr_start:%8.8x\taddr_offset:%8.8x\n",PageSize,PageMask,addr_start,addr_offset);
printf("pc:%8.8x\n", *(unsigned int *)pc);
ptr = (char *)pc + addr_offset;
//the PG12 is UART4-TX in the board ,set it as output port
data = *(unsigned int *)(ptr+0xdc);
data &= ~(7<<16);
data |= 1<<16;
*(unsigned int *)(ptr+0xdc) = data;
//set it high level .If set low , data &= 0<<12 replacedata |= 1<<12
data = *(unsigned int *)(ptr+0xe8);
data |= 1<<12;
*(unsigned int *)(ptr+0xe8) = data;
return 0;
}
通过I/O映射的方式,控制寄存器。
页:
[1]