#include "sys.h"
#include "usart.h"
#if SYSTEM_SUPPORT_OS
#include "includes.h"
#endif
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while((USART3->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
USART3->DR = (u8) ch;
return ch;
}
#endif
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
#if EN_USART3_RX //èç1ûê1Äüá˽óêÕ
char USART_RX_BUF[USART_REC_LEN];
u16 USART_RX_STA=0;
void uart3_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ê1ÄüUSART1£¬GPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOB.10
//USART3_RX GPIOB.11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOB.11
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
}
void USART3_IRQHandler(void)
{
u8 Res;
#if SYSTEM_SUPPORT_OS
OSIntEnter();
#endif
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART3);
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)
{
if(Res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x8000;
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
}
}
}
}
#if SYSTEM_SUPPORT_OS
OSIntExit();
#endif
}
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////
//èç1ûê1óÃucos,Ôò°üà¨ÏÂÃæμÄí·Îļt¼′¿é.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos ê1óÃ
#endif
//¼óèëòÔÏÂ′úÂë,Ö§3Öprintfoˉêy,¶ø2»DèòaÑ¡Ôñuse MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿aDèòaμÄÖ§3Öoˉêy
struct __FILE
{
int handle;
};
FILE __stdout;
//¶¨òå_sys_exit()òÔ±üÃaê1óðëÖ÷»úÄ£ê½
_sys_exit(int x)
{
x = x;
}
//Öض¨òåfputcoˉêy
int fputc(int ch, FILE *f)
{
while((USART3->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
USART3->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART3_RX //èç1ûê1Äüá˽óêÕ
//′®¿ú3ÖD¶Ï·tÎñ3ìDò
//×¢òa,¶áè¡USARTx->SRÄü±üÃaÄaÃûÆäÃîμÄ′íÎó
char USART_RX_BUF[USART_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬ ½óêÕíê3é±êÖ¾
//bit14£¬ ½óêÕμ½0x0d
//bit13~0£¬ ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
u16 USART_RX_STA=0; //½óêÕ×′쬱ê¼Ç
void uart3_init(u32 bound){
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ê1ÄüUSART3£¬GPIOBê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOB.10
//USART3_RX GPIOB.113õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOB.11
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//′®¿ú2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
USART_Cmd(USART3, ENABLE); //ê1Äü′®¿ú3
}
void USART3_IRQHandler(void) //′®¿ú3ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART3); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART_RX_STA|=0x8000; //½óêÕíê3éáË
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
}
}
}
}
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntExit();
#endif
}
#endif
一周热门 更多>