#include "i2c_config.h"
#include "lcdconfig.h"
#include "config.h"
#include "clock.h"
#include "memoria.h"
#include "SPI_function.h"
#define on_delta portb.f6
#define off_delta portb.f7
#define type_dat portc.f0
#define enter portc.f5
//----------------------------------------------//
// Configuro nombres de puerto I2C //
//----------------------------------------------//
// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections
/////////////////////////////////////////////////
void spiMotor(char sendRpm);
unsigned int spi16receive(void);
void main (void);
void visual_menu(char menu,char x_lcd,char y_lcd);
void rutina_1(void);
void rutina_2(void);
void rutina_3(void);
void rutina_4(void);
void interrupt(void);
char onOFF=1;
char rpm=0;
char txt1[9];
char menu=1;
char valida=0;
char clock_max=0;
char x_lcd=1, y_lcd=1;
int prueba=0,temperatura=0;
char contador=0;
char seg_crono, min_crono, hours_crono;
char seg_set, min_set, hours_set;
char setDia, setMes, setAnio;
unsigned char Vtmr0 = 61; // value star TMR0
//Interrpt rutine
void interrupt(void)
{ INTCON.GIE=0;
if (INTCON.T0IF)
{
if (contador==101){contador=0;seg_crono++;}
if (seg_crono==60){seg_crono=0;min_crono++;}
if (min_crono==60){min_crono=0;hours_crono++;}
else{TMR0 = Vtmr0;contador++;}
INTCON.T0IF = 0;//TMR0 register not overflow
INTCON.GIE=1; //Enable global interrupts
}
}
void main (void){
config();
// Initialize Soft I2C communication
Soft_I2C_Init();
SOFT_I2C_Start(); // Issue start signal
SOFT_I2C_Write(0xD0); // Address DS1307
SOFT_I2C_Write(7);
SOFT_I2C_Write(0x10);
SOFT_I2C_Stop(); // Issue repeated start signal
INTCON.GIE=0;
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time();
delay_ms(1000);
visual_menu(menu,1,1);
while(1){
if(porta.f4==0){
delay_ms(500);
menu++;
switch(menu){
case 1: x_lcd=1,y_lcd=1;break;
case 2: x_lcd=2,y_lcd=1;break;
case 3: x_lcd=1,y_lcd=1;break;
case 4: x_lcd=2,y_lcd=1;break;
}
if(menu>4)menu=4;
visual_menu(menu,x_lcd,y_lcd);
}
if(porta.f5==0){
delay_ms(500);
menu--;
switch(menu){
case 1: x_lcd=1,y_lcd=1;break;
case 2: x_lcd=2,y_lcd=1;break;
case 3: x_lcd=1,y_lcd=1;break;
case 4: x_lcd=2,y_lcd=1;break;
}
if(menu<1)menu=1;
visual_menu(menu,x_lcd,y_lcd);
}
if(enter==0)switch(menu){
//////////////////////////////Inicio programa
case 1:
Lcd_Cmd(_LCD_CLEAR);
rutina_1();
break;
case 2:
rutina_2();
break;
case 3:
rutina_3();
break;
case 4:
rutina_4();
break;
}
}
}
void rutina_1(void){
Lcd_Out(1,1,"Inicio");
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
on_delta=1;
valida=1;
delay_ms(50);
on_delta=0;
Write_Mem_clock(0);
contador=0;
seg_crono=0;
seg_set=10;
min_crono=0;
hours_crono=0;
TMR0 =0;
//INTCON.GIE=1;
while(valida==1){
if((seg_crono==seg_set)&&(min_crono==min_set)&&(hours_crono==hours_set))
{valida=0;}
if(enter==0)valida=0;
Lcd_Out(1,1,"SetTime:");
Lcd_Chr(1,11,':');
Lcd_Chr(1,14,':');
Lcd_Chr(1,9, (hours_set/10) + 48);
Lcd_Chr(1,10,(hours_set%10) + 48);
Lcd_Chr(1,12, (min_set / 10) + 48);
Lcd_Chr(1,13,(min_set % 10) + 48);
Lcd_Chr(1,15,(seg_set / 10) + 48);
Lcd_Chr(1,16,(seg_set % 10) + 48);
Lcd_Out(2,4,"Time:");
Lcd_Chr(2,11,':');
Lcd_Chr(2,14,':');
Lcd_Chr(2,9, (hours_crono/10) + 48);
Lcd_Chr(2,10, (hours_crono%10) + 48);
Lcd_Chr(2,12, (min_crono / 10) + 48);
Lcd_Chr(2,13,(min_crono % 10) + 48);
Lcd_Chr(2,15,(seg_crono / 10) + 48);
Lcd_Chr(2,16,(seg_crono % 10) + 48);
spiMotor(rpm);// verificar informacion+59-/-*/-*/-*/*-/-*7894+87484613135135 ojo
rpm=SPI_read; //lectura de rpm
delay_us(10);
temperatura=spi16receive();
delay_ms(1000);
Inttostr(temperatura,txt1);
Lcd_Out(1,1,txt1);
}
INTCON.GIE=0;
contador=0;
seg_crono=0;
min_crono=0;
hours_crono=0;
off_delta=1;
delay_ms(500);
off_delta=0;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Proceso");
Lcd_Out(2,1,"Terminado");
delay_ms(1000);
visual_menu(1,1,1);
menu=1;
return;
}
void rutina_2(void){
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,4,"SET RPM:");
valida=1;
rpm=0;
while(valida==1){
if((porta.f4==0)&&(rpm<=255))rpm++,delay_ms(100);//incremento de las rpm
if((porta.f5==0)&&(rpm>0))rpm--,delay_ms(100);//decremento de las rpm
if(enter==0)valida=0;
bytetostr(rpm,txt1);
Lcd_Out(2,1,txt1);
spiMotor(rpm);
bytetostr(SPI_read,txt1); /// quitar
Lcd_Out(2,7,txt1); ////quitar
}
//spiMotor(rpm); //linea que va al enviar las rpm
visual_menu(1,1,1);
menu=1;
delay_ms(500);
return;
}
void rutina_3(void){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
delay_ms(100);
Lcd_Out(2,1,"Tiempo proceso");
Lcd_Out(1,1,"SET: hh:mm:ss");
delay_ms(500);
seg_crono=0;
min_crono=0;
hours_crono=0;
rpm=0;
valida=1;
while(valida>=1){
if((porta.f4==0)&&(rpm<clock_max))rpm++,delay_ms(200);//incremento
if((porta.f5==0)&&(rpm>0))rpm--,delay_ms(200);//decremento
if(enter==0){valida++;rpm=0;delay_ms(500);}
switch(valida){
case 1:
hours_set=rpm;
Lcd_Chr(1,6,(hours_set / 10) + 48);
Lcd_Chr(1,7,(hours_set % 10) + 48);
clock_max=12;
break;
case 2:
min_set=rpm;
Lcd_Chr(1,9,(min_set / 10) + 48);
Lcd_Chr(1,10,(min_set % 10) + 48);
clock_max=60;
break;
case 3:
seg_set=rpm;
Lcd_Chr(1,12,(seg_set / 10) + 48);
Lcd_Chr(1,13,(seg_set % 10) + 48);
clock_max=60;
break;
case 4:
valida=0;
break;
}
}
delay_ms(500);
visual_menu(1,1,1);
menu=1;
return;
}
void rutina_4(void){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
delay_ms(1000);
Lcd_Out(1,1,"SET: hh:mm:ss");
Lcd_Out(2,1,"SET: DD:MM:YYYY");
delay_ms(500);
seg_crono=0;
min_crono=0;
hours_crono=0;
setDia=0;
setMes=0;
setAnio=0;
valida=1;
while(valida>=1){
if((porta.f4==0)&&(rpm<clock_max))rpm++,delay_ms(200);//incremento
if((porta.f5==0)&&(rpm>0))rpm--,delay_ms(200);//decremento
if(enter==0){valida++;rpm=0;delay_ms(500);}
switch(valida){
case 1:
hours_crono=rpm;
Lcd_Chr(1,6,(hours_crono/10) + 48);
Lcd_Chr(1,7,(hours_crono%10) + 48);
clock_max=12;
break;
case 2:
min_crono=rpm;
Lcd_Chr(1,9,(min_crono/10) + 48);
Lcd_Chr(1,10,(min_crono%10) + 48);
clock_max=60;
break;
case 3:
seg_crono=rpm;
Lcd_Chr(1,12,(seg_crono / 10) + 48);
Lcd_Chr(1,13,(seg_crono % 10) + 48);
clock_max=60;
break;
case 4:
setDia=rpm;
Lcd_Chr(2,6,(setDia/10) + 48);
Lcd_Chr(2,7,(setDia%10) + 48);
clock_max=31;
break;
case 5:
setMes=rpm;
Lcd_Chr(2,9, (setMes / 10) + 48);
Lcd_Chr(2,10,(setMes % 10) + 48);
clock_max=12;
break;
case 6:
setAnio=rpm;
Lcd_Chr(2,12,'2');
Lcd_Chr(2,13,'0');
Lcd_Chr(2,14,(setAnio / 10) + 48);
Lcd_Chr(2,15,(setAnio % 10) + 48);
clock_max=40;
break;
case 7:
valida=0;
Set_Time(seg_crono,min_crono,hours_crono,setDia,setMes,setAnio); //ss,mm,hh,day,month,year
break;
}
}
delay_ms(500);
visual_menu(1,1,1);
menu=1;
return;
}
void visual_menu(char menu,char x_lcd,char y_lcd){
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
delay_ms(10);
if((menu==1)||(menu==2)){
Lcd_Out(1,1,"1.Inicio");
delay_ms(10);
Lcd_Out(2,1,"2.Set RPM");
}
if((menu==3)||(menu==4)){
Lcd_Out(1,1,"3.Set Tiempo");
delay_ms(10);
Lcd_Out(2,1,"4.Igualar Hora");
}
Lcd_Out(x_lcd,y_lcd,"");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
return;
}