# برنامه نویسی میکروکنترلر (MicroController) ها و MicroProcessor ها > برنامه نویسی سیستم های Embeded > آردوئینو (Arduino) >  مشکل با نمایش تاریخ شمسی در نمایشگر tft 2.4

## systam

سلام 
مثال خود کتابخانه 

/*
 * this example show the simplest way to converting a
 * GREGORIAN Date in format of YYYY/M/D (ex.2010/2/20)
 * to its SHAMSI equivalent. check library root folder for more
 * informations.
 */


#include "DateConvL.h"
uint16_t current_year, current_month, current_day;
DateConvL dateC;


void setup() {
  Serial.begin(9600);
  SetCurrentDate();
  dateC.ToShamsi(current_year,current_month,current_  day); // converts global values of date and stores them to dateC
  Serial.print(dateC.global_year, DEC);
  Serial.print('/');
  Serial.print(dateC.global_month, DEC);
  Serial.print('/');
  Serial.print(dateC.global_day, DEC);
  Serial.println();
}


void loop() {}




/*
 * this function get current time and set date integers declared above
 * IT IS NOT A PART OF LIBRARY but used to display current shamsi date
 * come from : https://gist.github.com/djohnson001/6df673a8d7f8ac04246a
 */
String SetCurrentDate()
{
  char const *date = __DATE__;
  char s_month[5];
  int _month, _day, _year;
  static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  sscanf(date, "%s %d %d", s_month, &current_day, &current_year);
  current_month = ((strstr(month_names, s_month)-month_names)/3) + 1;
}



اینم عکس خروجی

من برنامه ای که دارم استفاده میکنم 
کد اینه 

// Arduino real time clock and temperature monitor with DS3231 and SSD1306 OLED
#include "DHT.h"
#include "DateConvL.h"
uint16_t current_year, current_month, current_day;
DateConvL dateC;
#define I_DHT22 19
#define DHTTYPE DHT22
//Object declaration
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SPI.h>
#include "EEPROM.h"
#include <Fonts/FreeSansBold24pt7b.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0  
#define WHITE   0xFFFF
#define ORANGE          0xFD20
#define GREENYELLOW     0xAFE5
#define DARKGREEN       0x03E0
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft(A3, A2, A1, A0, A4);
DHT dht(I_DHT22, DHTTYPE);
#define button1    9                       // Button B1 is connected to Arduino pin 9
#define button2    8                       // Button B2 is connected to Arduino pin 8


void setup(void) {
  tft.reset();
  Serial.begin(9600);




    pinMode(button1, INPUT_PULLUP);
    pinMode(button2, INPUT_PULLUP);
    
    uint16_t g_identifier;  
    g_identifier = tft.readID(); //
    Serial.print("ID = 0x");
    Serial.println(g_identifier, HEX);
    if (g_identifier == 0x00D3 || g_identifier == 0xD3D3) g_identifier = 0x9481; // write-only shield
    if (g_identifier == 0xFFFF) g_identifier = 0x9341; // serial
    tft.begin(g_identifier);  
    tft.setRotation(1);
    tft.fillScreen(BLACK);
    Wire.begin();
    dht.begin();
    delay(1000);
 
  tft.drawRoundRect(7, 7, 305, 220, 10, ORANGE); 
  tft.setTextColor(WHITE, BLACK);
  tft.drawRect(117, 56, 3, 3, WHITE);     // Put degree symbol ( ° )
}


char Time[]     = "  :  :  ";
char Calendar[] = "  /  /20  ";
char temperature[] = " 00.00";
char temperature_msb;
byte i, second, minute, hour, day, date, month, year, temperature_lsb;


void tft_day(){
  switch(day){
    case 1:  draw_text(70, 20, " SUNDAY  ", 4); break;
    case 2:  draw_text(70, 0, " MONDAY  ", 4); break;
    case 3:  draw_text(70, 0, " TUESDAY ", 4); break;
    case 4:  draw_text(70, 0, "WEDNESDAY", 4); break;
    case 5:  draw_text(70, 0, "THURSDAY ", 4); break;
    case 6:  draw_text(70, 0, " FRIDAY  ", 4); break;
    default: draw_text(70, 0, "SATURDAY ", 4);
  }
}


