一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

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

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

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

算法&編程學(xué)院 ? 來源:網(wǎng)絡(luò)整理 ? 作者:佚名 ? 2018-02-27 09:05 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

STK600 之 Atmega128硬件I2C 讀寫高精度時鐘芯片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)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • DS3231
    +關(guān)注

    關(guān)注

    2

    文章

    51

    瀏覽量

    24108
  • 時鐘芯片
    +關(guān)注

    關(guān)注

    2

    文章

    271

    瀏覽量

    40808
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

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

    STM32H7CubeMX配置硬件I2C,讀寫失敗是什么問題呀?

    STM32H7CubeMX配置硬件I2C,讀寫失敗什么問題呀,同樣的操作F4和F1都能正常使用,應(yīng)該不存在i2c地址錯誤,操作都是一的,但
    發(fā)表于 06-12 06:21

    I2C通訊的實時時鐘芯片D8563

    功耗極低,有助于延長電池壽命,特別適合用于便攜式設(shè)備或長時間運行的系統(tǒng)。此外,8563芯片還具備鬧鐘與定時器功能,通過I2C通信接口與外部設(shè)備進行通信,具有高速率的特點,使得數(shù)據(jù)交換更加高效。因此,它在智能家居系統(tǒng)中可用于控制設(shè)備
    的頭像 發(fā)表于 06-11 15:11 ?353次閱讀
    <b class='flag-5'>I2C</b>通訊的實時<b class='flag-5'>時鐘</b><b class='flag-5'>芯片</b>D8563

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

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

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

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

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

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

    I2C總線與Arduino的接口示例

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

    I2C總線的工作模式介紹

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

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

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

    I2C總線故障排除技巧

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

    I2C總線與單片機的連接

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

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

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

    全志模塊設(shè)備開發(fā)I2C編程基礎(chǔ)介紹

    )I2C核心層: ?I2C核心提供了I2C總線驅(qū)動和設(shè)備驅(qū)動的注冊、注銷方法,I2C通信方法(algorithm)的上層部分,并且還提供了一系列與具體
    發(fā)表于 12-16 09:45

    怎么通過I2C或SPI讀寫AIC3254的寄存器?

    我看了AIC3254的數(shù)據(jù)手冊,沒有看沒明白怎么通過I2C或SPI讀寫AIC3254的寄存器,求大俠指點
    發(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 ?731次閱讀

    TLV320AIC3263 i2c無法進行通信,通過i2c讀寫函數(shù),讀寫寄存器失敗怎么解決?

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