OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 1875|回复: 1

求助

[复制链接]

2

主题

2

帖子

0

精华

新手入门

积分
3
金钱
3
注册时间
2017-12-2
在线时间
2 小时
发表于 2017-12-4 22:39:36 | 显示全部楼层 |阅读模式
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;    //&Auml;&not;&Egrave;&Iuml;&Icirc;&ordf;0
delay_ms(10);  //&micro;&Egrave;&acute;&yacute;10ms,&Aacute;&not;&ETH;&oslash;&sup3;&not;&sup1;&yacute;10ms&Atilde;&raquo;&Oacute;&ETH;&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&Ograve;&raquo;&cedil;&ouml;&Ecirc;&yacute;&frac34;&Yacute;,&Ocirc;ò&Egrave;&Iuml;&Icirc;&ordf;&frac12;&Oacute;&Ecirc;&Otilde;&frac12;á&Ecirc;&oslash;
if(rxlen==RS485_RX_CNT&&rxlen)//&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&Aacute;&Euml;&Ecirc;&yacute;&frac34;&Yacute;,&Ccedil;&Ograve;&frac12;&Oacute;&Ecirc;&Otilde;&Iacute;ê&sup3;&Eacute;&Aacute;&Euml;
{
  for(i=0;i<rxlen;i++)
  {
   buf=RS485_RX_BUF;
  }  
  *len=RS485_RX_CNT; //&frac14;&Ccedil;&Acirc;&frac14;±&frac34;&acute;&Icirc;&Ecirc;&yacute;&frac34;&Yacute;&sup3;¤&para;&Egrave;
  RS485_RX_CNT=0;  //&Ccedil;&aring;&Aacute;&atilde;
}
}

数码管显示程序没问题,程序目标是每当按键按下时,数据从主机传到从机(0123456),并在数码管上显示数据。但是数据始终不能从主机传送到从机,该如何改?

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

558

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
164846
金钱
164846
注册时间
2010-12-1
在线时间
2098 小时
发表于 2017-12-5 01:13:42 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-4-19 18:45

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表