2014-11-19 5 views
0

실시간 시계의 시간과 날짜를 설정하려고하는데 직렬 모니터에 입력 한 데이터를 읽는 데 문제가 있습니다. 아래 코드에서 날짜를 변경할지 묻는 메시지가 표시되고 "y"를 입력하고 Enter 키를 누릅니다. 그 후, 그것은 1 년을 요구하고, "14"를 입력하고 Enter를 누르면 아무 일도 일어나지 않습니다.바이트가 Arduino 코드에서 읽히지 않습니다

#include <Wire.h> 
const int DS1307 = 0x68; // Address of DS1307 see data sheets 
const char* days[] = 
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 
const char* months[] = 
{"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"}; 

// Initializes all values: 
byte second = 0; 
byte minute = 0; 
byte hour = 0; 
byte weekday = 0; 
byte monthday = 0; 
byte month = 0; 
byte year = 0; 

void setup() { 
    Wire.begin(); 
    Serial.begin(9600); 
    delay(2000); // This delay allows the MCU to read the current date and time. 

    Serial.print("The current date and time is: "); 
    printTime(); 
    Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor"); 
    Serial.println("Would you like to set the date and time now? Y/N"); 

    while (!Serial.available()) delay(10); 
    if (Serial.read() == 'y' || Serial.read() == 'Y') 

    // This set of functions allows the user to change the date and time 
    { 
    Serial.read(); 
    setTime(); 
    Serial.print("The current date and time is now: "); 
    printTime(); 
    } 


    Serial.println("Thank you."); 
} 

// Continuous function for converting bytes to decimals and vice versa 
void loop() { 
} 
byte decToBcd(byte val) { 
    return ((val/10*16) + (val%10)); 
} 
byte bcdToDec(byte val) { 
    return ((val/16*10) + (val%16)); 
} 


// This set of codes is allows input of data 
void setTime() { 
    Serial.print("Please enter the current year, 00-99. - "); 
    year = readByte(); 
    Serial.println(year); 
    Serial.print("Please enter the current month, 1-12. - "); 
    month = readByte(); 
    Serial.println(months[month-1]); 
    Serial.print("Please enter the current day of the month, 1-31. - "); 
    monthday = readByte(); 
    Serial.println(monthday); 
    Serial.println("Please enter the current day of the week, 1-7."); 
    Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat - "); 
    weekday = readByte(); 
    Serial.println(days[weekday-1]); 
    Serial.print("Please enter the current hour in 24hr format, 0-23. - "); 
    hour = readByte(); 
    Serial.println(hour); 
    Serial.print("Please enter the current minute, 0-59. - "); 
    minute = readByte(); 
    Serial.println(minute); 
    second = 0; 
    Serial.println("The data has been entered."); 

    // The following codes transmits the data to the RTC 
    Wire.beginTransmission(DS1307); 
    Wire.write(byte(0)); 
    Wire.write(decToBcd(second)); 
    Wire.write(decToBcd(minute)); 
    Wire.write(decToBcd(hour)); 
    Wire.write(decToBcd(weekday)); 
    Wire.write(decToBcd(monthday)); 
    Wire.write(decToBcd(month)); 
    Wire.write(decToBcd(year)); 
    Wire.write(byte(0)); 
    Wire.endTransmission(); 
    // Ends transmission of data 
} 


byte readByte() { 
    while (!Serial.available()) delay(10); 
    byte reading = 0; 
    byte incomingByte = Serial.read(); 
    while (incomingByte != '\n') { 
    if (incomingByte >= '0' && incomingByte <= '9') 
     reading = reading * 10 + (incomingByte - '0'); 
    else; 
    incomingByte = Serial.read(); 
    } 
    Serial.flush(); 
    return reading; 
} 


void printTime() { 
    char buffer[3]; 
    const char* AMPM = 0; 
    readTime(); 
    Serial.print(days[weekday-1]); 
    Serial.print(" "); 
    Serial.print(months[month-1]); 
    Serial.print(" "); 
    Serial.print(monthday); 
    Serial.print(", 20"); 
    Serial.print(year); 
    Serial.print(" "); 
    if (hour > 12) { 
    hour -= 12; 
    AMPM = " PM"; 
    } 
    else AMPM = " AM"; 
    Serial.print(hour); 
    Serial.print(":"); 
    sprintf(buffer, "%02d", minute); 
    Serial.print(buffer); 
    Serial.println(AMPM); 
} 


void readTime() { 
    Wire.beginTransmission(DS1307); 
    Wire.write(byte(0)); 
    Wire.endTransmission(); 
    Wire.requestFrom(DS1307, 7); 
    second = bcdToDec(Wire.read()); 
    minute = bcdToDec(Wire.read()); 
    hour = bcdToDec(Wire.read()); 
    weekday = bcdToDec(Wire.read()); 
    monthday = bcdToDec(Wire.read()); 
    month = bcdToDec(Wire.read()); 
    year = bcdToDec(Wire.read()); 
} 

업데이트 1 :

#include <Wire.h> 
const int DS1307 = 0x68; // Address of DS1307 see data sheets 
const char* days[] = 
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 
const char* months[] = 
{"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"}; 

// Initializes all values: 
byte second = 0; 
byte minute = 0; 
byte hour = 0; 
byte weekday = 0; 
byte monthday = 0; 
byte month = 0; 
byte year = 0; 

