

/*
Led display program using 16K33 (20 pin package) with 89s52 controller
*/
#include <c:c51incstdio.h>
#include <c:c51increg52.h>
#include <c:c51incintrins.h>

#define uchar unsigned char
#define uint unsigned int

#define SDA     P25
#define SCL     P24

uint    ms10;

uchar   serialout,
        serialout1,
        eepromcounter,

        eaddr;
        
int     stop(),
        start(),        
        outdata(),
        shout(),
        init_driver();

void singecommandshout();

init_driver()            /*Driver initialisation program start here */
{
serialout1 = 0x21;
singecommandshout();        //Turns on internal oscillator  0x21

serialout1 = 0xa2;
singecommandshout();        //ROW/INT Set Reg.
                            //Last time I missed this instruction in initialisation
serialout1 = 0x81;
singecommandshout();        //Display on blinking off       0x81
}

void singecommandshout()
{
start();
serialout = 0xe0;
shout();
serialout = serialout1;
shout();
stop();                                                        
}

int outdata()           /*Driver initialisation program start here */
{
start();                //start command to HT16K33
serialout = 0xe0;       //HT16K33 Selection addr
shout();            
serialout = eaddr;      //display address to where you want to write
shout();                //This addr ranging from 0x00 to 0x0e
                        //Note that only EVEN no. address is valid

serialout = tmp2;       //data to write
shout();            
stop();
}

stop()                  //stop subroutine for HT16K33
{
SDA = 0;
SCL = 1;
_nop_();
SDA = 1;
_nop_();
_nop_();
_nop_();
}

start()                 //start subroutine for HT16K33
{
SDA = 1;
SCL = 1;
_nop_();
SDA = 0;
_nop_();
_nop_();
SCL = 0;
_nop_();
}

int shout()      //Data transfer to HT16K33 subroutine
{
eepromcounter = 0x08;                   //8 bit data transmission so counter loaded with 0x08
while(eepromcounter)                    //while counter does not become 0 out data to 16K33
        {        
        serialout = serialout << 1;
        SDA = CY;                       //move carry on SDA line
        _nop_();
        SCL = 1;                        //give clock pulse
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        SCL = 0;
        --eepromcounter;                //decrement the counter by 1
        }
SDA = 1;                                //make SDA high
_nop_();
_nop_();        
SCL = 1;                                //give clock pulse
_nop_();
_nop_();
SCL = 0;
}

void main(void)
{
SP = 0xa0;

TMOD = 0x11;    //0001 0001     //both timers are initialised to 16 bit mode

TR0 = 1;
TR1 = 1;

TH0 = 0xfc;                     //load timer 0 for 1 ms
TL0 = 0x17;                     

TH1 = 0xec;                     //load timer 1 for 5 ms
TL1 = 0x78;

P0 = 0xff;                      //make all port as output port
P1 = 0xff;
P2 = 0xff;
P3 = 0xff;

eaddr = 0;

P37 = 0;                        //buzzer o/p line not the part of this display program

init_driver();                  //call driver initialisation routine

IE = 0x8a;

for(;;);
}

timecalc() interrupt 1 using 1
{
TH0 = 0xfc;
TL0 = 0x17;

if(eaddr == 0)
        tmp2 = 0x30;    //code for 1
if(eaddr == 0x02)
        tmp2 = 0x6d;    //code for 2
if(eaddr == 0x04)
        tmp2 = 0x75;    //code for 3
if(eaddr == 0x06)
        tmp2 = 0x36;    //code for 4
if(eaddr == 0x08)
        tmp2 = 0x57;    //code for 5
if(eaddr == 0x0a)
        tmp2 = 0x5f;    //code for 6
if(eaddr == 0x0c)
        tmp2 = 0x70;    //code for 7
if(eaddr == 0x0e)
        tmp2 = 0x7f;    //code for 8

outdata();

eaddr = eaddr + 2;
if(eaddr > 15)                
        eaddr = 0;        
}

time() interrupt 3 using 3
{
TH1 = 0xfc;
TL1 = 0x17;
}
