新手入门
- 积分
- 3
- 金钱
- 3
- 注册时间
- 2017-12-2
- 在线时间
- 2 小时
|
9金钱
本帖最后由 x156 于 2017-12-4 22:46 编辑
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "Nixietube.h"
#include "beep.h"
#include "usart.h"
#include "rs485.h"
int main(void)
{
u8 key;
u8 cnt=0;
u8 rs485buf[1];
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
Nixietube_init();
KEY_Init();
RS485_Init(9600);
BEEP_Init();
while(1)
{
key=KEY_Scan(2);
if(key==KEY2_PRES)
{
rs485buf[0]=cnt;
RS485_Send_Data(rs485buf,1);
RS485_Receive_Data(rs485buf,&key);
BEEP=1;
delay_ms(300);
cnt++;
if(rs485buf[0]<=9)
{
Nixietube_light(1, rs485buf[0]);
}
else
{
Nixietube_light(1, rs485buf[0]/10);
delay_ms(1);
Nixietube_light(2, rs485buf[0]%10);
delay_ms(1);
}
}
BEEP=0;
if(cnt<=9)
{
Nixietube_light(1, cnt);
}
else
{
Nixietube_light(1, cnt/10);
delay_ms(1);
Nixietube_light(2, cnt%10);
delay_ms(1);
}
}
}
485配置:
#include "sys.h"
#include "rs485.h"
#include "delay.h"
#ifdef EN_USART2_RX
u8 RS485_RX_BUF[64];
u8 RS485_RX_CNT=0;
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
res =USART_ReceiveData(USART2);
if(RS485_RX_CNT<64)
{
RS485_RX_BUF[RS485_RX_CNT]=res;
RS485_RX_CNT++;
}
}
}
#endif
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
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);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);
#ifdef EN_USART2_RX
USART_InitStructure.USART_BaudRate = bound;
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);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
RS485_TX_EN=0;
}
void RS485_Send_Data(u8 *buf,u8 len)
{
u8 t;
RS485_TX_EN=1;
for(t=0;t<len;t++)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_SendData(USART2,buf[t]);
}
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
RS485_RX_CNT=0;
RS485_TX_EN=0;
}
void RS485_Receive_Data(u8 *buf,u8 *len)
{
u8 rxlen=RS485_RX_CNT;
u8 i=0;
*len=0; //ĬÈÏΪ0
delay_ms(10); //µÈ´ý10ms,Á¬Ðø³¬¹ý10msûÓнÓÊÕµ½Ò»¸öÊý¾Ý,ÔòÈÏΪ½ÓÊÕ½áÊø
if(rxlen==RS485_RX_CNT&&rxlen)//½ÓÊÕµ½ÁËÊý¾Ý,ÇÒ½ÓÊÕÍê³ÉÁË
{
for(i=0;i<rxlen;i++)
{
buf=RS485_RX_BUF;
}
*len=RS485_RX_CNT; //¼Ç¼±¾´ÎÊý¾Ý³¤¶È
RS485_RX_CNT=0; //ÇåÁã
}
}
数码管显示程序没问题,程序目标是每当按键按下时,数据从主机传到从机(0123456),并在数码管上显示数据。但是数据始终不能从主机传送到从机,该如何改?
|
|