temp = ADC_Read(0);
temp2 = ADC_Read(1);
temp = temp - temp2;
unsigned char Read_Buffer[64] absolute 0x500;
unsigned char Write_Buffer[64] absolute 0x541;
unsigned char num,flag;
#ifndef DS1307
#define DS1307 0xD0
#endif
#define SW5MINS PORTD.F2
#define SW30MINS PORTD.F3
// LCD module connections
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB2_bit;
sbit LCD_D5 at LATB3_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
void Hid_Enable(char *Read_Buffer, char *Write_Buffer);
char HID_Write(char *Write_Buffer, char len);
char x; // Loop variable
int cnt;
double temp, temp2;
char sTemp[23], sTemp2[23];
char colon[] = ":";
char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
int am_pm = 0;
char time_format[3];
unsigned long int usb_tx_cnt_val, usb_tx_cnt = 0;
bit usb_tx_flag;
void interrupt()
{
usb_tx_cnt++;
USB_Interrupt_Proc();
if(usb_tx_cnt == usb_tx_cnt_val) {
usb_tx_flag = 1;
}
TMR0L = 100; //Reload Value
INTCON.TMR0IF = 0; //Re-Enable Timer-0 Interrupt
}
void clear_buffer(unsigned char buffer[])
{
unsigned int x = 0;
while(buffer[x] != '\0')
{
buffer[x] = '\0';
x++;
}
}
void Zero_Fill(char *value) {
if (value[1] == 0) {
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}
void Write_Time() {
I2C2_Start(); // issue start signal
I2C2_Wr(DS1307); // address DS1307
I2C2_Wr(0); // start from word at address (REG0)
I2C2_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec)
I2C2_Wr(0); // write 0 to minutes word to (REG1)
I2C2_Wr(0x17); // write 17 to hours word (24-hours mode)(REG2)
I2C2_Wr(0x02); // write 2 - Monday (REG3)
I2C2_Wr(0x04); // write 4 to date word (REG4)
I2C2_Wr(0x05); // write 5 (May) to month word (REG5)
I2C2_Wr(0x01); // write 01 to year word (REG6)
I2C2_Stop(); // issue stop signal
I2C2_Start(); // issue start signal
I2C2_Wr(0xD0); // address DS1307
I2C2_Wr(0); // start from word at address 0
I2C2_Wr(0); // write 0 to REG0 (enable counting + 0 sec)
I2C2_Stop(); // issue stop signal
}
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
I2C2_Start();
I2C2_Wr(DS1307);
I2C2_Wr(0);
I2C2_Repeated_Start();
I2C2_Wr(0xD1);
*sec =I2C2_Rd(1);
*min =I2C2_Rd(1);
*hr =I2C2_Rd(1);
*week_day =I2C2_Rd(1);
*day =I2C2_Rd(1);
*mn =I2C2_Rd(1);
*year =I2C2_Rd(0);
I2C2_Stop();
}
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
Lcd_Out(1,1,"Date:");
switch(week_day){
case 1: txt="Sun"; break;
case 2: txt="Mon"; break;
case 3: txt="Tue"; break;
case 4: txt="Wed"; break;
case 5: txt="Thu"; break;
case 6: txt="Fri"; break;
case 7: txt="Sat"; break;
}
Lcd_Out(1,7,txt);
Lcd_Chr(1,11,(day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1,12, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,13,'/');
Lcd_Chr(1,14,(mn / 10) + 48);
Lcd_Chr(1,15,(mn % 10) + 48);
Lcd_Chr(1,16,'/');
Lcd_Out(1,17,"2");
Lcd_Out(1,18,"0");
Lcd_Chr(1,19, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,20, (year % 10) + 48);
if(hr >= 12) {
Lcd_Out(2,7,"PM");
am_pm = 1;
}
else if(hr < 12) {
Lcd_Out(2,7,"AM");
am_pm = 0;
}
Lcd_Chr(2,11,(hr / 10) + 48);
Lcd_Chr(2,12,(hr % 10) + 48);
Lcd_Out(2,13,":");
Lcd_Chr(2,14,(min / 10) + 48);
Lcd_Chr(2,15,(min % 10) + 48);
Lcd_Out(2,16,":");
Lcd_Chr(2,17,(sec / 10) + 48);
Lcd_Chr(2,18,(sec % 10) + 48);
}
void load_Write_Buffer() {
if(am_pm == 1) {
time_format[0] = 'P';
time_format[1] = 'M';
}
else if(am_pm == 0) {
time_format[0] = 'A';
time_format[1] = 'M';
}
Write_Buffer[0] = 'D';
Write_Buffer[1] = 'a';
Write_Buffer[2] = 't';
Write_Buffer[3] = 'e';
Write_Buffer[4] = ':';
Write_Buffer[5] = ' ';
Write_Buffer[6] = txt[0];
Write_Buffer[7] = txt[1];
Write_Buffer[8] = txt[2];
Write_Buffer[9] = ' ';
Write_Buffer[10] = (day / 10) + 48;
Write_Buffer[11] = (day % 10) + 48;
Write_Buffer[12] = '/';
Write_Buffer[13] = (mn / 10) + 48;
Write_Buffer[14] = (mn % 10) + 48;
Write_Buffer[15] = '/';
Write_Buffer[16] = '2';
Write_Buffer[17] = '0';
Write_Buffer[18] = (year / 10) + 48;
Write_Buffer[19] = (year % 10) + 48;
Write_Buffer[20] = ' ';
Write_Buffer[21] = 'T';
Write_Buffer[22] = 'i';
Write_Buffer[23] = 'm';
Write_Buffer[24] = 'e';
Write_Buffer[25] = ':';
Write_Buffer[26] = ' ';
Write_Buffer[27] = (hr / 10) + 48;
Write_Buffer[28] = (hr % 10) + 48;
Write_Buffer[29] = ':';
Write_Buffer[30] = (min1 / 10) + 48;
Write_Buffer[31] = (min1 % 10) + 48;
Write_Buffer[32] = ':';
Write_Buffer[33] = (sec / 10) + 48;
Write_Buffer[34] = (sec % 10) + 48;
Write_Buffer[35] = ' ';
Write_Buffer[36] = time_format[0];
Write_Buffer[37] = time_format[1];
Write_Buffer[38] = 'T';
Write_Buffer[39] = 'e';
Write_Buffer[40] = 'm';
Write_Buffer[41] = 'p';
Write_Buffer[42] = 'e';
Write_Buffer[43] = 'r';
Write_Buffer[44] = 'a';
Write_Buffer[45] = 't';
Write_Buffer[46] = 'u';
Write_Buffer[47] = 'r';
Write_Buffer[48] = 'e';
Write_Buffer[49] = ':';
Write_Buffer[50] = ' ';
cnt = 51;
for(cnt=51;cnt<59;cnt++) {
Write_Buffer[cnt] = sTemp[cnt-51];
}
Write_Buffer[60] = ' ';
Write_Buffer[61] = '0';
}
void main() {
TRISA = 0b00000011;
PORTA = 0x00;
LATA = 0x00;
TRISB = 0b00000000;
PORTB = 0x00;
LATB = 0x00;
TRISD = 0b00001111;
LATD = 0b00000000;
PORTD = 0b00000000;
TRISC = 0x00;
PORTC = 0x00;
LATC = 0x00;
ANCON0 = 0b11111100;
ANCON1 = 0b10011100;
ADCON0 = 0b00000000;
//ADCON1 = 0b10110101;
ADCON1 = 0b10110101;
//CONFIG3H = 0b00001010;
CM1CON.CON = 0;
CM2CON.CON = 0;
INTCON = 0;
INTCON2 = 0xF5;
INTCON3 = 0xC0;
RCON.IPEN = 0;
PIE1 = 0;
PIE2 = 0;
PIR1 = 0;
PIR2 = 0;
T0CON = 0x47; // Prescaler = 256 0b01000111
TMR0L = 100; // Timer count is 256-156 = 100
INTCON.TMR0IE = 1; // Enable T0IE
T0CON.TMR0ON = 1; // Turn Timer 0 ON
INTCON = 0xE0; // Enable interrupts
cnt = 0;
usb_tx_flag = 1;
usb_tx_cnt_val = 38146;
usb_tx_cnt = 0;
Write_Buffer[62] = '1';
Write_Buffer[63] = '1';
Delay_ms(10);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"RTC And Temperature");
Lcd_Out(2,1,"Data Logger");
I2C2_Init(100000);
Hid_Enable(&Read_Buffer,&Write_Buffer);
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
txt = "Time:";
Lcd_Out(2,1,txt);
Lcd_Out(3,1,"Temp:");
ADC_Init();
while(1) {
//Write_Time();
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // read time from RTC(DS1307)
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // format date and time
Display_Time(sec, min1, hr, week_day, day, mn, year);
temp = ADC_Read(0);
temp2 = ADC_Read(1);
temp = temp - temp2;
temp = temp * 0.1827042186019435;
FloatToStr(temp, sTemp);
Lcd_Out(3,7,sTemp);
if(usb_tx_flag == 1) {
load_Write_Buffer();
while(!HID_Write(&Write_Buffer,64));
Write_Buffer[62] = '0';
Write_Buffer[63] = '0';
usb_tx_flag = 0;
usb_tx_cnt = 0;
}
if(SW5MINS == 1) {
Delay_ms(30);
if(SW5MINS == 1) {
usb_tx_cnt_val = 38146;
usb_tx_cnt = 38145;
Write_Buffer[63] = '1';
}
}
if(SW30MINS == 1) {
Delay_ms(30);
if(SW30MINS == 1) {
usb_tx_cnt_val = 228876;
usb_tx_cnt = 228875;
Write_Buffer[63] = '2';
}
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?