UPP
ياجماعه شغال معي على البروتيس تمااام
لكن على السيركت مو راضي ينحفظ التاايم يرجع يحسب من اول تايم حطيته
مع انو افترضت بالبدايه ارقام من عندى وبعدين تجهالت الداله عشان يحسب تلقائي
هذا الكود تبعي
كود:
#ifndef DS1307
#define DS1307 0xD0
#endif
// Software I2C connections
sbit Soft_I2C_Scl at RB1_bit;
sbit Soft_I2C_Sda at RB0_bit;
sbit Soft_I2C_Scl_Direction at TRISB1_bit;
sbit Soft_I2C_Sda_Direction at TRISB0_bit;
// End Software I2C connections
sbit LCD_RS at RB3_bit;
sbit LCD_RW at RD0_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RD1_bit;
sbit LCD_D5 at RD2_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD4_bit;
sbit LCD_RS_Direction at TRISB3_bit;
sbit LCD_RW_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISD1_bit;
sbit LCD_D5_Direction at TRISD2_bit;
sbit LCD_D6_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD4_bit;
//char colon[] = ":";
char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
// for some other testing
char txtSec[10];
char txtMin[10];
char txtHour[10];
char txtWeekDay[10];
char txtDay[10];
char txtMn[10];
char txtYear[5];
char txtDisplay[39];
// for some testing
int intSec, intMin, intHour, intWeekDay, intMn, intDay, intYear;
void Write_Time() {
Soft_I2C_Start(); // issue start signal
Soft_I2C_Write(DS1307); // address DS1307
Soft_I2C_Write(0); // start from word at address (REG0)
//Soft_I2C_Write(0x00); // write $80 to REG0. (pause counter + 0 sec)
//Soft_I2C_Write(0x00); // write 0 to minutes word to (REG1)
Soft_I2C_Write(0x22); // write 17 to hours word (24-hours mode)(REG2) (sec)*
Soft_I2C_Write(0x48); // write 2 - Monday (REG3) ,( min ) *
Soft_I2C_Write(0x21); // write 4 to date word (REG4) (hr) *
Soft_I2C_Write(0x02); // write 5 (May) to month word (REG5) (day)*
Soft_I2C_Write(0x13); // write 01 to year word (REG6) (year)*
Soft_I2C_Write(0x05);
Soft_I2C_Write(0x13);
Soft_I2C_Stop(); // issue stop signal
Soft_I2C_Start(); // issue start signal
Soft_I2C_Write(DS1307); // address DS1307
Soft_I2C_Write(0); // start from word at address 0
Soft_I2C_Write(0); // write 0 to REG0 (enable counting + 0 sec)
Soft_I2C_Stop(); // issue stop signal
}
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
Soft_I2C_Start();
Soft_I2C_Write(DS1307);
Soft_I2C_Write(0x00);
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
*sec =Soft_I2C_Read(1);
*min =Soft_I2C_Read(1);
*hr =Soft_I2C_Read(1);
*week_day =Soft_I2C_Read(1);
*day =Soft_I2C_Read(1);
*mn =Soft_I2C_Read(1);
*year =Soft_I2C_Read(0);
Soft_I2C_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) {
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_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt);
Lcd_Chr(1, 6, (day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,8,'.');
Lcd_Chr(1, 9, (mn / 10) + 48);
Lcd_Chr(1,10, (mn % 10) + 48);
Lcd_Chr(1,11,'.');
Lcd_Chr(1,12, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,13, (year % 10) + 48);
txt = "Time";
Lcd_Out(2,1,txt);
Lcd_Chr(2, 6, (hr / 10) + 48);
Lcd_Chr(2, 7, (hr % 10) + 48);
Lcd_Chr(2,8,':');
Lcd_Chr(2, 9, (min / 10) + 48);
Lcd_Chr(2,10, (min % 10) + 48);
Lcd_Chr(2,11,':');
Lcd_Chr(2,12, (sec / 10) + 48);
Lcd_Chr(2,13, (sec % 10) + 48);
Lcd_Cmd(_LCD_CURSOR_OFF);
}
void Init_Main_Simple() {
ADCON1 = 0x0F; // page 268, disable analaog
CMCON = 0x07;
INTCON2 = 0x80; // disable pull up in port b
// clears internal latches
LATB = 0x03; // enable internal pull ups
LATA = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
// Make all outputs
TRISA = 0x00;
TRISB = 0x03;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Soft_I2C_Init(); // initialize I2C
Lcd_Out(1,1,"Time & Date");
}
// this is just for testing to see which pins were really set
// to digital... and which pins were really working as analog.
// portb is not included because it's already working as lcd,
// and for the RTC through I2C.
void main() {
Init_Main_Simple();
Write_Time(); // HERE I WILL IGNOR IT
Delay_ms(100);
while (1) {
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_Direct(sec, min1, hr, week_day, day, mn, year); // prepare and display on LCD
Display_Time(sec, min1, hr, week_day, day, mn, year);
Delay_ms(1000);
// some implementation of testing to see which pins are
// working as digital, and which are still not working
}
}
وهنااا كو د الاخ SEMMOR فيه احد يقدر يغير لى بلكود فقط ال سي دي لانو نوع حقتو GLCD وانا (LCD2X16) حاولت بس يطلع ارر ف الكود
كود:
// Software I2C connections
sbit Soft_I2C_Scl at RC6_bit;
sbit Soft_I2C_Sda at RC7_bit;
sbit Soft_I2C_Scl_Direction at TRISC6_bit;
sbit Soft_I2C_Sda_Direction at TRISC7_bit;
// End Software I2C connections
// Glcd module connections
char GLCD_DataPort at PORTD;
sbit GLCD_CS1 at RC0_bit;
sbit GLCD_CS2 at RC1_bit;
sbit GLCD_RS at RB3_bit;
sbit GLCD_RW at RB4_bit;
sbit GLCD_EN at RB5_bit;
sbit GLCD_RST at RE2_bit;
sbit GLCD_CS1_Direction at TRISC0_bit;
sbit GLCD_CS2_Direction at TRISC1_bit;
sbit GLCD_RS_Direction at TRISB3_bit;
sbit GLCD_RW_Direction at TRISB4_bit;
sbit GLCD_EN_Direction at TRISB5_bit;
sbit GLCD_RST_Direction at TRISE2_bit;
// End Glcd module connections
//time variables
char _time[9];
unsigned short _sec;
unsigned short _min;
unsigned short _hr;
unsigned short _week_day;
//date variables
char _date[11];
unsigned short _day;
unsigned short _mon;
unsigned short _yr;
//writes to RTC DS1307 using software I2C
void _i2c_write_ds1307(unsigned short _addr, unsigned short _data)
{
Soft_I2C_Init();
Soft_I2C_Start();
Soft_I2C_Write(0xd0);
Soft_I2C_Write(_addr);
Soft_I2C_Write(_data);
Soft_I2C_Stop();
}
//reads from RTC DS1307 using software I2C
unsigned short _i2c_read_ds1307(unsigned short _addr)
{
unsigned short i2c_read_data;
Soft_I2C_Init();
Soft_I2C_Start();
Soft_I2C_Write(0xd0);
Soft_I2C_Write(_addr);
Soft_I2C_Start();
Soft_I2C_Write(0xd1);
i2c_read_data = Soft_I2C_Read(0);
Soft_I2C_Stop();
return i2c_read_data;
}
//converts time and dates upper char from BCD To Decimal
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}
//converts time and dates lower char from BCD To Decimal
unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}
//setting DS1307
void set_time_date()
{
_i2c_write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
_i2c_write_ds1307(1,0x01); //write min 27
_i2c_write_ds1307(2,0x01); //write hour 14
_i2c_write_ds1307(3,0x02); //write day of week 2:Monday
_i2c_write_ds1307(4,0x01); // write date 17
_i2c_write_ds1307(5,0x01); // write month 6 June
_i2c_write_ds1307(6,0x13); // write year 8 --> 2008
_i2c_write_ds1307(7,0x10); //SQWE output at 1 Hz
_i2c_write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
}
//get time and data
void get_time_date()
{
glcd_write_text("TIME: ", 0, 0, 1);
glcd_write_text("DATE: ", 0, 2, 1);
_sec=_i2c_read_ds1307(0); // read second
_min=_i2c_read_ds1307(1); // read minute
_hr=_i2c_read_ds1307(2); // read hour
_week_day=_i2c_read_ds1307(3); // read week day
_day=_i2c_read_ds1307(4); // read day
_mon=_i2c_read_ds1307(5); // read month
_yr=_i2c_read_ds1307(6); // read year
//calculate time digits
_time[0] = BCD2UpperCh(_hr);
_time[1] = BCD2LowerCh(_hr);
_time[2] = ':';
_time[3] = BCD2UpperCh(_min);
_time[4] = BCD2LowerCh(_min);
_time[5] = ':';
_time[6] = BCD2UpperCh(_sec);
_time[7] = BCD2LowerCh(_sec);
_time[8] = '\0';
//calculate date digits
_date[0] = BCD2UpperCh(_day);
_date[1] = BCD2LowerCh(_day);
_date[2] ='/';
_date[3] = BCD2UpperCh(_mon);
_date[4] = BCD2LowerCh(_mon);
_date[5] ='/';
_date[6] = '2';
_date[7] = '0';
_date[8] = BCD2UpperCh(_yr);
_date[9] = BCD2LowerCh(_yr);
_date[10] = '\0';
//display time & date
glcd_write_text(_time, 40, 0, 1);
glcd_write_text(_date, 40, 2, 1);
Delay_ms(100);
}
void main()
{
glcd_init();
glcd_fill(0x00);
set_time_date();
for(;;)
{
get_time_date();
}
}