Está en la página 1de 13

/*

* GccApplication2.c
*
* Created: 20/03/2019 11:07:21 a. m.
* Author : Julian Guerrero y Alejandro Ramirez
*/
//----------------Defino librerias y frecuencia------------------------
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 1000000UL //Defino frecuencia CPU

int seg = 0;
int tx = 0;
int adc = 0;
int est = 0;
int main(void)
{
// Declaro puertos de entrada o salida, son 8 bits, los 0 son entradas los 1 son
salida
DDRC = 0b01111101; //Com digitos 1, 2, 3 y ADC read
DDRD = 0b11111111; //7 segmentos
DDRB = 0b01111101; //Led
PORTC = 0b00000000;
PORTD = 0b00000000;
PORTB = 0b00000000;

//-----------------------USART - DEFINICION-------------------------//

UCSR0A = 0b00000000; //RX 1 cuando se completa la recepcion de un dato, TX 1


cuando se completa la trasmicion de un dato,
//UDRE0 1 El registro UDR0 está vacío, FEO 1 error de recepcion, DORn 1 error
de sobreescritura,
//UPEn 1 error de paridad, U2Xn 0 velocidad normal , MPCMn 0 se usa para modo
sincrono
UCSR0B = 0b00000000; //RX 1 interrupcion por recepcion, TX 1 interrupcion por
trasmicion, UDRIEn 1 interrupcion por q UDRO esta vacio
//RXENn 1 habilita RXD, TXENn 1 habilita TXD, UCSZn2 Numero de bits a
trasmitir,
//RXB8n 1 comunicacion de 9 bits, TXB8n 1 comunicacion de 9 bits
UCSR0C = 0b00000110; //UMSELn1, UMSELn0 Escojo el modo de trasmicion 00
asincrono, UPMn1, UPMn0 Escojo si uso y como el bit de paridad
//USBSn 0 1 bits de parada 1 2 bits de parada, UCSZn0, UCSZn1 Numero de bits
a trasmitir 011 8 bits, UCPOLn 0 en el modo sincrono
UBRR0 = 6; //BAUD=9600, UBRR0=6 Ecuación, 9600 por que se aproxima
a 10k.

//-----------------------Timer - Prescaler-------------------------//

TCCR0A = 0b01000010;
TCCR0A = 0b01000010;
TCCR0B = (1 << CS02)|(1 << CS00); // Prescalizador en 1024
TIMSK0 = 0b00000010;
TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20); //Prescalizador en 1024
TCNT2 = 252; //cuenta a 3 ms PD0 A PB6
OCR0A = 3;
sei();
//-----------------------ADC - DEFINICION-------------------------//

ADMUX = 0b11000001; //(1<<REFS0)|(1<<REFS1)|(1<<MUX0)


ADCSRA = 0b00000100; //(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
//Declaracion e inicializacion de variables auxiliares

//-------------------- VARIBLES GLOBALES -------------------------//

int estado = 0;
unsigned int temp = 0;
uint16_t cont = 0;
int u = 0;
int d = 0;
int c = 0;
int aux = 1; //Auxiliar para elegir cual de los 3 digitos se prende
int aux2 = 0; //Auxilar para contar 1 seg
int aux3 = 0; //Auxiliar para encender y apagar led
int bandera = 0;
int bandera2 = 0;
int contADC = 0;
int cont7SEG = 0;
int contled = 0;
//int aux2 = 0;
int Enviar = 0;
int Recivir = 0;
//int ReinicioTimer = 254;
//int LED = 0;
//int LED1 = 0;
char Convercion = 0;
//UDR0 = 0b01000101;

