c语言uart串口通信编程s3c2410为例

 嵌入式linux系统串口通信示例代码

/*
author:sun
date: summer 2012
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <fcntl.h>
#include <termios.h>

//#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(void)
{
	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 &= ~CSIZE;	
	options.c_cflag |= CS8;				

	options.c_lflag &= (~ICANON);  

	tcsetattr(fd,TCSANOW,&options);
	
	
	return fd;
	
}

int main(int argc, char *argv[])
{
	int fd;
	char buf;
	fd=uart_init();
	while(read(fd,&buf,2)>0);
	printf("%s",buf);
	tcsetattr(fd,TCSANOW,&oldoptions);
	return 0;
}
/*
author:sun
date: summer 2012
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include "s5pv210-gpio.h"

struct termios options, oldoptions;

int uart_init(void)
{
	int fd;
	char test[50];
	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;			
	options.c_cflag &= ~CSIZE;	
	options.c_cflag |= CS8;				

	options.c_lflag &= (~ICANON);  	
	
	tcsetattr(fd,TCSANOW,&options);
	
	return fd;	
}
int ledd()
{
	int fb;
	fb= open("/dev/gpH0",O_WRONLY);
	if(fb<0)
	{
		perror("open error");
	}
	ioctl(fb,GPIO_SET_PIN_OUT,0x1);
	ioctl(fb,GPIO_SET_PIN_OUT,0x2);
	ioctl(fb,GPIO_SET_PIN_OUT,0x3);
	
}
int main(int argc, char *argv[])
{
	int fa;
	char buf;
	int led;
	fa=uart_init();
	led=ledd();
	char led=0x01;
	char down=0x00;
	while(read(fa,&buf,1)>0);
	{	printf("%d",buf);
		
	switch(buf)
		
	case 1:
	{
			led=0x02;
			write(fb,&led,sizeof(led));
			sleep(1);
			write(fb,&down,sizeof(led));
	}
		
	case 2:
	{
		led=0x04;
		write(fb,&led,sizeof(led));
		sleep(1);
		write(fb,&down,sizeof(led));
	}	
	case 3:
	{
		led=0x08;
		write(fb,&led,sizeof(led));
		sleep(1);
		write(fb,&down,sizeof(led));
	}	
	case 4:
	{
		led=0x02;
		while(1)
		{
			sleep(1);
			write(fb,&led,sizeof(led));	
			led=led<<1;
			if(led==0x2<<3)
			{
				led=0x02;
			}
		}	
	}
}
	
   tcsetattr(fa,TCSANOW,&oldoptions);
	return 0;
}
/*
author:sun
date: summer 2012
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <fcntl.h>
#include <termios.h>

#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(void)
{
	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;
}

int main(int argc, char *argv[])
{
	
	tcsetattr(fd,TCSANOW,&oldoptions);
	return 0;
}
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值