|
|
|
|
#include "usart2.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u8 USART2_RX_BUF[USART2_REC_LEN]; //<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>USART_REC_LEN<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>.ĩ<EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>
|
|
|
|
|
u16 USART2_RX_STA; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
void uart2_Init(u32 baudrate)
|
|
|
|
|
{
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹ<EFBFBD><EFBFBD>USART2<EFBFBD><EFBFBD>GPIOAʱ<EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GPIOA.2
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GPIOA.3
|
|
|
|
|
|
|
|
|
|
//Usart1 NVIC <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//<EFBFBD><EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>VIC<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
//USART <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
USART_InitStructure.USART_BaudRate = baudrate;//<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//<EFBFBD>ֳ<EFBFBD>Ϊ8λ<EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD>ʽ
|
|
|
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;//һ<EFBFBD><EFBFBD>ֹͣλ
|
|
|
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>żУ<EFBFBD><EFBFBD>λ
|
|
|
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //<EFBFBD>շ<EFBFBD>ģʽ
|
|
|
|
|
|
|
|
|
|
USART_Init(USART2, &USART_InitStructure); //<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2
|
|
|
|
|
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
USART_Cmd(USART2, ENABLE); //ʹ<EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD>2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USART2_IRQHandler(void) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
u8 Res;
|
|
|
|
|
|
|
|
|
|
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>(<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x0d 0x0a<EFBFBD><EFBFBD>β)
|
|
|
|
|
{
|
|
|
|
|
Res =USART_ReceiveData(USART2); //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
if((USART2_RX_STA&0x8000)==0)//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if(USART2_RX_STA&0x4000)//<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD>0x0d
|
|
|
|
|
{
|
|
|
|
|
if(Res!=0x0a)USART2_RX_STA=0;//<EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼ
|
|
|
|
|
else USART2_RX_STA|=0x8000; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
else //<EFBFBD><EFBFBD>û<EFBFBD>յ<EFBFBD>0X0D
|
|
|
|
|
{
|
|
|
|
|
if(Res==0x0d)USART2_RX_STA|=0x4000;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
USART2_RX_BUF[USART2_RX_STA&0X3FFF]=Res ;
|
|
|
|
|
USART2_RX_STA++;
|
|
|
|
|
if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
static u8 USART2_TX_BUF[200];
|
|
|
|
|
|
|
|
|
|
void u2_printf(char* fmt,...)
|
|
|
|
|
{
|
|
|
|
|
u16 i,j;
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap,fmt);
|
|
|
|
|
vsprintf((char*)USART2_TX_BUF,fmt,ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
i=strlen((const char*)USART2_TX_BUF); //<EFBFBD>˴η<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for(j=0;j<i;j++) //ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
while((USART2->SR&0X40)==0); //ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
USART2->DR=USART2_TX_BUF[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uart2_send(char* fmt, int len)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
for(i=0;i<len;i++){
|
|
|
|
|
USART_SendData(USART2, fmt[i]);//<EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//<EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uart2_test(void)
|
|
|
|
|
{
|
|
|
|
|
u32 len,t;
|
|
|
|
|
static u32 times=0;
|
|
|
|
|
if(USART2_RX_STA&0x8000)
|
|
|
|
|
{
|
|
|
|
|
len=USART2_RX_STA&0x3fff;//<EFBFBD>õ<EFBFBD><EFBFBD>˴ν<EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
u2_printf("\r\n usart2:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>ϢΪ:\r\n");
|
|
|
|
|
for(t=0;t<len;t++)
|
|
|
|
|
{
|
|
|
|
|
USART_SendData(USART2, USART2_RX_BUF[t]);//<EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//<EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
u2_printf("\r\n\r\n");//<EFBFBD><EFBFBD><EFBFBD>뻻<EFBFBD><EFBFBD>
|
|
|
|
|
USART2_RX_STA=0;
|
|
|
|
|
}else
|
|
|
|
|
{
|
|
|
|
|
times++;
|
|
|
|
|
if(times%200==0)u2_printf("usart2:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD>Իس<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|