初级会员

- 积分
- 54
- 金钱
- 54
- 注册时间
- 2013-6-28
- 在线时间
- 4 小时
|

楼主 |
发表于 2014-7-17 11:17:46
|
显示全部楼层
#include "stm32f4xx.h"
#include <stdio.h>
#include "uart.h"
//?®??1????·??????ò
//×???,????USARTx->SR??±??????????????í?ó
u8 USART_RX_BUF[USART_REC_LEN]; //????????,×??óUSART_REC_LEN??×???.
//????×???
//bit15?? ?????ê??±ê??
//bit14?? ??????0x0d
//bit13~0?? ???????????§×???????
u16 USART_RX_STA=0; //????×???±ê??
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
/*static void Delay_ARMJISHU(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--)
{
}
}*/
void STM32_Shenzhou_COMInit(USART_InitTypeDef* USART_InitStruct)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Enable GPIO clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* Enable UART clock */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); /* Connect PXx to USARTx_Tx*/
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_Init(USART3, USART_InitStruct); /* USART configuration */
#if EN_USART3_RX //??????????????
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//??????????2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×???????2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//????????
#endif
USART_Cmd(USART3, ENABLE); /* Enable USART */
}
void STM32_AD9854_COMInit(USART_InitTypeDef* USART_InitStruct)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable UART clock */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); /* Connect PXx to USARTx_Tx*/
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_Init(USART1, USART_InitStruct); /* USART configuration */
#if EN_USART1_RX //??????????????
//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_ITConfig(USART1, USART_IT_RXNE, ENABLE);//????????
#endif
USART_Cmd(USART1, ENABLE); /* Enable USART */
}
void Printf_Init(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 57600;
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;
STM32_Shenzhou_COMInit(&USART_InitStructure);
}
void AD9854_COM_Init(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 57600;
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;
STM32_AD9854_COMInit(&USART_InitStructure);
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART3, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{}
return ch;
}
void USART1_IRQHandler(void) //?®??1????·??????ò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //????????(????????????±?????0x0d 0x0a?á??)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //????????????????
//USART_RX_STA = 0x8000;
//USART_RX_BUF[0]= Res ;
if((USART_RX_STA&0x8000)==0)//???????ê??
{
if(USART_RX_STA&0x4000)//????????0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//?????í?ó,????????
else
{
USART_RX_STA|=0x8000; //?????ê????
//printf("?????ê??\r\n");
}
}
else //????????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;//?????????í?ó,????????????
}
}
}
}
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntExit();
#endif
}
void USART3_IRQHandler(void) //?®??1????·??????ò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntEnter();
#endif
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //????????(????????????±?????0x0d 0x0a?á??)
{
Res =USART_ReceiveData(USART3);//(USART1->DR); //????????????????
if((USART_RX_STA&0x8000)==0)//???????ê??
{
if(USART_RX_STA&0x4000)//????????0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//?????í?ó,????????
else
{
USART_RX_STA|=0x8000; //?????ê????
//printf("?????ê??\r\n");
}
}
else //????????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;//?????????í?ó,????????????
}
}
}
}
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntExit();
#endif
}
|
|