Спойлер
Код: Выделить всё
/*
* DS1307.h
* library for Seeed RTC module
*
* Copyright (c) 2013 seeed technology inc.
* Author : FrankieChu
* Create Time : Jan 2013
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __DS1307_H__
#define __DS1307_H__
#include <Arduino.h>
#define DS1307_I2C_ADDRESS 0x68
#define MON 1
#define TUE 2
#define WED 3
#define THU 4
#define FRI 5
#define SAT 6
#define SUN 7
class DS1307
{
private:
uint8_t decToBcd(uint8_t val);
uint8_t bcdToDec(uint8_t val);
public:
void begin();
void startClock(void);
void stopClock(void);
void setTime(void);
void getTime(void);
void fillByHMS(uint8_t _hour, uint8_t _minute, uint8_t _second);
void fillByYMD(uint16_t _year, uint8_t _month, uint8_t _day);
void fillDayOfWeek(uint8_t _dow);
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t dayOfWeek;// day of week, 1 = Monday
uint8_t dayOfMonth;
uint8_t month;
uint16_t year;
};
#endif
Код: Выделить всё
/*
* DS1307.h
* library for Seeed RTC module
*
* Copyright (c) 2013 seeed technology inc.
* Author : FrankieChu
* Create Time : Jan 2013
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <Wire.h>
#include "DS1307.h"
uint8_t DS1307::decToBcd(uint8_t val)
{
return ( (val/10*16) + (val%10) );
}
//Convert binary coded decimal to normal decimal numbers
uint8_t DS1307::bcdToDec(uint8_t val)
{
return ( (val/16*10) + (val%16) );
}
void DS1307::begin()
{
Wire.begin();
}
/*Function: The clock timing will start */
void DS1307::startClock(void) // set the ClockHalt bit low to start the rtc
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00); // Register 0x00 holds the oscillator start/stop bit
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
second = Wire.read() & 0x7f; // save actual seconds and AND sec with bit 7 (sart/stop bit) = clock started
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write((uint8_t)second); // write seconds back and start the clock
Wire.endTransmission();
}
/*Function: The clock timing will stop */
void DS1307::stopClock(void) // set the ClockHalt bit high to stop the rtc
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00); // Register 0x00 holds the oscillator start/stop bit
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
second = Wire.read() | 0x80; // save actual seconds and OR sec with bit 7 (sart/stop bit) = clock stopped
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write((uint8_t)second); // write seconds back and stop the clock
Wire.endTransmission();
}
/****************************************************************/
/*Function: Read time and date from RTC */
void DS1307::getTime()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f);// Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
/*******************************************************************/
/*Frunction: Write the time that includes the date to the RTC chip */
void DS1307::setTime()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write(decToBcd(second));// 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set bit 6
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void DS1307::fillByHMS(uint8_t _hour, uint8_t _minute, uint8_t _second)
{
// assign variables
hour = _hour;
minute = _minute;
second = _second;
}
void DS1307::fillByYMD(uint16_t _year, uint8_t _month, uint8_t _day)
{
year = _year-2000;
month = _month;
dayOfMonth = _day;
}
void DS1307::fillDayOfWeek(uint8_t _dow)
{
dayOfWeek = _dow;
}
Спойлер
только что затухание написал
женьщина пришла
все не получится дальше писать
Код: Выделить всё
// art100 to write 20151218 FadeLed3
#include <Timer.h> //Таймеры я вроде обхожусь
//pins---------------------------
#define PINLED1 3
//consts-------------------------
Timer t;//запускаем функцию таймеров
//----------
void setup(){
pinMode(PINLED1,OUTPUT);
}
//============
void loop(){
int TimerFade1=t.every(14,FadeLed1,(void*)2);//плавное моргание светодиодом
t.update(); // timers чтоб не прерываться удобней было
}
//============
//----------
int fadeAmount1=1;
int brightness1=0;
void FadeLed1(void* context){
brightness1=brightness1+fadeAmount1;
if(brightness1==0 || brightness1==255){ fadeAmount1=-fadeAmount1; }
analogWrite(PINLED1,brightness1);
}
//----------