Structure for command of control of stepper motor with 8 byte
1 - 0x01 start byte
3 - 0x00 reserv
4 - HI byte for number of steps (hex mode)
5 - MI byte for number of steps (hex mode)
6 - MI byte for number of steps (hex mode)
7 - LO byte for number of steps (hex mode) 8 - 0xFF - end byte
Send from stepper motor to PC for control
TxData[0] = 0x30;
TxData[1] = 0x31;
TxData[2] = 0x32;
TxData[3] = 0x33;
TxData[4] = 0x34;
TxData[5] = 0x35;
TxData[6] = 0x0D;
TxData[7] = 0x0A;
C# send command
=======================================================================
private void SendCommand(byte address, byte direction, UInt32 number_of_step){
byte[] send_to_stpepper = { address, direction, 0, (byte)(number_of_step >> 24), (byte)(number_of_step >> 16), (byte)(number_of_step >> 8), (byte)(number_of_step), 0xFF };
if (serialPort_stepper.IsOpen)
{
serialPort_stepper.Write(send_to_stpepper, 0, 8);
}
}
Keil
========================================================================
if(HAL_GPIO_ReadPin(GPIOB, PC_Manual_Pin) == GPIO_PIN_SET) // Manual control
{
if(HAL_GPIO_ReadPin(GPIOB, Manual_DIR_Pin) == 0) // set direcrion of rotation
{
HAL_GPIO_WritePin(GPIOB, DIR_Pin, GPIO_PIN_SET); // ser forward
}
else
{
HAL_GPIO_WritePin(GPIOB, DIR_Pin, GPIO_PIN_RESET); //set rewind
}
if((HAL_GPIO_ReadPin(GPIOB, SW2_Pin))&&(HAL_GPIO_ReadPin(GPIOB, SW1_Pin)) != 0) // control of SW1 SW2
{
nFAULT();
one_step();
}
if(((HAL_GPIO_ReadPin(GPIOB, DIR_Pin)) == 0) && (HAL_GPIO_ReadPin(GPIOB, SW1_Pin) == 0))
{
nFAULT(); // rotation
one_step();
}
if(((HAL_GPIO_ReadPin(GPIOB, DIR_Pin)) == 1) && (HAL_GPIO_ReadPin(GPIOB, SW2_Pin) == 0))
{
nFAULT(); // rotation
one_step();
}
}
//==================================================================================
//==================================================================================
else // PC control
{
HAL_UART_Receive_DMA(&huart1, RxData, 8);
if(RxData[7] == 0xFF)
{
if(RxData[1] == 0) // set direcrion of rotation
{
HAL_GPIO_WritePin(GPIOB, DIR_Pin, GPIO_PIN_SET); // ser forward
}
else
{
HAL_GPIO_WritePin(GPIOB, DIR_Pin, GPIO_PIN_RESET); //set rewind
}
number_of_step = (RxData[3]<<24) + (RxData[4]<<16) + (RxData[5]<<8) + RxData[6];
for(uint32_t sss = 0; sss < 8; sss++) {RxData[sss] = 0;}
for(i=0; i < number_of_step; i++)
{
i++;
nFAULT(); // rotation
one_step();
if((HAL_GPIO_ReadPin(GPIOB, SW1_Pin)) == 0)
{
i = number_of_step;
TxData[0] = 3;
HAL_UART_Transmit_DMA(&huart1, TxData, 1);
}
if((HAL_GPIO_ReadPin(GPIOB, SW2_Pin)) == 0)
{
i = number_of_step;
TxData[0] = 4;
HAL_UART_Transmit_DMA(&huart1, TxData, 1);
}
if(RxData[7] == 0xFF)
{
number_of_step = (RxData[3]<<24) + (RxData[4]<<16) + (RxData[5]<<8) + RxData[6];
for(uint32_t sss = 0; sss < 8; sss++) {RxData[sss] = 0;}
TxData[0] = 2;
HAL_UART_Transmit_DMA(&huart1, TxData, 1);
}
}
HAL_GPIO_WritePin(GPIOB, nENBL_Pin, GPIO_PIN_SET);
TxData[0] = 1;
HAL_UART_Transmit_DMA(&huart1, TxData, 1);
}
}
void nFAULT(void) { while(HAL_GPIO_ReadPin(GPIOB, nFAULT_Pin) == 0) { } } void one_step(void) { HAL_GPIO_WritePin(GPIOB, nENBL_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOB, STEP_Pin, GPIO_PIN_SET); HAL_Delay(time_of_step); HAL_GPIO_WritePin(GPIOB, STEP_Pin, GPIO_PIN_RESET); }
Комментариев нет:
Отправить комментарий
ваше мнение...