Friday, September 12, 2008

PIC CLOCK FINAL VERSION

As promised in the earlier post, here comes the photos and a video of the PIC CLOCK using the DS1307. The features of the clock are;

1. Displaying the Time, date, month and year
2. Displaying the Temperature
3. Display Time, Date & Month and Temperature Alternatively
4. Battery backup
5. Display illumination reduced at night (From 10pm to 6am)
6. Highly accurate. (DS1307 RTC and temperature compensated crystal DS32kHz)

The following video shows how the clock is in action. It displays Time, date and temperature alternatively. All the parameters (hour, minute, second, date, month, year etc.) can be set by selecting the appropriate mode and increment buttons. Display blinks when entered into the change mode. There are modes to display Time, Seconds, Date , Year and Temperature continuously. Everything is fixed inside a floppy disk box. I'm gonna wrap it with something to hide the internals, may be with a dark sticker.




Some pictures of the clock in different angles;






















Parts List

PIC16F628A microcontroller
DS1307 Real Time Clock IC
DS32kHZ Crystal
MAX7219/21 Display Driver
DS18S20 Temperature Sensor
4 Seven Segment LED Displays
2 AA Battery Holder (3v)
7805 5v regulator
9v ac power pack
IC holders, wire, vero board and lots of patience 


Code snippets I used for the project;

Defining the SPI & I2C connection in PORTB. Display driver MAX7221 uses SPI while DS1307 RTC uses I2C.

void InitMain() {
Soft_Spi_Config(&PORTB, 4, 5, 3);
Soft_I2C_Config(&PORTB, 0, 1);

}//~


Initializing the RTC.

void ds1307_init(){
char seconds=0;

Soft_I2C_Start();
Soft_I2C_Write(0xD0); // WR to RTC
Soft_I2C_Write(0x00); // REG 0
Soft_I2C_Start();
Soft_I2C_Write(0xD1); // RD from RTC
seconds = Bcd2Dec(Soft_I2C_Read(1)& 0x7F); // Read current "seconds" in DS1307
Soft_I2C_Stop();


Delay_us(3);

Soft_I2C_Start();
Soft_I2C_Write(0xD0); // WR to RTC
Soft_I2C_Write(0x00); // REG 0

Soft_I2C_Write(Dec2Bcd(seconds)); // Start oscillator with current "seconds value
Soft_I2C_Start();
Soft_I2C_Write(0xD0); // WR to RTC
Soft_I2C_Write(0x07); // Control Register
Soft_I2C_Write(0x80); // Disable squarewave output pin
Soft_I2C_Stop();
}//~end of function


Initialize MAX7221

void max7219_init1() {
PORTA &= 0xFD; // SELECT MAX
Soft_SPI_write(0x09); // BCD mode for digit decoding
Soft_SPI_write(0xFF);
PORTA |= 2; // DESELECT MAX

PORTA &= 0xFD; // SELECT MAX
Soft_SPI_write(0x0A);
Soft_SPI_write(0x03); // Segment luminosity intensity
PORTA |= 2; // DESELECT MAX

PORTA &= 0xFD; // SELECT MAX
Soft_SPI_write(0x0B);
Soft_SPI_write(0x03); // Display refresh
PORTA |= 2; // DESELECT MAX

PORTA &= 0xFD; // SELECT MAX
Soft_SPI_write(0x0C);
Soft_SPI_write(0x01); // Turn on the display
PORTA |= 2; // DESELECT MAX

PORTA &= 0xFD; // SELECT MAX
Soft_SPI_write(0x00);
Soft_SPI_write(0xFF); // No test
PORTA |= 2; // DESELECT MAX
}


Display the values using MAX7221

void Write_7219(int x, int y) {

PORTA &= 0xFD;
Soft_SPI_write(x); // send i to max7219 (digit place)
Soft_SPI_write(y);

PORTA |=2;

}


Reading & writing to the RTC

