Advertisement
phillip_bourdon234

RTC_DS1307_Driver.c

Feb 28th, 2021
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "system_stm32f10x.h"
  3. #include "I2C_Driver.h"
  4. #include "RTC_Driver.h"
  5.  
  6. void rtc_init(void)
  7. {
  8.     i2c_init();
  9. }
  10.  
  11. void rtc_set_time(uint8_t hours, uint8_t minutes, uint8_t seconds)
  12. {
  13.     uint8_t seconds_HEX = 0;
  14.     uint8_t minutes_HEX = 0;
  15.     uint8_t hours_HEX = 0;
  16.    
  17.     //the DS1307 module stores hexadceimal values in its
  18.     //registers; for example, 30 seconds in the
  19.     //DS1307 module is 0x30. However in decimal, this is
  20.     //48. Therefor the code below changes the numbers in decimal
  21.     //to hex before being sent.
  22.     seconds_HEX = ((seconds/10) * 16) + (seconds%10);
  23.     minutes_HEX = ((minutes/10) * 16) + (minutes%10);
  24.     hours_HEX =   ((hours/10) * 16) + (hours%10);  
  25.    
  26.     i2c_write(RTC_ADDRESS, SECOND_REG, seconds_HEX);
  27.     i2c_write(RTC_ADDRESS, MINUTE_REG, minutes_HEX);
  28.     i2c_write(RTC_ADDRESS, HOUR_REG,    hours_HEX);
  29. }  
  30.  
  31. void rtc_set_date(uint8_t month, uint8_t date, uint8_t year)
  32. {
  33.     uint8_t month_HEX = 0;
  34.     uint8_t date_HEX = 0;
  35.     uint8_t year_HEX = 0;
  36.    
  37.     //the DS1307 module stores hexadceimal values in its
  38.     //registers; for example, 30 seconds in the
  39.     //DS1307 module is 0x30. However in decimal, this is
  40.     //48. Therefor the code below changes the numbers in decimal
  41.     //to hex before being sent.
  42.     month_HEX = ((month/10) * 16) + (month%10);
  43.     date_HEX = ((date/10) * 16) + (date%10);
  44.     year_HEX =   ((year/10) * 16) + (year%10); 
  45.    
  46.     i2c_write(RTC_ADDRESS, MONTH_REG, month_HEX);
  47.     i2c_write(RTC_ADDRESS, DATE_REG, date_HEX);
  48.     i2c_write(RTC_ADDRESS, YEAR_REG, year_HEX);
  49. }  
  50.  
  51. uint8_t rtc_get_seconds(void)
  52. {
  53.     uint8_t seconds;
  54.     seconds = i2c_read(RTC_ADDRESS, SECOND_REG);
  55.    
  56.     //the DS1307 module stores hexadceimal values in its
  57.     //registers; for example, 30 seconds in the
  58.     //DS1307 module is 0x30. However in decimal, this is
  59.     //48. Therefor the code below changes the numbers from hex
  60.     //to decimal before being returned.
  61.     seconds = ((seconds >> 4) * 10) + (seconds & 0x0F);
  62.    
  63.     return seconds;
  64. }
  65.  
  66. uint8_t rtc_get_minutes(void)
  67. {
  68.     uint8_t minutes;
  69.     minutes = i2c_read(RTC_ADDRESS, MINUTE_REG);
  70.    
  71.     //the DS1307 module stores hexadceimal values in its
  72.     //registers; for example, 30 seconds in the
  73.     //DS1307 module is 0x30. However in decimal, this is
  74.     //48. Therefor the code below changes the numbers from hex
  75.     //to decimal before being returned.
  76.     minutes = ((minutes >> 4) * 10) + (minutes & 0x0F);
  77.    
  78.     return minutes;
  79. }
  80.  
  81. uint8_t rtc_get_hours(void)
  82. {
  83.     uint8_t hours;
  84.     hours = i2c_read(RTC_ADDRESS, HOUR_REG);
  85.    
  86.     //the DS1307 module stores hexadceimal values in its
  87.     //registers; for example, 30 seconds in the
  88.     //DS1307 module is 0x30. However in decimal, this is
  89.     //48. Therefor the code below changes the numbers from hex
  90.     //to decimal before being returned.
  91.     hours = ((hours >> 4) * 10) + (hours & 0x0F);
  92.    
  93.     return hours;
  94. }
  95.  
  96. uint8_t rtc_get_date(void)
  97. {
  98.     uint8_t date;
  99.     date = i2c_read(RTC_ADDRESS, DATE_REG);
  100.    
  101.     //the DS1307 module stores hexadceimal values in its
  102.     //registers; for example, 30 seconds in the
  103.     //DS1307 module is 0x30. However in decimal, this is
  104.     //48. Therefor the code below changes the numbers from hex
  105.     //to decimal before being returned.
  106.     date = ((date >> 4) * 10) + (date & 0x0F);
  107.    
  108.     return date;
  109. }
  110.  
  111. uint8_t rtc_get_month(void)
  112. {
  113.     uint8_t month;
  114.     month = i2c_read(RTC_ADDRESS, MONTH_REG);
  115.    
  116.     //the DS1307 module stores hexadceimal values in its
  117.     //registers; for example, 30 seconds in the
  118.     //DS1307 module is 0x30. However in decimal, this is
  119.     //48. Therefor the code below changes the numbers from hex
  120.     //to decimal before being returned.
  121.     month = ((month >> 4) * 10) + (month & 0x0F);
  122.    
  123.     return month;
  124. }
  125.  
  126. uint8_t rtc_get_year(void)
  127. {
  128.     uint8_t year;
  129.     year = i2c_read(RTC_ADDRESS, YEAR_REG);
  130.    
  131.     //the DS1307 module stores hexadceimal values in its
  132.     //registers; for example, 30 seconds in the
  133.     //DS1307 module is 0x30. However in decimal, this is
  134.     //48. Therefor the code below changes the numbers from hex
  135.     //to decimal before being returned.
  136.     year = ((year >> 4) * 10) + (year & 0x0F);
  137.    
  138.     return year;
  139. }
  140.  
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement