SKT之AVR硬件I2C的操作函数

分享到:

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

继续阅读
基于ATmega128和CH374的USB接口设计

以ATmega128和CH374为核心,通过SPI串口通讯方式设计了一款USB接口,系统硬件简单,不需要编写复杂的USB驱动程序,利用其动态链接库即可实现USB与PC的通信,研发简单,易于实现。

开源门禁系统设计项目—基于ATmega328

门禁系统设计要求:基于atmega328单片机设计,具有通话、振铃、摘机、通话、开锁功能,还要有键盘和显示电路。

sam4s16c的片内flash读写

最近一直在玩sam4s16c,网上的所有论坛基本找不到atmel的资料,真心的不喜欢,但是工作需要,没办法。只能静下心来仔细研究Datasheet,别无他径。

把握MCU发展轨迹 正确选择MCU完成产品创新

面对缤纷多彩的 MCU 世界,如何在新品设计,老产品更新换代中正确的选择芯片和供应商将是要面临的一个重要的题目,因为无论是产品的更新还是 MCU 的更新速度都远远超出设计者预想,正确把握 MCU 发展趋势,利用 MCU 帮助产品创新是电子设计者正在考虑的问题。

SAM4L8教程之USB复合设备的实现

大家用usb数据线连接开发板的DEBUG USB,打开设备管理器会发现主机识别出有3个设备,这就是usb多功能的复合设备。下面是我探索将MSC,usb mouse和usb keyboard集合于1个设备的教程。

©2019 Microchip Corporation
facebook google plus twitter linkedin youku weibo rss