STK600之Atmega128硬件I2C 读写高精度时钟芯片DS3231函数样例

描述

STK600 之 Atmega128硬件I2C 读写高精度时钟芯片DS3231函数

STK600 用于程序的下载 连接JTAG口至mega128目标板即可
//-----------------------------------------------------------------------------
unsigned char DS3231_DATA[19] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                                                                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};
unsigned char Date_Data[14]   = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                                 0x00,0x00,0x00};
unsigned char Data_temp[6]    = {0x00,0x00,0x00,0x00,0x00,0x00};        
unsigned char Buffer_Data[20] = {0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,
                                 0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,};                                           
                                                           
                                                                                                                                                                       
void DS3221_initial(void)
{
  DS3231_RESETH;
  DS3231_RESETL;
  delay_ms(300);
  DS3231_RESETH;
}                                                                 
                                                                 
void twi_start_state(void)
{
  TWCR = TWCR | 0xA4;
  twi_intcheck();     
}

void twi_stop_state(void)
{
  TWCR = TWCR | 0x94;
  TWCR = TWCR | 0x84;
}


void twi_slaw(unsigned char address)
{  
  address = address << 1;
  TWDR = address;
  TWCR = TWCR & 0xDF;
  TWCR = TWCR | 0x84;
}

void twi_slar(unsigned char address)
{
  address = address << 1;
  address = address | 0x01;
  TWDR = address;
  TWCR = TWCR & 0xDF;
  TWCR = TWCR | 0x84;
}


void twi_wordadd_write(unsigned char address)
{
  TWDR = address;
  TWCR = TWCR & 0xDF;
  TWCR = TWCR | 0x84;
}

void twi_datawrite(unsigned char data)
{
  TWDR = data;
  TWCR = TWCR | 0x84;
}

unsigned char twi_dataread(void)
{   
  unsigned char temp_a;
 
  twi_intcheck();
  temp_a = TWDR;
  return temp_a;
}

void twi_MT(unsigned char sladdress,unsigned char wordaddress,unsigned char *ds3231data,unsigned char datalength)
{
  unsigned char temp_a;
  unsigned char temp_b;  
               
  twi_start_state();      
  twi_intcheck();
  twi_slaw(sladdress);      
  twi_intcheck();   
  twi_wordadd_write(wordaddress);      
  twi_intcheck();

  for(temp_a = 0;temp_a < datalength;temp_a++)
        {
         temp_b = *ds3231data;      
         twi_datawrite(temp_b);      
         ++ds3231data;               
         twi_intcheck();         
        }
  twi_intclear();                        
  twi_stop_state();                 
}

void twi_MR(unsigned char sladdress,
                   unsigned char wordaddress,
                   unsigned char *ds3231data,
                   unsigned char datalength)
{
    unsigned char temp_a;
        
    twi_start_state();
    twi_intcheck();
    twi_slar(sladdress);
    twi_intcheck();
    twi_intclear();        
        
    for(temp_a = 0;temp_a < datalength;temp_a++)
      {
        *ds3231data = twi_dataread();     
         ++ds3231data;   
          twi_intclear();
       }
     twi_stop_state();                  
}

void twi_MTR(unsigned char sladdress,
                    unsigned char wordaddress,
                    unsigned char *ds3231data,
                    unsigned char datalength)
{
    unsigned char temp_a;
        
    twi_start_state();
    twi_intcheck();
    twi_slaw(sladdress);
    twi_intcheck();
    twi_wordadd_write(wordaddress);     
    twi_intcheck();      
    twi_start_state();
    twi_intcheck();
    twi_slar(sladdress);
    twi_intcheck();
    twi_intclear();        
        
     for(temp_a = 0;temp_a < datalength;temp_a++)
           {
            *ds3231data = twi_dataread();
            ++ds3231data;
                if(temp_a < (datalength - 1))
                  {
                   twi_intclear();
                  }
           }
        twi_intcheck();
        TWCR = TWCR & 0xBF;
        twi_stop_state();        
        TWCR = 0x44;
}


