📄 i2ctimer.c
字号:
/*! \file i2ceeprom.c \brief Interface for standard I2C EEPROM memories. */
//*****************************************************************************
//
// File Name : 'i2ceeprom.c'
// Title : Interface for standard I2C EEPROM memories
// Author : Pascal Stang - Copyright (C) 2003
// Created : 2003.04.23
// Revised : 2003.04.23
// Version : 0.1
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
//#include <avr/io.h>
//#include <avr/signal.h>
//#include <avr/interrupt.h>
#include "i2c.h"
#include "i2cTimer.h"
// Standard I2C bit rates are:
// 100KHz for slow speed
// 400KHz for high speed
typedef struct decd
{
unsigned char sec;
unsigned char min;
unsigned char hours;
unsigned char day;
unsigned char Date;
unsigned char Month;
unsigned char yearr;
}tm;
typedef struct bt
{
unsigned Seconds :4; //sec
unsigned Seconds_10 :3;
unsigned ST :1;
unsigned Minutes :4; //min
unsigned Minutes_10 :3;
unsigned bit5 :1;
unsigned Hours :4; //hours
unsigned Hours_10 :2;
unsigned CB :1;
unsigned CEB :1;
unsigned Day :3; //day
unsigned bit11 :5;
unsigned Date :4; //Date
unsigned Date_10 :2;
unsigned bit14 :2;
unsigned Month :4; //Month
unsigned Month_10 :1;
unsigned bit17 :3;
unsigned Years :4; //yearr
unsigned Years_10 :4;
}bits;
union
{
struct decd timdecod;
struct bt timebits;
}times;
struct decd NOW;
// functions
void i2cTimerInit(void)
{
// although there is no code here
// don't forget to initialize the I2C interface itself
}
u08 i2cTimerRead(u08 i2cAddr)
{
u08 packet[8];
// prepare address
packet[0] = 0;
// send memory address we wish to access to the memory chip
i2cMasterSendNI(i2cAddr, 1, packet);
// retrieve the data at this memory address
i2cMasterReceiveNI(i2cAddr, 8, packet);
times.timdecod.sec = packet[0];
times.timdecod.min = packet[1];
times.timdecod.hours = packet[2];
times.timdecod.day = packet[3];
times.timdecod.Date = packet[4];
times.timdecod.Month = packet[5];
times.timdecod.yearr = packet[6];
NOW.sec = (times.timebits.Seconds_10*10)+times.timebits.Seconds;
NOW.min = (times.timebits.Minutes_10*10)+times.timebits.Minutes;
NOW.hours = (times.timebits.Hours_10*10)+times.timebits.Hours;
NOW.Date = (times.timebits.Date_10*10)+times.timebits.Date;
NOW.Month = (times.timebits.Month_10*10)+times.timebits.Month;
NOW.yearr = (times.timebits.Years_10*10)+times.timebits.Years;
// return data
return packet[0];
}
void i2cTimerWrite(u08 i2cAddr)
{
u08 packet[8];
times.timebits.Seconds = NOW.sec % 10;
times.timebits.Seconds_10 = NOW.sec / 10;
times.timebits.ST = 0;
times.timebits.Minutes = NOW.min % 10;
times.timebits.Minutes_10 = NOW.min / 10;
times.timebits.Hours = NOW.hours % 10;
times.timebits.Hours_10 = NOW.hours / 10;
times.timebits.CB = 0;
times.timebits.CEB = 0;
times.timebits.Day = NOW.day % 10;
times.timebits.Date = NOW.Date % 10;
times.timebits.Date_10 = NOW.Date / 10;
times.timebits.Month = NOW.Month % 10;
times.timebits.Month_10 = NOW.Month / 10;
times.timebits.Years = NOW.yearr % 10;
times.timebits.Years_10 = NOW.yearr / 10;
// prepare address + data
//packet[0] = memAddr;
//packet[2] = data;
packet[0] = 0;
packet[1] = 0;
packet[2] = times.timdecod.min;;
packet[3] = times.timdecod.hours;
packet[4] = times.timdecod.day;
packet[5] = times.timdecod.Date;
packet[6] = times.timdecod.Month;
packet[7] = times.timdecod.yearr;
// send memory address we wish to access to the memory chip
// along with the data we wish to write
i2cMasterSendNI(i2cAddr, 8, packet);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -