Wednesday, June 16, 2010

Code For Major

This is partially finished code. It currently reads the time from the RTC, converts the values from hex into decimal (this makes the logic easier). Those values are then converted to the base value that is selected for display on the screen. The time shown on the screen can also be selected between 24hr time and AM/PM. The time stored on the RTC can also be adjusted using wired buttons by simultaneously holding the time button and pressing the hour or minute buttons (but the changes are not visible until the time button is released).

#include
#include

byte SW0 = 4;
byte SW1 = 5;
byte SW2 = 6;
byte SW3 = 3;
byte SW4 = 2;

LiquidCrystal lcd(7,8,9,10,11,12);

boolean pm = false;
boolean miltime = false;
boolean refresh = false;

int base; //0=dec,1=binary,2=hex,3=octal
int hrs;
int mins;
byte hexhrs;
byte hexmins;
byte hexsecs;
byte hexday;
byte hexdate;
byte hexmonth;
byte hexyear;

void setup()

{
Wire.begin();
Serial.begin(9600);

pinMode(SW0, INPUT); //time
pinMode(SW1, INPUT); //minutes
pinMode(SW2, INPUT); //hours
pinMode(SW3, INPUT); //24hr/AM:PM
pinMode(SW4, INPUT); //change number base

digitalWrite(SW0, HIGH);
digitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
digitalWrite(SW3, HIGH);
digitalWrite(SW4, HIGH);

lcd.begin(16, 2);
}

void loop()
{
retrieve_time();

//convert hex values from rtc into decimal values to make logic easier
hrs = hexhrs/16;
hrs *= 10;
hrs += hexhrs%16;
mins = hexmins/16;
mins *= 10;
mins += hexmins%16;

print_time();

//print_date();
lcd.setCursor(0, 1);


if (refresh == true){
lcd.clear();
refresh = !refresh;
}

if (!(digitalRead(SW0))) set_time(); // hold the switch to set time
if (!(digitalRead(SW3))){
miltime = !miltime;
refresh = true;
}
if (!(digitalRead(SW4))){
++ base;
if (base > 3) base = 0;
refresh = true;
}
delay(1000); //wait a second before next output
}


///////////////////////////////////////////////////////////////////////////////////
void retrieve_time(){
Wire.beginTransmission(0x68);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(0x68, 7);
hexsecs = Wire.receive();
hexmins = Wire.receive();
hexhrs = Wire.receive();
hexday = Wire.receive();
hexdate = Wire.receive();
hexmonth = Wire.receive();
hexyear = Wire.receive();
}

void print_time(){
if (miltime == false){
if (hexhrs != 0 || hexhrs != 18){
pm = false;
if (hrs >= 12) pm = true;

if (hexhrs >= 19 && hexhrs <= 35){
hrs -= 12;
}
else if (hexhrs == 0){
hrs += 12;
pm = false;
}
else
{
pm = true;
}
}
}
lcd.setCursor(0, 0);
switch (base){
case 0:{
lcd.print("DEC ");
if (hrs < 10) lcd.print("0");
lcd.print(hrs,DEC);
lcd.print(":");
if (mins < 10) lcd.print("0");
lcd.print(mins,DEC);
}
break;
case 1:{
lcd.print("B ");
if (miltime == true){
if (hrs <= 15) lcd.print("0");
if (hrs <= 7) lcd.print("0");
if (hrs <= 3) lcd.print("0");
if (hrs <= 1) lcd.print("0");
lcd.print(hrs,BIN);
lcd.print(":");
}
else
{
if (hrs <= 7) lcd.print("0");
if (hrs <= 3) lcd.print("0");
if (hrs <= 1) lcd.print("0");
lcd.print(hrs,BIN);
lcd.print(":");
}
if (mins <= 31) lcd.print("0");
if (mins <= 15) lcd.print("0");
if (mins <= 7) lcd.print("0");
if (mins <= 3) lcd.print("0");
if (mins <= 1) lcd.print("0");
lcd.print(mins,BIN);
}
break;
case 2:{
lcd.print("HEX ");
if (hrs < 16 && miltime == true) lcd.print("0");
lcd.print(hrs,HEX);
lcd.print(":");
if (mins < 16) lcd.print("0");
lcd.print(mins,HEX);
}
break;
case 3:
{
lcd.print("OCT ");
if (hrs < 8) lcd.print("0");
lcd.print(hrs,OCT);
lcd.print(":");
if (mins < 8) lcd.print("0");
lcd.print(mins,OCT);
//lcd.print(":");
//if (secs < 10) lcd.print("0");
//lcd.print(secs,DEC);
}
break;
}
if (miltime == false){
if (pm == true){
lcd.print(" PM");
}
if (pm == false){
lcd.print(" AM");
}
}
}

void print_date(){
if (hexdate < 10) lcd.print("0");
lcd.print(hexdate, HEX);
lcd.print("-");
lcd.print("20");
if (hexmonth < 10) lcd.print("0");
lcd.print(hexmonth,HEX);
lcd.print("-");
if (hexyear < 10) lcd.print("0");
lcd.println(hexyear, HEX);
}

void set_time(){
byte hours = hexhrs;
byte minutes = hexmins;


while (!digitalRead(SW0)) // set time switch must be released to exit
{
while (!digitalRead(SW1)) // set minutes
{
minutes++;
if ((minutes & 0x0f) > 9) minutes = minutes + 6;
if (minutes > 0x59) minutes = 0;
delay(500);

}

while (!digitalRead(SW2)) // set hours
{
hours++;
if ((hours & 0x0f) > 9) hours = hours + 6;
if (hours > 0x23) hours = 0;
delay(500);
}

Wire.beginTransmission(0x68); // activate DS1307
Wire.send(0); // where to begin
Wire.send(0x00); //seconds
Wire.send(minutes); //minutes
Wire.send(0x80 | hours); //hours (24hr time)
Wire.send(0x06); // Day 01-07
Wire.send(0x01); // Date 0-31
Wire.send(0x05); // month 0-12
Wire.send(0x09); // Year 00-99
Wire.send(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7.
Wire.endTransmission();
}
}

No comments:

Post a Comment