//¼óèëòÔÏÂ′úÂë,Ö§3Öprintfoˉêy,¶ø2»DèòaÑ¡Ôñuse MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿aDèòaμÄÖ§3Öoˉêy
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef¡ˉ d in stdio.h. */
FILE __stdout;
//¶¨òå_sys_exit()òÔ±üÃaê1óðëÖ÷»úÄ£ê½
_sys_exit(int x)
{
x = x;
}
//Öض¨òåfputcoˉêy
int fputc(int ch, FILE *f)
{
while((UART4->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
UART4->DR = (u8) ch;
return ch;
}
#endif
//end
//////////////////////////////////////////////////////////////////
#include "sys.h"
#include "usart.h"
#include <string.h>
//¼óèëòÔÏÂ′úÂë,Ö§3Öprintfoˉêy,¶ø2»DèòaÑ¡Ôñuse MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿aDèòaμÄÖ§3Öoˉêy
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef¡ˉ d in stdio.h. */
FILE __stdout;
//¶¨òå_sys_exit()òÔ±üÃaê1óðëÖ÷»úÄ£ê½
_sys_exit(int x)
{
x = x;
}
//Öض¨òåfputcoˉêy
int fputc(int ch, FILE *f)
{
while((UART4->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
UART4->DR = (u8) ch;
return ch;
}
#endif
//end
//////////////////////////////////////////////////////////////////
#if EN_UART4_RX //èç1ûê1Äüá˽óêÕ
//′®¿ú1ÖD¶Ï·tÎñ3ìDò
//×¢òa,¶áè¡USARTx->SRÄü±üÃaÄaÃûÆäÃîμÄ′íÎó
u8 USART_RX_BUF[USART_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬ ½óêÕíê3é±êÖ¾
//bit14£¬ ½óêÕμ½0x0d
//bit13~0£¬ ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
u16 USART_RX_STA=0; //½óêÕ×′쬱ê¼Ç
void UART4_IRQHandler(void)
{
u8 res;
if(UART4->SR&(1<<5))//½óêÕμ½êy¾Y
{
res=UART4->DR;
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ê¼½óêÕ
}
}
}
}
}
#endif
//3õê¼»ˉIO ′®¿ú1
//pclk1:pclk1ê±ÖóÆμÂê(Mhz)
//bound:2¨ìØÂê
void uart_init(u32 pclk1,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk1*1000000)/(bound*16);//μÃμ½USARTDIV@OVER8=0
mantissa=temp; //μÃμ½Õûêy2¿·Ö
fraction=(temp-mantissa)*16; //μÃμ½D¡êy2¿·Ö@OVER8=0
mantissa<<=4;
mantissa+=fraction;
RCC->AHB1ENR|=1<<0; //ê1ÄüPORTA¿úê±Öó
// RCC->APB2ENR|=1<<4; //ê1Äü′®¿ú1ê±Öó
RCC->APB1ENR|=1<<19; //U4ê±Öóê1Äü
// GPIO_Set(GPIOA,PIN9|PIN10,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU);//PA9,PA10,¸′óÃ1|Äü,éÏà-êä3ö
GPIO_Set(GPIOA,PIN0|PIN1,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU);//PA0,PA1,¸′óÃ1|Äü,éÏà-êä3ö
// GPIO_AF_Set(GPIOA,9,7); //PA9,AF7
// GPIO_AF_Set(GPIOA,10,7);//PA10,AF7
GPIO_AF_Set(GPIOA,0,8); //PA9,AF7
GPIO_AF_Set(GPIOA,1,8);//PA10,AF7
//2¨ìØÂêéèÖÃ
UART4->BRR=mantissa; //2¨ìØÂêéèÖÃ
UART4->CR1&=~(1<<15); //éèÖÃOVER8=0
UART4->CR1|=1<<3; //′®¿ú·¢Ëíê1Äü
#if EN_UART4_RX //èç1ûê1Äüá˽óêÕ
//ê1Äü½óêÕÖD¶Ï
UART4->CR1|=1<<2; //′®¿ú½óêÕê1Äü
UART4->CR1|=1<<5; //½óêÕ»o3åÇø·Ç¿ÕÖD¶Ïê1Äü
MY_NVIC_Init(3,3,UART4_IRQn,2);//×é2£¬×îμíóÅÏ輶
#endif
UART4->CR1|=1<<13; //′®¿úê1Äü
}
void SendString(u8*data)
{
int t;
int i;
t=strlen((const char*)data);
for(i=0;i<t;i++)
{
UART4->DR=data;
while((UART4->SR&0X40)==0);//μè′y·¢Ëí½áêø
}
}
一周热门 更多>