参考蓝牙例程写的一个串口定时中断程序,也有前辈写过类似的,不过是用库函数写的,我看不太懂,就自己仿着写了一个,大家帮我看下哪里有问题,我下载到mini板子上后,发送几个字符过去,确不会返回字符!! 这跟我想的不一样,我自己也是检查了好几遍了,还是不通,麻烦大家帮看下哪里的问题,多谢啦
[mw_shl_code=c,true]USART.C[/mw_shl_code]
[mw_shl_code=c,true]#include "sys.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////
//±????ò???©?§?°??????????×÷???í???????????????ü????????
//Mini STM32??·?°?
//?®??1??????
//??????×?@ALIENTEK
//????????:www.openedv.com
//????????:2010/5/27
//°?±???V1.3
//°??¨?ù??????°?±?????
//Copyright(C) ??????×? 2009-2019
//All rights reserved
//********************************************************************************
//V1.3???????÷
//?§???????????????????®???¨?????è??.
//????????printf???§??
//???????®???????ü??????.
//??????printf??????×?·????§??bug
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//?????????ú??,?§??printf????,?????è??????use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×????è?????§??????
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef?? d in stdio.h. */
FILE __stdout;
//?¨??_sys_exit()??±???????°??÷?ú????
_sys_exit(int x)
{
x = x;
}
//???¨??fputc????
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//???··???,?±??·????ê±?
USART1->DR = (u8) ch;
return ch;
}
#endif
//end
//////////////////////////////////////////////////////////////////
#ifdef EN_USART1_RX //??????????????
//?®??1????·??????ò
//×???,????USARTx->SR??±??????????????í?ó
u8 USART_RX_BUF[USART_MAX_RECV_LEN]; //????????,×??ó64??×???.
//????×???
//bit7???????ê??±ê??
//bit6????????0x0d
//bit5~0?????????????§×???????
u8 USART_RX_STA=0; //????×???±ê??
void USART1_IRQHandler(void)
{
u8 res;
if(USART1->SR&(1<<5))
{
res=USART1->DR;
if(USART_RX_STA<USART_MAX_RECV_LEN)
{
TIM4->CNT=0;
if(USART_RX_STA==0)TIM4_Set(1);
USART_RX_BUF[USART_RX_STA++]=res;
}else
{
USART_RX_STA|=1<<15;
}
}
}
#endif
//??????IO ?®??1
//pclk2 CLK2?±??????(Mhz)
//bound:?¨????
//CHECK OK
//091209
void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);//????USARTDIV
mantissa=temp; //??????????·?
fraction=(temp-mantissa)*16; //??????????·?
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2; //????PORTA???±??
RCC->APB2ENR|=1<<14; //?????®???±??
GPIOA->CRH&=0XFFFFF00F;
GPIOA->CRH|=0X000008B0;//IO×????è??
RCC->APB2RSTR|=1<<14; //?????®??1
RCC->APB2RSTR&=~(1<<14);//????????
//?¨?????è??
USART1->BRR=mantissa; // ?¨?????è??
USART1->CR1|=0X200C; //1??????,?????é??.
#ifdef EN_USART1_RX //??????????????
//????????????
USART1->CR1|=1<<8; //PE????????
USART1->CR1|=1<<5; //??????????·???????????
MY_NVIC_Init(2,3,USART1_IRQChannel,2);//×é2??×?????????
TIM4_Init(99,7199);
USART_RX_STA=0;
TIM4_Set(0);
#endif
}
//?¨?±?÷4????·??????ò
void TIM4_IRQHandler(void)
{
if(TIM4->SR&0X01)//???ü??????
{
USART_RX_STA|=1<<15; //±ê???????ê??
TIM4->SR&=~(1<<0); //????????±ê????
TIM4_Set(0); //??±?TIM4
}
}
//?è??TIM4??????
//sta:0????±?;1,????;
void TIM4_Set(u8 sta)
{
if(sta)
{
TIM4->CNT=0; //?????÷????
TIM4->CR1|=1<<0; //?????¨?±?÷4
}else TIM4->CR1&=~(1<<0);//??±??¨?±?÷4
}
//?¨???¨?±?÷??????????
//??????????????APB1??2±?????APB1??36M
//arr??×?????×°????
//psc???±???¤·?????
void TIM4_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<2; //TIM4?±??????
TIM4->ARR=arr; //?è?¨?????÷×?????×°??
TIM4-> SC=psc; //?¤·????÷
TIM4->DIER|=1<<0; //???í?ü??????
TIM4->CR1|=0x01; //?????¨?±?÷4
MY_NVIC_Init(1,3,TIM4_IRQChannel,2);//????2??×???????3??×é2 ??2????????×???
}
[/mw_shl_code]
[mw_shl_code=c,true]mian.c[/mw_shl_code]
[mw_shl_code=c,true]#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "key.h"
//#include "timer.h"
//Mini STM32??·?°?·????ú??3
//?®?????é
//??????×?@ALIENTEK
//????????:www.openedv.com
int main(void)
{
u8 len;
u8 t;
//u16 times=0;
Stm32_Clock_Init(9); //?????±???è??
delay_init(72); //???±??????
uart_init(72,9600); //?®??????????9600
LED_Init(); //????????LED??????????????
//Timerx_Init(4999,7199);
while(1)
{
delay_ms(500);
LED1=!LED1;
if(USART_RX_STA&(1<<15))
{
len=USART_RX_STA&0x3f;
for(t=0;t<len;t++)
{
USART1->DR=USART_RX_BUF[t];
while((USART1->SR&0X40)==0);//????·????á??
}
USART_RX_STA=0;
TIM4_Set(0);
}
}
}
[/mw_shl_code]
///中文注释为什么复制过来都是乱码啊
只能把工程都打包传上来了 |