您的位置 首页 传感器

ARM 与 51单片机通讯

硬件mini2440核心板,另外一块带串口的单片机学习板(挂着DS18B20),学习板连续输出DS18B20的温度值,波特率19200。在一个文件夹里新建…

硬件 mini2440中心板,别的一块带串口的单片机学习板(挂着DS18B20),学习板接连输出DS18B20的温度值,波特率19200。

在一个文件夹里新建一个文件,我的是voltage_set.c(今后用来调压)

#include
#include
#include
#include
#include
#include
#include
#include
#include

#define FALSE -1
#define TRUE1
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, //
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
perror(“tcsetattr fd1”);
return;
}
tcflush(fd,TCIOFLUSH); //????
}
}

int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options; //?¨???????á??
if ( tcgetattr( fd,&options) != 0) //?×???????????????è??options??,±???
{
perror(“SetupSerial 1”);
return(FALSE);
}
options.c_cflag &= ~CSIZE; //?????è??c_cflag??????°?????????????
switch (databits)
{
case 7:
options.c_cflag |= CS7; //?è??c_cflag????????????7??
break;
case 8:
options.c_cflag |= CS8; //?è??c_cflag????????????8??
break;
default:
fprintf(stderr,”Unsupported data size\n”); //???????????§??
return (FALSE);
}
switch (parity) //?è?????????é??c_cflag??c_iflag???§
{
case n:
case N:
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case o:
case O: options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case e:
case E:
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
default:
fprintf(stderr,”Unsupported parity\n”);
return (FALSE);
}

switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,”Unsupported stop bits\n”);
return (FALSE);
}

if (parity != n)
options.c_iflag |= INPCK;


options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;

tcflush(fd,TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror(“SetupSerial 3”);
return (FALSE);
}
return (TRUE);
}

int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY????·?????open????
if (-1 == fd)
{
perror(“Cant Open Serial Port”);
return -1;
}
else
return fd;
}

int main(int argc, char **argv)
{
int fd;
int nread;
char buff[512];
char *dev =”/dev/ttySAC1″;
fd = OpenDev(dev);
if (fd>0)
{
printf(“Open Serial succeed\n”);
set_speed(fd,19200);
}
else
{
printf(“Cant Open Serial Port!\n”);
exit(0);
}
if (set_Parity(fd,8,1,N)== FALSE)
{
printf(“Set Parity Error\n”);
exit(1);
}

while(1)
{
int i;
//char buff1[2]={0x5a,0x5a};
//write(fd,buff1,1);
while((nread = read(fd,buff,512))>0)
{
printf(“\nLen %d\n”,nread);
buff[nread+1]=\0;
for(i=0;iprintf(“0x%x”,buff[i]);
printf(“\n”);
}
}
//close(fd);
//exit(0);
}

完了之后arm-linux-gcc -o voltage_set voltage_set.c

生成voltage_set

下载到mini2440中心板中

履行#./voltage_set

把学习板的地与中心板的地相连,学习板的TX与中心板的RX2相连就可以看到温度值了,当然,我用16进制显现在超级终端上,关掉程序用ctrl+c

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/chuanganqi/274970.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部