void DS3231_tft(){
  // Convert BCD to decimal
  second = (second >> 4) * 10 + (second & 0x0F);
  minute = (minute >> 4) * 10 + (minute & 0x0F);
  hour   = (hour >> 4)   * 10 + (hour & 0x0F);
  date   = (date >> 4)   * 10 + (date & 0x0F);
  month  = (month >> 4)  * 10 + (month & 0x0F);
  year   = (year >> 4)   * 10 + (year & 0x0F);
  // End conversion


  Time[7]     = second % 10 + 48;
  Time[6]     = second / 10 + 48;
  Time[4]     = minute % 10 + 48;
  Time[3]     = minute / 10 + 48;
  Time[1]     = hour   % 10 + 48;
  Time[0]     = hour   / 10 + 48;
  Calendar[9] = year   % 10 + 48;
  Calendar[8] = year   / 10 + 48;
  Calendar[4] = month  % 10 + 48;
  Calendar[3] = month  / 10 + 48;
  Calendar[1] = date   % 10 + 48;
  Calendar[0] = date   / 10 + 48;
  if(temperature_msb < 0){
    temperature_msb = abs(temperature_msb);
    temperature[0] = '-';
  }
  else
    temperature[0] = ' ';
  temperature_lsb >>= 6;
  temperature[2] = temperature_msb % 10  + 48;
  temperature[1] = temperature_msb / 10  + 48;
  if(temperature_lsb == 0 || temperature_lsb == 2){
    temperature[5] = '0';
    if(temperature_lsb == 0) temperature[4] = '0';
    else                     temperature[4] = '5';
  }
  if(temperature_lsb == 1 || temperature_lsb == 3){
    temperature[5] = '5';
    if(temperature_lsb == 1) temperature[4] = '2';
    else                     temperature[4] = '7';
  }
//  Temperature (Celcius)
  SetCurrentDate();
  dateC.ToShamsi(current_year,current_month,current_  day); // converts global values of date and stores them to dateC
  char thisString [5] = {dateC.global_year, '/', dateC.global_month, '/', dateC.global_day };


  draw_text(45,  60, thisString , 4);                     // tft the date (format: dd/mm/yyyy)
  draw_text(70, 100, Time, 4);                         // tft the time   
  draw_text(35, 175, "TEMPERATURE =", 2);
  draw_text(185, 175, temperature , 2);
  draw_text(255, 175, "C", 2);// tft the temperature
}


void blink_parameter(){
  byte j = 0;
  while(j < 10 && digitalRead(button1) && digitalRead(button2)){
    j++;
    delay(25);
  }
}


