Artemmm » 22 апр 2014, 11:28
#include "stm32f4xx.h"
#include <stdio.h>
void USART2_ini(void);
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_Init_Usa;
GPIO_InitTypeDef GPIO_Init_Button;
void Button_ini(void);
uint16_t delya_count=0;
void SysTick_Handler(void)
{
if (delya_count>0)
{
delya_count--;
}
}
void delay_ms(uint16_t delya_temp)
{
delya_count = delya_temp;
while(delya_count){}
}
void SendStrtoPC(char* str_p, uint16_t count)
{
uint16_t i=0;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==1)
{
while(i< count)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET){}
USART_SendData ( USART2, str_p[i]);
i++;
}
}
}
int main (void)
{
char str[30];
SysTick_Config(SystemCoreClock/1000);
USART2_ini();
Button_ini();
sprintf(str, "0xC2,1023");
while(1)
{
delay_ms(500);
SendStrtoPC(str, 30);
}
}
void USART2_ini(void)
{
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
}
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_Init_Usa.USART_BaudRate = 9600;
USART_Init_Usa.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init_Usa.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
USART_Init_Usa.USART_Parity = USART_Parity_No;
USART_Init_Usa.USART_StopBits = USART_StopBits_1;
USART_Init_Usa.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2, &USART_Init_Usa);
USART_Cmd(USART2, ENABLE);
}
}
void Button_ini(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_Init_Button.GPIO_Pin = GPIO_Pin_0;
GPIO_Init_Button.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init_Button.GPIO_OType = GPIO_OType_PP;
GPIO_Init_Button.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init_Button.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_Init_Button);
GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
}
на драйвере имеется СОМ порт (RX TX GND), соответственно PORTA3 на TX, PORTA2 на RX, GND на GND. Скорость передачи 9600.
Посылаются команды вот часть из них:
0xC8 – 0xCB: set motor 2
This command takes one data byte and returns nothing. It immediately sets the speed of the
specified motor equal to the data byte and the motor direction based on the two least significant
bits of the command byte. The direction bits work as follows:
00 = brake low (command 0xC0/0xC8)
01 = reverse (command 0xC1/0xC9)
10 = forward (command 0xC2/0xCA)
11 = brake low (command 0xC3/0xCB)
0xC4 – 0xC7: accelerate motor 1
0xCC – 0xCF: accelerate motor 2
This command takes one data byte and returns nothing. It sets the target speed of of the
specified motor equal to the data byte and the target motor direction based on the two least
significant bits of the command byte. The direction bits work as follows:
00 = brake low (command 0xC4/0xCC)
01 = reverse (command 0xC5/0xCD)
10 = forward (command 0xC6/0xCE)
11 = brake low (command 0xC7/0xCF)
Motor speed is updated 100 times per second. If it is below its target speed and if its current
direction is the same as its target direction, each update will adjust the speed by increasing it by
a tenth of its acceleration parameter. If it is above its target speed and its direction is in the
target direction, the update will merely set its speed equal to the target value (i.e. there is no
deceleration). Every 10ms, the following acceleration logic is performed:
if motor speed < target speed, motor speed = motor speed + acceleration/10
if motor speed > target speed, motor speed = target speed
If the target direction differs from the current direction, the first update will brake the motor at
100% duty cycle for the amount of time specified by the its brake duration parameter. It will
then set the motor speed to zero and accelerate from there to the target speed in the target
direction.
If you intend to use the current-limiting feature of the TReX, you should use acceleration
commands to control your motor speed. This is because acceleration commands schedule
motor updates to happen as part of a fixed update cycle that also takes care of the current-
limiting logic. “Set motor” commands set the motor speed the instant they're received and will,
at least temporarily, override any current-limiting actions the TReX is taking. If you want to
limit your current but you don't want acceleration, set the acceleration parameter to zero; this
essentially requests infinite acceleration.
Note: acceleration does not apply to braking or to a speed decrease that does not also result in a
change of direction. Motor speed can also be influenced by current-limit settings, which add
additional considerations to the logic detailed in this section. Please see the current limit
section of the parameter documentation for more details.