新手入门
- 积分
- 13
- 金钱
- 13
- 注册时间
- 2016-4-11
- 在线时间
- 141 小时
|

楼主 |
发表于 2016-8-11 09:36:13
|
显示全部楼层
程序是我用485程序改的,485通讯没问题,收发都正常。232能发不能收
串口初始化:
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, 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(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
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);
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);
USART_Cmd(USART2, ENABLE);
}
主函数如下 :
int main(void)
{
double temperature;
double t=0;
int a=0,i=0,b=0;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
RS485_Init(9600); //′®¿ú3õê¼»ˉÎa9600
LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
while(DS18B20_Init()) //DS18B203õê¼»ˉ PE0
{
GPIO_SetBits(GPIOB,GPIO_Pin_12);
}
while(1)
{
RS485=0;
b=USART_ReceiveData(USART2);
if(b==116)
{
GPIO_SetBits(GPIOB,GPIO_Pin_12);
delay_ms(100);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}
delay_ms(100);
if(a%10==0) // 100ms2é¼ˉò»′Î
{
temperature=DS18B20_Get_Temp();
t=temperature/10;
delay_ms(10);
RS485=1;
delay_ms(10);
printf("t1=%4.1f¡æ ",t);
delay_ms(10);
RS485=0;
}
delay_ms(10);
a++;
}
}
其中RS485=0 =1 是485的使能端,和232没有关系。 |
|