unsigned short read_ds1307(unsigned short address)
{
Soft_I2C_Start();
Soft_I2C_Write(0xd0);
Soft_I2C_Write(address);
Soft_I2C_Start();
Soft_I2C_Write(0xd1);
data=BCD2DEC(Soft_I2C_Read(0));
Soft_I2C_Stop();
return(data);
}

void Write_DS1307(unsigned short Address, unsigned short _Data) {

Soft_I2C_Start(); // start a serial transfer
Soft_I2C_Write(0xD0); // which device, 1101000 = DS1307, + direction bit (R=1,W=0)
// thus 11010000 = 0xD0 ie write to DS1307
// need to tell the DS1307 which address is to be written to
Soft_I2C_Write(Address); // send the address to be written to the DS1307
Soft_I2C_Write(_Data); // send actual data to be written into Address on the DS1307
Soft_I2C_Stop(); // finished serial transfer (and release the I2C bus)
}


Read Temperature (DS18S20) & Display

void Read_temp()
{
ow_reset(&PORTB,2); // onewire reset signal
ow_write(&PORTB,2,0xCC); // issue command to DS1820
ow_write(&PORTB,2,0x44) ;

delay_us(120);

ow_reset(&PORTB,2);
ow_write(&PORTB,2,0xCC); // issue command to DS1820
ow_write(&PORTB,2,0xBE);

delay_ms(500);

j1 = ow_read(&PORTB,2); // get result
_Decimal = j1 & 0x01 ;
j1 = j1 >> 1 ;

}

void display_temp()
{
Read_temp();
j = j1%10;
j = j+0x80;
Write_7219(2,j);
j = j1/10 ;
Write_7219(1,j);

if (_decimal==0)
Write_7219(3,0x00);
else
Write_7219(3,0x05);

Write_7219(4,0x0F);



}


