初级会员

- 积分
- 64
- 金钱
- 64
- 注册时间
- 2015-3-15
- 在线时间
- 1 小时
|

楼主 |
发表于 2015-3-16 20:15:25
|
显示全部楼层
回复【6楼】jermy_z:
---------------------------------
回复【4楼】xuande:
---------------------------------
以下是我的程序,不知怎么回事,串口调试助手接到的是连续不断的00,我想达到的是串口3接收条码扫描信息(设备是现成的),然后串口4把接收的发送调试助手,求大虾指教下,谢谢,谢谢
这是uart1.c的
#include "sys.h"
#include "uart1.h"
u8 USART3_RX_BUF[USART_REC_LEN];//??????????×??óUSART_REC_LEN×???.
u8 n=0;
u8 i=0;
u16 USART3_RX_STA=0;//????×???±ê??
void uart_init(u32 bound){
//????????
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_Configuration();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3|RCC_APB1Periph_UART4, ENABLE);
USART_DeInit(USART3);
USART_DeInit(UART4);
//USART3_TX   B.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX   B.11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOB, &GPIO_InitStructure);
//UART4_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PC10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì
GPIO_Init(GPIOC, &GPIO_InitStructure);
//UART4_RX   C11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOC, &GPIO_InitStructure);
//USART3 ??????
USART_InitStructure.USART_BaudRate = bound;//??°??¨????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);
//UART4 ??????
USART_InitStructure.USART_BaudRate = bound;//??°??¨????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(UART4, &USART_InitStructure);
//Uart4 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????NVIC?????÷
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//????????
USART_Cmd(UART4, ENABLE); //????????
}
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//Uart4 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????NVIC?????÷
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//????????
USART_Cmd(UART4, ENABLE); //????????
}
void USART3_IRQHandler(void) //????3????·??????ò
{
u8 Res3;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //
{
Res3=USART_ReceiveData(USART3);//(USART3->DR); //????????????????
USART3_RX_BUF[USART3_RX_STA]=Res3;
USART3_RX_STA++;
if(USART3_RX_STA==5)
{
USART3_RX_STA=0;
i=USART3_RX_BUF[0]+USART3_RX_BUF[1]+USART3_RX_BUF[2]+USART3_RX_BUF[3];
if(USART3_RX_BUF[0]==0XFF&&USART3_RX_BUF[1]==0X81&&i==USART3_RX_BUF[4])
{
n=0x01;
}
else {n=0;}
}
}
}
这是uart.h的
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
#define USART_REC_LEN 5 //?¨??×??ó×????? 200
extern u8 USART3_RX_BUF[USART_REC_LEN]; //????????,×??óUSART_REC_LEN×???????????????·?
extern u8 n;
extern u16 USART3_RX_STA;
void uart_init(u32 bound);
#endif
这是main.c的
#include "stm32f10x.h"
#include "sys.h"
#include "uart1.h"
#include "led.h"
int main(void)
{
u8 t=0;
//NVIC_Configuration(); //?è??NVIC????·?×é2??2??????????,2???ì????????
uart_init(115200);
while(1)
{
LED_GPIO_Config();
LED1( ON );
USART_Cmd(USART3, ENABLE);
while(n!=0X01)
{
USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);//????????±?????
n=0;
for(t=0;t<5;t++)
{
USART_SendData(UART4, USART3_RX_BUF[t]);//?ò????4·???????
while(USART_GetFlagStatus(UART4,USART_FLAG_TC)!=SET);//????·????á??
}
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //????????????
}
USART_Cmd(USART3, DISABLE);
}
} |
|