-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainservo.c
94 lines (60 loc) · 1.92 KB
/
mainservo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <main.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//extender definicion a archivo main_init.c
UART_HandleTypeDef huart1 = {0}; //using uart 1 --manejador
TIM_HandleTypeDef htim3 = {0};
TIM_HandleTypeDef htim2 = {0}; //timer2 32 bit timer
TIM_HandleTypeDef htim4 = {0};
char mess[] = "UART\r\n";
long setpoint = 0;
uint8_t byte, idx = 0;
uint8_t buffer[50];
int main(void)
{
System_Init();
HAL_UART_Transmit(&huart1, (uint8_t*)mess, strlen(mess), HAL_MAX_DELAY);
HAL_UART_Receive_IT(&huart1, &byte, 1);
//HAL_UART_Receive_IT(&huart1, &byte, 1); //modo: recibir solo un byte y llamar funcion callback a traves de USART_IRQ
//HAL_Delay(1000); // Delay for 1 second
while(1)
{
//HAL_UART_Receive_IT(&huart1, &byte, 1); //modo: recibir solo un byte y llamar funcion callback a traves de USART_IRQ
snprintf((char*)buffer, sizeof(buffer), "Setpoint: %ld\r\n", setpoint);
HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen((char*)buffer), HAL_MAX_DELAY);
HAL_Delay(1000); //1 second delay
}
return 0;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
//que timer se manda llamar dependiendo de la actividad
if(htim -> Instance == TIM3) //en este caso el TIM3 es el que genera la interrupcion
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
//PID Algorithm control
//1. read position
//2. compare to set point - initially zero
//3. calculate output (0 to 999)
}
//manda llamar una vez que recibe la cantidad de bytes indicados,
//se ejecuta a traves del IRQHandler
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(byte == '\n')
{
buffer[idx++] = byte;
HAL_UART_Transmit(&huart1, buffer, idx, HAL_MAX_DELAY);
idx = 0;
}
else
{
buffer[idx++] = byte;
}
setpoint = strtol((char*)buffer, NULL, 10); //convert string to long int
HAL_UART_Receive_IT(&huart1, &byte, 1); //start next receive
}
void Error_Handler(void)
{
while(1);
}