专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
急,stm32 按键复位后 才正常运行
2019-08-17 02:58
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
2307
9
1036
师兄们 最近我遇到一个很奇怪的问题,就是按复位键后 单片机才正常运行(仿真也能正常运行),下载其他程序就能正常运行!很纳闷……实在想不到问题出在哪
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
9条回答
jackiesunhao
2019-08-17 18:09
//***********************************************************//
//***************************motor.c*************************//
//***********************************************************//
#include "sys.h"
#include "motor.h"
#include "sys_tick.h"
#include "usart.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "mystring.h"
#include "delay.h"
#define FW_Ratio 375 // 方位比例
s16 FW_Max_Speed = 240; // 0.1°/s
const s16 FW_Motor_Acc = 500;
volatile s32 old_fwspeed = 0;
void motorinit(void);
void Set_FWSpeed(s32 ispeed);
void Set_Channel_NVIC(u8 NVIC_Channel, u8 Priority);//设置中断
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
////********* 方位 孔子和 *************8
void Motor_Init(void)
{
GPIOB->CRH &= ~(((u32)(0xffff))<<16); //PB12 ~ PB15 通用推挽
GPIOB->CRH |= 0x3333 << 16;
GPIOD->CRH &= ~(0xff); //PD8/PD9 通用推挽
GPIOD->CRH |= 0x33;
////////////////////////////
////***** T5 **************
RCC->APB1ENR |= 8; //使能TIM5时钟
TIM5->CR1 |= 1<<4; //向下计数
TIM5->DIER = 0;
TIM5->DIER |= 1; //允许更新中断
TIM5->PSC = 11; ///72 /(11+1) = 6M T2时钟;
Set_Channel_NVIC(50, 3);//低优先级1
////////////////////////////
////***** T2 **************
}
////---------------------------------------------------------------------------
////********* *************8
//电机最低速度 1.512°/s , 0.0042转/s
void Set_FWSpeed(s32 ispeed) //0.1°/s
{
s32 tempspeed=0;
u8 speeddatastr[16]={0};
u8 temp_speeddatastr[6]={0};
// char speedTemp;
speeddatastr[0] = 0X73; // 's'
speeddatastr[1] = 0X20; // ' '
speeddatastr[2] = 0X72; // 'r'
speeddatastr[3] = 0X30; // '0'
speeddatastr[4] = 0X78; // 'x'
speeddatastr[5] = 0X32; // '2'
speeddatastr[6] = 0X66; // 'f'
speeddatastr[7] = 0X20; // ' '
// speeddatastr[8] = 'D'; //速度方向位
speeddatastr[9] = 0X30; // '0'
speeddatastr[10] = 0X30; // '0'
speeddatastr[11] = 0X30; // '0'
speeddatastr[12] = 0X30; // '0'
speeddatastr[13] = 0X30; // '0'
speeddatastr[14] = 0X30; // '0'
speeddatastr[15] = 0x0D; // ' '
if (FW_Max_Speed < ispeed)
ispeed = FW_Max_Speed;
else if (-FW_Max_Speed > ispeed)
ispeed = -FW_Max_Speed;
old_fwspeed = ispeed;
if(0 < ispeed)
{
speeddatastr[8] = 0x2D;
}
else
{
speeddatastr[8] = 0x30;
}
tempspeed =(abs(ispeed) *6250/3 ); //转换为电机速度 转/s,减速度比为 1:375
my_itoa(tempspeed, temp_speeddatastr, 10) ;
speeddatastr[9] = temp_speeddatastr[0]; // '0'
speeddatastr[10] = temp_speeddatastr[1]; // '0'
speeddatastr[11] = temp_speeddatastr[2]; // '0'
speeddatastr[12] = temp_speeddatastr[3]; // '0'
speeddatastr[13] = temp_speeddatastr[4]; // '0'
speeddatastr[14] = temp_speeddatastr[5]; // '0'
Uart4_Send1(&speeddatastr[0] , 16);
}
void Driver_Init()
{
char EN_D_Cmd[11] ={0x73,0x20,0x72,0x30,0x78,0x32,0x34,0x20,0x31,0x31,0x0d};
char ACC_D_Cmd[11] ={0x73,0x20,0x72,0x30,0x78,0x33,0x36,0x20,0x35,0x30,0x0d};
char DEC_D_Cmd[11] ={0x73,0x20,0x72,0x30,0x78,0x33,0x37,0x20,0x35,0x30,0x0d};
Uart4_send_str(&EN_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&EN_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&EN_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&DEC_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&ACC_D_Cmd[0],11);
SysTimeCnt = 0 ;
}
void Urgent_Stop_FW()
{
char STOP_D_Cmd[10] ={0x73,0x20,0x72,0x30,0x78,0x32,0x34,0x20,0x30,0x0d};
char DEC_D_Cmd[10] ={0x73,0x20,0x72,0x30,0x78,0x33,0x37,0x20,0x31,0x0d};
SysTimeCnt = 0;
Uart4_send_str(&STOP_D_Cmd[0],11);
while(500 > SysTimeCnt);
SysTimeCnt = 0;
while(500 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&DEC_D_Cmd[0],10);
}
下面是主函数
//****************************************************************//
//**************************** main.c *************************//
//****************************************************************//
#include <stm32f10x_lib.h>
#include <stdlib.h>
#include "sys.h"
#include "main.h"
#include "peripheral.h"
#include "motor.h"
#include "usart.h"
int main(void)
{
s16 ispeed =-120;
SysTimeCnt = 0;
Sys_Init(); //cpu 系统初始化,时钟、中断等
Peripheral_Init(); //MCU外设接口初始化,uart, pwm, timer,spi等
TIM_Timer_Init();
Motor_Init();
Uart_Init();
Driver_Init();
while(1)
{
if(SysTimeCnt >=200)
{
SysTimeCnt = 0;
Set_FWSpeed(ispeed);
}
}
}
加载中...
查看其它9个回答
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
//***************************motor.c*************************//
//***********************************************************//
#include "sys.h"
#include "motor.h"
#include "sys_tick.h"
#include "usart.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "mystring.h"
#include "delay.h"
#define FW_Ratio 375 // 方位比例
s16 FW_Max_Speed = 240; // 0.1°/s
const s16 FW_Motor_Acc = 500;
volatile s32 old_fwspeed = 0;
void motorinit(void);
void Set_FWSpeed(s32 ispeed);
void Set_Channel_NVIC(u8 NVIC_Channel, u8 Priority);//设置中断
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
////********* 方位 孔子和 *************8
void Motor_Init(void)
{
GPIOB->CRH &= ~(((u32)(0xffff))<<16); //PB12 ~ PB15 通用推挽
GPIOB->CRH |= 0x3333 << 16;
GPIOD->CRH &= ~(0xff); //PD8/PD9 通用推挽
GPIOD->CRH |= 0x33;
////////////////////////////
////***** T5 **************
RCC->APB1ENR |= 8; //使能TIM5时钟
TIM5->CR1 |= 1<<4; //向下计数
TIM5->DIER = 0;
TIM5->DIER |= 1; //允许更新中断
TIM5->PSC = 11; ///72 /(11+1) = 6M T2时钟;
Set_Channel_NVIC(50, 3);//低优先级1
////////////////////////////
////***** T2 **************
}
////---------------------------------------------------------------------------
////********* *************8
//电机最低速度 1.512°/s , 0.0042转/s
void Set_FWSpeed(s32 ispeed) //0.1°/s
{
s32 tempspeed=0;
u8 speeddatastr[16]={0};
u8 temp_speeddatastr[6]={0};
// char speedTemp;
speeddatastr[0] = 0X73; // 's'
speeddatastr[1] = 0X20; // ' '
speeddatastr[2] = 0X72; // 'r'
speeddatastr[3] = 0X30; // '0'
speeddatastr[4] = 0X78; // 'x'
speeddatastr[5] = 0X32; // '2'
speeddatastr[6] = 0X66; // 'f'
speeddatastr[7] = 0X20; // ' '
// speeddatastr[8] = 'D'; //速度方向位
speeddatastr[9] = 0X30; // '0'
speeddatastr[10] = 0X30; // '0'
speeddatastr[11] = 0X30; // '0'
speeddatastr[12] = 0X30; // '0'
speeddatastr[13] = 0X30; // '0'
speeddatastr[14] = 0X30; // '0'
speeddatastr[15] = 0x0D; // ' '
if (FW_Max_Speed < ispeed)
ispeed = FW_Max_Speed;
else if (-FW_Max_Speed > ispeed)
ispeed = -FW_Max_Speed;
old_fwspeed = ispeed;
if(0 < ispeed)
{
speeddatastr[8] = 0x2D;
}
else
{
speeddatastr[8] = 0x30;
}
tempspeed =(abs(ispeed) *6250/3 ); //转换为电机速度 转/s,减速度比为 1:375
my_itoa(tempspeed, temp_speeddatastr, 10) ;
speeddatastr[9] = temp_speeddatastr[0]; // '0'
speeddatastr[10] = temp_speeddatastr[1]; // '0'
speeddatastr[11] = temp_speeddatastr[2]; // '0'
speeddatastr[12] = temp_speeddatastr[3]; // '0'
speeddatastr[13] = temp_speeddatastr[4]; // '0'
speeddatastr[14] = temp_speeddatastr[5]; // '0'
Uart4_Send1(&speeddatastr[0] , 16);
}
void Driver_Init()
{
char EN_D_Cmd[11] ={0x73,0x20,0x72,0x30,0x78,0x32,0x34,0x20,0x31,0x31,0x0d};
char ACC_D_Cmd[11] ={0x73,0x20,0x72,0x30,0x78,0x33,0x36,0x20,0x35,0x30,0x0d};
char DEC_D_Cmd[11] ={0x73,0x20,0x72,0x30,0x78,0x33,0x37,0x20,0x35,0x30,0x0d};
Uart4_send_str(&EN_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&EN_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&EN_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&DEC_D_Cmd[0],11);
SysTimeCnt = 0;
while(200 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&ACC_D_Cmd[0],11);
SysTimeCnt = 0 ;
}
void Urgent_Stop_FW()
{
char STOP_D_Cmd[10] ={0x73,0x20,0x72,0x30,0x78,0x32,0x34,0x20,0x30,0x0d};
char DEC_D_Cmd[10] ={0x73,0x20,0x72,0x30,0x78,0x33,0x37,0x20,0x31,0x0d};
SysTimeCnt = 0;
Uart4_send_str(&STOP_D_Cmd[0],11);
while(500 > SysTimeCnt);
SysTimeCnt = 0;
while(500 > SysTimeCnt);
SysTimeCnt = 0;
Uart4_send_str(&DEC_D_Cmd[0],10);
}
下面是主函数
//****************************************************************//
//**************************** main.c *************************//
//****************************************************************//
#include <stm32f10x_lib.h>
#include <stdlib.h>
#include "sys.h"
#include "main.h"
#include "peripheral.h"
#include "motor.h"
#include "usart.h"
int main(void)
{
s16 ispeed =-120;
SysTimeCnt = 0;
Sys_Init(); //cpu 系统初始化,时钟、中断等
Peripheral_Init(); //MCU外设接口初始化,uart, pwm, timer,spi等
TIM_Timer_Init();
Motor_Init();
Uart_Init();
Driver_Init();
while(1)
{
if(SysTimeCnt >=200)
{
SysTimeCnt = 0;
Set_FWSpeed(ispeed);
}
}
}
一周热门 更多>