To display time, date and to edit the parameters, I wrote separate modules using the above basic building blocks. If somebody needs the schematic and the complete source / hex, drop me an email or leave a comment with the email address. I used Mikroelektronika forum (http://www.mikroe.com/forum/) heavily and almost all the above coding segments are taken from the forum. Therefore, full credit goes to all the people who contributed to the forum.

Wednesday, August 27, 2008

PIC Clock - with DS1307

After so many sleepless nights I managed to run a clock using a PIC, this time using the Maxim RTC chip, DS1307. This time I didn't use multiplexing to drive the four seven segments, instead I used another Maxim product, MAX7221 display driver. It has SPI interface and can drive upto 8 seven segments with display brightness control and that is cool... Since I'm using the PIC16F628 at the moment, I had to use Sotware SPI to drive the display. It worked without giving much problems but later came the most difficult part, DS1307. Oh boy, what a mess... It gave me thousand and one problems and took nearly a month to get it worked. But truly speaking I leaned a lot and mastered the DS1307. This fellow is really great and extremely easy to communicate via the I2C interface. I had problems because I used Software I2C routines in MikroC. I think SOFT_I2C has a problem with PORTA and after so many trial and error attempts it worked when I moved the DS1307 to PORTB. Now both DS1307 (I2C) and MAX7221 (SPI) are in PORTB and work without any trouble. DS1307 needs an external crystal (32.768kHz) to operate and again Maxim has another great product DS32KHZ, a temperature compensated 32.768kHz crystal which I found extremely accurate. So far the clock is running smoothly and accurately, which needs lot of other things, a way to set time, view calendar, set alarm etc. And I'm thinking of attaching a Temperature sensor too. Pictures, videos, schematic and code segments will appear shortly.

Wednesday, July 30, 2008

LED (OIL) LAMP

This time I am using another PIC, 16F628, which is better than 16F84 in number of ways. It has internal Oscillator, more IO pins, PWM, more space (twice) etc. As my first program with 16F628, I wrote this quick and dirty code segment to simulate an oil lamp. It uses the built in PWM to change the duty cycle of the LED.

Here is the video



Here is the code in MikroC

unsigned short j, oj,i;

void InitMain() {


PORTB = 0x0; // set PORTB to 0
TRISB = 0; // designate PORTBpins as output
PWM_Init(5000); // initialize PWM module
}//~

void main() {
CMCON=7;

initMain();

j = 127; // initial value for j
oj = 0; // oj will keep the 'old j' value
PWM_Start(); // start PWM

while (1) { // endless loop

j=rand(); // Generate Random Number
if (j>=255)
j=254;

if (j==0)
j=1;

if (j>oj)
{
for (i=oj;i<=j;i++)
PWM_Change_Duty(i); //Smoothen the level change
}
else
if (j {
for (i=oj;i>=j;i--)
PWM_Change_Duty(i);
}
else
PWM_Change_Duty(j); // set new duty ratio,

oj = j; // memorize it
PORTB = oj; // and display on PORTB

Vdelay_ms(200+j); // slow down change pace a little
}
}//~!

PIC Clock - Final Version

My PIC clock is almost done. Only thing remaining is that to give it a nice look. (at the moment, it is inside an ice cream box and it deserves a better place than that). It has following functionalities;
• 12H/24H mode; when in 12H mode, a PM indicator is used.
• Battery backup; when the mains power is not available, it automatically gets power from the batteries and keeps the clock ticking.
• Ability to work totally on batteries. i.e. When the mains power is not available, it automatically switches into battery mode and the display goes blank. But if you want to see the time, you can get back the normal display manually.

Here are some pics

24H mode

12H mode

Inside view





(Video quality is not that great but clear enough to see that the clock is running :-) )

Thursday, June 26, 2008

Convocation

Yesterday, I got my MBA in IT at the General Convocation of University of Moratuwa. I started my MBA early 2006 and continued it through 2006 and 2007, occupying every single free time slot. Still I cant believe that I completed it having 3.76 GPA and 'A' pass for the Research Project. I had the opportunity to publish and present a paper at International Conference of Business Management held at University of Jayawardanapura (ICBM 2008). I was thrilled to see most of my colleagues got their MBA titles yesterday, but there were several who aren't that fortunate. I missed the accompany of my beloved wife (she too graduated in the convocation) since she was out of the country. She went to present a paper in an IEEE conference in Canada.

Friday, May 23, 2008

PIC Clock

Finally I managed to build the initial version of the Desktop clock using PIC16F84. Still its in the vero board as you see in below video.



It shows Time (Hour, Minute) and Seconds alternatively. It has two buttons to advance hours and minutes. An Interrupt routine is used to calculate the base time and to multiplex the four seven segments. The program is written in C and compiled using MikroC compiler. I got lot of ideas from so many people and used code samples from various resources. Specially the MikroC comes with very useful code samples. Here is the program listing. It is not optimized yet. You'll find unused variables here and there. Comments and suggestions are welcome.


#include "Display_utils.h"

char kraj;
unsigned short zz, v, j, countsec,displaysec;
//unsigned int i;
unsigned short por[4];

unsigned char sec, mins, hrs, ticks, correction;
char oldstate_min, oldstate_hr;


void interrupt() {

if(INTCON.T0IF)
{
ticks++;
correction++;
TMR0=101; //96; //102

if (correction==180) // Correction routine
{
ticks--;
correction=0;
}
}



PORTA = ~0;
PORTB = por[v];
PORTA = ~zz; // turn on appropriate 7seg. display
zz <<= 1;
if (zz > 8u)
zz = 1; // prepare mask for digit

v++ ;
if (v > 3u)
v = 0; // turn on 1st, turn off 2nd 7seg.
//Delay_ms(1);
//TMR0 = 0;
INTCON = 0x20;
}//~

void main() {
OPTION_REG = 0x84;
j = 0;
v = 0;
zz = 1;
TMR0 = 101;
INTCON = 0xA0; // Disable PEIE,INTE,RBIE,T0IE
PORTA = ~0;
TRISA = 0;
PORTB = 0;
TRISB = 0;
sec=0;
mins=0;
ticks=0;
correction=0;
hrs=0;
oldstate_min=0;
oldstate_hr=0;


do {

if (ticks==200)
{
sec++;
countsec++;
ticks=0;

//if (PORTB.F7==1)
// {
// PORTB.F7=1;
// }
// else
// {
// PORTB.F7=0;
// }
//}
if (sec>59)
{
mins++;
sec=0;

//}

if (mins>59)
{
hrs++;
mins=0;
//}


if (hrs>23)
{
hrs=0;
}

}
}
}
TRISB=0b00000011;
if (Button(&PORTB, 0, 1, 1))
oldstate_min = 1;
if (oldstate_min && Button(&PORTB, 0, 1, 0)) {
mins++;
if (mins>59)
mins=0;
oldstate_min = 0;
}
if (Button(&PORTB, 1, 1, 1))
oldstate_hr = 1;
if (oldstate_hr && Button(&PORTB, 1, 1, 0)) {
hrs++;
if (hrs>23)
hrs=0;
oldstate_hr = 0;
}

TRISB=0b00000000;



if (countsec==5)
{
countsec=0;
if (displaysec==1)
{
displaysec=0;
}
else
{
displaysec=1;
}
}


if (displaysec==0)
{
por[0]=mask(hrs/10u);
j=(char)(hrs%10u);
por[1]=mask(j);

por[3]=mask(mins/10u);
j=(char)(mins%10u);
por[2]=mask(j);
}
else
{
por[0]=mask(5);
//j=(char)(mins%10u);
por[1]=mask(11);

por[3]=mask(sec/10u);
j=(char)(sec%10u);
por[2]=mask(j);
}


} while(1); // endless loop
}//~




Display_utils.h file


//-------------- Returns mask for common cathode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
case 11 : return 0x79;
} //case end
}//~

