0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

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

算法&编程学院 来源:网络整理 作者:佚名 2018-02-27 09:05 次阅读

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;
}

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • DS3231
    +关注

    关注

    2

    文章

    51

    浏览量

    23724
  • 时钟芯片
    +关注

    关注

    2

    文章

    225

    浏览量

    39550
收藏 人收藏

    评论

    相关推荐

    STM32F103利用软件模拟I2C读写EEPROM,超过385个字节就读写不了的原因?

    在STM32F103的芯片中,利用软件模拟I2C读写EEPROM,在使用页写入的方式进行读写时,能够读写385个字节,(EEPROM
    发表于 03-19 07:45

    STM32H7使用硬件I2C进行读写操作失败的原因?

    我想使用硬件I2C实现如下操作: 写入设备地址(8位写地址)——写入寄存器地址(不产生STOP)——写入设备地址(8位从地址)——读取多个寄存器 使用F4系列单片机时,可以通过
    发表于 03-11 08:12

    如何在树莓派Pico上编程使用DS1302时钟模块?

    常用的计时时钟芯片DS1302、DS1307、DS3231,各型号还有衍生型号。
    的头像 发表于 11-28 09:16 894次阅读

    基于ATmega128的光伏并网发电系统设计

    电子发烧友网站提供《基于ATmega128的光伏并网发电系统设计.pdf》资料免费下载
    发表于 10-25 10:58 0次下载
    基于<b class='flag-5'>ATmega128</b>的光伏并网发电系统设计

    基于ATmega128和CH374的USB接口设计

    电子发烧友网站提供《基于ATmega128和CH374的USB接口设计.pdf》资料免费下载
    发表于 10-25 10:07 1次下载
    基于<b class='flag-5'>ATmega128</b>和CH374的USB接口设计

    基于Atmega128车祸自动定位报警系统的设计

    电子发烧友网站提供《基于Atmega128车祸自动定位报警系统的设计.pdf》资料免费下载
    发表于 10-10 10:46 0次下载
    基于<b class='flag-5'>Atmega128</b>车祸自动定位报警系统的设计

    基于ATmega128的球管电压控制系统设计与实现

    电子发烧友网站提供《基于ATmega128的球管电压控制系统设计与实现.pdf》资料免费下载
    发表于 10-08 10:46 0次下载
    基于<b class='flag-5'>ATmega128</b>的球管电压控制系统设计与实现

    如何使用NUC240利用I2C控制DS3231

    应用:示例使用 NUC240 来使用 I2C 控制 DS3231 BSP 版本: NUC230/240 Series BSP CMSIS v3.01.001 硬件
    发表于 08-22 06:44

    ATMEGA128微处理器中文数据手册

    ATmega128 是一个很复杂的微处理器,其 I/O 数目为 AVR 指令集所保留的 64 个 I/O 的超 集。为了保持对 ATmega103 的兼容性, ATmega103 的 I/O 位置
    发表于 08-16 18:11 1次下载

    基于 FPGA 的模拟 I2C协议系统设计

    I2C 接口做为不同芯片间的通信协议。I2C 串行总线一般有两根信号线,一根是双向的数据线SDA,另一根是时钟线SCL。所有接到 I2C
    发表于 08-14 18:21

    如何共享Heltec WIFI_Kit_8 (OLED-NodeMcu) 的I2C引脚?

    DS3231 RTC,共享相同的 SDA/SCL 引脚。 这行得通吗,还是会发生资源冲突(因为两个库共享同一个 i2c 连接)?
    发表于 05-31 07:00

    让ESP-12E与PN532 (SPI) 和DS3231 (I2C) 一起使用时遇到的问题求解

    大家好,我在让 ESP-12E 与 PN532 (SPI) 和 DS3231 (I2C) 一起使用时遇到 问题 只有单独的草图。 所以我想通过为 SPI 和 I2C 提供单独的引脚来实现这一点,我
    发表于 05-29 07:46

    CH32V103基础教程37-I2C-软件模拟I2C读写EEPROM

    前面章节第14章已经进行过硬件IIC读写EEPROM的实验,本章教程将使用软件模拟IIC读写EEPROM,并通过串口调试助手将读写结果打印显示。 1、
    发表于 04-23 16:04

    CH32V103基础教程14-硬件I2C读写EEPROM

    本帖最后由 mushenmu 于 2023-4-17 15:46 编辑 本章教程将使用CH32V103的硬件I2C读写EEPROM24C02,并通过串口调试助手将
    发表于 04-17 15:29

    CW32单片机I2C接口读写EEPROM芯片介绍

    :MCU采用页写和顺序读操作时序完成EERPOM的访问。1.I2C读写EEPROM芯片中断函数(I2C分为
    发表于 04-12 10:45