//第一个板子和第二个板子其他程序一样
int main(void)
{
Init_All_Periph();
UART3Init();
LCD_Init(); // LCD初始化
LCD_Clear(WHITE);
RS485_SendByte(0x08);
while(1)
{
}
}
//第二个板子的程序,接受到显示 打开发送中断 回发
int main(void)
{
Init_All_Periph();
UART3Init();
LCD_Init(); // LCD初始化
LCD_Clear(WHITE);
while(1)
{
}
}
void UART3Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_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_Tx | USART_Mode_Rx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
GPIO_ResetBits(GPIOB, GPIO_Pin_2);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
/* UART2中断配置 */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Delay(unsigned short time)
{
unsigned short i, j;
for(; time > 0; time--){
for(j = 0; j < 10; j++){
for(i = 0; i < 1000; i++);
}
}
}
void RS485_SendByte(unsigned char temp)
{
// while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
GPIO_SetBits(GPIOB, GPIO_Pin_2);
Delay(10);
USART_SendData(USART3, temp);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
Delay(10);
GPIO_ResetBits(GPIOB, GPIO_Pin_2);
GPIO_ResetBits(GPIOD, GPIO_Pin_8);
//return(temp);
}
unsigned char RS485_GetByte(void)
{
if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
{//i=1;
//USART_ClearITPendingBit(USART3,USART_IT_RXNE);
LCD_ShowNum(0+i,0+i,USART_ReceiveData(USART3),1,16);
if(USART_ReceiveData(USART3)==1)
{
GPIO_SetBits(GPIOD, GPIO_Pin_8);
}//*
i++;
flag=1;
}
return(USART_ReceiveData(USART3)); //?????
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
LCD_ShowNum(0+i,0+i,USART_ReceiveData(USART3),1,16);
if(USART_ReceiveData(USART3)==0x08)
{
GPIO_SetBits(GPIOD, GPIO_Pin_8);
}//*
hh[i]=USART_ReceiveData(USART3);
i++;
if(i==10)
{i=0;}
USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
Delay(1);
}
if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
{
RS485_SendByte(hh[i]);
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
Delay(1);
}
}
RS485_SendByte()发送函数我测过没有问题啊,现在不知道问题出在哪里 求助 ,谢谢 |
|