#define ON 0
#define OFF 1
// Lcd pinout settings
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
//variables
unsigned short bin_code,gol_L, gol_V,fal_V,fal_L,periodo_juego;
volatile bit bandera, marcador, resta,programe_mode;
volatile bit seg_si, min_si, gol_l_si, gol_v_si, falta_l_si, falta_v_si;
char temp[2],str_format[2];
bit fin_libre_L,fin_libre_v;
volatile int cont;
volatile short int minuto,minuto_sub,segundo_sub, segundo;
volatile short xx[4] = {0x0, 0x40, 0x10, 0x50};
//Rutina de dos digitos
char *format_num(int valor, char str_format[]) {
str_format[0] = valor / 10 + '0';
str_format[1] = valor % 10 + '0';
return str_format;
//
}
//Rutina de posicionamiento del cursor Lcd 16x4
void Dsp_Txt (int x, int y, char *texto){
int cmd_pos = 0;
cmd_pos = 0x80 +xx[x-1] + y - 1;
Lcd_Cmd (cmd_pos);
Lcd_Out_Cp(texto);
}
int falta_libre(int total_faltas ){
int resto_faltas, entero_faltas;
resto_faltas = total_faltas % 5;
entero_faltas = total_faltas / 5;
if ((entero_faltas>1 && resto_faltas==0))
return 0;
else
return 1;
}
//interrupcion por rb0
void InitTimer1(){
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xD8;
TMR1L = 0xEF;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt(){
if (intf_bit){
bin_code = porta;
marcador = 1;
intf_bit = 0;
if(bin_code==0){
if(bandera==OFF){
gol_L_si = ON;
if(resta==ON){
gol_L--;
resta=OFF;
}
else {
gol_L++;
}
}
resta=OFF;
portd.f5=0;
}
if(bin_code==1){
if(bandera==OFF){
gol_V_si = ON;
if(resta==ON){
gol_V--;
resta=OFF;
}
else {
gol_V++;
}
}
resta=OFF;
portd.f5=0;
}
if(bin_code==2){
bandera = ~bandera;
}
if(bin_code==4){
falta_l_si = ON;
if(resta==ON){
resta=OFF;
fal_L--;
portd.f5=0;
}
else{
fal_L++;
fin_libre_L=0;
}
}
if(bin_code==5){
falta_v_si = ON;
if(resta==ON){
resta=OFF;
fal_V--;
portd.f5=0;
}
else{
fal_V++;
fin_libre_V=0;
}
}
if(bin_code==8){
if(bandera==OFF){
programe_mode=ON;
portd.f3=1;
}
}
if (bin_code == 9){
resta = ON;
portd.f5=1;
}
if (bin_code == 10){
}
}
if (TMR1IF_bit){
TMR1IF_bit = 0;
TMR1H = 0xD8;
TMR1L = 0xEF;
if(bandera==ON){
cont++;
if(cont==100){
segundo--;
segundo_sub++;
seg_si = ON;
cont=0;
if (segundo < 0){
min_si = ON;
minuto--;
segundo = 59;
minuto_sub++;
segundo_sub = 0;
}
}
}
}
}
//comienza programa
void main() {
Trisa = 0b00001111;
Trisb = 0b00000001;
Trisc = 0;
portc = 0;
Trisd = 0;
portd = 0;
Trise = 0;
porte = 0;
porta = 0;
InitTimer1();
INTE_bit = 1;
GIE_bit = 1;
adcon1 = 0b00000111;
cont=0;
periodo_juego = 1;
bandera=OFF;
gol_L=0;
gol_V=0;
segundo=0;
minuto=25;
fal_L = 0;
fal_V = 0;
portd.f2 = 1; //encendido y pic andando
portd.f6 = 1; //comunicacion establecida
programe_mode=ON;
resta = OFF;
fin_libre_L=1;
fin_libre_V=1;
min_si = OFF;
seg_si = OFF;
gol_l_si = OFF;
gol_v_si = OFF;
falta_l_si = OFF;
falta_v_si = OFF;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
delay_ms(50);
Dsp_Txt(1,1,"Tiempo 25:00");
Dsp_Txt(1,11,":");
Dsp_Txt(2,1,"Goles");
Dsp_Txt(2,7,"L:00");
Dsp_Txt(2,12,"V:00");
Dsp_txt(3,1,"Falta");
Dsp_Txt(3,7,"L:00");
Dsp_Txt(3,12,"V:00");
Dsp_Txt(4,1,"Periodo");
Dsp_Txt(4,9,"1");
Dsp_Txt(4,11,"00:00");
marcador = 0;
do{
if (min_si == ON) {
Dsp_Txt(1,9,format_num(minuto,str_format));
Dsp_Txt(4,11,format_num(minuto_sub,str_format));
min_si=OFF;
}
if (seg_si == ON){
Dsp_Txt(1,12,format_num(segundo,str_format));
Dsp_Txt(4,14,format_num(segundo_sub,str_format));
seg_si = OFF;
}
if(gol_l_si == ON){
gol_L_si = OFF;
Dsp_Txt(2,9,format_num(gol_L,str_format));
delay_ms(50);
}
if (gol_v_si == ON){
Dsp_Txt(2,14,format_num(gol_V,str_format));
gol_v_si = OFF;
delay_ms(50);
}
if( falta_l_si == ON){
Dsp_Txt(3,9,format_num(fal_L,str_format));
falta_l_si = OFF;
}
if(falta_v_si == ON){
Dsp_Txt(3,14,format_num(fal_V,str_format));
falta_v_si = OFF;
}
while((bandera==OFF)&&(bin_code==6)&&(portb.f0==1)){
portd.f0 =1;
}
if(fin_libre_L==0 && falta_libre(fal_L)==0){
bandera= OFF;
fin_libre_L=1;
portd.f0=1;
delay_ms(500);
portd.f0=0;
}
if(fin_libre_V==0&&falta_libre(fal_V)==0){
bandera= OFF;
fin_libre_V=1;
portd.f0=1;
delay_ms(500);
portd.f0=0;
}
portd.f0=0;
if(bandera==OFF)portd.f4=1;
else portd.f4=0;
}while(1);
}