weiyaogun 发表于 2017-11-3 19:50:44

关于232转485的问题

小弟初学,现在在做一个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_DEGPIO_SetBits(GPIOA,GPIO_Pin_8);
#define Clr_DEGPIO_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);
        }



求大神!!!!!

贺工 发表于 2017-11-4 08:00:10

一般硬件做法是:232转TTL,TTL加485芯片转485.我不知道你的硬件方案对不对。首先确定硬件方案。485通信和串口编程差不多,如果发送完不想做点延时的话,就采用串口中断发送。有问题可以加我微信:longskill
页: [1]
查看完整版本: 关于232转485的问题