AN #129 - Graphical Clock





  • A graphic LCD, 240*128
  • 1 quartz of 32768 Hz for the timer, only one quartz the internal clock of µP are good enough for the other job
  • 1 Atmega32 (323) or other with timer supporting the 32Khz (Atmega16 ?)
  • 2 buttons, + 1 for reset
  • The program


Difficulties


  • Graphic program
  • Angle calculation using floating point
  • Use of arrays
  • Works on Date Time


How to do


Our ATMEGA programmer card. very simple and the Kanda dongle (not show), µP, 4 ports, 1max232 some R and C, 2 diodes 1 led.



How many I/O ?


  • 8 for LCD data port B
  • 6 to drive the LCD port D
  • 2 for buttons port C
  • 2 for Xtal port C


Port A will be use for analogue signals

What we want to do


A desk clock (3 hands) with full date, adjustable and with automatic setting of date following month. (not for 29th feb).

Why a 32768 Hz xtal ?


I divide this number 15 times :

32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1

Hourra ! It give me a good second

Trigonometry and clock are not compatible?


Trigonometric circle (Whaou ! it's so far.)goes to the opposite way that a clock. The origin is located at 3 o'clock



Sinus and Cosinus goes to -1 to +1 (see figure 1)

We count in radian and we need second, minute, hour, in pixel.

How and why ?


This program is made for demonstrate how is easy to program in Basic-Bascom

Decomposing it:

  • A header where I explain everything you need to do it (in Rem)
  • The compiling orders, library used
  • The statement of variables and constants
  • The statement of sub and functions
  • The statement of Config


This last point is a great news in Basic language. Let us look how to use a graphic LCD

Config Graphlcd = 240 * 128 , Dataport = Portd , Controlport = Portb , Ce = 1 , Cd = 4 , Wr = 0 , Rd = 2 , Reset = 3 , Fs = 7 , Mode = 6

' now you can send data to your graphic LCD as you do in Visual Basic '( I made my program in VB before translate it in Bascom !) 'The numbers following the name of pins graphicLCD are the name of the Port.pin ex: CE=Portb.1 'Mode means the numbers of characters per line in text format.

Config Clock = Soft ' again that's all you need to use the 1second timer !


And after follow the main program:

2 loops imbricates and 8 sub, or function or gosub (see it)

How I resolved the difficulties


Graphic Lcd, well where is the difficulty ? You must use one drived by a T6963C and the size is limited to 240*64 or 240*128.

Ex of text : Locate Y,X : LCD "coucou"

Graph as VisualBasic® Ex : Line (X1, Y1)-(X2,Y2), color 'color means 0 clear 255 write

Take care : Co-ordinates must be bytes or integers not single !

It is possible to insert Bitmap files see the statement SHOWPIC X, Y, label

Floating point, again where are the difficulties ?


Remember to use same type of variables to received a calculation

Remember to make only one operation at each time

Remember to use Round, and Int statement (see the program) before send to GraphLCD

Ex :Sub Anglerad(W As Single) ' sin and cos

W = W * 6    '60*6=360°

Angle_rad = Angle - W

Angle_rad = Angle_rad * Pirad ' in Radian Pirad=3.1415926/180

Sinangle = Sin(angle_rad)

Cosangle = Cos(angle_rad)

End Sub
Use of Arrays


I like use it. For a customer of mine, I did a cells counter where I play with Array(array(array(Variable))) ! I'm very proud ! I translate it in Basic Bascom, I decompose each of them and it works too !

Well here we have 2 arrays for month and day-of-week, very useful for setting

Remember:

1) dim your array

Dim jour(7) as string * 8


A string is a variable using letters and alphanumeric characters. Here the day must be shorter or equal to 8 characters.

Or Dim Temp(24) as integer ( or byte, long, single) for numbers

The number between bracket is the index.

A array regroups variables under a same name. The are called by their index

Index must be shorter than 65535, index 0 is forbidden

Ex : Temp per hour


Dim temp(24) as integer ' les temps can be negative !

Dim J as byte

Temp (1)=10

Temp(2)=15

Temp(3)=portA '

Temp(J)=20 ' J (index) must be lower or equal than 24 here !


Work on DATE and TIME


When the compiler find Config Clock = Soft ; it make the internal variables _day, _month, _year, _sec, _min, _hour available.

So again Too Easy

Use of interrupts


Config clock need the line Enable interrupts, you must know that.

Take care to the End in your program , here it is better to use STOP see the help of these functions and you understand

Download source code in an129.zip