初级会员
- 积分
- 53
- 金钱
- 53
- 注册时间
- 2015-7-23
- 在线时间
- 0 小时
|
发表于 2015-7-26 13:53:57
|
显示全部楼层
我用的是F103串口1的。我可以发。
你先改成串口1试试
USART.C的
#include "sys.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////
//????????ucos,?ò°ü?¨???????·????????.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h" //ucos ????
#endif
//////////////////////////////////////////////////////////////////////////////////
//±????ò?????§?°??????????×÷???í???????????????ü????????
//ALIENTEK STM32??·?°?
//????1??????
//??????×?@ALIENTEK
//????????:www.openedv.com
//????????:2012/8/18
//°?±???V1.5
//°??¨?ù??????°?±?????
//Copyright(C) ?????????í??×????????????? 2009-2019
//All rights reserved
//********************************************************************************
//V1.3???????÷
//?§???????????????????????¨?????è??.
//????????printf???§??
//???????????????ü??????.
//??????printf??????×?·????§??bug
//V1.4???????÷
//1,??????????????IO??bug
//2,??????USART_RX_STA,????????×??ó????×???????2??14??·?
//3,??????USART_REC_LEN,?????¨??????×??ó???í??????×?????(???ó??2??14??·?)
//4,??????EN_USART1_RX??????·???
//V1.5???????÷
//1,????????UCOSII???§??
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//?????????ú??,?§??printf????,?????è??????use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×????è?????§??????
struct __FILE
{
int handle;
};
FILE __stdout;
//?¨??_sys_exit()??±???????°??÷?ú????
_sys_exit(int x)
{
x = x;
}
//???¨??fputc????
int fputc(int ch, FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,(uint8_t)ch);
return ch;
}
#endif
/*????microLib??·?·¨*/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
#if EN_USART1_RX //??????????????
//????1????·??????ò
//×???,????USARTx->SR??±??????????????í?ó
u8 USART_RX_BUF[USART_REC_LEN]; //????????,×??óUSART_REC_LEN??×???.
//????×???
//bit15?? ?????ê??±ê??
//bit14?? ??????0x0d
//bit13~0?? ???????????§×???????
u16 USART_RX_STA=0; //????×???±ê??
//??????IO ????1
//bound:?¨????
void uart_init(u32 bound){
//GPIO?????è??
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //????USART1??GPIOA?±??
USART_DeInit(USART1); //????????1
//USART1_TX  A.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA9
//USART1_RX  A.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA10
//USART ???????è??
USART_InitStructure.USART_BaudRate = bound;//??°??è????9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×??¤??8??????????
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(USART1, &USART_InitStructure); //??????????
#if EN_USART1_RX //??????????????
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//????????
#endif
USART_Cmd(USART1, ENABLE); //????????
}
void USART1_IRQHandler(void) //????1????·??????ò
{
int c;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){} //??????
c= (USART_ReceiveData(USART1)); //??????
USART_SendData(USART1,c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}
}
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntExit();
#endif
#endif
MAIN.C的
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
//ALIENTEK????STM32??·?°????é4
//???????é
//?????§????www.openedv.com
//?????????í??×?????????????
int main(void)
{
u8 t;
u8 len;
u16 times=0;
delay_init(); //???±??????????
NVIC_Configuration(); //?è??NVIC????·?×é2:2??????????????2???ì????????
uart_init(9600); //????????????9600
LED_Init(); //LED??????????
KEY_Init(); //????????°??ü??????????????
while(1)
{
LED0=!LED0;
delay_ms(200);
}
}
USART.H的
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
//////////////////////////////////////////////////////////////////////////////////
//±????ò?????§?°??????????×÷???í???????????????ü????????
//Mini STM32??·?°?
//????1??????
//??????×?@ALIENTEK
//????????:www.openedv.csom
//????????:2011/6/14
//°?±???V1.4
//°??¨?ù??????°?±?????
//Copyright(C) ??????×? 2009-2019
//All rights reserved
//********************************************************************************
//V1.3???????÷
//?§???????????????????????¨?????è??.
//????????printf???§??
//???????????????ü??????.
//??????printf??????×?·????§??bug
//V1.4???????÷
//1,??????????????IO??bug
//2,??????USART_RX_STA,????????×??ó????×???????2??14??·?
//3,??????USART_REC_LEN,?????¨??????×??ó???í??????×?????(???ó??2??14??·?)
//4,??????EN_USART1_RX??????·???
//////////////////////////////////////////////////////////////////////////////////
#define USART_REC_LEN 200 //?¨??×??ó????×????? 200
#define EN_USART1_RX 1 //?????¨1??/?????¨0??????1????
extern u8 USART_RX_BUF[USART_REC_LEN]; //????????,×??óUSART_REC_LEN??×???.??×?????????·?
extern u16 USART_RX_STA; //????×???±ê??
//??????????????????????????×????????ê?¨??
void uart_init(u32 bound);
#endif |
|