Thursday, February 21, 2008

PIC Programming

Programming a micro controller has been in my mind for nearly a decade. I have done micro controller programming when I was an under graduate. That was for the 8080 processor. It is too big for small projects. I was looking for a suitable type of micro since there are couple of good ones PICs and AVRs. But I couldn't find any AVRs in the market but PICs are available for an affordable price. I bought two PICs, 16F877A and 16F84A, for Rs. 460/- and Rs. 320/- respectively.

The first step was to build a programmer (burner). I saw different types of programmers but thought of building a JDM type one. It has the advantage of operating without an external power supply and is much simpler to build. It takes power from the PC serial port. The disadvantage is, modern laptops cannot provide the required voltage level. Since most of the laptops (including mine) do not have a serial port, it has no effect.

Found the following circuit and built it on a vero board. It can program PICs upto 40 pins so there wont be any problem for a long time. (Full credit goes to the designer of the circuit, I just build his circuit)



This is how the final one looks like;



Then needed the software to connect the hardware to the PC. After reading many comments, I wanted to settle with IC-Prog. It is a great software which supports many types of programmers including the JDM. It also has a hardware test option to verify the hardware. Initially it didn't verify my hardware correctly and after so many sleepless nights, I found the bug. I have connected the Serial port incorrectly; more precisely, I have swapped the left hand side and the right hand side of the port. After rearranging, it worked like a charm.

I tested the programmer and the PIC using the famous "hello world" LED blinking program which ended quite successfully. Then I wrote a counting program using two seven segment LEDs with 16F84. Two segments are in multiplexed mode. The following video shows it in action.



These are my future plans with PICs.

- Build a clock with small digits (desktop clock)
- Build a wall clock with a big display
- Include the stealth mode to the clocks. i.e. it will retain its time when there is no mains power. I hope to use a real time clock IC like DS1307 or DS1302 with a battery backup.
- Include a thermometer function to the clock to display the room temperature. For this, I will use DS1820 IC.

Although I already have necessary hardware i.e PICs, clock ICs, temperature ICs (I got them from Dallas Semiconductors as samples), writing software takes a lot of time. Therefore it is impossible to figure out how long it will take to complete the above things. May be before next Christmas... :-)

