新手上路
- 积分
- 26
- 金钱
- 26
- 注册时间
- 2020-5-9
- 在线时间
- 6 小时
|
#include "sys.h"
#include "rs485.h"
#include "delay.h"
#include "js.h"
extern u16 shu;
extern u8 gewei;
extern u8 shiwei;
extern u8 baiwei;
extern u8 qianwei;
extern u8 shuju[7];
#ifdef EN_USART2_RX //èç1ûê1Äüá˽óêÕ
void USART2_IRQHandler(void)
{
char res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½óêÕμ½êy¾Y
{
res=USART_ReceiveData(USART2); //¶á衽óêÕμ½μÄêy¾Y
if(res == '-')
{
shu--;
}
RS485_Send_Data();
}
}
#endif
void RS485_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//¸′λ′®¿ú2
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//í£Ö1¸′λ
#ifdef EN_USART2_RX //èç1ûê1Äüá˽óêÕ
USART_InitStructure.USART_BaudRate = 115200;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8λêy¾Y3¤¶è
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;///ÆæÅ¼D£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); ; //3õê¼»ˉ′®¿ú
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú `
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //ê1Äü′®¿ú2ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÏèÕ¼óÅÏè¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //′óóÅÏè¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äüía2¿ÖD¶Ïí¨μà
NVIC_Init(&NVIC_InitStructure);
#endif
RS485_TX_EN=0; //ĬèÏÎa½óêÕÄ£ê½
}
void RS485_Send_Data()
{
u8 t;
//u8 shuzhi[3]={0x01,0x02,0x03};
RS485_TX_EN=1; //éèÖÃÎa·¢ËíÄ£ê½
jishu_Init();
for(t=0;t<7;t++)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_SendData(USART2,shuju[t]);
}
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
RS485_TX_EN=0; //éèÖÃÎa½óêÕÄ£ê½
}
|
|