I am working on a crosswalk project using an ATTiny4313 and the clock is set in the simulator as 8Mhz. The rest of the code works fine, I have made multiple tests and I am pretty sure it works. I have to generate delays using interruptions, in this case, I am generating the interruption by comparison between the Timer 1 and the OCR1A register. As you can see in the code, I set that register as 255, so according to the datasheet, by this equation and using 1024 as the prescaler:
The f_{OC} will be 15.26 Hz, which would be 65.5 ms of period T, which means that every 65.5 ms I am generating an interruption, from what I can understand. My subroutine is simply increasing an int variable called segs, so the delay function I made is pretty simple. What I want my code to do, to test the correct functioning of this delay is to blink an LED and leave it ON for whatever the argument in seconds I pass to it, so that if call it like delay(2); it should do nothing for that amount of time but turns out my LED stays ON forever, I have tried everything but nothing is working, maybe I could get some help in here.
This is the hardware:
And this is the code in C:
The f_{OC} will be 15.26 Hz, which would be 65.5 ms of period T, which means that every 65.5 ms I am generating an interruption, from what I can understand. My subroutine is simply increasing an int variable called segs, so the delay function I made is pretty simple. What I want my code to do, to test the correct functioning of this delay is to blink an LED and leave it ON for whatever the argument in seconds I pass to it, so that if call it like delay(2); it should do nothing for that amount of time but turns out my LED stays ON forever, I have tried everything but nothing is working, maybe I could get some help in here.
This is the hardware:
And this is the code in C:
C:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
// Pines del semáforo peatonal 1
#define RED_PEATON1 PB7
#define GREEN_PEATON1 PB6
// Pines del semáforo peatonal 2
#define RED_PEATON2 PB5
#define GREEN_PEATON2 PB4
// Pines del semáforo vehicular
#define RED_VEHICULAR PB3
#define GREEN_VEHICULAR PB2
// Definición de los estados para la FSM
#define esperando_boton 0
#define blink_vehicular 1
#define detener_trafico 2
#define blink_peatonal 3
// Variables globales del programa
int boton;
int segs = 0;
int un_seg = 0; // Un acumulador
int tres_segs = 0;
int diez_segs = 0;
// Preambulo de funciones
void boton_init();
void timer_init();
void cruzando();
void detenido();
void delay(float seconds);
// Interrupciones
ISR(INT0_vect){ // Subrutina que alterna B1 entre 1 y 0
boton = !boton;
}
ISR(TIMER1_COMPA_vect){
segs++;
}
// Inicio de la función main
int main(void)
{
boton_init();
sei();
timer_init();
DDRB |= (1 << RED_PEATON1) | (1 << GREEN_PEATON1) | (1 << RED_PEATON2) | (1 << GREEN_PEATON2); // Pines de semaforos peatonales como salidas
DDRB |= (1 << RED_VEHICULAR) | (1 << GREEN_VEHICULAR); // Pines del semáforo vehicular como salidas
boton = 0; // Inicializo la variable B1 en 0
while (1)
{
if (boton == 1)
{
for (int i = 0; i < 10; i++)
{
PORTB ^= (1 << RED_PEATON1);
delay(2);
}
boton = 0;
} else
{
PORTB &= ~(1 << RED_PEATON1);
}
}
}
// Final de la función main
// Funciones del programa
void boton_init() { // Se establece para detectar flancos positivos en el pin PD2
GIMSK |= (1 << INT0); // Habilitar la INT0 (interrupción externa)
MCUCR |= (1 << ISC01); // Configurar como flanco descendente
}
void timer_init(){
TCCR1B |= (1 << WGM12) | (1 << CS12) | (1 << CS10); // Esto me activa el prescaler de 1024 y el modo CTC
TIMSK |= (1 << OCIE1A);
OCR1A = 18; // Esto va a hacer que se genere una interrupción cada ~10ms
}
void cruzando(){
PORTB |= (1 << GREEN_PEATON1)| (1 << GREEN_PEATON2) | (1 << GREEN_VEHICULAR);
PORTB &= ~(1 << RED_PEATON1) & ~(1 << RED_PEATON2) & ~(1 << RED_VEHICULAR);
}
void detenido(){
PORTB &= ~(1 << GREEN_PEATON1) & ~(1 << GREEN_PEATON2) & ~(1 << GREEN_VEHICULAR);
PORTB |= (1 << RED_PEATON1) | (1 << RED_PEATON2) | (1 << RED_VEHICULAR);
}
void delay(float seconds) {
TCNT1 = 0x00; // Reseteo del timer 1
segs = 0; // Reseteo a la variable segs
while (segs < 15*seconds) {
// No haga nada
}
}