while (1)
{
/*while(TCNT0 != 255); //5ms
contADC++;
cont7SEG++;
contled++;
Enviar ++;
Recivir ++;
TCNT0 = 252;*/

switch (est)
{

case 0:
////////////////////////////////////////////////////////////////////////////////////
///////////////////////7
//if (Enviar >= 10)
//{
aux2++;
if (aux2 == 1)
{
if (u == 1)
{
Convercion = 0b11100001;
}
if (u == 2)
{
Convercion = 0b11100010;
}
if (u == 3)
{
Convercion = 0b11100011;
}
if (u == 4)
{
Convercion = 0b11100100;
}
if (u == 5)
{
Convercion = 0b11100101;
}
if (u == 6)
{
Convercion = 0b11100110;
}
if (u == 7)
{
Convercion = 0b11100111;
}
if (u == 8)
{
Convercion = 0b11101000;
}
if (u == 9)
{
Convercion = 0b11101001;
}
if (u == 0)
{
Convercion = 0b11100000;
}
UDR0 = Convercion;// 0b11100000;// //Envia
el codigo UDR0
UCSR0B = 0b00001000;// Activo trans
while(!(UCSR0A&(1<<5))); //Mientras el registro
esta lleno, espera
//UCSR0B = 0b00001000;
//while(!(UCSR0A&(1<<5))); //Mientras el
registro esta lleno, espera
//UDR0 = 0b11110000;
//UDR0 = 0b11110000;
//UCSR0A = 0b00000000;// Apago trans
//UCSR0B = 0b00000000;
Enviar = 0;
aux2++;
}
if (aux2 == 2)
{
if (d == 1)
{
Convercion = 0b10100001;
}
if (d == 2)
{
Convercion = 0b10100010;
}
if (d == 3)
{
Convercion = 0b10100011;
}
if (d == 4)
{
Convercion = 0b10100100;
}
if (d == 5)
{
Convercion = 0b10100101;
}
if (d == 6)
{
Convercion = 0b10100110;
}
if (d == 7)
{
Convercion = 0b10100111;
}
if (d == 8)
{
Convercion = 0b10101000;
}
if (d == 9)
{
Convercion = 0b10101001;
}
if (d == 0)
{
Convercion = 0b10100000;
}
UDR0 = Convercion; //0b10100000; //Envia el
codigo UDR0
//UCSR0B = 0b00001000;// Activo trans
//PORTB = 0b00000100;
//while(!(UCSR0A&(1<<5))); //Mientras el
registro esta lleno, espera
//UCSR0B = 0b00001000;
while(!(UCSR0A&(1<<5))); //Mientras el registro
esta lleno, espera
//UDR0 = 0b11110000;
//UCSR0A = 0b00000000;// Apago trans
//UCSR0B = 0b00000000;
Enviar = 0;
aux2++;
}
if (aux2 == 3)
{
if (c == 1)
{
Convercion = 0b10100001;
}
if (c == 2)
{
Convercion = 0b10100010;
}
if (c == 3)
{
Convercion = 0b10100011;
}
if (c == 4)
{
Convercion = 0b10100100;
}
if (c == 5)
{
Convercion = 0b10100101;
}
if (c == 6)
{
Convercion = 0b10100110;
}
if (c == 7)
{
Convercion = 0b10100111;
}
if (c == 8)
{
Convercion = 0b10101000;
}
if (c == 9)
{
Convercion = 0b10101001;
}
if (c == 0)
{
Convercion = 0b10100000;
}
//UDR0 = 0b10010000;//Convercion;
//Envia el codigo UDR0
//UCSR0B = 0b00001000;// Activo trans
//PORTB = 0b00000100;
//while(!(UCSR0A&(1<<5))); //Mientras el
registro esta lleno, espera
//UCSR0B = 0b00001000;
//while(!(UCSR0A&(1<<5))); //Mientras el registro
esta lleno, espera
UDR0 = Convercion;//0b10100000;
Enviar = 0;
aux2 = 0;
//UCSR0A = 0b00000000;// Apago trans
UCSR0B = 0b00000000;
//UCSR0B = 0b00000000;
}
//}
//UCSR0B = 0b00000000;
////////////////////////////////////////////////////////////////////////////////////
///////////////////////

//estado = 1;
//cont7SEG = 0;
break;

case 1: //Manejo digito 1

//if (cont7SEG == 2 && aux == 1) {//2 ms


//PORTC = 0b00000001;
PORTC = 0b00000000;
PORTD = 0b00000000;
//PORTB = 0b00000000;
if (u == 1)
{
//Convercion = 0b00010101;
PORTC = 0b00001000; //habilito
PORTD = 0b00010000; //NEW
PORTC = 0b00001001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//2 ya
if (u == 2)
{
//Convercion = 0b00100101;
PORTC = 0b00001000; //habilito
PORTD = 0b10101100; //NEW
PORTC = 0b00001001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//3 ya
if (u == 3)
{
//Convercion = 0b00110101;
PORTC = 0b00001000; //habilito
PORTD = 0b00111100; //NEW
PORTC = 0b00001001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//4 ya
if (u == 4)
{
//Convercion = 0b01000101;
PORTC = 0b00001000; //habilito
PORTD = 0b00011000; //NEW
PORTC = 0b00001101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//5 ya
if (u == 5)
{
//Convercion = 0b01010101;
PORTC = 0b00001000; //habilito
PORTD = 0b00111100; //NEW
PORTC = 0b00001100;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//6 ya
if (u == 6)
{
//Convercion = 0b01100101;
PORTC = 0b00001000; //habilito
PORTD = 0b10111110; //NEW
PORTC = 0b00001100;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//7 ya
if (u == 7)
{
//Convercion = 0b01110101;
PORTC = 0b00001000; //habilito
PORTD = 0b00010100; //NEW
PORTC = 0b00001001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//8 ya
if (u == 8)
{
//Convercion = 0b10000101;
PORTC = 0b00001000; //habilito
PORTD = 0b10111100; //NEW
PORTC = 0b00001101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//9 ya
if (u == 9)
{
//Convercion = 0b10010101;
PORTC = 0b00001000; //habilito
PORTD = 0b00011100; //NEW
PORTC = 0b00001101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//0 ya
if (u == 0)
{
//Convercion = 0b00000101;
PORTC = 0b00001000; //habilito
PORTD = 0b10110100; //NEW
PORTC = 0b00001101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
aux++;
cont7SEG = 0;
//}
/*if (cont7SEG == 0)
{
estado = 2;
}*/
break;
//estado = 2;
//break;

case 2: //Manejo digito 2

//if (cont7SEG == 2 && aux == 2) {//2 ms


PORTC = 0b00000000;
PORTD = 0b00000000;
//PORTB = 0b00000000;
if (d == 1)
{
//Convercion = 0b00010101;
PORTC = 0b00010000; //habilito
PORTD = 0b01010001; //NEW
PORTC = 0b00010001;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//2 ya
if (d == 2)
{
//Convercion = 0b00100101;
PORTC = 0b00010000; //habilito
PORTD = 0b11101100; //NEW
PORTC = 0b00010001;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//3 ya
if (d == 3)
{
//Convercion = 0b00110101;
PORTC = 0b00010000; //habilito
PORTD = 0b01111100; //NEW
PORTC = 0b00010001;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//4 ya
if (d == 4)
{
//Convercion = 0b01000101;
PORTC = 0b00010000; //habilito
PORTD = 0b01011000; //NEW
PORTC = 0b00010101;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//5 ya
if (d == 5)
{
//Convercion = 0b01010101;
PORTC = 0b00010000; //habilito
PORTD = 0b01111100; //NEW
PORTC = 0b00010100;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//6 ya
if (d == 6)
{
//Convercion = 0b01100101;
PORTC = 0b00010000; //habilito
PORTD = 0b11111100; //NEW
PORTC = 0b00010100;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//7 ya
if (d == 7)
{
//Convercion = 0b01110101;
PORTC = 0b00010000; //habilito
PORTD = 0b01010100; //NEW
PORTC = 0b00010001;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//8 ya
if (d == 8)
{
//Convercion = 0b10000101;
PORTC = 0b00010000; //habilito
PORTD = 0b11111100; //NEW
PORTC = 0b00010101;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//9 ya
if (d == 9)
{
//Convercion = 0b10010101;
PORTC = 0b00010000; //habilito
PORTD = 0b01011100; //NEW
PORTC = 0b00010101;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
//0 ya
if (d == 0)
{
//Convercion = 0b00000101;
PORTC = 0b00010000; //habilito
PORTD = 0b11110100; //NEW
PORTC = 0b00010101;
//PORTB = 0b01000000;
//PORTD = 0b00000000; //apago
//PORTC = 0b00000000; //desabilito
}
aux++;
cont7SEG = 0;
/*}
if (cont7SEG == 0)
{
estado = 3;
}*/
break;

case 3: //Manejo digito 3

//if (cont7SEG == 2 && aux == 3) { //2 ms


PORTC = 0b00000000;
PORTD = 0b00000000;
//PORTB = 0b00000000;
if (c == 1)
{
//Convercion = 0b00010101;
PORTC = 0b00100000; //habilito
PORTD = 0b00010000; //NEW
PORTC = 0b00100001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//2 ya
if (c == 2)
{
//Convercion = 0b00100101;
PORTC = 0b00100000; //habilito
PORTD = 0b10101100; //NEW
PORTC = 0b00100001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//3 ya
if (c == 3)
{
//Convercion = 0b00110101;
PORTC = 0b00100000; //habilito
PORTD = 0b00111100; //NEW
PORTC = 0b00100001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//4 ya
if (c == 4)
{
//Convercion = 0b01000101;
PORTC = 0b00100000; //habilito
PORTD = 0b00011000; //NEW
PORTC = 0b00100101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//5 ya
if (c == 5)
{
//Convercion = 0b01010101;
PORTC = 0b00100000; //habilito
PORTD = 0b00111100; //NEW
PORTC = 0b00100100;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//6 ya
if (c == 6)
{
//Convercion = 0b01100101;
PORTC = 0b00100000; //habilito
PORTD = 0b10111100; //NEW
PORTC = 0b00100100;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//7 ya
if (c == 7)
{
//Convercion = 0b01110101;
PORTC = 0b00100000; //habilito
PORTD = 0b00010100; //NEW
PORTC = 0b00100001;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//8 ya
if (c == 8)
{
//Convercion = 0b10000101;
PORTC = 0b00100000; //habilito
PORTD = 0b10111100; //NEW
PORTC = 0b00100101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//9 ya
if (c == 9)
{
//Convercion = 0b10010101;
PORTC = 0b00100000; //habilito
PORTD = 0b00011100; //NEW
PORTC = 0b00100101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
//0 ya
if (c == 0)
{
//Convercion = 0b00000101;
PORTC = 0b00100000; //habilito
PORTD = 0b10110100; //NEW
PORTC = 0b00100101;
//PORTB = 0b01000000;
//PORTB = 0b00000000; //apago
//PORTD = 0b00000000; //desabilito
}
aux = 1;
cont7SEG = 0;

/*}
if (cont7SEG == 0)
{
estado = 4;

}*/
break;
//estado = 0;
//break;

case 4:

//if(contADC >= 80){


ADCSRA = 0b11000111; //Activo e inicio el ADC
while(ADCSRA & (1<<ADSC)); //espero a que termine la
conversion
ADCSRA &= 0X7F;//Desbilito ADC
ADCSRA &=~ (1 << ADEN); //reinicio ADC
temp = ADC;
cont = temp*1.07422; //Leo y convierto de bin a grados
u = ((cont%100)%10);
d = (cont%100)/10;
c = (cont/100);
contADC = 0;
//}
/*estado = 0;
cont7SEG--;*/
break;

default:

break;
}
}
}
ISR(TIMER0_COMPA_vect){
seg++;
tx++;
adc++;
if (seg == 2)
{
est = 1;
}
if (seg == 3)
{
est = 2;
}
if (seg == 4)
{
est = 3;
seg=0;
}
if (tx == 10)
{
est = 0;
tx = 0;
}
if (adc == 80)
{
est = 4;
adc = 0;
seg--;
}
}

También podría gustarte