นาฬิกาแบบไทยๆ

Started by JENG, February 17, 2015, 01:38:46 PM

Previous topic - Next topic

JENG

หากถูกใจช่วยกด like เป็นกำลังใจกันด้วยคับ จะทำ project ง่ายๆมาฝากอีก

ปล.หากมีข้อสงสัยรายละเอียด source code กรุณาถามโดย pm. มานะคับ ถามในกระทุ้ผมขอสงวนสิทธิ์ไม่ตอบนะคับ




/*
Program : numthai rtc clock
Author  : Mr.Somlak Mangnimit
Date    : 16/02/2015
*/

#include <Wire.h>
#define ds1337_id 0x68
#define Seconds  0
#define Minutes  1
#define Hour     2
#define Dow      3
#define Date     4
#define Month    5
#define Year     6
unsigned char rtc_data[7];

#define max7219clkPin 10
#define max7219csPin  9
#define max7219dinPin 8

unsigned char numthai[]=
{
  0x00, 0x7C, 0x82, 0x82, 0x82, 0x82, 0x7C, 0x00,       //0
  0x00, 0x0C, 0x92, 0x92, 0x8A, 0x42, 0x3C, 0x00,       //1
  0x7F, 0x80, 0x9C, 0x92, 0x84, 0x82, 0x7C, 0x00,       //2
  0x7C, 0xA2, 0x46, 0x08, 0x06, 0x82, 0x7C, 0x00,       //3
  0x78, 0x84, 0x82, 0xB2, 0xD2, 0x82, 0x01, 0x00,       //4
  0x78, 0x84, 0x86, 0xB5, 0xD2, 0x82, 0x01, 0x00,       //5
  0x01, 0x42, 0xA4, 0xC2, 0x82, 0x82, 0x7C, 0x00,       //6
  0x7C, 0xA2, 0x4C, 0x02, 0xFC, 0x41, 0x3E, 0x00,       //7
  0x78, 0x84, 0x84, 0x64, 0x84, 0xB2, 0x71, 0x00,       //8
  0x78, 0xC4, 0xA4, 0x4E, 0x12, 0x26, 0x49, 0x00,       //9
 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,       //blank //10
  0x00, 0x00, 0x00, 0x6C, 0x6C, 0x00, 0x00, 0x00,       //colon //11
};

//------------------------------------------------------------
void maxWriteByte(unsigned char data)
//------------------------------------------------------------
{
  unsigned char i = 8;
 
  while(i--)
  {
    if(data & 0x80)digitalWrite(max7219dinPin,HIGH);
    else digitalWrite(max7219dinPin,LOW);
    digitalWrite(max7219clkPin,HIGH);
    digitalWrite(max7219clkPin,LOW);
    data <<= 1;
  }
}

//------------------------------------------------------------
void maxWriteNop(unsigned char cnt)
//------------------------------------------------------------
{
  while(cnt--)
  {
    maxWriteByte(0);
    maxWriteByte(0);
  }
}

//------------------------------------------------------------
void maxWriteOp(unsigned char addr, unsigned char data)
//------------------------------------------------------------
{
  maxWriteByte(addr);
  maxWriteByte(data);
}

//------------------------------------------------------------
void maxWrite(unsigned char dev, unsigned char addr, unsigned char data)
//------------------------------------------------------------
{
  digitalWrite(max7219csPin,LOW);
 
  switch(dev)
  {
    case 1:
    maxWriteNop(4);
    maxWriteOp(addr,data);
    break;
   
    case 2:
    maxWriteNop(3);
    maxWriteOp(addr,data);
    maxWriteNop(1);
    break;
   
    case 3:
    maxWriteNop(2);
    maxWriteOp(addr,data);
    maxWriteNop(2);
    break;
   
    case 4:
    maxWriteNop(1);
    maxWriteOp(addr,data);
    maxWriteNop(3);
    break;
   
    case 5:
    maxWriteOp(addr,data);
    maxWriteNop(4);
    break;
  }
 
  digitalWrite(max7219csPin,HIGH);
}

