[ create a new paste ] login | about

Link: http://codepad.org/TKgAkK6E    [ raw code | output | fork ]

C, pasted on Jan 30:
#include <Wire.h> 
 
/* Địa chỉ của DS1307 */
const byte DS1307 = 0x68;
/* Số byte dữ liệu sẽ đọc từ DS1307 */
const byte NumberOfFields = 7;
 
/* khai báo các biến thời gian */
int second, minute, hour, day, wday, month, year;
 
void setup()
{
  Wire.begin();
  /* cài đặt thời gian cho module */
  setTime(12, 30, 45, 1, 8, 2, 15); // 12:30:45 CN 08-02-2015
  Serial.begin(9600);
}
 
void loop()
{
  /* Đọc dữ liệu của DS1307 */
  readDS1307();
  /* Hiển thị thời gian ra Serial monitor */
  digitalClockDisplay();
  delay(1000);
}
 
void readDS1307()
{
        Wire.beginTransmission(DS1307);
        Wire.write((byte)0x00);
        Wire.endTransmission();
        Wire.requestFrom(DS1307, NumberOfFields);
        
        second = bcd2dec(Wire.read() & 0x7f);
        minute = bcd2dec(Wire.read() );
        hour   = bcd2dec(Wire.read() & 0x3f); // chế độ 24h.
        wday   = bcd2dec(Wire.read() );
        day    = bcd2dec(Wire.read() );
        month  = bcd2dec(Wire.read() );
        year   = bcd2dec(Wire.read() );
        year += 2000;    
}
/* Chuyển từ format BCD (Binary-Coded Decimal) sang Decimal */
int bcd2dec(byte num)
{
        return ((num/16 * 10) + (num % 16));
}
/* Chuyển từ Decimal sang BCD */
int dec2bcd(byte num)
{
        return ((num/10 * 16) + (num % 10));
}
 
void digitalClockDisplay(){
    // digital clock display of the time
    Serial.print(hour);
    printDigits(minute);
    printDigits(second);
    Serial.print(" ");
    Serial.print(day);
    Serial.print(" ");
    Serial.print(month);
    Serial.print(" ");
    Serial.print(year); 
    Serial.println(); 
}
 
void printDigits(int digits){
    // các thành phần thời gian được ngăn chách bằng dấu :
    Serial.print(":");
        
    if(digits < 10)
        Serial.print('0');
    Serial.print(digits);
}
 
/* cài đặt thời gian cho DS1307 */
void setTime(byte hr, byte min, byte sec, byte wd, byte d, byte mth, byte yr)
{
        Wire.beginTransmission(DS1307);
        Wire.write(byte(0x00)); // đặt lại pointer
        Wire.write(dec2bcd(sec));
        Wire.write(dec2bcd(min));
        Wire.write(dec2bcd(hr));
        Wire.write(dec2bcd(wd)); // day of week: Sunday = 1, Saturday = 7
        Wire.write(dec2bcd(d)); 
        Wire.write(dec2bcd(mth));
        Wire.write(dec2bcd(yr));
        Wire.endTransmission();
}


Output:
Line 18: error: Wire.h: No such file or directory
Line 4: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'DS1307'
Line 6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'NumberOfFields'
In function 'setup':
Line 13: error: 'Wire' undeclared (first use in this function)
Line 13: error: (Each undeclared identifier is reported only once
Line 13: error: for each function it appears in.)
Line 16: error: 'Serial' undeclared (first use in this function)
t.c: At top level:
Line 29: warning: conflicting types for 'readDS1307'
Line 22: warning: previous implicit declaration of 'readDS1307' was here
In function 'readDS1307':
Line 30: error: 'Wire' undeclared (first use in this function)
Line 30: error: 'DS1307' undeclared (first use in this function)
Line 31: error: 'byte' undeclared (first use in this function)
Line 31: error: expected ')' before numeric constant
Line 33: error: 'NumberOfFields' undeclared (first use in this function)
t.c: At top level:
Line 45: error: expected ')' before 'num'
Line 50: error: expected ')' before 'num'
Line 55: warning: conflicting types for 'digitalClockDisplay'
Line 24: warning: previous implicit declaration of 'digitalClockDisplay' was here
In function 'digitalClockDisplay':
Line 57: error: 'Serial' undeclared (first use in this function)
t.c: At top level:
Line 69: warning: conflicting types for 'printDigits'
Line 58: warning: previous implicit declaration of 'printDigits' was here
In function 'printDigits':
Line 71: error: 'Serial' undeclared (first use in this function)
t.c: At top level:
Line 79: error: expected ')' before 'hr'


Create a new paste based on this one


Comments: