新手入门
- 积分
- 10
- 金钱
- 10
- 注册时间
- 2016-8-29
- 在线时间
- 0 小时
|
1金钱
void Uasrt2_Timer_Init(void)
{
Usart2_Timer.Enable=TIMER_DELAY_OFF;
Usart2_Timer.Delay_Store =12;
Usart2_Timer.Delay=Usart2_Timer.Delay_Store;
}
//========================================
void Usart2_Timer_Inter(void)
{
OS_ERR err;
if(Usart2_Timer.Enable==TIMER_DELAY_ON)
{
Usart2_Timer.Delay--;
if(Usart2_Timer.Delay==0)
{
Usart2_Timer.Enable=TIMER_DELAY_OFF;
Usart2_Timer.Delay=Usart2_Timer.Delay_Store;
OSSemPost(&USART2_RX_PACKGE_SEM,OS_OPT_POST_1,&err);//·¢ËÍÐźÅÁ¿
}
}
}
//=========================================
// ´®¿Ú³õʼ»¯³ÌÐò
//=========================================
// ¸ÃͨѶ¿ÚΪRS485ͨѶ¿Ú
// Éæ¼°£ºTX /RX /DIR Èý¸ùͨѶ¿ØÖÆÏß
//-----------------------------------------
void USART2_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
Uasrt2_Timer_Init(); //ÊÕ°ü¶¨Ê±Æ÷³õʼ»¯
Rst_Usart2_TX_Buff(); //Çå¿Õ·¢ËÍ»º³åÇø
Rst_Usart2_RX_Buff(); //Çå¿Õ½ÓÊÕ»º³åÇø
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ʹÄÜGPIOAʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //ʹÄÜUSART2ʱÖÓ
//´®¿Ú2Òý½Å¸´ÓÃÓ³Éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2¸´ÓÃΪUSART2
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3¸´ÓÃΪUSART2
//USART2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA2ÓëGPIOA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ
GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»¯A2£¬A3
//PG8ÍÆÍìÊä³ö£¬485ģʽ·½Ïò¿ØÖÆ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE); //ʹÄÜGPIOGʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOG8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //Êä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ
GPIO_Init(GPIOG,&GPIO_InitStructure); //³õʼ»¯G8
//USART2 ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound; //²¨ÌØÂÊÉèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //×Ö³¤Îª8λÊý¾Ý¸ñʽ
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(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú2
USART_Cmd(USART2, ENABLE); //ʹÄÜ´®¿Ú 2
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //´®¿Ú1ÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //×ÓÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷¡¢
USART_ClearITPendingBit(USART2,USART_IT_TC); //Çå³ý·¢ËÍÖжϱêÖ¾
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Çå³ý½ÓÊÕÖжϱêÖ¾
Usart2TxdIntDis(); //½ûÖ¹´®¿Ú2·¢ËÍÊý¾ÝÍê³ÉÖжÏ
Usart2RxdIntDis(); //½ûÖ¹´®¿Ú2½ÓÊÕÊý¾ÝÍê³ÉÖжÏ
USART2_DIR_RX();
}
//============================================
void Rst_Usart2_TX_Buff(void) //Çå¿Õ·¢ËÍ»º³åÇø
{
Usart2_Data.Tx.Rd=0;
Usart2_Data.Tx.Wr=0;
}
void Rst_Usart2_RX_Buff(void) //Çå¿Õ½ÓÊÕ»º³åÇø
{
Usart2_Data.Rx.Rd=0;
Usart2_Data.Rx.Wr=0;
}
//***************************************************
// ·¢ËÍÊý¾Ý×¼±¸¼°Æô¶¯
//***************************************************
void USART2_Send_Start(unsigned char *sbuff,unsigned short int sLength)
{
unsigned short int i;
if(sLength==0) return;
for(i=0;i<sLength;i++)
{
Usart2_Data.Tx.Buff[Usart2_Data.Tx.Wr]=*(sbuff+i);
Usart2_Data.Tx.Wr++;
if(Usart2_Data.Tx.Wr==UART_BUFF_LENGTH) Usart2_Data.Tx.Wr=0;
if(Usart2_Data.Tx.Wr==Usart2_Data.Tx.Rd)
{
Usart2_Data.Tx.Rd++;
if(Usart2_Data.Tx.Rd==UART_BUFF_LENGTH) Usart2_Data.Tx.Rd=0;
}
}
USART2_DIR_TX();
Usart2TxdIntEn();
}
//--------------------------------------
void Usart2_Send_Data(void) //´®¿Ú·¢ËÍÊý¾Ý
{
if(Usart2_Data.Tx.Rd!=Usart2_Data.Tx.Wr)
{
USART_SendData(USART2,Usart2_Data.Tx.Buff[Usart2_Data.Tx.Rd]);
Usart2_Data.Tx.Rd++;
if(Usart2_Data.Tx.Rd>=UART_BUFF_LENGTH) Usart2_Data.Tx.Rd=0;
}
else
{
Usart2TxdIntDis();
USART2_DIR_RX();
}
//OSSemPost(&USART2_TX_END_SEM,OS_OPT_POST_1,&err);//·¢ËÍÐźÅÁ¿
}
//*******************************************************
void Usart2_Receive_Data(void) //´®¿Ú½ÓÊÕÊý¾Ý
{
Usart2_Data.Rx.Buff[Usart2_Data.Rx.Wr]=USART_ReceiveData(USART2);
Usart2_Data.Rx.Wr++;
if(Usart2_Data.Rx.Wr==UART_BUFF_LENGTH) Usart2_Data.Rx.Wr=0;
if(Usart2_Data.Rx.Wr==Usart2_Data.Rx.Rd) Usart2_Data.Rx.Rd++;
if(Usart2_Data.Rx.Rd==UART_BUFF_LENGTH) Usart2_Data.Rx.Rd=0;
}
//------------------------------------------------------
unsigned short int Usart2_Take_Data(unsigned char *sbuff)
{
unsigned short int sLength=0;
if(Usart2_Data.Rx.Rd==Usart2_Data.Rx.Wr) return sLength;
for(;Usart2_Data.Rx.Rd!=Usart2_Data.Rx.Wr;)
{
*(sbuff+sLength)=Usart2_Data.Rx.Buff[Usart2_Data.Rx.Rd];
Usart2_Data.Rx.Rd++;
if(Usart2_Data.Rx.Rd==UART_BUFF_LENGTH) Usart2_Data.Rx.Rd=0;
sLength++;
}
return sLength;
}
//*********************************************
// ´®¿ÚÖжϷþÎñ³ÌÐò
//*********************************************
void USART2_IRQHandler(void) //´®¿Ú2ÖжϷþÎñ³ÌÐò
{
#if SYSTEM_SUPPORT_OS //ʹÓÃUCOS²Ù×÷ϵͳ
OSIntEnter();
#endif
//-----------------------------------
if(USART_GetITStatus(USART2,USART_IT_TC)==SET) // ·¢ËÍÊý¾ÝÖжÏ
{
Usart2_Send_Data(); //´®¿Ú·¢ËÍÊý¾Ý
USART_ClearITPendingBit(USART2,USART_IT_TC); //Çå³ýÖжϴý´¦Àíλ
}
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //½ÓÊÕÊý¾ÝÖжÏ
{
Usart2_Receive_Data(); //´®¿Ú½ÓÊÕÊý¾Ý
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Çå³ýÖжϴý´¦Àíλ
Usart2_Timer_Enable(); //Æô¶¯×îºóÒ»¸ö×Ö½ÚÅжÏ
}
//-----------------------------------
#if SYSTEM_SUPPORT_OS
OSIntExit(); //Í˳öÖжÏ
#endif
}
//====================================================
//*****************************************
// ´®¿Ú2ͨѶ½âÎö´¦Àí
//*****************************************
unsigned char Temp_Buff[1000];
void task2_task(void *p_arg)
{
unsigned short int sLength;
OS_ERR err;
p_arg = p_arg;
Usart2RxdIntEn();
USART2_DIR_RX();
while(1)
{
OSSemPend(&USART2_RX_PACKGE_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //ÇëÇóÐźÅÁ¿
sLength= Usart2_Take_Data(&Temp_Buff[0]);
if(sLength!=0) USART2_Send_Start(&Temp_Buff[0],sLength);
}
}
//******************************************
|
|