Code: Select all
char *texto1 = "Distanci1";
char *texto2 = "Distanci2";
char txt1[6];
int value_channel_1, value_channel_2, contador,teste;
int num;
char filename[14] = "TESTE00xTXT"; // File names
char a_la_ligne[]={0x0D,0x0A};
char tab[]={0x09};
unsigned char sec, mnt, hr, day, mn, year , year_eeprom;
unsigned char sec_bcd , mnt_bcd , hr_bcd , day_bcd ,mn_bcd , year_bcd , year_eeprom_bcd ,year_eeprom_bits ;
char *txt, tnum[4];
short flag_display_time;
long loop =0;
int i;
void dormir ()
{
WDTCON.SWDTEN=1; //Liga o WDT
for (i = 0; i < 110; i++){
delay_ms(250);
PORTD.F0=1;
delay_ms(250);
PORTD.F0=0;
delay_ms(250);
asm{clrwdt}; // se usar capslock da erro na compilação é aconselhavel o CLRWDT antes da instrucção SLEEP
//o CLRWDT poe o WDT a zeros
asm{sleep}; //ENTRA E M SLEEP ainda nao foi implementado na parte sem LCD
// //Tenho de dizer que é um comando de assembly. Daí o asm{}
}
WDTCON.SWDTEN=0; //Desliga o WDT senao ddepois de acordar faz resete
}
void display_lcd_Bcd (unsigned char bb)
{ Lcd_Chr_Cp(((bb>>4)& 0x0F)+48);
Lcd_Chr_Cp((bb&0x0F)+48);
}
char *R_Trim(char *str1){
while (*str1 == ' ')
str1++;
return str1;
}//~
void Zero_Fill(char *value) { // fill text repesentation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}//~
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time(char *sec, char *mnt, char *hr, char *day, char *mn, char *year) {
// Soft_I2c_Config(&PORTB,2,1);
Soft_I2C_Start();
Soft_I2C_Write(0xA0);
Soft_I2C_Write(2);
Soft_I2C_Start();
Soft_I2C_Write(0xA1);
*sec = Soft_I2C_Read(1);
*mnt = Soft_I2C_Read(1);
*hr = Soft_I2C_Read(1);
*day = Soft_I2C_Read(1);
*mn = Soft_I2C_Read(0);
Soft_I2C_Stop();
}//~
//-------------------- Formats date and time
void Transform_Time(char *sec, char *mnt, char *hr, char *day, char *mn, char *year) {
*sec = ((*sec & 0xF0) >> 4)*10 + (*sec & 0x0F);
*mnt = ((*mnt & 0xF0) >> 4)*10 + (*mnt & 0x0F);
*hr = ((*hr & 0xF0) >> 4)*10 + (*hr & 0x0F);
*year = (*day & 0xC0) >> 6;
*day = ((*day & 0x30) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
}//~
//-------------------- Output values to LCD
void Display_Time(char sec, char mnt, char hr, char day, char mn, char year) {
char *tc;
ByteToStr(day, tnum);
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,7, tc);
ByteToStr(mn, tnum);
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,10, tc);
year_eeprom=Eeprom_read(0);
year_eeprom_bits=year_eeprom & 3 ;
if (year != year_eeprom_bits){year_eeprom++ ; Eeprom_write(0,year_eeprom);}
year_eeprom_bcd=Dec2Bcd(year_eeprom);
Lcd_Cmd(128+14);
display_lcd_bcd(year_eeprom_bcd);
ByteToStr(hr,tnum);
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(2,7,tc);
ByteToStr(mnt,tnum);
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(2,10,tc);
ByteToStr(sec,tnum);
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(2,13,tc);
Lcd_Chr(2,15,' ');
flag_display_time=1;
}//~
void convers_char_to_write_to_mmc(char m)
{ ByteToStr(m,tnum);
tnum[0]=tnum[1];
tnum[1]=tnum[2];
if (tnum[0]==' ') {tnum[0]='0';}
if (tnum[1]==' ') {tnum[1]='0';}
Mmc_Fat_Write(tnum,2) ;
}
void write_time_and_day_on_mmc_file()
{ Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
convers_char_to_write_to_mmc(hr);
Mmc_Fat_Write(":",1);
convers_char_to_write_to_mmc(mnt);
Mmc_Fat_Write(":",1);
convers_char_to_write_to_mmc(sec);
Mmc_Fat_Write(" ",1);
Mmc_Fat_Write(a_la_ligne,2);
}
//-------------------- display time/ data to Lcd ---------------------------------------
void see_time()
{
txt = "DATE: ";
LCD_Out(1,1,txt);
Lcd_Chr(1,9,':');
Lcd_Chr(1,12,':');
txt = "TIME: ";
LCD_Out(2,1,txt);
Lcd_Chr(2,9,':');
Lcd_Chr(2,12,':');
Lcd_Chr(2,15,' ');
txt = "20";
LCD_Out(1,13,txt);
Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
Display_Time(sec, mnt, hr, day, mn, year);
}
void see_time2()
{
Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
}
// WRITE ADC VALUE TO THE MMC/SD
void write_adc_value_on_mmc(int m) //m = valeur de l'echantillon
{ intToStr(m, txt1);
//Mmc_Fat_Write(" ",2);
Mmc_Fat_Write(&txt1[0],1);
Mmc_Fat_Write(&txt1[1],1);
Mmc_Fat_Write(&txt1[2],1);
Mmc_Fat_Write(&txt1[3],1);
Mmc_Fat_Write(&txt1[4],1);
Mmc_Fat_Write(&txt1[5],1);
Mmc_Fat_Write(tab,1); //To have a TAB, i.e., to make a new column
// Mmc_Fat_Write(a_la_ligne,2); //To change the line
}
void select_file()
{
Lcd_Out(2,1," NEW FILE... ");
loop=1001;
while (loop<1000000)
{LongToStr(loop, filename);
filename[8]='T';filename[9]='X';filename[10]='T';
if (!Mmc_Fat_Assign(filename, 1)) {
Lcd_Chr(2,1,filename[0]);
Lcd_Chr_CP(filename[1]);
Lcd_Chr_CP(filename[2]);
Lcd_Chr_CP(filename[3]);
Lcd_Chr_CP(filename[4]);
Lcd_Chr_CP(filename[5]);
Lcd_Chr_CP(filename[6]);
Lcd_Chr_CP(filename[7]);
Lcd_Out_CP(".TXT");
Mmc_Fat_Assign(&filename, 0xA0);
mmc_Fat_Rewrite();
Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
year=Eeprom_read(0)+2000;
mmc_Fat_Set_File_Date( year,mn,day,hr,mnt,sec);
Mmc_Fat_Write(a_la_ligne,2);
loop=1000000;
}
loop=loop+1001;
}
}
void select_file2()
{
// Lcd_Out(2,1," NEW FILE... ");
loop=1001;
while (loop<1000000)
{LongToStr(loop, filename);
filename[8]='T';filename[9]='X';filename[10]='T';
if (!Mmc_Fat_Assign(filename, 1)) {
//
Mmc_Fat_Assign(&filename, 0xA0);
mmc_Fat_Rewrite();
Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
year=Eeprom_read(0)+2000;
mmc_Fat_Set_File_Date( year,mn,day,hr,mnt,sec);
Mmc_Fat_Write(a_la_ligne,2);
loop=1000000;
}
loop=loop+1001;
}
}
//----------------INIT FILE
void init_file()
{ Lcd_Out(1,1,"INIT MEMORY ");
//--- init the FAT library
Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
// use fat16 quick format instead of init routine if a formatting is needed
if (!Mmc_Fat_Init(&PORTC, 2)) {
// reinitialize spi at higher speed
Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
Lcd_Out_Cp("OK");
select_file();
//M_Create_New_File();
}
//Mmc_Fat_Init Returns 0 if initialization is successful, 1 if boot sector was not found and 255 if card was not detected.
else {Lcd_Out_Cp("FAIL");
Lcd_Out(2,1,"SYSTEM STOPPED");
while(1);}
}
void init_file2()
{
//--- init the FAT library
Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
// use fat16 quick format instead of init routine if a formatting is needed
if (!Mmc_Fat_Init(&PORTC, 2)) {
// reinitialize spi at higher speed
Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
select_file2();
//M_Create_New_File();
}
//Mmc_Fat_Init Returns 0 if initialization is successful, 1 if boot sector was not found and 255 if card was not detected.
}
//-------------------- Setup time/Date RTC------
void setup_RTC()
{ Lcd_Out(1,1,"SETUP TIME/DATE:");
delay_ms(1000);
Lcd_Cmd(Lcd_Clear);
Lcd_Out(1,1,"DATE:");
Lcd_Out(2,1,"TIME:");
hr=12;
mnt=0;
sec=0;
year=7;
mn=1;
day=1;
hr_bcd=Dec2Bcd(hr);
mnt_bcd=Dec2Bcd(mnt);
sec_bcd=Dec2Bcd(sec);
year_bcd=Dec2Bcd(year);
mn_bcd=Dec2Bcd(mn);
day_bcd=Dec2Bcd(day);
Lcd_CMD(192+7);
display_lcd_Bcd(hr_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(mnt_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(sec_bcd);
Lcd_CMD(128+7);
display_lcd_Bcd(day_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(mn_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(year_bcd);
while (Button(&PORTB,7,1,0)==255);
while (Button(&PORTB,7,1,0)==0) {
if (Button(&PORTB,6,1,0)==255) {day++ ; if (day==32) {day=1;}}
if (Button(&PORTB,5,1,0)==255) {day-- ; if (day==255) {day=31;}}
Delay_ms(200);
day_bcd=Dec2Bcd(day);
Lcd_CMD(128+7);
display_lcd_Bcd(day_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(mn_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(year_bcd);
}
while (Button(&PORTB,7,1,0)==255);
while (Button(&PORTB,7,1,0)==0) {
if (Button(&PORTB,6,1,0)==255) {mn++ ; if (mn==13) {mn=1;}}
if (Button(&PORTB,5,1,0)==255) {mn-- ; if (mn==255) {mn=12;}}
Delay_ms(200);
mn_bcd=Dec2Bcd(mn);
Lcd_CMD(128+7);
display_lcd_Bcd(day_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(mn_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(year_bcd);
}
while (Button(&PORTB,7,1,0)==255);
while (Button(&PORTB,7,1,0)==0) {
if (Button(&PORTB,6,1,0)==255) {year++ ; if (year==51) {year=0;}}
if (Button(&PORTB,5,1,0)==255) {year-- ; if (year==255) {year=50;}}
Delay_ms(200);
year_bcd=Dec2Bcd(year);
Lcd_CMD(128+7);
display_lcd_Bcd(day_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(mn_bcd);
Lcd_Chr_Cp('/');
display_lcd_Bcd(year_bcd);
}
Eeprom_Write(0,year);
if (year.F0==1) {day_bcd.F6=1;}
if (year.F1==1) {day_bcd.F7=1;}
while (Button(&PORTB,7,1,0)==255);
while (Button(&PORTB,7,1,0)==0) {
if (Button(&PORTB,6,1,0)==255) {hr++ ; if (hr==24) {hr=0;}}
if (Button(&PORTB,5,1,0)==255) {hr-- ; if (hr==255) {hr=23;}}
Delay_ms(200);
Lcd_CMD(192+7);
hr_bcd=Dec2Bcd(hr);
display_lcd_Bcd(hr_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(mnt_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(sec_bcd);
}
while (Button(&PORTB,7,1,0)==255);
while (Button(&PORTB,7,1,0)==0) {
if (Button(&PORTB,6,1,0)==255) {mnt++ ; if (mnt==60) {mnt=0;}}
if (Button(&PORTB,5,1,0)==255) {mnt-- ; if (mnt==255) {mnt=59;}}
Delay_ms(200);
Lcd_CMD(192+7);
mnt_bcd=Dec2Bcd(mnt);
display_lcd_Bcd(hr_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(mnt_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(sec_bcd);
}
while (Button(&PORTB,7,1,0)==255);
while (Button(&PORTB,7,1,0)==0) {
if (Button(&PORTB,6,1,0)==255) {sec++ ; if (sec==60) {sec=0;}}
if (Button(&PORTB,5,1,0)==255) {sec-- ; if (sec==255) {sec=59;}}
Delay_ms(200);
Lcd_CMD(192+7);
sec_bcd=Dec2Bcd(sec);
display_lcd_Bcd(hr_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(mnt_bcd);
Lcd_Chr_Cp(':');
display_lcd_Bcd(sec_bcd);
}
Soft_I2C_Start();
Soft_I2C_Write(0xA0);
Soft_I2C_Write(0);
Soft_I2C_Write(0x80);
Soft_I2C_Write(0);
Soft_I2C_Write(sec_bcd);
Soft_I2C_Write(mnt_bcd);
Soft_I2C_Write(hr_bcd);
Soft_I2C_Write(day_bcd);
Soft_I2C_Write(mn_bcd);
Soft_I2C_Stop();
Soft_I2C_Start();
soft_I2C_Write(0xA0);
Soft_I2C_Write(0);
Soft_I2C_Write(0);
Soft_I2C_Stop();
}
void main() {
TRISB=255;
Soft_I2c_Config(&PORTB,2,1);
Delay_ms(500);
teste=PORTB,4; //PORTB 4 é onde estão os transistores (switch) para saber se o LCD está conectado
if (teste>100) {
ADCON1 = 8; //para que Vref+(se quisermos por o adc de 0->3,3V) e Vref- estejam configurados no pin 4 e 5
TRISA = 0xFF; //PORT A is an input
PORTC = 0;
TRISC = 0b11010011;
Delay_ms(100);
Lcd_Init(&PORTD); // Initialize LCD connected to PORTD
Delay_ms(100);
Lcd_Cmd(Lcd_CLEAR); // Clear display
Delay_ms(200);
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
num=0;
TRISB.F0=1;
TRISB.F3=1;
if (Button(&PORTB,7,1,0)==255) //CONDICAO COM LCD
{setup_rtc();}
Delay_ms(1000);
Lcd_Cmd(Lcd_Clear);
init_file();
Lcd_Cmd(Lcd_Clear);
flag_display_time=0;
TRISB=255;
while (num < 1) {
see_time();
delay_ms(500);
value_channel_1=Adc_read(0); //get results for AD convertion
LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
Lcd_Out_Cp(texto1);
LCD_CMD(208); // segunda linha
IntToStr(value_channel_1, txt1);
LCD_Chr_CP(txt1[0]);
LCD_Chr_CP(txt1[1]);
LCD_Chr_CP(txt1[2]);
LCD_Chr_CP(txt1[3]);
LCD_Chr_CP(txt1[4]);
LCD_Chr_CP(txt1[5]);
Delay_ms(500);
value_channel_2=Adc_read(1); //get results for AD convertion
LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
Lcd_Out_Cp(texto2);
LCD_CMD(208); // segunda linha
IntToStr(value_channel_2, txt1);
LCD_Chr_CP(txt1[0]);
LCD_Chr_CP(txt1[1]);
LCD_Chr_CP(txt1[2]);
LCD_Chr_CP(txt1[3]);
LCD_Chr_CP(txt1[4]);
LCD_Chr_CP(txt1[5]);
Delay_ms(200);
write_adc_value_on_mmc(value_channel_1); //m = valeur de l'echantillon
write_adc_value_on_mmc(value_channel_2);
write_time_and_day_on_mmc_file();
delay_ms(500);
PORTD.F0=1;
delay_ms(1000);
PORTD.F0=0;
delay_ms(500);
}
}
}