Thursday, November 08, 2007

Hi...

Well, it's more than 6 months after my last post. Few things kept me away from blogging;
MBA - which is almost finished now
Office work - few interesting projects occupied me
Laziness - Oh.. never ending

Hmmm.. from where should I begin...

I'll start from the personal things...
My kid... now he's one year old... can walk without being assisted. But somebody has to follow him all the time b'cos he wants to explore everything, everywhere. :-)



I changed my car... Toyota Carina Si MyRoad



A more spacious, comfortable ride than before. But not so good for the pocket...

Thursday, April 19, 2007

Michael Dell uses Ubuntu

Look at Michael Dell's biography here
He uses Ubuntu 7.04 in his home laptop. Must be a beta version since the final version is due this week. (I'm waiting for it to install in my acer notebook)

Wednesday, April 18, 2007

Cricket Humor

I saw the following video sometime back at IESL in the time of our MBA get-together. I found it very hilarious and what a guy Vickram is...

Mihiri has an equally good one here



Tuesday, March 20, 2007

Acer Aspire



My Notebook arrived yesterday. It's Acer Apire 5630 series Core 2 Duo 1.66 GHz (T5500 processor) , 15.4" Wide Screen, 1GB RAM, 160 GB HD, WiFi, 1.3 MP WebCam, 5-in-1 card reader etc. Not that flashy with compared to the notebook I dreamed from Apple (macbook). My poor cash flow prevented me from going for it. This one was $1025 (roughly SLR 115,000) and bought from Amazon. Still couldn't play around with it that much and haven't tested WiFi, LAN and Modem yet. There's a bluetooth button but specs don't say anything about it.

Today I saw the same one with 2GB RAM and nVidia GO 7000 for a lesser price ($999) in amazon. Sigh...

Tuesday, February 27, 2007

Mevinu's pics update


"Am I good for a desktop background?"


"Hey, I can roll now..."


"Hi grand ma, Hi grand pa"

Tech-GSM, two thumbs up...!

I am learning E-commerce as a subject in my MBA and our lecturer most of the times mentions how electronic commerce can be applied in Sri Lankan situation. Here is a good example. I found Tech-GSM web site, a promising company entered into the electronic commerce arena, selling memory related items, and doing it successfully in the local context. They don't have many products listed in their site, most of them are pendrives, SD cards, DIMMS etc. When you want to buy, an E-mail is generated and they'll contact you via your contact details via E-mail or phone. When the order is confirmed, they'll deliver it within Colombo city limits free of charge and you can pay for it at the time of delivery.
I bought a Kingston Data traveler II 2GB pendrive for Rs 3850/- which I found very competitive pricing even when compared with the price given in Amazon.com (only Rs.150 difference). I chose DT II merely because of the data R/W speed which is mentioned as 7 MB/s write and 11 MB/s read. The maximum waiting time to write the entire capacity is less than 5 minutes. (cool...)
To my surprise, they delivered it in the same day I confirmed the order. And the product is Original Kingston which I verified via the Kingston website. Two thumbs up for Tech-GSM guys...

Wednesday, December 27, 2006

SMS Calendar Events

Google Calendar offers free SMS notification service to your mobile phone about the events you save as calendar events. This facility is currently available for Dialog & Celltel connections only. Mmm... neat facility if you have a volatile memory. But there are limitations, only 20 notifications per day and 150 per month.

Wednesday, December 20, 2006

Pics update...



My kid is 3 months old. His name is 'Mevinu Dinsath'. Now he recognizes me and happy whenever I'm around. Most of the days he sees me only in the morning and I think he knows it. So he wakes up early in the morning and he wants me to speak with him. Sometimes I fell sleep while talking to him but he kicks me or shouts loudly to wake me up.
My MBA exam for the 3rd semester is going to start on 30th December and I'm thinking of stay at home for 3-4 days to study. But don't know how long I can do it with the little one.



Mevinu with his mother




with his grandma

Tuesday, October 17, 2006

New Look

I transfered my blog to blogger beta... Mmmm... Looks nice to me...

Life Upgrade!


I rarely post about myself and this is no exception. I became a father on September 18. The little one is a boy and the above picture was taken after 2 hours of birth. (more photos will come soon...)
My friends and colleagues ask how's the life now? My answer is now I feel that I'm living :-) Well, there is no need to mention that I'm busy. Didn't sleep continuously more than two hours since the little one wakes up every two hours. It was very hard during the first two weeks but now I'm used to it. To make the things worse, MBA work load in this semester is like hell. So much of work compared to the previous semester. That's why I couldn't post a single blog during the last three months. I couldn't see any difference in the future also.

