查看: 4935|回复: 2

[提问] 调试max485,如何发送字符串

[复制链接]
发表于 2013-10-13 08:50:10 | 显示全部楼层 |阅读模式
关键词: max485调试
最近调试485,我想把上位机发送的数据再返回到上位机串口调试助手显示:usart3串口,pB2控制收发,pb10发送,Pb11接收,程序只能收发一个字符,请教各位网友如何收发字符串,我把我的代码写上。
#include "stm32f10x.h"
#include
#include "delay.h"


void RCC_Configuration(void)
{
  ErrorStatus HSEStartUpStatus;                    //¶¨òåía2¿¸ßËù¾§ìåÆô¶ˉ×′ì¬Ã¶¾ù±äá¿
  RCC_DeInit();                                    //¸′λRCCía2¿é豸¼Ä′æÆ÷μ½Ä¬èÏÖμ
  RCC_HSEConfig(RCC_HSE_ON);                       //′ò¿aía2¿¸ßËù¾§Õñ
  HSEStartUpStatus = RCC_WaitForHSEStartUp();      //μè′yía2¿¸ßËùê±Öó×¼±¸oÃ
  if(HSEStartUpStatus == SUCCESS)                  //ía2¿¸ßËùê±ÖóòѾ-×¼±eoÃ
  {
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //¿aÆôFLASHÔ¤¶á»o3å1|Äü£¬¼óËùFLASHμĶáè¡¡£ËùóD3ìDòÖD±ØDëμÄó÷¨.λÖãoRCC3õê¼»ˉ×óoˉêyàïÃ棬ê±ÖóÆeÕñÖ®oó
    FLASH_SetLatency(FLASH_Latency_2);                    //flash2ù×÷μÄÑóê±
             
    RCC_HCLKConfig(RCC_SYSCLK_Div1);               //ÅäÖÃAHB(HCLK)ê±Öóμèóú==SYSCLK
    RCC_PCLK2Config(RCC_HCLK_Div1);                //ÅäÖÃAPB2(PCLK2)Öó==AHBê±Öó
    RCC_PCLK1Config(RCC_HCLK_Div2);                //ÅäÖÃAPB1(PCLK1)Öó==AHB1/2ê±Öó

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //ÅäÖÃLLê±Öó == ía2¿¸ßËù¾§ìåê±Öó * 9 = 72MHz
    RCC_PLLCmd(ENABLE);                                   //ê1ÄüPLLê±Öó

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //μè′yPLLê±Öó¾íD÷
    {
    }
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //ÅäÖÃÏμí3ê±Öó = PLLê±Öó
    while(RCC_GetSYSCLKSource() != 0x08)                  //¼ì2éPLLê±ÖóêÇ·ñ×÷ÎaÏμí3ê±Öó
    {
    }
  }

}


void NVIC_Configuration(void)
{
   NVIC_InitTypeDef NVIC_InitStructure;


   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);


   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);


   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;          
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                  
   NVIC_Init(&NVIC_InitStructure);                                                   
}


void USART3_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
       
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        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);



        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        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_Rx | USART_Mode_Tx;
        USART_Init(USART3, &USART_InitStructure);
        USART_Cmd(USART3, ENABLE);

   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

    USART_ClearFlag(USART3, USART_FLAG_TC);   
}


void Uart3_PutChar(u8 ch)
{
       
        GPIO_SetBits(GPIOB,GPIO_Pin_2);
        delay_ms(1);

    USART_SendData(USART3, (u8) ch);
    while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);

        delay_ms(2);
        GPIO_ResetBits(GPIOB,GPIO_Pin_2);
}


void USART3_IRQHandler(void)         
{
  u8 dat;

  while(USART_GetITStatus(USART3, USART_IT_RXNE) !=SET);   

       
         dat = USART_ReceiveData(USART3);

        Uart3_PutChar(dat);     

  //USART_ClearITPendingBit(USART3,USART_IT_RXNE );                
        //USART_ClearFlag(USART3,USART_FLAG_RXNE);                                                                  

}


int main(void)
{
        //u8 dat;
       
        delay_init(72);
  RCC_Configuration();
  NVIC_Configuration();
  USART3_Configuration();

       
         while(1)
         {
                 // GPIO_ResetBits(GPIOB,GPIO_Pin_12);
                  // while(USART_GetFlagStatus(USART3,USART_FLAG_RXNE)!=SET);
            //dat = USART_ReceiveData(USART3);                 
      //Uart3_PutChar(dat);     
         }
}


发表于 2013-10-16 09:08:20 | 显示全部楼层
void USART3_IRQHandler(void)         
{
  u8 dat;

  while(USART_GetITStatus(USART3, USART_IT_RXNE) !=SET);    //-------------------在中断中,你这样做真的没问题么?
.....}
 楼主| 发表于 2013-10-16 22:22:30 | 显示全部楼层
qq381973111 发表于 2013-10-16 09:08
void USART3_IRQHandler(void)         
{
  u8 dat;

新手啊,该怎么做,感激不尽
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表