向各位大侠求助啊,两个问题:1、为什么电脑的串口调试助手只收到一遍的ABCDEFG........XYZ 以后就不发了
2、485.c中的USART2_IRQHandler是串口收到数据才会被调用的,实际上是串口发送了ABCD就会触发set_usart_485_handler发生蜂鸣和继电器开关动作
这个是485.c
#include "bitband.h"
#include "usart_485.h"
typedef void (*set_U_485_handler) (unsigned char c);
set_U_485_handler handler_485;
void usart_485_init(void)
{
GPIO_InitTypeDef Gpio_Value;
USART_InitTypeDef Usart_Value;
NVIC_InitTypeDef Nvic_Value;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
Gpio_Value.GPIO_Mode = GPIO_Mode_AF;
Gpio_Value.GPIO_OType = GPIO_OType_PP;
Gpio_Value.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
Gpio_Value.GPIO_PuPd = GPIO_PuPd_NOPULL;
Gpio_Value.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &Gpio_Value);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
Gpio_Value.GPIO_Mode = GPIO_Mode_OUT;
Gpio_Value.GPIO_OType = GPIO_OType_PP;
Gpio_Value.GPIO_Pin = GPIO_Pin_6;
Gpio_Value.GPIO_PuPd = GPIO_PuPd_NOPULL;
Gpio_Value.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &Gpio_Value);
PDOut(6) = 1;//PD6输出高电平,它连接了MAX485ESA的DE和RE
Usart_Value.USART_BaudRate = 115200;
Usart_Value.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
Usart_Value.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
Usart_Value.USART_Parity = USART_Parity_No;
Usart_Value.USART_StopBits = USART_StopBits_1;
Usart_Value.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2, &Usart_Value);
Nvic_Value.NVIC_IRQChannel = USART2_IRQn;
Nvic_Value.NVIC_IRQChannelCmd = ENABLE;
Nvic_Value.NVIC_IRQChannelPreemptionPriority = 2;
Nvic_Value.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&Nvic_Value);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void usart_485_send(unsigned char c)
{
//PDOut(6) = 1;
USART_SendData(USART2, (uint16_t)c);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART2, USART_FLAG_TXE);
//PDOut(6) = 0;
}
unsigned char usart_485_recv(void)
{
return 0;
}
void set_485_recv_handler(set_U_485_handler h)
{
handler_485 = h;
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
{
handler_485(USART_ReceiveData(USART2));
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
以下是main.c
void set_usart_485_handler(unsigned char c)
{
switch(c)
{
case 'A': switch_on(0); buzzer_on(); break;
case 'B': switch_on(1); buzzer_off(); break;
case 'C': switch_on(2); buzzer_on(); break;
case 'D': switch_off(0); switch_off(1); switch_off(2); buzzer_off(); break;
}
}
int main(void)
{
//unsigned char c_232 = 'A';
unsigned char c_485 = 'E';
//unsigned char c_232_buf[11] = "1111111111";
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
led_init();
buzzer_init();
switch_init();
button_init();
delay_init();
eint_init();
//usart_232_init();
usart_485_init();
//set_232_recv_handler(set_usart_232_handler);
set_485_recv_handler(set_usart_485_handler);
while(1)
{
//usart_232_send(c_232++);
usart_485_send(c_485++);
delay_ms(10);//tzx addded
//if(c_232 >= 'Z')
// c_232 = 'A';
if(c_485 >= 'Z')
c_485 = 'E';
switch_on(0);
delay_ms(500);
switch_off(0);
delay_ms(500);
//c_232_buf[9]++;
}
}
|