//------------------------------------------------------------
void writeDigit(unsigned char digit, unsigned int num)
//------------------------------------------------------------
{
  num *= 8;
  for(int i=0; i<8; i++)maxWrite(digit,i+1,numthai[num+i]);
}

//------------------------------------------------------------
void initial_max7219(void)
//------------------------------------------------------------
{
  maxWrite(1,0x09, 0x00); //decode: BCD
  maxWrite(2,0x09, 0x00); //decode: BCD
  maxWrite(3,0x09, 0x00); //decode: BCD
  maxWrite(4,0x09, 0x00); //decode: BCD
  maxWrite(5,0x09, 0x00); //decode: BCD
 
  maxWrite(1,0x0a, 0x03); //Brightness
  maxWrite(2,0x0a, 0x03); //Brightness
  maxWrite(3,0x0a, 0x03); //Brightness
  maxWrite(4,0x0a, 0x03); //Brightness
  maxWrite(5,0x0a, 0x03); //Brightness
 
  maxWrite(1,0x0b, 0x07); //
  maxWrite(2,0x0b, 0x07); //
  maxWrite(3,0x0b, 0x07); //
  maxWrite(4,0x0b, 0x07); //
  maxWrite(5,0x0b, 0x07); //
 
  maxWrite(1,0x0c, 0x01); //
  maxWrite(2,0x0c, 0x01); //
  maxWrite(3,0x0c, 0x01); //
  maxWrite(4,0x0c, 0x01); //
  maxWrite(5,0x0c, 0x01); //
 
  maxWrite(1,0x0f, 0x00); //
  maxWrite(2,0x0f, 0x00); //
  maxWrite(3,0x0f, 0x00); //
  maxWrite(4,0x0f, 0x00); //
  maxWrite(5,0x0f, 0x00); //
}

//------------------------------------------------------------
void rtc_read()
//------------------------------------------------------------
{
  Wire.beginTransmission(ds1337_id);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  Wire.requestFrom(ds1337_id, 7);
  for(int i=0; i<7; i++)rtc_data[i] = Wire.read();
}

//------------------------------------------------------------
void rtc_write()
//------------------------------------------------------------
{
  Wire.beginTransmission(ds1337_id);
  Wire.write((byte)0x00);
  for(int i=0; i<7; i++)Wire.write(rtc_data[i]);
  Wire.endTransmission();
}

//------------------------------------------------------------
void print_digit(unsigned char bin1, unsigned char bin2)
//------------------------------------------------------------
{
  writeDigit(1,bin1>>4);
  writeDigit(2,bin1&0x0f);
  writeDigit(4,bin2>>4);
  writeDigit(5,bin2&0x0f);
}

//------------------------------------------------------------
void setup()
//------------------------------------------------------------
{
  pinMode(max7219clkPin,OUTPUT);
  pinMode(max7219csPin,OUTPUT);
  pinMode(max7219dinPin,OUTPUT);
  delay(50);
  Wire.begin();
  initial_max7219();
 
  rtc_data[Hour]    = 0x23;
  rtc_data[Minutes] = 0x59;
  rtc_data[Seconds] = 0x00;
  rtc_write();
}

//------------------------------------------------------------
void loop()
//------------------------------------------------------------
{
  rtc_read();
  print_digit(rtc_data[Hour], rtc_data[Minutes]);
  if(rtc_data[Seconds]&0x01)writeDigit(3,10);
  else writeDigit(3,11);
  delay(50);
}
สามารถติดตาม electoday ได้ที่

Facebook
www.facebook.com/groups/coopmicro

Google+
https://plus.google.com/communities/103482067769375459277

☺☺☺ความรู้ และความฉลาด ไม่ใช่สิ่งเดียวกัน จะมีประโยชน์อะไร ถ้าฉลาดแต่อยู่ในกะลา☺☺☺


yupetch

ดีจังเลยล่ะท่านJENG
ขอบคุณครับ


electrical55

ดีสวยดีครับชอบมาก

pickkajoo_en


suhanrin

โห... แสดงภาษาไทยได้ด้วย สุดๆ

mackie2005

สุดยอดน่ะคร๊าบ
ขอบคุณสำหรับ Project ดีดี  ;D

suhanrin