First of all i have been asking too many questions lately, questions are not stopping me from developing my projects but i can not stop thinking about them unless i find an answer.
I was making myself a ds3231 based lcd clock i wrote a lot of functions for each register in ds3231. Read_Seconds Read_Hour .... why not just use one ? well because for every data i retrieve from ds3231 i have to make some calculations to get the real char value of real time and it is different for every register. So program works fine not going to post it all here because it is very long and none would take time to read it. I ll move on and ask the question. After compiling the the whole project i get these values for ram and data usage
Program Memory Usage : 7246 bytes 22.1 % Full
Data Memory Usage : 240 bytes 11.7 % Full (Yes i still have plenty of ram but seeing this is bothering me as of i am not using ram efficiently)
Now what i think is 240 bytes is very large for variables given in functions. I did not use any global variable so this brought up the question is the 240 value compile time ram usage?(All the variables in ram at the same time) Because from what i read and know is every variable i declare in a scope will end up getting deleted if not deleted will be available for overwriting for other datas when the scope ends.If i am correct my program should not consume more than around 50 byte at a time.
I tried to find an answer to this by starting up a simple project writing variables in functions and globally setting them to port b ... but this time data usage was less then the variables i declared globally.
It is maybe because i set them only once and the compiler did not take them as variable because i never changed their value again? ... I really do not know how it gets handled by compiler.
I once heard from my teacher that ram handling in assembly is better then C because control of the ram addresses is all the time in your hands. Is this the kind of problem i am facing now?
Here is my ds3231 header file to be an example
#define DS3231 0b11010000 #define I2C_READ 1 #define I2C_WRITE 0 unsigned char read_ds3231(unsigned char Rdata) { unsigned char ret; i2c_start_wait(DS3231+I2C_WRITE); // set device address and write mode i2c_write(Rdata); // write address = 1 i2c_rep_start(DS3231+I2C_READ); // set device address and read mode ret = i2c_readNak(); // read one byte from EEPROM i2c_stop(); return ret; } void write_ds3231(unsigned char Wdata_adress, unsigned char Wdata_data) { i2c_start_wait(DS3231+I2C_WRITE); i2c_write(Wdata_adress); i2c_write(Wdata_data); i2c_stop(); } unsigned char Read_second(void) { unsigned char ret; //returning data byte unsigned char data; //read data byte unsigned char seconds; //data byte for second section unsigned char seconds10; //data byte for 10sec section data=read_ds3231(0x00); // read the second register seconds10=data&0b01110000; //bitwise operation for 10sec section seconds10=(seconds10>>4); //bitwise operation to left to read out value seconds10=seconds10*10; //give the real 10sec value seconds=data&0b00001111; //give the real seconds value ret=seconds10+seconds; //Seconds in unsigned char return ret; //retrun the value } unsigned char Read_minutes(void) { unsigned char ret; //returning data byte unsigned char data; //read data byte unsigned char minutes; //data byte for minute section unsigned char minutes10; //data byte for 10minute section data=read_ds3231(0x01); // read the second register minutes10=data&0b01110000; //bitwise operation for 10min section minutes10=(minutes10>>4); //bitwise operation to left to read out value minutes10=minutes10*10; //give the real 10min value minutes=data&0b00001111; //give the real minutes value ret=minutes10+minutes; //minutes in unsigned char return ret; //retrun the value } unsigned char Read_hour_24(void) { unsigned char ret; //returnning data byte unsigned char data; //read data byte unsigned char hour; //data byte for hour section unsigned char hour_1; //data byte for 10hour section unsigned char hour_2; //data byte for 20hour section data=read_ds3231(0x02); hour_2=data&0b00100000; hour_2=(hour_2>>5); hour_2=hour_2*20; //if 20 hour is set 10 hour bit is 0 hour_1=data&0b00010000; hour_1=(hour_1>>4); hour_1=hour_1*10; //10 hour bit hour=data&0b00001111; ret=hour_2+hour_1+hour; return ret; } unsigned char Read_day(void) { unsigned char ret; unsigned char data; unsigned char day; data=read_ds3231(0x03); day=data; ret=day; return ret; } unsigned char Read_date(void) { unsigned char ret; unsigned char data; unsigned char date; unsigned char date_10; data=read_ds3231(0x04); date_10=data&0b00110000; date_10=(date_10>>4); date_10=date_10*10; date=data&0b00001111; ret=date_10+date; return ret; } unsigned char Read_month(void) { unsigned char ret; unsigned char data; unsigned char month; unsigned char month_10; data=read_ds3231(0x05); month_10=data&0b00010000; month_10=(month_10>>4); month_10=month_10*10; month=data&0b00001111; ret=month_10+month; return ret; } unsigned char Read_year(void) { unsigned char ret; unsigned char data; unsigned char year; unsigned char year_10; data=read_ds3231(0x06); year_10=data&0b11110000; year_10=(year_10>>4); year_10=year_10*10; year=data&0b00001111; ret=year_10+year; return ret; } void Write_second(unsigned char second) { unsigned char send; unsigned char _10second; unsigned char _second; _10second=second/10; _10second=(_10second<<4); _second=second%10; send=_second|_10second; write_ds3231(0x00,send); } void write_minute(unsigned char minute) { unsigned char send; unsigned char _10minute; unsigned char _minute; _10minute=minute/10; _10minute=(_10minute<<4); _minute=minute%10; send=_minute|_10minute; write_ds3231(0x01,send); } void write_hour_24(unsigned char hour) { unsigned char send; unsigned char _hour; unsigned char _10hour; unsigned char _20hour; if(hour>=20) { _20hour=1; _20hour=(_20hour<<5); } if(hour<20 && hour>=10) { _10hour=1; _10hour=(_10hour<<4); } _hour=hour%10; if(hour>=20) { send=_20hour|_hour; write_ds3231(0x02,send); } if(hour<20 && hour>=10) { send=_10hour|_hour; write_ds3231(0x02,send); } if(hour<10) { send=_hour; write_ds3231(0x02,send); } } void write_day(unsigned char day) { unsigned char send; unsigned char _day; _day=day; send=_day; write_ds3231(0x03,send); } void write_date(unsigned char date) { unsigned char send; unsigned char _date; unsigned char _10date; _10date=date/10; _10date=(_10date<<4); _date=date%10; send=_10date|_date; write_ds3231(0x04,send); } void write_month(unsigned char month) { unsigned char send; unsigned char _month; unsigned char _10month; _10month=month/10; _10month=(_10month<<4); _month=month%10; send=_10month|_month; write_ds3231(0x05,send); } void write_year(unsigned char year) { unsigned char send; unsigned char _year; unsigned char _10year; _10year=year/10; _10year=(_10year<<4); _year=year%10; send=_10year|_year; write_ds3231(0x06,send); } unsigned char Read_A1M1_seconds(void) { unsigned char ret; //returning data byte unsigned char data; //read data byte unsigned char seconds; //data byte for second section unsigned char seconds10; //data byte for 10sec section data=read_ds3231(0x07); // read the second register seconds10=data&0b01110000; //bitwise operation for 10sec section seconds10=(seconds10>>4); //bitwise operation to left to read out value seconds10=seconds10*10; //give the real 10sec value seconds=data&0b00001111; //give the real seconds value ret=seconds10+seconds; //Seconds in unsigned char return ret; //retrun the value } unsigned char Read_A1M2_minutes(void) { unsigned char ret; //returning data byte unsigned char data; //read data byte unsigned char minutes; //data byte for minute section unsigned char minutes10; //data byte for 10minute section data=read_ds3231(0x08); // read the second register minutes10=data&0b01110000; //bitwise operation for 10min section minutes10=(minutes10>>4); //bitwise operation to left to read out value minutes10=minutes10*10; //give the real 10min value minutes=data&0b00001111; //give the real minutes value ret=minutes10+minutes; //minutes in unsigned char return ret; //retrun the value } unsigned char Read_A1M3_Hour(void) { unsigned char ret; //returnning data byte unsigned char data; //read data byte unsigned char hour; //data byte for hour section unsigned char hour_1; //data byte for 10hour section unsigned char hour_2; //data byte for 20hour section data=read_ds3231(0x09); hour_2=data&0b00100000; hour_2=(hour_2>>5); hour_2=hour_2*20; //if 20 hour is set 10 hour bit is 0 hour_1=data&0b00010000; hour_1=(hour_1>>4); hour_1=hour_1*10; //10 hour bit hour=data&0b00001111; ret=hour_2+hour_1+hour; return ret; } void Write_A1M1_Seconds(unsigned char second) { unsigned char send; unsigned char _10second; unsigned char _second; _10second=second/10; _10second=(_10second<<4); _second=second%10; send=_second|_10second; write_ds3231(0x07,send); } void Write_A1M2_Minutes(unsigned char minute) { unsigned char send; unsigned char _10minute; unsigned char _minute; _10minute=minute/10; _10minute=(_10minute<<4); _minute=minute%10; send=_minute|_10minute; write_ds3231(0x08,send); } void Write_A1M3_Hour(unsigned char hour) { unsigned char send; unsigned char _hour; unsigned char _10hour; unsigned char _20hour; if(hour>=20) { _20hour=1; _20hour=(_20hour<<5); } if(hour<20 && hour>=10) { _10hour=1; _10hour=(_10hour<<4); } _hour=hour%10; if(hour>=20) { send=_20hour|_hour; write_ds3231(0x09,send); } if(hour<20 && hour>=10) { send=_10hour|_hour; write_ds3231(0x09,send); } if(hour<10) { send=_hour; write_ds3231(0x09,send); } } void Write_A1M4_Date(void) { write_ds3231(0x0A,0b11111111); } unsigned char Read_Control(void) { unsigned char ret; unsigned char data; data=read_ds3231(0x0E); ret=data; return ret; } void Write_Control( unsigned char action) // 1 for setting bit 0 for unsetting bit { if(action==1) { write_ds3231(0x0E,0b00011101); } if(action==0) { write_ds3231(0x0E,0b00011100); } } void Write_Status(unsigned char status) { unsigned char data; data=read_ds3231(0x0F); data=status&data; write_ds3231(0x0F,data); }