STM8控制步进电机,但指令要发好多次才能成功?为什么?
时间:10-02
整理:3721RD
点击:
各位大神求教下,我用STM8控制步进电机工作,但是指令要发送好多次步进电机才会成功执行,请问是代码问题还是硬件问题?
很急,在线等,求各位大神帮帮忙!
下面是代码:
/**
******************************************************************************
*STM8芯片控制单片机
*uart1从电脑接收数据
*uart3将数据发送给max485
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "main.h"
#include "stm8s_conf.h"
#include "stm8s_uart1.h"
#include "stm8s_uart3.h"
/* define ------------------------------------------------------------------*/
#define Max485_Send GPIO_WriteHigh(GPIOD, GPIO_PIN_4)
#define Max485_Rec GPIO_WriteLow(GPIOD, GPIO_PIN_4)
#define uint unsigned int
#define uchar unsigned char
/* 定义全局变量 ------------------------------------------------------------------*/
unsigned char flag1,flag2;
unsigned int temp;
unsigned int data_rec1[7];
unsigned int data_rec2[7];
unsigned int Data_Add = 0;
/* 函数声明 ------------------------------------------------------------------*/
void Gpio_Init(void);
void HalUart_Init(void);
void delay(void);
void Host1_RecData(void);
void Host3_RecData(void);
void HalUart1_SendData(unsigned int *data, uint length);
/* GPIOD初始化 ------------------------------------------------------------------*/
void Gpio_Init(void)
{
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW);
}
/* Uart串口初始化 ------------------------------------------------------------------*/
void HalUart_Init(void)
{
CLK_DeInit();
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIdiv1);
CLK_HSICmd(ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1,ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART3, ENABLE);
//配置串口
UART1_DeInit();
UART3_DeInit();
//串口初始化
UART1_Init((u32)19200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
UART3_Init((u32)19200, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO, UART3_MODE_TXRX_ENABLE);
//接收寄存器不为空中断
UART1_ITConfig(UART1_IT_RXNE_OR,ENABLE);
UART3_ITConfig(UART3_IT_RXNE_OR,ENABLE);
//启用端口
UART1_Cmd(ENABLE);
UART3_Cmd(ENABLE);
}
/* 延时函数 ------------------------------------------------------------------*/
void delay(void)
{
int i = 800;
while(i>0)
i--;
}
/* UART1接收数据(上位机传数据到下位机)------------------------------------------------------------------*/
void Host1_RecData(void)
{
temp = UART1_ReceiveData8();
switch(temp)
{
case 0x00://锁键盘
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x4b;
data_rec1[5]=0x31;
data_rec1[6]=0x0d;
break;
case 0x01://解键盘
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x4b;
data_rec1[5]=0x30;
data_rec1[6]=0x0d;
break;
case 0x02://启动
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x43;
data_rec1[5]=0x31;
data_rec1[6]=0x0d;
break;
case 0x03://关闭
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x43;
data_rec1[5]=0x30;
data_rec1[6]=0x0d;
break;
case 0x04://正转
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x42;
data_rec1[5]=0x31;
data_rec1[6]=0x0d;
break;
case 0x05://反转
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x42;
data_rec1[5]=0x30;
data_rec1[6]=0x0d;
break;
}
}
/* UART1发送数据(下位机传数据到上位机)------------------------------------------------------------------*/
void Uart1_SendByte(uint data)
{
while(!UART1_GetFlagStatus(UART1_FLAG_TXE));
UART1_SendData8(data);
}
void HalUart1_SendData(unsigned int *data, uint length)
{
unsigned char i;
for(i=0;i<length;i++)
{
Uart1_SendByte(data[i]);
}
}
/* UART3接收数据------------------------------------------------------------------*/
void Host3_RecData(void)
{
data_rec2[Data_Add] = UART3_ReceiveData8();
/*
if(Data_Add == 0)
{
if(data_rec2[0] != 0x0a)
{
Data_Add = 0;
return;
}
}
else if(Data_Add == 1)
{
if(data_rec2[0] != 0x0a||data_rec2[1] != 0x44)
{
Data_Add = 0;
return;
}
}
*/
Data_Add++;
if(Data_Add == 6)
{
Data_Add = 0;
HalUart1_SendData(data_rec2,7);
}
}
/* UART3发送数据------------------------------------------------------------------*/
void HalUart3_SendByte(uint data)
{
while(!UART3_GetFlagStatus(UART3_FLAG_TXE));
UART3_SendData8(data);
}
/*
* 主函数
*/
void main(void)
{
uint i;
Gpio_Init();
HalUart_Init();
enableInterrupts(); //开启总中断
while(1)
{
if(flag1 == 1)
{
flag1 = 0;
Host1_RecData();
Max485_Send;
for(i = 0;i < 7; i++)
{
HalUart3_SendByte(data_rec1[i]);
}
delay();
delay();
delay();
Max485_Rec;
}
if(flag2 == 1)
{
Host3_RecData();
flag2 = 0;
}
}
}
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
/* Add your own code to manage an assert error */
/* Infinite loop */
while (1)
{
}
}
中断代码:
@near @interrupt void UART1_RX_IRQHandler (void)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
flag1=1;
UART1_ClearFlag(UART1_FLAG_RXNE);
return;
}
@near @interrupt void UART3_RX_IRQHandler (void)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
flag2=1;
UART3_ClearFlag(UART3_FLAG_RXNE);
return;
}
很急,在线等,求各位大神帮帮忙!
下面是代码:
/**
******************************************************************************
*STM8芯片控制单片机
*uart1从电脑接收数据
*uart3将数据发送给max485
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "main.h"
#include "stm8s_conf.h"
#include "stm8s_uart1.h"
#include "stm8s_uart3.h"
/* define ------------------------------------------------------------------*/
#define Max485_Send GPIO_WriteHigh(GPIOD, GPIO_PIN_4)
#define Max485_Rec GPIO_WriteLow(GPIOD, GPIO_PIN_4)
#define uint unsigned int
#define uchar unsigned char
/* 定义全局变量 ------------------------------------------------------------------*/
unsigned char flag1,flag2;
unsigned int temp;
unsigned int data_rec1[7];
unsigned int data_rec2[7];
unsigned int Data_Add = 0;
/* 函数声明 ------------------------------------------------------------------*/
void Gpio_Init(void);
void HalUart_Init(void);
void delay(void);
void Host1_RecData(void);
void Host3_RecData(void);
void HalUart1_SendData(unsigned int *data, uint length);
/* GPIOD初始化 ------------------------------------------------------------------*/
void Gpio_Init(void)
{
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW);
}
/* Uart串口初始化 ------------------------------------------------------------------*/
void HalUart_Init(void)
{
CLK_DeInit();
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIdiv1);
CLK_HSICmd(ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1,ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART3, ENABLE);
//配置串口
UART1_DeInit();
UART3_DeInit();
//串口初始化
UART1_Init((u32)19200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
UART3_Init((u32)19200, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO, UART3_MODE_TXRX_ENABLE);
//接收寄存器不为空中断
UART1_ITConfig(UART1_IT_RXNE_OR,ENABLE);
UART3_ITConfig(UART3_IT_RXNE_OR,ENABLE);
//启用端口
UART1_Cmd(ENABLE);
UART3_Cmd(ENABLE);
}
/* 延时函数 ------------------------------------------------------------------*/
void delay(void)
{
int i = 800;
while(i>0)
i--;
}
/* UART1接收数据(上位机传数据到下位机)------------------------------------------------------------------*/
void Host1_RecData(void)
{
temp = UART1_ReceiveData8();
switch(temp)
{
case 0x00://锁键盘
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x4b;
data_rec1[5]=0x31;
data_rec1[6]=0x0d;
break;
case 0x01://解键盘
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x4b;
data_rec1[5]=0x30;
data_rec1[6]=0x0d;
break;
case 0x02://启动
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x43;
data_rec1[5]=0x31;
data_rec1[6]=0x0d;
break;
case 0x03://关闭
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x43;
data_rec1[5]=0x30;
data_rec1[6]=0x0d;
break;
case 0x04://正转
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x42;
data_rec1[5]=0x31;
data_rec1[6]=0x0d;
break;
case 0x05://反转
data_rec1[0]=0x0a;
data_rec1[1]=0x44;
data_rec1[2]=0x30;
data_rec1[3]=0x31;
data_rec1[4]=0x42;
data_rec1[5]=0x30;
data_rec1[6]=0x0d;
break;
}
}
/* UART1发送数据(下位机传数据到上位机)------------------------------------------------------------------*/
void Uart1_SendByte(uint data)
{
while(!UART1_GetFlagStatus(UART1_FLAG_TXE));
UART1_SendData8(data);
}
void HalUart1_SendData(unsigned int *data, uint length)
{
unsigned char i;
for(i=0;i<length;i++)
{
Uart1_SendByte(data[i]);
}
}
/* UART3接收数据------------------------------------------------------------------*/
void Host3_RecData(void)
{
data_rec2[Data_Add] = UART3_ReceiveData8();
/*
if(Data_Add == 0)
{
if(data_rec2[0] != 0x0a)
{
Data_Add = 0;
return;
}
}
else if(Data_Add == 1)
{
if(data_rec2[0] != 0x0a||data_rec2[1] != 0x44)
{
Data_Add = 0;
return;
}
}
*/
Data_Add++;
if(Data_Add == 6)
{
Data_Add = 0;
HalUart1_SendData(data_rec2,7);
}
}
/* UART3发送数据------------------------------------------------------------------*/
void HalUart3_SendByte(uint data)
{
while(!UART3_GetFlagStatus(UART3_FLAG_TXE));
UART3_SendData8(data);
}
/*
* 主函数
*/
void main(void)
{
uint i;
Gpio_Init();
HalUart_Init();
enableInterrupts(); //开启总中断
while(1)
{
if(flag1 == 1)
{
flag1 = 0;
Host1_RecData();
Max485_Send;
for(i = 0;i < 7; i++)
{
HalUart3_SendByte(data_rec1[i]);
}
delay();
delay();
delay();
Max485_Rec;
}
if(flag2 == 1)
{
Host3_RecData();
flag2 = 0;
}
}
}
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
/* Add your own code to manage an assert error */
/* Infinite loop */
while (1)
{
}
}
中断代码:
@near @interrupt void UART1_RX_IRQHandler (void)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
flag1=1;
UART1_ClearFlag(UART1_FLAG_RXNE);
return;
}
@near @interrupt void UART3_RX_IRQHandler (void)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
flag2=1;
UART3_ClearFlag(UART3_FLAG_RXNE);
return;
}
一句也看不懂,但我知道问题总可以解决的,帮你顶一下,
好用吗?
谢谢,问题解决了,不是代码问题!是硬件没共地导致的的!