你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

关于232转485的问题

[复制链接]
weiyaogun 提问时间:2017-11-3 19:50 /
小弟初学,现在在做一个232转485的板子,目前在485这里出了问题,利用75176芯片进行ttl到485的变换,程序结构也挺简单,但是485要么无法进中断要么无法正确发出,求大神帮看看!

主程序在:
#include "stm32f10x.h"
#include "usart1.h"
#include "led.h"
//#include "CAN.h"


void NVIC_Configuration(void)
{   

        NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);


  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  /* Enable the USART3 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
       
        /* Enable the CAN Interrupt */
  //NVIC_InitStructure.NVIC_IRQChannel =USB_LP_CAN1_RX0_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
       
}

        int main(void)
{
       
        SystemInit();//ÅäÖÃϵͳʱÖÓΪ 72M
        LED_Init();//led³õʼ»¯
        LED(ON);       
        USART1_Config(); //USART1 ÅäÖà      
        USART3_Config(); //USART3 ÅäÖà      
        //CAN_Config();
        NVIC_Configuration();// ½ÓÊÕÖжÏÅäÖÃ
       
       
       
  while (1){;}
}       




串口程序在:
/*DE¸úGPIOA8¹Ü½ÅÏàÁ¬*/
#define Set_DE  GPIO_SetBits(GPIOA,GPIO_Pin_8);
#define Clr_DE  GPIO_ResetBits(GPIOA,GPIO_Pin_8);

void delay_ms(u16 nms)
{
        u32 temp;
        SysTick->LOAD=9000*nms;
        SysTick->VAL=0X00;
        SysTick->CTRL=0X01;
        do{ temp=SysTick->CTRL;}
        while((temp&0x01)&&(!(temp&(1<<16))));
        SysTick->CTRL=0x00;
        SysTick->VAL=0X00;

}
//USART1³õʼ»¯£¬PA8:RS485_DE$ PA9:RS485_TX$ PA8:RS485_RX$
void USART1_Config(void)
{
                GPIO_InitTypeDef GPIO_InitStructure;
                USART_InitTypeDef USART_InitStructure;

        /* ʹÄÜ USART1 ʱÖÓ*/
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
       

                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
                 Clr_DE;
       

        /* USART1 ʹÓÃIO¶Ë¿ÚÅäÖà */   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //¸¡¿ÕÊäÈë
  GPIO_Init(GPIOA, &GPIO_InitStructure);   //³õʼ»¯GPIOA
          
        /* USART1 ¹¤×÷ģʽÅäÖà */
        USART_InitStructure.USART_BaudRate = 9600;        //²¨ÌØÂÊÉèÖãº9600
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;        //Êý¾ÝλÊýÉèÖãº8λ
        USART_InitStructure.USART_StopBits = USART_StopBits_1;         //ֹͣλÉèÖãº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(USART1, &USART_InitStructure);  //³õʼ»¯USART1
       
    /*ʹÄÜ´®¿Ú1½ÓÊÕÖжÏ*/

USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
  USART_Cmd(USART1, ENABLE);// USART1ʹÄÜ
USART_ClearFlag(USART2,USART_FLAG_TC);
}
       



void RS485_SendByte(unsigned char SendData )
{          
        Set_DE;
                                delay_ms(200);
                                USART_ClearFlag(USART1,USART_FLAG_TC);
        USART_SendData(USART1,SendData);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//TXE=·¢ËͼĴæÆ÷Ϊ¿Õ
                                delay_ms(200);
                                Clr_DE;
}                      


//USART3³õʼ»¯£¬PB10:RS232_TX$ PB11:RS232_RX$
void USART3_Config(void)
{
                GPIO_InitTypeDef GPIO_InitStructure;
                USART_InitTypeDef USART_InitStructure;

        /* ʹÄÜ USART3 ʱÖÓ*/
                RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
       

/* USART3 ʹÓÃIO¶Ë¿ÚÅäÖà */   
  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);   //³õʼ»¯GPIOA
          
        /* USART3 ¹¤×÷ģʽÅäÖà */
        USART_InitStructure.USART_BaudRate = 9600;        //²¨ÌØÂÊÉèÖãº9600
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;        //Êý¾ÝλÊýÉèÖãº8λ
        USART_InitStructure.USART_StopBits = USART_StopBits_1;         //ֹͣλÉèÖãº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);  //³õʼ»¯USART3
       
    /*ʹÄÜ´®¿Ú3½ÓÊÕÖжÏ*/
  USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  USART_Cmd(USART3,ENABLE);// USART3ʹÄÜ
}
       
         /*·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý*/
void UART3SendByte(unsigned char SendData )
{          
        USART_SendData(USART3,SendData);
        while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);            
                             
}  





中断:
        void USART1_IRQHandler(void)
{ unsigned char i=0;

        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {       
    /* Read one byte from the receive data register */
               
    i = USART_ReceiveData(USART1);        
       
    UART3SendByte(1);                   //485½ÓÊÕµ½µÄÊý¾Ýͨ¹ý232·¢ËͳöÈ¥
//cansend(i);
  }


}


void USART3_IRQHandler(void)
{
         unsigned char i=0;
       
  if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  {       
    /* Read one byte from the receive data register */
    i = USART_ReceiveData(USART3);
   //232½ÓÊÕµ½µÄÊý¾Ýͨ¹ý·¢ËͳöÈ¥
                 RS485_SendByte(i);
         
               
//cansend(i);
        }



求大神!!!!!
收藏 评论1 发布时间:2017-11-3 19:50

举报

1个回答
贺工 回答时间:2017-11-4 08:00:10
一般硬件做法是:232转TTL,TTL加485芯片转485.我不知道你的硬件方案对不对。首先确定硬件方案。485通信和串口编程差不多,如果发送完不想做点延时的话,就采用串口中断发送。有问题可以加我微信:longskill

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版