0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
电子发烧友
开通电子发烧友VIP会员 尊享10大特权
海量资料免费下载
精品直播免费看
优质内容免费畅学
课程9折专享价
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

STK600之Atmega128硬件I2C 讀寫(xiě)高精度時(shí)鐘芯片DS3231函數(shù)樣例

算法&編程學(xué)院 ? 來(lái)源:網(wǎng)絡(luò)整理 ? 作者:佚名 ? 2018-02-27 09:05 ? 次閱讀

STK600 之 Atmega128硬件I2C 讀寫(xiě)高精度時(shí)鐘芯片DS3231函數(shù)

STK600 用于程序的下載 連接JTAG口至mega128目標(biāo)板即可
//-----------------------------------------------------------------------------
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;
}

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • DS3231
    +關(guān)注

    關(guān)注

    2

    文章

    51

    瀏覽量

    24046
  • 時(shí)鐘芯片
    +關(guān)注

    關(guān)注

    2

    文章

    263

    瀏覽量

    40587
收藏 1人收藏

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    tpl0401的I2C讀寫(xiě)應(yīng)該follow什么個(gè)格式?

    tpl0401的I2C讀寫(xiě)應(yīng)該follow什么個(gè)格式。有沒(méi)有具體的寄存器地址。 write: char tmp[2]={0x0,0x0}; tmp[1] = writevalue
    發(fā)表于 02-11 06:24

    I2C總線通信原理 如何設(shè)計(jì)I2C總線電路

    、高效的通信接口標(biāo)準(zhǔn)。I2C總線通信原理主要包括以下要點(diǎn): 總線結(jié)構(gòu) : I2C總線僅使用兩條信號(hào)線,即串行數(shù)據(jù)線(SDA)和串行時(shí)鐘線(SCL)。 SDA線路用于傳輸實(shí)際的數(shù)據(jù)信息,而SCL線路則用于同步通信的
    的頭像 發(fā)表于 01-31 15:01 ?903次閱讀

    I2C總線的優(yōu)缺點(diǎn)分析

    I2C總線作為一種廣泛使用的串行通信協(xié)議,以其簡(jiǎn)單性和高效性在嵌入式系統(tǒng)中占據(jù)著重要地位。 I2C總線的優(yōu)點(diǎn) 1. 簡(jiǎn)單性和低成本 I2C總線只需要兩條線(數(shù)據(jù)線SDA和時(shí)鐘線SCL)
    的頭像 發(fā)表于 01-17 15:50 ?935次閱讀

    I2C總線與Arduino的接口示例

    基礎(chǔ) I2C總線由兩條線組成:數(shù)據(jù)線(SDA)和時(shí)鐘線(SCL)。SDA用于傳輸數(shù)據(jù),而SCL用于同步數(shù)據(jù)傳輸。I2C設(shè)備可以是主設(shè)備或從設(shè)備。主設(shè)備生成時(shí)鐘信號(hào)并啟動(dòng)數(shù)據(jù)傳輸,從設(shè)備
    的頭像 發(fā)表于 01-17 15:34 ?1363次閱讀

    I2C總線的工作模式介紹

    兩根線組成:數(shù)據(jù)線(SDA)和時(shí)鐘線(SCL)。數(shù)據(jù)線負(fù)責(zé)傳輸數(shù)據(jù),而時(shí)鐘線則用于同步數(shù)據(jù)傳輸。I2C總線是全雙工的,意味著數(shù)據(jù)可以在兩個(gè)方向上傳輸。它支持多個(gè)主設(shè)備和從設(shè)備,但在同一時(shí)刻只能有一個(gè)主設(shè)備控制總線。
    的頭像 發(fā)表于 01-17 15:32 ?799次閱讀

    I2C總線協(xié)議詳細(xì)解析

    總線以其簡(jiǎn)單性、靈活性和低功耗而聞名,廣泛應(yīng)用于嵌入式系統(tǒng)中。 2. I2C總線的基本組成 I2C總線由兩條線組成:數(shù)據(jù)線(SDA)和時(shí)鐘線(SCL)。SDA用于傳輸數(shù)據(jù),而SCL用于
    的頭像 發(fā)表于 01-17 15:22 ?741次閱讀

    I2C總線故障排除技巧

    I2C總線是一種廣泛使用的串行通信協(xié)議,它允許多個(gè)設(shè)備在兩條線上(數(shù)據(jù)線SDA和時(shí)鐘線SCL)進(jìn)行通信。由于其簡(jiǎn)單性和靈活性,I2C總線在嵌入式系統(tǒng)中非常流行。然而,當(dāng)I2C總線出現(xiàn)問(wèn)
    的頭像 發(fā)表于 01-17 15:20 ?1944次閱讀

    I2C總線與單片機(jī)的連接

    組成:數(shù)據(jù)線(SDA)和時(shí)鐘線(SCL)。SDA負(fù)責(zé)數(shù)據(jù)的傳輸,而SCL則用于同步數(shù)據(jù)傳輸。I2C協(xié)議支持多主機(jī)和多從機(jī)的通信,每個(gè)設(shè)備都有一個(gè)唯一的地址。通信過(guò)程中,主機(jī)負(fù)責(zé)生成時(shí)鐘信號(hào),從機(jī)則根據(jù)
    的頭像 發(fā)表于 01-17 15:18 ?1006次閱讀

    I2C總線應(yīng)用實(shí)例分析

    。I2C總線使用兩條線進(jìn)行通信:數(shù)據(jù)線(SDA)和時(shí)鐘線(SCL)。這種雙線制設(shè)計(jì)使得I2C總線能夠在多個(gè)設(shè)備之間共享,而不需要為每個(gè)設(shè)備單獨(dú)布線。 應(yīng)用實(shí)例:溫濕度傳感器 假設(shè)我們正在設(shè)計(jì)一個(gè)智能家居系統(tǒng),需要實(shí)時(shí)監(jiān)測(cè)室內(nèi)的溫
    的頭像 發(fā)表于 01-17 15:09 ?626次閱讀

    怎么通過(guò)I2C或SPI讀寫(xiě)AIC3254的寄存器?

    我看了AIC3254的數(shù)據(jù)手冊(cè),沒(méi)有看沒(méi)明白怎么通過(guò)I2C或SPI讀寫(xiě)AIC3254的寄存器,求大俠指點(diǎn)
    發(fā)表于 11-05 08:22

    RISC V的I2C操作

    system_i2c_0_io_scl_writeEnable = !system_i2c_0_io_scl_write;Interface配置示例I2C寄存器設(shè)置在I2C的設(shè)置中第
    的頭像 發(fā)表于 11-01 11:06 ?616次閱讀

    TLV320AIC3263 i2c無(wú)法進(jìn)行通信,通過(guò)i2c讀寫(xiě)函數(shù),讀寫(xiě)寄存器失敗怎么解決?

    麻煩幫忙分析下以下問(wèn)題的原因 1、i2c無(wú)法進(jìn)行通信,通過(guò)i2c讀寫(xiě)函數(shù)(系統(tǒng)提供),讀寫(xiě)寄存器失?。? 寫(xiě)寄存器,返回-5: 讀寄存
    發(fā)表于 10-28 07:32

    TMP117 具有 SMBus? 和I2C兼容接口的高精度、低功耗數(shù)字溫度傳感器數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《TMP117 具有 SMBus? 和I2C兼容接口的高精度、低功耗數(shù)字溫度傳感器數(shù)據(jù)表.pdf》資料免費(fèi)下載
    發(fā)表于 08-12 10:50 ?0次下載
    TMP117 具有 SMBus? 和<b class='flag-5'>I2C</b>兼容接口的<b class='flag-5'>高精度</b>、低功耗數(shù)字溫度傳感器數(shù)據(jù)表

    DS3231 RTC模塊的I2C通信失敗的原因?怎么解決?

    ;< 1)); if (!i2c_master_checkAck()) { os_printf(\"在 i2C 上寫(xiě)入 DS3231 時(shí)出錯(cuò)rn\"
    發(fā)表于 07-11 06:12

    ESP32C3 I2C no ack無(wú)應(yīng)答怎么解決?

    (一)現(xiàn)象 我使用idf開(kāi)發(fā)esp32c3,使用i2c外設(shè)時(shí)出現(xiàn)問(wèn)題。I2C正常發(fā)送了出去,但從機(jī)沒(méi)有應(yīng)答。 我此前使用過(guò)nxp的lpc55s69進(jìn)行過(guò)測(cè)試,并將這兩次的波形使用邏輯分析儀采集出來(lái)
    發(fā)表于 06-14 07:31

    電子發(fā)燒友

    中國(guó)電子工程師最喜歡的網(wǎng)站

    • 2931785位工程師會(huì)員交流學(xué)習(xí)
    • 獲取您個(gè)性化的科技前沿技術(shù)信息
    • 參加活動(dòng)獲取豐厚的禮品