Thursday, July 20, 2006

mmmm....

My Office PC is dual boot; having Windows 2003 server and Ubuntu 6.06. I have been in windows for a long time therefore all of my work related files, developments and mail are in windows partitions (ntfs). Ever since I changed to Ubuntu, I was searching ways & means of full access to ntfs partitions. It only gave me read-only access. But today I found this great link on how to get access to ntfs partitions under linux. I immediately implemeted it but due to some reason, it couldn't mount both partitions instead it mounted only one. At least, I have one partition which I have full access.

Well, what more I need? EMail. Yes, I love OutLook. It's a great program. Couldn't believe its from M$. I'm using Thunderbird from mozilla in Linux. The pain is, I cannot use two mail clients in different OSs. Its not only wasting my precious space but also finding a particular mail drives me to nuts. Sometimes I cannot remember which mail is in which mail client. But the beauty of a cross platform mail client like Thunderbird is, it works in both OSs. I found this great article on how to configure Thunderbird to have a single mailbox (profile) no matter in which OS you work. Now I've lost one reason to goto Windows.

Friday, July 14, 2006

wiki

I thought of starting a corporate knowledge base in our company to share views and thoughts of employees mainly on technical topics. I had previous experience of wiki based engines since 'trac' uses a wiki engine to maintain its knowledge base. I googled for wiki engines and came across so many but only two of them impressed me, Twiki & MediaWiki. After installing both, MediaWiki came to the top of the list because of the features it has and the easiness to install. Now it is up and running successfuly. Later I came to konow that wikipedia, the best knowledge base in the world, is also running using MediaWiki. Cool...

Friday, June 30, 2006

The Move

Now I am on the verge of moving to Ubuntu from Windows. I spend 90% of my time on Ubuntu in my dual boot system. Still I'm testing the OS and so far it has being great. I can do most of the things I did in windows without any problem and sometimes better than in windows. Browse using FireFox, Email via Thunderbird, Chat using Gaim, Documentation using OpenOffice etc. And I managed to get most of the third party software to burn CDs, media players, mp3 players and popular codecs to watch dvds and divx movies. Printing, File sharing, SSH, connecting to Remote Desktops and vmware clients are also possible without any glitch. What I missed was .Net & Delphi. I don't do much development work these days and I keep my windows partition for that type of work.

Two days back, I received Ubuntu & Kubuntu CDs which I ordered from Ubuntu web site. They are shipping 3 types of Ubuntu variants for the lates release 6.06 Dapper Drake.
1. Ubuntu - The normal distribution wiht Gnome desktop
2. Kubuntu - Ubuntu with KDE (K-Desktop Environment)
3. Edubuntu - Ubuntu for the entire Family - This has lot of educational programmes for kids.

(I forgot to order Edubuntu but I managed to download it from the site.)

P.S.
I downloaded Ubuntu from a mirror site even before they officially release it :-) I did a kernel upgrade to suit my PIV Hyper Threading PC. Now it recognises two CPUs. Actually there aren't two CPUs but Hyper Threading simulates two CPUs. I installed a customised version of Firefox called SwiftFox which is optimised for PIVs. It loads and runs faster than the normal FireFox.