void twi_intcheck(void)
{
    unsigned char temp_a;

    temp_a = TWCR & 0x80;
    while(temp_a == 0x00)
       {
        temp_a = TWCR & 0x80;
       }
}


void twi_intclear(void)
{
    TWCR = TWCR | 0x84;
}

void DS3231toDate(unsigned char *ds3231data,unsigned char *Datedata)
{
    unsigned char temp_a;
        for(temp_a = 0;temp_a < 7;temp_a++)
           {
            *Datedata = *ds3231data & 0x0F;
            ++Datedata;
              *Datedata = *ds3231data >> 4;
            ++Datedata;
            ++ds3231data;
           }
        temp_a = 0;
}

void DS3231TD_set(unsigned char year,
                             unsigned char month,
                             unsigned char date,
                             unsigned char day,
                             unsigned char hour,
                             unsigned char minute,
                             unsigned char second,
                             unsigned char time_import,
                             unsigned char *ds3231data)
{
    *ds3231data = second;
    ++ds3231data;
    *ds3231data = minute;
    ++ds3231data;
        
        if(time_import == Time12)
          {
           if(hour > 0x12)
             {
                  hour = hour - 0x12;
                 }
           else;
           *ds3231data = hour | 0x40;
          }
        else
          {
           *ds3231data = hour & 0xBF;
          }
        ++ds3231data;   

    *ds3231data = day;
        ++ds3231data;

    *ds3231data = date;
        ++ds3231data;

    *ds3231data = month;
        ++ds3231data;        

    *ds3231data = year;
                        
        twi_MT(DS3231address,0x00,&DS3231_DATA[0],7);
}

void temp_convert(unsigned char *temp_data,
                  unsigned char *ds3231data)
{
        unsigned char temp_b;
        temp_b = *ds3231data;
        if((temp_b & 0x80) > 0)
          {*temp_data = negative;}
        else
          {*temp_data = positive;}
        temp_b = temp_b << 1;  
        temp_b = temp_b >> 1;
        ++temp_data;
        *temp_data = temp_b / 100;
        ++temp_data;
        *temp_data = (temp_b % 100) / 10;
        ++temp_data;
        *temp_data = (temp_b % 100) % 10;
        ++ds3231data;
        temp_b = *ds3231data;
        temp_b = temp_b >> 6;
        temp_b = temp_b * 25;
        ++temp_data;
        *temp_data = temp_b / 10;
        ++temp_data;
        *temp_data = temp_b % 10;        
}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
评论(0)
发评论
h1654156014.6379 2022-08-23
0 回复 举报
http://house.china.com.cn/sousou/%25B4%25FA%25C0%25ED%25CD%25F8%252C%2B%25C3%25E5%25B1%25B1%25B9%25FB%25B2%25A9B2024.cN_1s.htm http://house.china.com.cn/sousou/%25B9%25D9%25B7%25BD%252C%2B%25CC%25DA%25C1%25FA%25BC%25AF%25CD%25C5%25D3%25E9%25C0%25D6%25B3%25C7B2024.cN%252C%25BC%25C3%25C4%25CF%25B7%25D6%25B9%25AB%25CB%25BE_17s.htm http://house.china.com.cn/sousou/%25D3%25E9%25C0%25D6%252C%2B%25C3%25E5%25B5%25E9%25B9%25FB%25B2%25A9%25B6%25AB%25B7%25BD%25BC%25AF%25CD%25C5B2024.cN_97s.htm http://house.china.com.cn/sousou/%25B9%25D9%25B7%25BD%25CD%25F8%25D5%25BE%252C%2B%25B9%25FB%25B8%25D2%25C1%25FA%25CC%25DA%25B9%25AB%25CB%25BEB2024.cN%252C%25B4%25F3%25CD%25AC%25B7%25D6%25B9%25AB%25CB%25BE_3s.htm http://house.china.com.cn/sousou/%25B9%25D9%25B7%25BD%25CD%25F8%25D5%25BE%252C%2B%25B9%25FB%25B8%25D2%25B2%25FD%25CA%25A2%25BC%25AF%25CD%25C5B2024.cN_79s.htm 收起回复

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分