/**
******************************************************************************
* @file : main.c
* @author : Auto-generated by STM32CubeIDE
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include <stdint.h>
#include "stm32f103xb.h"
#if !defined(__SOFT_FP__) && defined(__ARM_FP)
#warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif
void delay_ms(uint16_t t) {
volatile unsigned long l = 0;
for(uint16_t i = 0; i < t; i++) {
for(l = 0; l < 800; l++);
}
}
//----------Ejercicio 3----------
int main(void) {
RCC->APB2ENR |= (1 << 2); // Reloj Puerto A
// PA1 y PA2 como Salidas Push-Pull 2MHz
GPIOA->CRL &= ~(0xFF << 4); // Limpiar bits 4 a 11 (PA1 y PA2)
GPIOA->CRL |= (1 << 5) | (1 << 9); // MODE1=10, MODE2=10 (2MHz)
// PA0 como Entrada Pull-down
GPIOA->CRL &= ~(0xF << 0);
GPIOA->CRL |= (1 << 3);
GPIOA->ODR &= ~(1 << 0);
uint8_t sentido = 0; // 0: Giro A, 1: Giro B
while(1) {
if(GPIOA->IDR & (1 << 0)) { // Botón presionado (High)
// 1. Detener motor (Frenado)
GPIOA->ODR &= ~((1 << 1) | (1 << 2));
delay_ms(2000); // Esperar 2 segundos
// 2. Cambiar sentido
sentido = !sentido;
// Esperar a que suelte el botón (Debounce simple)
while(GPIOA->IDR & (1 << 0));
}
if(sentido == 0) {
GPIOA->ODR |= (1 << 1); // PA1 = 1
GPIOA->ODR &= ~(1 << 2); // PA2 = 0
} else {
GPIOA->ODR &= ~(1 << 1); // PA1 = 0
GPIOA->ODR |= (1 << 2); // PA2 = 1
}
}
}