#include <stdint.h>
/* ==================== REGISTRES ==================== */
/* RCC */
#define RCC_APB2ENR (*(volatile uint32_t*)0x40021018)
/* GPIOA */
#define GPIOA_CRL (*(volatile uint32_t*)0x40010800)
#define GPIOA_CRH (*(volatile uint32_t*)0x40010804)
/* ADC1 */
#define ADC_SR (*(volatile uint32_t*)0x40012400)
#define ADC_CR2 (*(volatile uint32_t*)0x40012408)
#define ADC_SMPR2 (*(volatile uint32_t*)0x40012410)
#define ADC_SQR3 (*(volatile uint32_t*)0x40012434)
#define ADC_DR (*(volatile uint32_t*)0x4001244C)
/* TIM1 */
#define TIM1_CR1 (*(volatile uint32_t*)0x40012C00)
#define TIM1_CCMR1 (*(volatile uint32_t*)0x40012C18)
#define TIM1_CCER (*(volatile uint32_t*)0x40012C20)
#define TIM1_PSC (*(volatile uint32_t*)0x40012C28)
#define TIM1_ARR (*(volatile uint32_t*)0x40012C2C)
#define TIM1_CCR1 (*(volatile uint32_t*)0x40012C34)
#define TIM1_BDTR (*(volatile uint32_t*)0x40012C44)
/* USART1 */
#define USART1_SR (*(volatile uint32_t*)0x40013800)
#define USART1_DR (*(volatile uint32_t*)0x40013804)
#define USART1_BRR (*(volatile uint32_t*)0x40013808)
#define USART1_CR1 (*(volatile uint32_t*)0x4001380C)
/* SysTick */
#define SYST_CSR (*(volatile uint32_t*)0xE000E010)
#define SYST_RVR (*(volatile uint32_t*)0xE000E014)
#define SYST_CVR (*(volatile uint32_t*)0xE000E018)
/* ==================== VARIABLES PID ==================== */
float consigne = 30.0f;
float Kp = 8.0f;
float Ki = 0.3f;
float Kd = 0.0f;
float erreur = 0.0f;
float erreur_precedente = 0.0f;
float integrale = 0.0f;
float temperature = 0.0f;
float commande = 0.0f;
#define DT 0.1f
/* ==================== DELAY ==================== */
void delay_ms(uint32_t ms)
{
while (ms--)
{
SYST_CSR = 0;
SYST_RVR = 8000U - 1U;
SYST_CVR = 0;
SYST_CSR = 5U;
while ((SYST_CSR & (1U << 16)) == 0U) {}
SYST_CSR = 0;
}
}
/* ==================== GPIO ==================== */
void gpio_init(void)
{
RCC_APB2ENR |= (1U << 2) | (1U << 0);
/* PA0: analog input */
GPIOA_CRL &= ~(0xFU);
/* PA8: TIM1_CH1 PWM output */
GPIOA_CRH &= ~(0xFU);
GPIOA_CRH |= 0xBU;
/* PA9: USART1 TX */
GPIOA_CRH &= ~(0xFU << 4);
GPIOA_CRH |= (0xBU << 4);
}
/* ==================== ADC ==================== */
void adc_init(void)
{
RCC_APB2ENR |= (1U << 9);
ADC_SMPR2 |= (7U << 0);
ADC_SQR3 = 0;
ADC_CR2 = (1U << 0);
delay_ms(2);
ADC_CR2 |= (1U << 3);
while (ADC_CR2 & (1U << 3)) {}
ADC_CR2 |= (1U << 2);
while (ADC_CR2 & (1U << 2)) {}
ADC_CR2 |= (1U << 20);
}
float read_temperature(void)
{
ADC_CR2 |= (1U << 22);
while (!(ADC_SR & (1U << 1))) {}
uint16_t valeur_adc = (uint16_t)ADC_DR;
float tension = (valeur_adc * 3.3f) / 4095.0f;
return tension * 100.0f;
}
/* ==================== PWM ==================== */
void pwm_init(void)
{
RCC_APB2ENR |= (1U << 11);
TIM1_PSC = 7;
TIM1_ARR = 999;
TIM1_CCMR1 = (6U << 4) | (1U << 3);
TIM1_CCER = 1U;
TIM1_BDTR |= (1U << 15);
TIM1_CCR1 = 0;
TIM1_CR1 = (1U << 7) | 1U;
}
void set_pwm(float valeur)
{
if (valeur > 100.0f) valeur = 100.0f;
if (valeur < 0.0f) valeur = 0.0f;
TIM1_CCR1 = (uint32_t)(valeur * 10.0f);
}
/* ==================== UART ==================== */
void pid_uart_init(void)
{
RCC_APB2ENR |= (1U << 14);
USART1_BRR = 0x0045;
USART1_CR1 = (1U << 13) | (1U << 3);
}
void uart_send_char(char c)
{
while (!(USART1_SR & (1U << 7))) {}
USART1_DR = (uint32_t)c;
}
void uart_send_text(const char *texte)
{
while (*texte)
{
uart_send_char(*texte++);
}
}
void uart_send_number(float valeur)
{
int32_t dixiemes = (int32_t)(valeur * 10.0f);
if (dixiemes < 0)
{
uart_send_char('-');
dixiemes = -dixiemes;
}
uart_send_char((char)('0' + (dixiemes / 10) / 100 % 10));
uart_send_char((char)('0' + (dixiemes / 10) / 10 % 10));
uart_send_char((char)('0' + (dixiemes / 10) % 10));
uart_send_char('.');
uart_send_char((char)('0' + dixiemes % 10));
}
/* ==================== PID ==================== */
float pid_calculate(float mesure)
{
erreur = consigne - mesure;
integrale += erreur * DT;
if (integrale > 100.0f) integrale = 100.0f;
if (integrale < -100.0f) integrale = -100.0f;
float derivee = (erreur - erreur_precedente) / DT;
float sortie = Kp * erreur
+ Ki * integrale
+ Kd * derivee;
erreur_precedente = erreur;
if (sortie > 100.0f) sortie = 100.0f;
if (sortie < 0.0f) sortie = 0.0f;
return sortie;
}
/* ==================== MAIN ==================== */
int main(void)
{
gpio_init();
adc_init();
pwm_init();
pid_uart_init();
uart_send_text("\r\n=== SYSTEME PID TEMPERATURE ===\r\n");
while (1)
{
temperature = read_temperature();
commande = pid_calculate(temperature);
set_pwm(commande);
uart_send_text("Temperature: ");
uart_send_number(temperature);
uart_send_text(" C | Consigne: ");
uart_send_number(consigne);
uart_send_text(" C | PWM: ");
uart_send_number(commande);
uart_send_text(" %\r\n");
delay_ms(100);
}
}
Loading
stm32-bluepill
stm32-bluepill