串行通信總結(實現兩個單片機之間的通信)http://blog.csdn.net/ktigerhero3/article/details/54134254
本文主要介紹串行通信及串行通信的應用。目標是實現單片機之間的通信。
1.串行通信的基本概念
串行是與並行想對應的,並行通信是指數據的各位同時被傳送。串行通信是將要傳送的數據一位位的依次順序發送。
串行通信實現的是兩個對象之間的數據傳遞,對象通常是單片機。通信實際上是在兩個單片機上連上線,通過線路來傳遞信息。
如圖,調制解調器非常重要,其作用是實現數字信號和模擬信號的轉換。但是注意,調制解調器是遠距離傳輸纔有用。近距離傳輸不需要調制解調器(零Modem方式)。因此進行單片機的實驗只需要將相應接口的線路連好就行。連接示意圖如圖
2.STM32單片機與電腦串行通信
1.信號線的連接
單片機與電腦通信通常用的是USB接口連接電腦。那麼就需要首先將串口轉爲USB,STM32上有相應的硬件實現該功能,我們只需要看電路圖線路是否連接。
以下是正點原子miniSTM32的連線步驟:
(1)查單片機電路圖,找到主板芯片上的U1_RXD與U_TXD接口。
(2)找到USB_232的RXD與TXD接口
(3)如果電路圖上線路未連接,將主板芯片的U1_RXD通過跳線與USB_232上的TXD連接,主板芯片的U1_TXD通過跳線與USB_232上的UXD連接。
2.程序的編寫
由於採用STM32官方固件庫,因此編寫串口通信程序非常簡單。
思路:
(1)初始化串口
(2) 調用USART_SendData函數向串口發送數據。
其中初始化串口包括
1) 串口時鐘使能,GPIO 時鐘使能
2) 串口復位
3) GPIO 端口模式設置 端口模式設置
4) 串口參數初始化
5) 開啓中斷並且初始化 NVIC (如果需要開啓中斷才這個步驟 )
6) 使能串口
7) 編寫中斷函數
那麼最簡單的串口通信程序如下,注意,由於沒有編寫中斷函數,此程序只發不收。發送的數據永遠是01。
代碼1
#include "stm32f10x.h"
void my_delay_ms(int time);
void my_delay_ms(int time)
{
int i=0;
while(time--)
{
i=12000;
while(i)
{
i--;
}
}
}
void uart_init(u32 bound){
//GPIO端口設置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA時鐘
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//搶佔優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的參數初始化VIC寄存器
//USART 初始化設置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長爲8位數據格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數據流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啓串口接受中斷
USART_Cmd(USART1, ENABLE); //使能串口1
}
uint16_t str=1;
int main()
{
u16 times=0;
uart_init(115200);
//key_init();
while(1)
{
times++;
my_delay_ms(10);
if(times%10000)
{
USART_SendData(USART1, str);//向串口1發送數據
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
在PC端打開串口調試助手,可以看到不斷接收到數據01
3.linux系統單片機與電腦串行通信
1.信號線的連接
本系統採用訊爲的開發板,開發板裝的爲linux系統,由於開發板自帶UART(串口)接口,因此使用UART轉USB線,一端連開發板的UART接口,一端連電腦的USB就行了,打開串口調試助手,就可以查看串口數據了。
2.程序的編寫
思路:
(1)在linux系統下安裝串口驅動
(2)編寫串口發送函數
串口發送函數步驟爲:
1)fopen打開串口對應的設備
2)設置參數,如波特率等
3)使用write函數向串口中寫數據
代碼和第4節類似。
打開串口調試助手,就能在電腦屏幕上看到所發送的數據了。
4.STM32單片機與linux系統單片機串行通信
1.信號線的連接
如果單片機都能和電腦通信,那麼兩個單片機的串口通信,只需要將串口線連接起來就行,準備三根跳線,第一根連接單1的RXD和單2的TXD,第二根連接單1的TXD和單2的RXD,第三根連接單1的GND和單2的GND。OK,可以發送數據了。
2.程序的編寫
本代碼實現下位機STM32發送數字1,上位機linux系統單片機接受到數字1並打印出來。
1.STM32程序和代碼1一樣,簡單的不斷髮送1。
2.linux系統單片機代碼如代碼2,簡單不斷讀發送的數據並輸出。
代碼2
#include <stdio.h>
#include <string.h>
#include <unistd.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);
int main(int argc, char* argv[])
{
printf("hello,run ok\n");
int fd, read_num = 0;
//char *uart3 = "/dev/ttySAC3";
char buffer[1024],buffer_test[1024];
memset(buffer, 0, 1024);
memset(buffer_test, 0, 1024);
if(argc < 2)
{
printf("usage: ./uarttest /dev/ttySAC3\n");
return 0;
}
if((fd = open(argv[1], O_RDWR|O_NOCTTY|O_NDELAY))<0)
{
printf("open %s is failed\n", argv[1]);
return 0;
}
else{
set_opt(fd, 115200, 8, 'N', 1);
int n=10000000;
int k=0;
while(k<n){
k++;
printf("%d\n",k);
sleep(1);
memset(buffer, 0, 256);
read_num = read(fd, buffer, 255);
printf("read_num=%d\n",read_num);
if(read_num>0){
printf("%s\n",buffer);
}else{
printf("read error\n");
}
}
fd=close(fd);
}
return 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;
}