中级会员
 
- 积分
- 309
- 金钱
- 309
- 注册时间
- 2014-5-1
- 在线时间
- 75 小时
|
5金钱
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
u8 TX_Buf[8]={0x01,0x03,0x00,0x01,0x00,0x01,0xD5,0xCA};
int main(void)
{
u8 i=0;
RCC_Configuration(); //?????±????????
GPIO_Configuration();//??????????
USART_Configuration();
NVIC_Configuration();
GPIO_SetBits(GPIOE,GPIO_Pin_7); //485先发送
for(i=0;i<8;i++)
{
USART_SendData(USART2,TX_Buf);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);//????·????ê??±ê????
}
delay_ms(10);
GPIO_ResetBits(GPIOE,GPIO_Pin_7); //485接收
while (1);
}
void RCC_Configuration(void)
{
SystemInit();//72m
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;//CS_485
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//TX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
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(USART2,&USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
void USART2_IRQHandler(void)
{
u8 temp=0,Count=0;
u8 WSD[7]={0,0,0,0,0,0,0};
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
temp=USART_ReceiveData(USART2);
WSD[Count]=temp;
Count++;
if(Count>7)
{
Count=0;
}
delay_ms(2);
GPIO_SetBits(GPIOE,GPIO_Pin_7);
}
}
我用示波器看了下波形,我发送TX_Buf[8]={0x01,0x03,0x00,0x01,0x00,0x01,0xD5,0xCA}这一串数据过去,能返回数据回来,但是就是进不了接收中断?请各位帮我看看。
|
|