更多配套资料CSDN地址:点赞+关注,功德无量。更多配套资料,欢迎私信。
物联技术666_嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记-CSDN博客物联技术666擅长嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记,等方面的知识,物联技术666关注机器学习,arm开发,物联网,嵌入式硬件,单片机领域.https://blog.csdn.net/weixin_39804904?type=blog
一、ARM LINUX 开发板LED灯GPIO的控制
~~~~~~~~~~~~~main.c
#include
#include
#include
#include
#include
#include
#include
#include"gpio.h"
int main(int argc,char * argv[])
{
int gpg_fd;
gpg_fd = open("/dev/gpgdriver", O_RDWR);
ioctl(gpg_fd, GPIO_CFG_PIN_OUT, GPIO_PIN_AREA(0,2));
char led = 0x1;
while(1)
{
write(gpg_fd, &led, 1);
led = led << 1;
usleep(300*1000);
if(led == 0x08)
{
led = 0x1;
}
}
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~gpio.h
#ifndef __USR_GPIO_H__
#define __USR_GPIO_H__
#define GPIO_SET_PIN 0
//#define GPIO_SET_MULTI_PIN 1
#define GPIO_SET_ALL_PIN 2
#define GPIO_CLR_PIN 3
//#define GPIO_CLR_MULTI_PIN 4 //等同GPIO_CLR_PIN
#define GPIO_CLR_ALL_PIN 5
/******************************
* the configration of gpio mode
******************************/
#define GPIO_CFG_PIN_OUT 6
//#define GPIO_CFG_MULTI_PIN_OUT 10 //等同GPIO_CFG_PIN_OUT
#define GPIO_CFG_PIN_IN 7
//#define GPIO_CFG_MULTI_PIN_IN 11 //等同GPIO_CFG_PIN_IN
#define GPIO_CFG_PIN_UP_ON 8
//#define GPIO_CFG_MULTI_PIN_UP_ON 12 //等同GPIO_CFG_PIN_UP_ON
#define GPIO_CFG_PIN_UP_OFF 9
//#define GPIO_CFG_MULTI_PIN_UP_OFF 13 //等同GPIO_CFG_PIN_UP_OFF
#define GPIO_PIN_AREA(startbit,endbit) ((startbit)|((endbit)<<4))
#define GPIO_PIN_BIT(bit) GPIO_PIN_AREA(bit,bit)
#endif
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
还少一个LED相关驱动文件.ko文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~键盘控制LED灯main.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "gpio.h"
#define LED1 0x31
#define LED2 0x32
#define LED3 0x33
#define LEDOFF 0x34
#define LEDON 0x35
struct termios options, oldoptions;
int uart_Init()
{
int fd;
fd = open("/dev/s3c2410_serial0", O_RDWR);
tcgetattr(fd, &oldoptions);
tcgetattr(fd, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag &= ~PARENB; //无奇偶校验位
options.c_cflag |= CSTOPB; //停止位为1位
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; //数据位为8位
options.c_lflag &= (~ICANON); //非标准模式(RAW模式), 无缓冲区,当接收时,输入的字符在它们被收到后立即被传送
//options.c_lflag &= (~ECHO); //关回显
//此处回显关不关是不影响本实验的,在手机实验的时候会有影响,因为手机
//用的串口1的终端是没有打开的,若是不关的话,会把所以的输入全部读到,所以
//要将回显关掉
tcsetattr(fd,TCSANOW,&options);
return fd;
}
void LED_right(int gpg_fd)
{
int led=0x04;
int i,j;
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
write(gpg_fd, &led, 1);
led = led >>1;
usleep(300*1000);
if(led == 0x00)
{
led = 0x4;
}
}
}
}
void LED_left(int gpg_fd)
{
int led=0x01;
int i,j;
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
write(gpg_fd, &led, 1);
led = led <<1;
usleep(300*1000);
if(led == 0x08)
{
led = 0x1;
}
}
}
}
void LED_flicker(int gpg_fd)
{
int led1=0x07;
int led2=0x00;
int i;
for(i=0;i<3;i++)
{
write(gpg_fd, &led1, 1);
usleep(300*1000);
write(gpg_fd, &led2, 1);
usleep(300*1000);
}
}
二、串口读取指令,并控制LED灯
int main(int argc, char **argv)
{
int uart_fd;
char key;
int gpg_fd;
uart_fd = uart_Init();
ioctl(gpg_fd, GPIO_CFG_PIN_OUT, GPIO_PIN_AREA(0,2));
gpg_fd = open("/dev/gpgdriver", O_RDWR);
while(1)
{
read(uart_fd,&key,sizeof(key));
if(key=='1')
LED_right(gpg_fd);
else if(key=='2')
LED_left(gpg_fd);
else if(key=='3')
LED_flicker(gpg_fd);
else printf("\nkey=%c\n",key);
}
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~gpio.h,同上
还少一个LED相关驱动文件.ko文件