void setup() { 
    Wire.begin(); 
    Serial.begin(9600); 
    delay(2000); // This delay allows the MCU to read the current date and time. 

    Serial.print("The current date and time is: "); 
    printTime(); 
    Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor"); 
    Serial.println("Would you like to set the date and time now? Y/N"); 

    while (!Serial.available()) delay(10); 
    if (Serial.read() == 'y' || Serial.read() == 'Y') 

    // This set of functions allows the user to change the date and time 
    { 
    Serial.read(); 
    setTime(); 
    Serial.print("The current date and time is now: "); 
    printTime(); 
    } 


    Serial.println("Thank you."); 
} 

// Continuous function for converting bytes to decimals and vice versa 
void loop() { 
} 
byte decToBcd(byte val) { 
    return ((val/10*16) + (val%10)); 
} 
byte bcdToDec(byte val) { 
    return ((val/16*10) + (val%16)); 
} 


// This set of codes is allows input of data 
void setTime() { 
    Serial.print("Please enter the current year, 00-99. - "); 
    year = readByte(); 
    Serial.println(year); 
    Serial.print("Please enter the current month, 1-12. - "); 
    month = readByte(); 
    Serial.println(months[month-1]); 
    Serial.print("Please enter the current day of the month, 1-31. - "); 
    monthday = readByte(); 
    Serial.println(monthday); 
    Serial.println("Please enter the current day of the week, 1-7."); 
    Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat - "); 
    weekday = readByte(); 
    Serial.println(days[weekday-1]); 
    Serial.print("Please enter the current hour in 24hr format, 0-23. - "); 
    hour = readByte(); 
    Serial.println(hour); 
    Serial.print("Please enter the current minute, 0-59. - "); 
    minute = readByte(); 
    Serial.println(minute); 
    second = 0; 
    Serial.println("The data has been entered."); 

    // The following codes transmits the data to the RTC 
    Wire.beginTransmission(DS1307); 
    Wire.write(byte(0)); 
    Wire.write(decToBcd(second)); 
    Wire.write(decToBcd(minute)); 
    Wire.write(decToBcd(hour)); 
    Wire.write(decToBcd(weekday)); 
    Wire.write(decToBcd(monthday)); 
    Wire.write(decToBcd(month)); 
    Wire.write(decToBcd(year)); 
    Wire.write(byte(0)); 
    Wire.endTransmission(); 
    // Ends transmission of data 
} 


byte readByte() { 
    byte reading = 0; 
    while (!Serial.available()) delay(10); 

    byte incomingByte = Serial.read(); 
    while (incomingByte != '\r') { 
    if (incomingByte >= '0' && incomingByte <= '9') 
     reading = reading * 10 + (incomingByte - '0'); 
    else; 
    incomingByte = Serial.read(); 
    } 
    //Serial.flush(); 
    return reading; 
} 


void printTime() { 
    char buffer[3]; 
    const char* AMPM = 0; 
    readTime(); 
    Serial.print(days[weekday-1]); 
    Serial.print(" "); 
    Serial.print(months[month-1]); 
    Serial.print(" "); 
    Serial.print(monthday); 
    Serial.print(", 20"); 
    Serial.print(year); 
    Serial.print(" "); 
    if (hour > 12) { 
    hour -= 12; 
    AMPM = " PM"; 
    } 
    else AMPM = " AM"; 
    Serial.print(hour); 
    Serial.print(":"); 
    sprintf(buffer, "%02d", minute); 
    Serial.print(buffer); 
    Serial.println(AMPM); 
} 


void readTime() { 
    Wire.beginTransmission(DS1307); 
    Wire.write(byte(0)); 
    Wire.endTransmission(); 
    Wire.requestFrom(DS1307, 7); 
    second = bcdToDec(Wire.read()); 
    minute = bcdToDec(Wire.read()); 
    hour = bcdToDec(Wire.read()); 
    weekday = bcdToDec(Wire.read()); 
    monthday = bcdToDec(Wire.read()); 
    month = bcdToDec(Wire.read()); 
    year = bcdToDec(Wire.read()); 
} 

답변

1

은 몇 가지 문제가 있습니다.

setTime();으로 전화하기 전에 Serial.read()이 추가로 있습니다. 이것은 1 바이트를 잃는다.

readByte()에서 바이트 대신 int를 반환 할 수 있지만 여기 실제 문제는 루프 외부에서 byte reading = 0;을 선언해야한다는 것입니다. 루프 반복마다 실수로 0으로 설정됩니다.

Serial.flush()은 Arduino 1.0 이후 발신 데이터를 플러시하는 데 사용됩니다. 들어오는 버퍼를 플러시한다는 의미 인 것 같습니다. 더 이상 수행하지 않습니다.

동결 문제는 아마도 루프 내부의 readByte() 때문일 수 있습니다. 일부 터미널은 줄 끝 부분에 '\n' 문자를 포함하지만 일부는 '\r'을 사용하고 일부는 둘 다 사용합니다. '\r'을 확인하고 '\n'을 무시해보세요. 당신의 프로그램은 보내지 않을 개행 문자를 끝없이 기다리고있을 것입니다!

+0

그래서'Serial.read()'를 삭제하고,'byte year = 0;'다음에'byte reading = 0;'을 선언하고'Serial.flush()'를 주석 처리하고' n''에서''\ r''로'readByte()'를 호출하지만 여전히 문제가 있습니다. – SuperAdmin

+0

질문을 최신 버전으로 업데이트하고 새로운 문제점을 설명하십시오. 실제로'Serial.read()'가 필요할 것입니다; 그것은 '\ r'문자를 소비하는 것처럼 보입니다. readByte() 함수 내에서 루프보다 위에 '바이트 읽기 = 0;'을 선언하십시오. 전역 적으로 선언하면 필요가있을 때 0으로 설정되지 않습니다. – UncleO

+0

업데이트 된 코드를 게시했습니다. – SuperAdmin

관련 문제