byte edit(byte x_pos, byte y_pos, byte parameter){
  char text[3];
  sprintf(text,"%02u", parameter);
  while(!digitalRead(button1));                      // Wait until button B1 released
  while(true){
    while(!digitalRead(button2)){                    // If button B2 is pressed
      parameter++;
      if(i == 0 && parameter > 31)                   // If date > 31 ==> date = 1
        parameter = 1;
      if(i == 1 && parameter > 12)                   // If month > 12 ==> month = 1
        parameter = 1;
      if(i == 2 && parameter > 99)                   // If year > 99 ==> year = 0
        parameter = 0;
      if(i == 3 && parameter > 23)                   // If hours > 23 ==> hours = 0
        parameter = 0;
      if(i == 4 && parameter > 59)                   // If minutes > 59 ==> minutes = 0
        parameter = 0;
      sprintf(text,"%02u", parameter);
      draw_text(x_pos, y_pos, text, 2);
      delay(200);                                    // Wait 200ms
    }
    draw_text(x_pos, y_pos, "  ", 2);
    blink_parameter();
    draw_text(x_pos, y_pos, text, 2);
    blink_parameter();
    if(!digitalRead(button1)){                       // If button B1 is pressed
      i++;                                           // Increament 'i' for the next parameter
      return parameter;                              // Return parameter value and exit
    }
  }
}
void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
  tft.setCursor(x_pos, y_pos);
  tft.setTextSize(text_size);
  tft.print(text);


}
String SetCurrentDate()
{
  char const *date = __DATE__;
  char s_month[5];
  int _month, _day, _year;
  static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  sscanf(date, "%s %d %d", s_month, &current_day, &current_year);
  current_month = ((strstr(month_names, s_month)-month_names)/3) + 1;
}
void loop() {


  if(!digitalRead(button1)){                         // If button B1 is pressed
    i = 0;
    while(!digitalRead(button1));                    // Wait for button B1 release
    while(true){
      while(!digitalRead(button2)){                  // While button B2 pressed
        day++;                                       // Increment day
        if(day > 7) day = 1;
        tft_day();                               // Call tft_day function
        delay(200);                                  // Wait 200 ms
      }
      draw_text(40, 0, "         ", 1);
      blink_parameter();                             // Call blink_parameter function
      tft_day();                                 // Call tft_day function
      blink_parameter();                             // Call blink_parameter function
      if(!digitalRead(button1))                      // If button B1 is pressed
        break;
    }


    date   = edit(4, 14, date);                      // Edit date
    month  = edit(40, 14, month);                    // Edit month
    year   = edit(100, 14, year);                    // Edit year
    hour   = edit(16, 35, hour);                     // Edit hours
    minute = edit(52, 35, minute);                   // Edit minutes


    // Convert decimal to BCD
    minute = ((minute / 10) << 4) + (minute % 10);
    hour = ((hour / 10) << 4) + (hour % 10);
    date = ((date / 10) << 4) + (date % 10);
    month = ((month / 10) << 4) + (month % 10);
    year = ((year / 10) << 4) + (year % 10);
    // End conversion


    // Write data to DS3231 RTC
    Wire.beginTransmission(0x68);               // Start I2C protocol with DS3231 address
    Wire.write(0);                              // Send register address
    Wire.write(0);                              // Reset sesonds and start oscillator
    Wire.write(minute);                         // Write minute
    Wire.write(hour);                           // Write hour
    Wire.write(day);                            // Write day
    Wire.write(date);                           // Write date
    Wire.write(month);                          // Write month
    Wire.write(year);                           // Write year
    Wire.endTransmission();                     // Stop transmission and release the I2C bus
    delay(200);                                 // Wait 200ms
  }


  Wire.beginTransmission(0x68);                 // Start I2C protocol with DS3231 address
  Wire.write(0);                                // Send register address
  Wire.endTransmission(false);                  // I2C restart
  Wire.requestFrom(0x68, 7);                    // Request 7 bytes from DS3231 and release I2C bus at end of reading
  second = Wire.read();                         // Read seconds from register 0
  minute = Wire.read();                         // Read minuts from register 1
  hour   = Wire.read();                         // Read hour from register 2
  day    = Wire.read();                         // Read day from register 3
  date   = Wire.read();                         // Read date from register 4
  month  = Wire.read();                         // Read month from register 5
  year   = Wire.read();                         // Read year from register 6
  Wire.beginTransmission(0x68);                 // Start I2C protocol with DS3231 address
  Wire.write(0x11);                             // Send register address
  Wire.endTransmission(false);                  // I2C restart
  Wire.requestFrom(0x68, 2);                    // Request 2 bytes from DS3231 and release I2C bus at end of reading
  temperature_msb = Wire.read();                // Read temperature MSB
  temperature_lsb = Wire.read();                // Read temperature LSB


  tft_day();
  DS3231_tft();                             // Diaplay time & calendar


  delay(50);   
   // Wait 50ms 
}
// End of code.

خروجیش هم اینه



دوستان راه حلی  داره بتونم تاریخ شمسی رو نمایش بدم

----------

