在原子的例程上将usart1修改到usart3,usart1时功能正常。
波特率的话我在main里面改成uart_init(36,9600);
现在的情况是电脑串口可以收,但是发指令无反应。
以下为修改过的usart.c代码:
#include "sys.h"
#include "usart.h"
#include "delay.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((USART3->SR&0X40)==0);//循环发送,直到发送完毕
USART3->DR = (u8) ch;
return ch;
}
#endif
//end
//////////////////////////////////////////////////////////////////
#ifdef EN_USART3_RX //如果使能了接收
u16 USART_RX_STA=0; //接收状态标记
int USART_SHOW_STA_temp=0;
int USART_SHOW_STA=0;
int legnum_temp=0;
int pos_temp=0;
int group_temp=0;
int showgroup_temp=0;
u8 groups[2]={4,1};
u16 sent[20][legs];
u16 send[20][legs]={{1650,700,900,1800,1000,984,1500,700,900,2100,1900,1480,2100,2300,1500,1740,1860,1100},
{1500,970,860,1800,1000,984,1300,1390,1650,2100,1900,1480,2056,1900,1800,1740,1860,1100},
{1900,1140,1260,1500,700,894,1520,1100,811,2100,2300,1500,2100,2000,1100,2100,2300,1350},
{1900,1140,1260,1200,1000,900,1520,1100,811,1350,1610,1700,2100,2000,1100,2140,2030,1500}};
void USART3_IRQHandler(void)
{
u8 res;
if(USART3->SR&(1<<5))//接收到数据
{
res=USART3->DR;
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000) //已接收0x0D
{
if(res!=0x0a)USART_RX_STA=0;
else
{
USART_RX_STA|=0x8000; //接收完成
if(!USART_SHOW_STA_temp)
{
send[group_temp-1][legnum_temp-1]=pos_temp;
legnum_temp=0;
}
USART_SHOW_STA=USART_SHOW_STA_temp;
if(group_temp>(int)groups[1]){groups[1]=group_temp;}
pos_temp=0;
group_temp=0;
USART_SHOW_STA_temp=0;
USART_RX_STA=0x0000;
}
}
else
{
if(res==0x0d)
USART_RX_STA|=0x4000;//收到0x0d
}
if(res==0x3f)USART_SHOW_STA_temp=1;//收到?
if(res==0x23) //收到#
USART_RX_STA|=0x0800;
if(res==0x4C) //收到L
{
USART_RX_STA|=0x1000;
if(USART_RX_STA&0x3000) //P之后收到L
{
send[group_temp-1][legnum_temp-1]=pos_temp;
legnum_temp=0;
pos_temp=0;
USART_RX_STA&=0xDFFF;//清空P标志位
}
if(USART_RX_STA&0x0800)
USART_RX_STA&=0xF7FF;//清空#标志位
}
if(res==0x50) //收到P
{
USART_RX_STA&=0xE000;
USART_RX_STA|=0x2000;
}
if((res>=0x30)&&(res<=0x39))
{
if(USART_RX_STA&0x0800)//已收到#
group_temp=group_temp*10+(int)(res&0x0F);
if(USART_RX_STA&0x1000)//已收到L
legnum_temp=legnum_temp*10+(int)(res&0x0F);
if(USART_RX_STA&0x2000)//已收到P
pos_temp=pos_temp*10+(int)(res&0x0F);
if(USART_SHOW_STA_temp==1)//已收到?
showgroup_temp=showgroup_temp*10+(int)(res&0x0F);
}
}
}
}
#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<<0;
AFIO->MAPR|=0X00<<4;
RCC->APB2ENR|=1<<3; //使能PORTB口时钟
RCC->APB1ENR|=1<<18; //使能串口时钟
GPIOB->CRH&=0XFFFF00FF;
GPIOB->CRH|=0X00008B00;//IO状态设置
RCC->APB1RSTR|=1<<18; //复位串口3
RCC->APB1RSTR&=~(1<<18);//停止复位
//波特率设置
USART3->BRR=mantissa; // 波特率设置
USART3->CR1|=0X200C; //1位停止,无校验位.
#ifdef EN_USART3_RX //如果使能了接收
//使能接收中断
USART3->CR1|=1<<8; //PE中断使能
USART3->CR1|=1<<5; //接收缓冲区非空中断使能
MY_NVIC_Init(3,3,USART3_IRQChannel,2);//组2,最低优先级
#endif
}
请教问题出在哪里,谢谢!
|