usart。初始化
#include "sys.h"
#include "usart.h"
#include "led.h"
#include "delay.h"
///////////////////////////////////////
//3õê¼»ˉêy¾Y
uint8_t USART1_RX_BUF[USART1_REC_LEN];// ¶¨òå½óêÕ»o3å ×î′óUSART1_REC_LEN ¸ö×Ö½úÄ©×Ö½úÎa»»DD·û
uint8_t USART2_RX_BUF[USART2_REC_LEN]; //¶¨òåUSART2±äÆμ·¢ËíμÄêy¾Y»o3å
uint16_t USART1_RX_CNT=0; //¶¨òåUSART1½óêÕ×Ö·ûêy
uint16_t USART2_RX_CNT=0; //¶¨òåUSART2½óêÕ×Ö·ûêy
uint8_t USART1_RX_Flag=0;//1±íê¾½óêÕ½áêø 0±íê¾½óêÕÎ′íê3é
uint8_t USART2_RX_Flag=0;//1±íê¾½óêÕ½áêø 0±íê¾½óêÕÎ′íê3é
uint8_t CommErr=0; //0±íê¾í¨Ñ¶Õy3£ 1±íê¾CRC′íÎó 2±íê¾ 1|ÄüÂë′íÎó 7±íê¾í¨Ñ¶3¬ê± 8±íê¾í¨Ñ¶Î′¿aê¼
uint8_t ErrPace=0; // 1′ú±í′®¿úTIM6′y»úê±¼ä3¬3¤£¬·ÅÆúêy¾YÖ¡
// 2′ú±í′®¿úTIM7′y»úê±¼ä3¬3¤£¬·ÅÆúêy¾YÖ¡
// 3′ú±í¼ì2aμ½Ôëòô¡¢Ö¡′íÎó»òÕßD£Ñé′íÎó
uint16_t USART1_Frame_Distance=9;
uint16_t USART2_Frame_Distance=5;
//′®¿ú1ÖD¶Ï·tÎñ3ìDò
//×¢òa,¶áè¡USARTx->SRÄü±üÃaÄaÃûÆäÃîμÄ′íÎó
//uint8_t USART1_RX_BUF[USART1_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬ ½óêÕíê3é±êÖ¾
//bit14£¬ ½óêÕμ½0x0d
//bit13~0£¬ ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
uint16_t USART1_RX_STA=0; //USART1½óêÕ×′쬱ê¼Ç
uint16_t USART2_RX_STA=0; //USART2½óêÕ×′쬱ê־λ
uint16_t USART3_RX_STA=0; //USART3½óêÕ×′쬱ê־λ
//////////////////////////////////////////////////////////////////////////////////
//èç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((USART1->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
USART1->DR = (u8) ch;
return ch;
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CRCD£Ñé ×Ô¼ooóÃæìí¼óμÄ
const u8 auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40} ;
const u8 auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,0x43, 0x83, 0x41, 0x81, 0x80, 0x40} ;
uint16_t CRC16(u8 *puchMsg, u16 usDataLen)
{
uint8_t uchCRCHi = 0xFF ;
uint8_t uchCRCLo = 0xFF ;
uint32_t uIndex ;
while (usDataLen--)
{
uIndex = uchCRCHi ^ *puchMsg++ ;
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return ((uchCRCHi<< 8) | (uchCRCLo)) ;
}//uint16 crc16(uint8 *puchMsg, uint16 usDataLen)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if USART1_RX_EN //èç1ûê1Äüá˽óêÕ
static void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1| RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9 | GPIO_Pin_2; //PA9 | PA2 (USART1.TX | USART2.TX)¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_9| GPIO_Pin_2);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10 | GPIO_Pin_3;//PA10 | PA3 (USART1.RX | USART2.RX)¸¡¿Õêäèë
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_10| GPIO_Pin_3);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11; //PB11(USART3.TX)¸′óÃíÆíìê½êä3ö
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_11);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //PB10(USART3.RX)¸¡¿Õêäèë
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_10);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4; //USART3¿ØÖƽÅ
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; //USART2¿ØÖƽŠ0½óêÕ 1·¢Ëí
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_ResetBits(GPIOD,GPIO_Pin_7);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate=19200;
USART_InitStructure.USART_Mode=USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_WordLength=USART_WordLength_9b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn | USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART2);
USART_DeInit(USART3);
USART_InitStructure.USART_BaudRate=19200;
USART_InitStructure.USART_Mode=USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_WordLength=USART_WordLength_9b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART2,&USART_InitStructure);
USART_Init(USART3,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
// USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
USART2_TX_EN=1;
USART_Cmd(USART1,ENABLE);
USART_Cmd(USART2,ENABLE);
// USART_Cmd(USART3,ENABLE);
// Timer7_Init();
Timer6_Init();
}
//¶¨ê±Æ÷63õê¼»ˉ£¬ÅD¶ÏÖ÷»úêy¾YêÇ·ñ½óêÕíê3é
void Timer6_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);
//TIM23õê¼»ˉ
TIM_DeInit(TIM6);
TIM_TimeBaseStructure.TIM_Period=(USART1_Frame_Distance-1)*10;
TIM_TimeBaseStructure.TIM_Prescaler=719;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseStructure);
//TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_InternalClockConfig(TIM6);
//TIM6ÖD¶Ï·Ö×éÅäÖÃTIM2_IRQn
/*
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; //TIM7ÖD¶ÏÔ′
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //ÇàÕ¼óÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; //Ïìó|óÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //IRQÖD¶Ïê1Äü
NVIC_Init(&NVIC_InitStructure); //3õê¼»ˉNVIC_InitStructure
*/
}
void Timer7_Init(void) //¶¨ê±Æ÷73õê¼»ˉ£¬ÅD¶Ï′ó»ú·μ»Øêy¾YêÇ·ñ½óêÕíê3é
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,ENABLE);
//TIM73õê¼»ˉéèÖÃ
TIM_TimeBaseStructure.TIM_Period=USART2_Frame_Distance*10;//ms éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler=7199; //óÃà′éèÖÃ×÷ÎaTimxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖ죬éèÖüÆêyÆμÂêÎa10khz
TIM_TimeBaseStructure.TIM_ClockDivision=0; //éèÖÃê±Öó·Ö¸î
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM7,&TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructureÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE); //ÔêDíTIM7¸üDÂÖD¶Ï
//TIM7ÖD¶Ï·Ö×éÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel=TIM7_IRQn; //TIM7ÖD¶ÏÔ′
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //ÇàÕ¼óÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2; //Ïìó|óÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //IRQÖD¶Ïê1Äü
NVIC_Init(&NVIC_InitStructure); //3õê¼»ˉNVIC_InitStructure
}
void USART1_IRQHandler(void)
{
uint8_t Res;
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE)!=RESET)
{
Res=USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
if((USART1_RX_STA&0x8000)==0) //½óêÕÎ′íê3é
{
if(USART1_RX_STA&0x4000) //½óêÕμ½áË0x0D
{
if(Res!=0x0a) USART1_RX_STA=0; //½óêÕ′íÎó£¬ÖØD¿aê¼
else USART1_RX_STA|=0x8000; //½óêÕíê3é
}
else //Î′½óêÕμ½0x0D
{
if(Res==0x0D) USART1_RX_STA|=0x4000;
else
{
USART1_RX_BUF[USART1_RX_STA&0x3fff]=Res;
USART1_RX_STA++;
TIM_ClearFlag(TIM6,TIM_FLAG_Update);
TIM_SetCounter(TIM6,0); //½óêÕμ½ò»¸ö×Ö½ú½«¶¨ê±Æ÷Çå0
TIM_Cmd(TIM6,ENABLE); //¿aê¼¼Æê±
if(USART1_RX_STA>(USART1_REC_LEN-1)) USART1_RX_STA=0;
}
}
}
}
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntExit();
#endif
}
#endif
/*
static void RS485_Init(void)
{
}
*/
void USART2_IRQHandler(void)
{
uint8_t Res;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
if(USART_GetFlagStatus(USART2,USART_FLAG_NE|USART_FLAG_FE|USART_FLAG_PE))
ErrPace=2; //¼ì2aμ½Ôëòô′íÎó¡¢Ö¡′íÎóoíD£Ñé′íÎó
else
ErrPace=0;
Res=USART_ReceiveData(USART2);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
if(((USART2_RX_STA&0X3FFF)<256)&&(ErrPace==0))
{
USART2_RX_BUF[USART2_RX_STA&0x3fff]=Res;
//USART2_RX_CNT++;
USART2_RX_STA++;
// TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //Çå3y¶¨ê±Æ÷TIM7μÄÖD¶Ï
TIM_ClearFlag(TIM6,TIM_FLAG_Update);
TIM_SetCounter(TIM6,0); //½óêÕμ½ò»¸ö×Ö½ú½«¶¨ê±Æ÷Çå0
TIM_Cmd(TIM6,ENABLE); //¿aê¼¼Æê±
// TIM_ClearITPendingBit(TIM7,TIM_IT_Update); //Çå3y¶¨ê±Æ÷TIM7μÄÖD¶Ï
// TIM_SetCounter(TIM7,0); //½óêÕμ½ò»¸ö×Ö½ú½«¶¨ê±Æ÷Çå0
// TIM_Cmd(TIM7,ENABLE); //¿aê¼¼Æê±
}
else
printf("BBBBBBBBBBBBB");
}
}
void TIM6_IRQHandler(void)
{
// uint8_t i;
// uint16_t CalCRC;
}
void TIM7_IRQHandler(void)
{
uint8_t i=0;
if(TIM_GetITStatus(TIM7,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM7,TIM_IT_Update); //Çå3yÖD¶Ï±êÖ¾
TIM_Cmd(TIM7,DISABLE); //1ض¨ê±Æ÷
//USART2_TX_EN=1;//í£Ö1½óêÕ£¬ÇD»»Îa·¢Ëí×′ì¬
printf("
à′×Ô±äÆμÆ÷μÄ·′à¡ÏûÏ¢Îa£o
");
USART2_RX_Flag=1; //USART2½áêø±ê¼Çλ
ErrPace=1;
USART2_RX_BUF[USART2_RX_CNT]=0x0D;
USART2_RX_BUF[++USART2_RX_CNT]=0x0A;
for(i=0;i<USART2_RX_CNT;i++)
{
USART_SendData(USART1,USART2_RX_BUF[i]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
printf("
");
}
}
void COMM_Init(void)
{
USART1_Init();
}
一周热门 更多>