232转485,在485上出了问题
2019-08-08 13:44 发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
小弟初学,现在在做一个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();//ÅäÖÃÏμí3ê±ÖóÎa 72M
LED_Init();//led3õê¼»ˉ
LED(ON);
USART1_Config(); //USART1 ÅäÖÃ
USART3_Config(); //USART3 ÅäÖÃ
//CAN_Config();
NVIC_Configuration();// ½óêÕÖD¶ÏÅäÖÃ
while (1){;}
}
串口程序在:
/*DE¸úGPIOA81ü½ÅÏàá¬*/
#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;
}
//USART13õê¼»ˉ£¬ A8:RS485_DE$ PA9:RS485_TX$ PA8:RS485_RX$
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* ê1Äü 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 ê1óÃIO¶Ë¿úÅäÖà */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
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); //3õê¼»ˉGPIOA
/* USART1 1¤×÷Ä£ê½ÅäÖà */
USART_InitStructure.USART_BaudRate = 9600; //2¨ìØÂêéèÖão9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //êy¾YλêyéèÖão8λ
USART_InitStructure.USART_StopBits = USART_StopBits_1; //í£Ö1λéèÖão1λ
USART_InitStructure.USART_Parity = USART_Parity_No ; //êÇ·ñÆæżD£Ñé£oÎT
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //ó2¼tá÷¿ØÖÆÄ£ê½éèÖãoûóDê1Äü
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//½óêÕóë·¢Ëí¶¼ê1Äü
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉUSART1
/*ê1Äü′®¿ú1½óêÕÖD¶Ï*/
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
USART_Cmd(USART1, ENABLE);// USART1ê1Äü
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=·¢Ëí¼Ä′æÆ÷Îa¿Õ
delay_ms(200);
Clr_DE;
}
//USART33õê¼»ˉ£¬ B10:RS232_TX$ PB11:RS232_RX$
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* ê1Äü USART3 ê±Öó*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* USART3 ê1óÃIO¶Ë¿úÅäÖà */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
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); //3õê¼»ˉGPIOA
/* USART3 1¤×÷Ä£ê½ÅäÖà */
USART_InitStructure.USART_BaudRate = 9600; //2¨ìØÂêéèÖão9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //êy¾YλêyéèÖão8λ
USART_InitStructure.USART_StopBits = USART_StopBits_1; //í£Ö1λéèÖão1λ
USART_InitStructure.USART_Parity = USART_Parity_No ; //êÇ·ñÆæżD£Ñé£oÎT
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //ó2¼tá÷¿ØÖÆÄ£ê½éèÖãoûóDê1Äü
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//½óêÕóë·¢Ëí¶¼ê1Äü
USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉUSART3
/*ê1Äü′®¿ú3½óêÕÖD¶Ï*/
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3,ENABLE);// USART3ê1Äü
}
/*·¢Ëíò»¸ö×Ö½úêy¾Y*/
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½óêÕμ½μÄêy¾Yí¨1y232·¢Ëí3öè¥
//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½óêÕμ½μÄêy¾Yí¨1y·¢Ëí3öè¥
RS485_SendByte(i);
//cansend(i);
}
求大神!!!!!
一周热门 更多>