//RS485头文件
#ifndef __RS485_H
#define __RS485_H
#include "sys.h"
void RS485_Init(void);
void RS485_Send_Data(void);
void RS485_Receive_Data(void);
extern u8 RS485_RX_BUF[64]; //½óêÕ»o3å,×î′ó64¸ö×Ö½ú
extern u8 RS485_RX_CNT; //½óêÕμ½μÄêy¾Y3¤¶è
extern u8 RS485_RX_OK; //½óêÕíê3é±êÖ¾ 1Îaíê3é
extern u8 RS485_TX_ON;
//Ä£꽿ØÖÆ
#define RS485_TX_EN PDout(7) //485Ä£꽿ØÖÆ.0,½óêÕ;1,·¢Ëí.
//èç1ûÏë′®¿úÖD¶Ï½óêÕ£¬Çë2»òa×¢êíòÔÏÂo궨òå
#define EN_USART2_RX 1 //0,2»½óêÕ;1,½óêÕ.
#endif
//RS485 C文件
#include "stm32f10x.h"
#include "RS485.h"
#include "delay.h"
#include "led.h"
#ifdef EN_USART2_RX //èç1ûê1Äüá˽óêÕ
u8 len;
u8 buf[64];
u8 RS485_RX_OK=0; //½óêÕíê3é
u8 RS485_TX_ON=0;
u8 RS485_RX_BUF[64]; //½óêÕ»o3å,×î′ó64¸ö×Ö½ú.
u8 RS485_RX_CNT=0; //½óêÕμ½μÄêy¾Y3¤¶è
#endif
void RS485_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART2_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
USART_DeInit(USART2);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART2_InitStructure.USART_BaudRate=19200; //2¨ìØÂê19200
USART2_InitStructure.USART_WordLength=USART_WordLength_8b; //êy¾Yλ8λ
USART2_InitStructure.USART_StopBits=USART_StopBits_1; //í£Ö1λ1λ
USART2_InitStructure.USART_Parity=USART_Parity_No ; //ÎTD£Ñé
USART2_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //ÎTó2¼tá÷
USART2_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //·¢Ëí/½óêÕê1Äü
USART_Init(USART2,&USART2_InitStructure); //3õê¼»ˉ′®¿ú3éÔ±±äá¿
USART_Cmd(USART2,ENABLE); //′®¿úê1Äü
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; //USART1諾ÖÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //í¨μàê1Äü
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //ÇàÕ¼óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; //×óóÅÏ輶Îa1
NVIC_Init(&NVIC_InitStructure); //3õê¼»ˉÖD¶Ï3éÔ±±äá¿
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE ); //′®¿úÖD¶Ïê1Äü(½óêÕ»o3åÇø·Ç¿Õ)
RS485_TX_EN=0; //ĬèÏÎa½óêÕÄ£ê½
}
void USART2_IRQHandler(void) //êy¾Y½óêÕÖD¶Ï
{
u8 res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½óêÕμ½êy¾Y
{
res =USART_ReceiveData(USART2); //¶á衽óêÕμ½μÄêy¾Y
if(RS485_RX_CNT<64)
{
RS485_RX_BUF[RS485_RX_CNT]=res; //¼Ç¼½óêÕμ½μÄÖμ
RS485_RX_CNT++; //½óêÕêy¾YÔö¼ó1
}
}
}
//RS4852éÑˉ½óêÕμ½μÄêy¾Y
//buf:½óêÕ»o′æê×μØÖ·
//len:¶áμ½μÄêy¾Y3¤¶è
void RS485_Receive_Data()
{
u8 rxlen=RS485_RX_CNT;
u8 i=0;
delay_ms(10); //μè′y10ms,á¬Dø3¬1y10msûóD½óêÕμ½ò»¸öêy¾Y,ÔòèÏÎa½óêÕ½áêø
if(rxlen==RS485_RX_CNT&&rxlen)//½óêÕμ½áËêy¾Y,Çò½óêÕíê3éáË
{
for(i=0;i<rxlen;i++)
{
buf[i]=RS485_RX_BUF[i];
}
len=RS485_RX_CNT; //¼Ç¼±¾′Îêy¾Y3¤¶è
LED0=0;
delay_ms(1000);
RS485_RX_CNT=0; //Çåáã
RS485_RX_OK=1; //½óêÕíê3é
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
LED0=1;
}
}
//RS485·¢Ëílen¸ö×Ö½ú.
//buf:·¢ËíÇøê×μØÖ·
//len:·¢ËíμÄ×Ö½úêy(ÎaáËoí±¾′úÂëμĽóêÕÆ¥Åä,Õaàィòé2»òa3¬1y64¸ö×Ö½ú)
void RS485_Send_Data()
{
u8 t;
RS485_TX_EN=1; //éèÖÃÎa·¢ËíÄ£ê½
RS485_RX_OK=0; //½óêÕíê3é
for(t=0;t<len;t++) //Ñ-»··¢Ëíêy¾Y
{
// while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_SendData(USART2,buf[t]);
LED1=0;
}
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) ==RESET);
delay_ms(1000);
RS485_RX_CNT=0;
RS485_TX_ON=0;
RS485_TX_EN=0; //éèÖÃÎa½óêÕÄ£ê½
USART_ClearITPendingBit(USART2,USART_IT_TC);
LED1=1;
}
//主函数
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
#include "RS485.h"
#include "delay.h"
int main(void)
{
u8 key;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÖD¶ÏóÅÏ輶·Ö×éÎa×é2£o2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
LED_Init(); //3õê¼»ˉóëLEDᬽóμÄó2¼t½ó¿ú
KEY_Init(); //°′¼ü3õê¼»ˉ
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
RS485_Init(); //3õê¼»ˉRS485
while(1)
{
key=KEY_Scan(0);
if(RS485_RX_OK==1) //èç1û½óêÕíê3éÔò·¢Ëí
{
if(key==KEY0_PRES) //KEY0°′ÏÂ,·¢Ëíò»′Îêy¾Y
{
delay_ms(100);
if(key==KEY0_PRES)
{
RS485_TX_ON=1;
}
}
if(RS485_TX_ON==1)
{
RS485_Send_Data();
}
}
else
{
RS485_Receive_Data();
}
}
}
// LED KEY程序都没有问题,所以就没有附上
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>