/*
Practica 03-Laboratorio Sistemas digitales y microprocesadores
ULISES ROSA-1104781
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
volatile uint8_t display1_data;
volatile uint8_t BUTTON=5;
uint8_t n1 = 0, n2 = 0, n3 = 0, n4 = 0;
uint16_t num1 = 0, num2 = 1, suma = 0;
int estado=0;
int Reset=1, Pausa=0, Sumar=0, Restar=0, Incrementar=0, Setear=0 ;
int numero=0, digito=0;
void Decodificador0(uint8_t num) {
switch (num) {
case 0:
display1_data = 0x01;
break;
case 1:
display1_data = 0x4F;
break;
case 2:
display1_data = 0x12;
break;
case 3:
display1_data = 0x06;
break;
case 4:
display1_data = 0x4C;
break;
case 5:
display1_data = 0x24;
break;
case 6:
display1_data = 0x20;
break;
case 7:
display1_data = 0x0F;
break;
case 8:
display1_data = 0x00;
break;
case 9:
display1_data = 0x0C;
break;
default:
display1_data = 0xFF; // Invalid number, display nothing
break;
}
}
void Multiplexacion_Displays() {
// Display 1
Decodificador0(n4);
PORTB = 0x00;
PORTD = display1_data;
PORTD |= (1 << PORTD7);
PORTB = 0x01;
Delay1ms();
// Display 2
Decodificador0(n3);
PORTB = 0x00;
PORTD = display1_data;
PORTD |= (0 << PORTD7);
PORTB = 0x02;
Delay1ms();
// Display 3
Decodificador0(n2);
PORTB = 0x00;
PORTD = display1_data;
PORTD |= (1 << PORTD7);
PORTB = 0x04;
Delay1ms();
// Display 4
Decodificador0(n1);
PORTB = 0x00;
PORTD = display1_data;
PORTD |= (1 << PORTD7);
PORTB = 0x08;
Delay1ms();
}
void Delay500ms() {
// Initialize Timer1
TCCR1A = 0x00;
TCCR1B = (1 << CS10) | (1 << CS12); // Prescaler = 1024
TCNT1 = 65535 - 7812; // Count for 0.1 second
while ((TIFR1 & (1 << TOV1)) == 0); // Wait for overflow
TIFR1 |= (1 << TOV1); // Clear the overflow flag
TCCR1B = 0x00; // Stop the timer
}
void Delay100ms() {
// Initialize Timer1
TCCR1A = 0x00;
TCCR1B = (1 << CS10) | (1 << CS12); // Prescaler = 1024
TCNT1 = 65535 - 1563; // Count for 2 ms
while ((TIFR1 & (1 << TOV1)) == 0) // Wait for overflow
{
Multiplexacion_Displays();
}
TIFR1 |= (1 << TOV1); // Clear the overflow flag
TCCR1B = 0x00; // Stop the timer
}
void Delay16ms() {
// Initialize Timer0
TCCR0A = 0x00;
TCCR0B = (1 << CS00) | (1 << CS02); // Prescaler = 64
TCNT0 = 256 - 250; // Count for 3 ms
while ((TIFR0 & (1 << TOV0)) == 0); // Wait for overflow
TIFR0 |= (1 << TOV0); // Clear the overflow flag
TCCR0B = 0x00; // Stop the timer
}
void Delay4ms() {
// Initialize Timer0
TCCR0A = 0x00;
TCCR0B = (1 << CS00) | (1 << CS02); // Prescaler = 1024
TCNT0 = 256 - 150; // Count for 4 ms
while ((TIFR0 & (1 << TOV0)) == 0); // Wait for overflow
TIFR0 |= (1 << TOV0); // Clear the overflow flag
TCCR0B = 0x00; // Stop the timer
}
void Delay1ms() {
// Initialize Timer0
TCCR0A = 0x00;
TCCR0B = (1 << CS00) | (1 << CS01); // Prescaler = 64
TCNT0 = 256 - 250; // Count for 4 ms
while ((TIFR0 & (1 << TOV0)) == 0); // Wait for overflow
TIFR0 |= (1 << TOV0); // Clear the overflow flag
TCCR0B = 0x00; // Stop the timer
}
ISR(PCINT1_vect) {
if (PINC & (1 << PC2)) { // Button 2
Incrementar=1;
Reset=0; Pausa=0; Sumar=0; Restar=0;
numero++;
if (numero==10)
{
numero=0;
}
}
if (PINC & (1 << PC1)) { // Button 1
Setear=1;
Reset=0; Pausa=0; Sumar=0; Restar=0; Incrementar=0; numero=0;
if (PINC & (1 << PC1))
{
digito++;
}
if (digito==5)
{
digito=1;
}
}
if (PINC & (1 << PC3))
{ // Button 3
Reset=1;
Pausa=0; Sumar=0; Restar=0; Incrementar=0; Setear=0; digito=0; numero=0;
}
if (PINC & (1 << PC0)) { // Button 0
Reset=0; Incrementar=0; Setear=0; digito=0; numero=0; Restar=0;
if (estado==0)
{
Sumar=1;
n1 = 0, n2 = 0, n3 = 0, n4 = 0;
num1 = 0, num2 = 1, suma = 0;
}
if (estado==1 or estado==3)
{
Pausa++;
}
if (Pausa==2)
{
Pausa=0;
}
if (estado==2 or estado==3)
{
Restar=1;
num1 = 9, num2 = 1, suma = 0;
Pausa=0; Sumar=0; Incrementar=0; Setear=0; digito=0; numero=0;
}
}
}
void setear_numeros()
{
if(digito==1)
{
// Display 3
n4=numero;
Decodificador0(n4);
PORTB = 0x00;
PORTD = display1_data;
PORTD |= (1 << PORTD7);
PORTB = 0x01;
Delay500ms();
PORTB = 0x00;
Delay500ms();
}
if(digito==2)
{
// Display 3
n3=numero;
Decodificador0(n3);
PORTB = 0x00;
PORTD = display1_data;
PORTD &= ~(1 << PORTD7);
PORTB = 0x02;
Delay500ms();
PORTB = 0x00;
Delay500ms();
}
if(digito==3)
{
// Display 2
n2=numero;
Decodificador0(n2);
PORTB = 0x00;
PORTD = display1_data;
PORTD |= (1 << PORTD7);
PORTB = 0x04;
Delay500ms();
PORTB = 0x00;
Delay500ms();
}
if(digito==4)
{
// Display 1
n1=numero;
Decodificador0(n1);
PORTB = 0x00;
PORTD = display1_data;
PORTD |= (1 << PORTD7);
PORTB = 0x08;
Delay500ms();
PORTB = 0x00;
Delay500ms();
}
}
void Suma ()
{
Multiplexacion_Displays();
Delay100ms();
// Ingreso de números (valores de 0 a 9999)
num1 = (n1 * 1000) + (n2 * 100) + (n3 * 10) + n4;
// Realizar la suma (asumiendo que también tienes valores para n1, n2, n3, y n4)
suma = num1 + num2;
// Descomponer la suma en n1, n2, n3 y n4
n1 = suma / 1000;
suma %= 1000;
n2 = suma / 100;
suma %= 100;
n3 = suma / 10;
n4 = suma % 10;
}
void Resta ()
{
Multiplexacion_Displays();
Delay100ms();
// Ingreso de números (valores de 0 a 9999)
num1 = (n1 * 1000) + (n2 * 100) + (n3 * 10) + n4;
// Realizar la suma (asumiendo que también tienes valores para n1, n2, n3, y n4)
suma = num1 - num2;
// Descomponer la suma en n1, n2, n3 y n4
n1 = suma / 1000;
suma %= 1000;
n2 = suma / 100;
suma %= 100;
n3 = suma / 10;
n4 = suma % 10;
}
int main() {
// Setup ports and pins
DDRD = 0xFF; // Port D as output
DDRB = 0xFF; // Port B as output
DDRC = 0x00; // Port C as input
// Enable Pin Change Interrupts on PC0, PC1, PC2, and PC3
PCMSK1 = 0x0F;
PCICR = (1 << PCIE1);
sei(); // Enable global interrupts
while (1) {
if (Sumar==1)
{
estado=1;
Suma();
while (Pausa==1)
{
Multiplexacion_Displays();
}
if (n1==9 and n2==9 and n3==9 and n4==9)
{
Reset=1;
Sumar=0;
estado=0;
}
}
if (Setear==1)
{
estado=2;
setear_numeros();
}
if (Restar==1)
{
estado=3;
Resta();
while (Pausa==1)
{
Multiplexacion_Displays();
}
if (n1==0 and n2==0 and n3==0 and n4==0)
{
Reset=1;
Restar=0;
estado=0;
}
}
if (Reset==1)
{
n1 = 0, n2 = 0, n3 = 0, n4 = 0;
num1 = 0, num2 = 0, suma = 0;
estado=0;
Multiplexacion_Displays();
}
}
return 0;
}