
#include <c:c51incstdio.h>
#include <c:c51increg52.h>
#include <c:c51incintrins.h>

#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long int

#define CS      P24
#define CLK     P25
#define DATA    P26
#define GRID    P20

uchar   ms100,
        datareg,
        data_buffer1,
        vfdcounter;

uint    ms10;

bit     firsttime;

int     init_vfd(),        
        write_vfd1(),
        data2vfd(),
        display();

init_vfd()
{
CS = 0;

data_buffer1 = 0x05;    //9 digit & 13 segment selection
data2vfd();

CS = 1;
}

write_vfd1()
{
CS = 0;

data_buffer1 = 0x44;    //fix address type
data2vfd();

CS = 1;

_nop_();
_nop_();

CS = 0;

data_buffer1 = 0xc0;   //dispaly address 0
data2vfd();

data_buffer1 = datareg; //actual data to write
data2vfd();

CS = 1;

_nop_();
_nop_();

CS = 0;

data_buffer1 = 0x8f;    //display on & pulsewidth = 14/16
data2vfd();             

CS = 1;
}

data2vfd()
{
vfdcounter = 0x08;
while(vfdcounter != 0)
        {        
        CLK = 0;

        DATA = data_buffer1 & 0x01;

        CLK = 1;

        --vfdcounter;

        data_buffer1 = data_buffer1 >> 1;       //left shift
        }
}                                   

void main(void)
{
SP = 0xa0;

TMOD = 0x01;    //0000 0001

TR0 = 1;

TH0 = 0xec;
TL0 = 0x78;

P0 = 0xff;
P1 = 0xff;
P2 = 0xff;
P3 = 0xff;

init_vfd();

P37 = 0;                        //Not the part of VFD program

firsttime = 1;

IE = 0x82;

for(;;);        
}

timecalc() interrupt 1 using 1
{
TH0 = 0xfc;
TL0 = 0x17;

GRID = 1;
++ms100;
if(ms100 == 15)
        {
        GRID = 0;
        ms100 = 0;
        display();
        }
}

display()
{
if(firsttime)
        {
        datareg = 0x3f; //code for 0
        write_vfd1();
        firsttime = 0;
        }
}

