stm32串口程序求教
stm32串口程序: 用串口助手发送一个到stm32串口1,与十六进制数组
比较:a[] = {0xad,0x2,0x6,0x3F,0x4B}
如果相同 ,将另一个数组发送出去,b[] = {0xad,0x2,0x6,0x3F,0x4B,0x43,0x43,0xa3,0xc3,0xb3,0x43};
要代码 ,要求中断实现,不要理论,回答请贴代码 ,我是菜鸟。。 stm32
[解决办法]
给你个代码学习下:
NVIC_InitTypeDef
NVIC_InitStructure;
/*
Enable
the
USART1
Interrupt
*/
NVIC_InitStructure.NVIC_IRQChannel
=
USART1_IRQChannel;
//
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority
=
1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority
=
10;
//
NVIC_InitStructure.NVIC_IRQChannelCmd
=
ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void
GPIO_USART_Configuration(void)
{
GPIO_InitTypeDef
GPIO_InitStructure;
/*
Configure
USART1
Tx
(PA.09)
as
alternate
function
push-pull
*/
GPIO_InitStructure.GPIO_Pin
=
GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode
=
GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed
=
GPIO_Speed_50MHz;
GPIO_Init(GPIOA,
&GPIO_InitStructure);
/*
Configure
USART1
Rx
(PA.10)
as
input
floating
*/
GPIO_InitStructure.GPIO_Pin
=
GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode
=
GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,
&GPIO_InitStructure);
}
/*******************************************************************************
*
Function
Name
:
USART_Configuration
*
Description
:
Configures
the
USART1.
*
Input
:
None
*
Output
:
None
*
Return
:
None
*******************************************************************************/
void
USART_Configuration(unsigned
long
baudrate)
{
USART_InitTypeDef
USART_InitStructure;
USART_ClockInitTypeDef
USART_ClockInitqlt;
/*
USART1
configuration
------------------------------------------------------*/
/*
USART1
configured
as
follow:
-
BaudRate
=
115200
baud
-
Word
Length
=
8
Bits
-
One
Stop
Bit
-
No
parity
-
Hardware
flow
control
disabled
(RTS
and
CTS
signals)
-
Receive
and
transmit
enabled
-
USART
Clock
disabled
-
USART
CPOL:
Clock
is
active
low
-
USART
CPHA:
Data
is
captured
on
the
middle
-
USART
LastBit:
The
clock
pulse
of
the
last
data
bit
is
not
output
to
the
SCLK
pin
*/
USART_InitStructure.USART_BaudRate
=
baudrate;
USART_InitStructure.USART_WordLength
=
USART_WordLength_8b;
USART_InitStructure.USART_StopBits
=
USART_StopBits_1;
USART_InitStructure.USART_Parity
=
USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl
=
USART_HardwareF
lowControl_None;
USART_InitStructure.USART_Mode
=
USART_Mode_Rx
[解决办法]
USART_Mode
_Tx;
//
USART_Init(USART1,
&USART_InitStructure);
USART_ClockInitqlt.USART_Clock=
USART_Clock_Disable;
USART_ClockInitqlt.USART_CPOL
=
USART_CPOL_Low;
//
USART_Cl
ockInitqlt.USART_CPHA
=
USART_CPHA_2Edge;
//
USART_ClockInitqlt.
USART_LastBit
=
USART_LastBit_Disable;//
USART_ClockInit(USART1,&USART_ClockInitqlt);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,
ENABLE);//
}
//
串口初始化
void
UART0_InitUART(
unsigned
long
baudrate
)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1
[解决办法]
RCC_APB2Peri
ph_GPIOA,
ENABLE);
NVIC_USART_Configuration();
GPIO_USART_Configuration();
USART_Configuration(baudrate);
UART0_RxTail
=
0;
UART0_RxHead
=
0;
UART0_TxTail
=
0;
UART0_TxHead
=
0;
}
//--------------------------------------------------
void
SIGNAL_Usart0_RX(void)
//
接收中断
{
u8
data;
u8
tmphead;
data
=
USART_ReceiveData(USART1);
//
Frame_counting
=
0;
tmphead
=
(
UART0_RxHead
+
1
)
&
UART0_RX_BUFFER_MAS
K;
//
UART0_RxHead
=
tmphead;
//
if
(
tmphead
==
UART0_RxTail
)
{
//
这里加入队列溢出保护
}
UART0_RxBuf[tmphead]
=
data;
}
//--------------------------------------------------
void
SIGNAL_Usart0_TX(void)
//
发送中断
{
u8
tmptail;
if
(
UART0_TxHead
!=
UART0_TxTail
)
{
tmptail
=
(
UART0_TxTail
+
1
)
&
UART0_TX_BUFFER_MAS
K;
UART0_TxTail
=
tmptail;
USART_SendData(USART1,
UART0_TxBuf[tmptail]);
}
else
{
USART_ITConfig(USART1,USART_IT_TXE,DISABLE);//
}
}
//
从接收队列读取一个字符
unsigned
char
UART0_ReceiveByte(
void
)
{
unsigned
char
tmptail;
while
(
UART0_RxHead
==
UART0_RxTail
);
//
tmptail
=
(
UART0_RxTail
+
1
)
&
UART0_RX_BUFFER_MASK;
UART0_RxTail
=
tmptail;
return
UART0_RxBuf[tmptail];
}
//
将一个字节放入发送队列
void
UART0_TransmitByte(
char
data
)
{
unsigned
char
tmphead;
tmphead
=
(
UART0_TxHead
+
1
)
&
UART0_TX_BUFFER_MASK;
while
(
tmphead
==
UART0_TxTail
);
UART0_TxBuf[tmphead]
=
data;
UART0_TxHead
=
tmphead;
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);//
}
//
发送一个字符串
void
UART0_Transmitstr(
char
*str)
//
{
//
unsigned
int
i="0";
while(*str)
{
UART0_TransmitByte(*str++);
}
}
//
清空接收缓冲区
void
UART0_ClearspatialReceiveBuffer(void)
{
unsigned
char
x;
x
=
0;
UART0_RxTail
=
x;
UART0_RxHead
=
x;
}
//
等待接收完成
void
UART0_RXEND(void)
{
//UART0_Transmitstr("wite_start-");
unsigned
int
x="0";
while((!UART0_DataInReceiveBuffer())&(x<100))//
{
x++;
DelayMs(10);//10ms
}
x=0;
while((Frame_counting<2)&&(x<1000))//?D??ê
y?Y?ó
ê
?í
ê
3é
,×
??à
1sí
?3?
{
x++;
DelayMs(1);//1ms
}
}
/*******************************************************************************
*
Function
Name
:
USART1_IRQHandler
*
Description
:
This
function
handles
USART1
global
interrupt
request.
*
Input
:
None
*
Output
:
None
*
Return
:
None
*******************************************************************************/
void
USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);//
SIGNAL_Usart0_RX();
}
if(USART_GetITStatus(USART1,USART_IT_TXE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_TXE);
//
SIGNAL_Usart0_TX();
}
}
[解决办法]
几步:第一配置时钟,时钟使能,第二配置中断,中断使能。第三配置GPIO。第四配置串口通信模式,比如波特率什么的。第五,实现串口中断程序 第六,在主循环里面处理中断接收缓冲区
[解决办法]
void SIGNAL_Usart0_RX(void) //接收中断
{
u8 data;
u8 tmphead;
data = USART_ReceiveData(USART1);
Frame_counting = 0;
tmphead = ( UART0_RxHead + 1 ) & UART0_RX_BUFFER_MASK;
UART0_RxHead = tmphead;
if ( tmphead == UART0_RxTail )
{
//这里加入队列溢出保护
}
UART0_RxBuf[tmphead] = data;
}
//--------------------------------------------------
void SIGNAL_Usart0_TX(void) //发送中断
{
u8 tmptail;
if ( UART0_TxHead != UART0_TxTail )
{
tmptail = ( UART0_TxTail + 1 ) & UART0_TX_BUFFER_MASK;
UART0_TxTail = tmptail;
USART_SendData(USART1,
UART0_TxBuf[tmptail]);
}
else
{
USART_ITConfig(USART1,USART_IT_TXE,DISABLE);//
}
}
//照着人家的写个中断程序,然后你在主循环里面处理UART0_TxBuf和UART0_RxBuf缓冲区就可以了。具体代码你自己写下嘛,又不难
[解决办法]
tmptail = ( UART0_TxTail + 1 ) & UART0_TX_BUFFER_MASK;这个一看了很久都没看明白,不是循环队列么? &应该写成%