/*  Filename     ZDS2430A.C
    Description  TEST DS2430A EEPROM
    Hardware	 SLAB-51
    Clock        11.0592 Mhz
    Compiler     Keil PK51 v7.10
    Engineer     Kriangsak B.
    Company      Sila Research Co.,Ltd. 
*/

#include <reg52.h>
#include <absacc.h>
#include <assert.h>
#include <ctype.h>
#include <intrins.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/********** I/O PORT **********/

sbit     LED   = P1^0;
sbit     TMDAT = P1^1;
sbit     KEY1  = P3^3;

/********** INT-RAM WORKING AREA **********/

unsigned char MEMBUF[32];

/********** BASIC FUNCTION **********/

void dmsec (unsigned int count) {           // mSec Delay
    unsigned int i;                         // for Keil CA51 (Speed x 1)
    while (count) {
        i = 115; while (i>0) i--;
        count--;
    }
}

void tmreset (void) {                  // Reset TX (Speed x1)
    unsigned int i;
    TMDAT = 0;
    i = 103; while (i>0) i--;          // Approx 900 uS 
    TMDAT = 1;
    i = 5; while (i>0) i--;
}

bit tmpre (void) {                     // Wait for Presence RX (Speed x1)
    unsigned char i;                   // return 0=OK 1=ERROR 
    unsigned int a;
    a = 0;
    while (TMDAT)
        if (a++>2000) return (1);
    while (~TMDAT);
    i = 5; while (i>0) i--;
    return (0);
}

bit tmrbit (void) {                    // read one bit (Speed x1)
    unsigned int i;
    bit dat;
    TMDAT = 0; i++;
    TMDAT = 1; i++; i++;
    dat = TMDAT;
    i = 9; while (i>0) i--;            // Approx 65 uS 
    return (dat);
}

unsigned char tmrbyte (void) {         // read one byte (Speed x1 x2)
    unsigned char i,j,dat;
    dat = 0;
    for (i=1;i<=8;i++) {
        j = tmrbit();
        dat = (j << 7) | (dat >> 1);
    }
    return (dat);
}

void tmwbyte (unsigned char dat) {     // write one byte (Speed x1)
    unsigned int i;
    unsigned char j;
    for (j=1;j<=8;j++) {
        if ((dat & 0x01)==1) {
            TMDAT = 0;                 // Write 1 
            i++; i++;                  // Approx 4 uS 
            TMDAT = 1;
            i = 9; while (i>0) i--;    // Approx 65 uS 
        }
        else {
            TMDAT = 0;                 // Write 0 
            i = 9; while (i>0) i--;    // Approx 65 uS 
            TMDAT = 1;
            i++; i++;                  // Approx 4 uS 
        }
        dat = dat >> 1;
    }
}

/*
void tmrrom (void) {                   // read ROM ID.
    unsigned char i;
    tmreset (); tmpre (); dmsec (1);
    tmwbyte (0x33);
    for (i=0;i<=7;i++)
        MEMBUF[i] = tmrbyte ();
}
*/

void tmreep (void) {                   // read 32 BYTE eeprom (DS2430A)
    unsigned char i;
    tmreset (); tmpre (); dmsec (1);
    tmwbyte (0xcc);                    // skip rom 
    tmwbyte (0xf0);                    // read memory
    tmwbyte (0);                       // address 
    for (i=0;i<=31;i++)
        MEMBUF[i] = tmrbyte ();
}

void tmweep (void) {                   // write 32 BYTE eeprom (DS2430A)
    unsigned char i;
    tmreset (); tmpre (); dmsec (1);
    tmwbyte (0xcc);                    // skip rom 
    tmwbyte (0x0f);                    // write scratchpad 
    tmwbyte (0);                       // address 
    for (i=0;i<=31;i++)
        tmwbyte (MEMBUF[i]);
    tmreset (); tmpre (); dmsec (1);
    tmwbyte (0xcc);                    // skip rom 
    tmwbyte (0x55);                    // copy scratchpad 
    tmwbyte (0xa5);
}

/********** START FUNCTION **********/

void start (void) {               // speed x 1
    SCON = 0x52;             	  // set RS232 parameter
    TMOD = 0x20;
    TH1 = 0xfd; PCON |= 0x80;     // 19200
    TR1  = 1;

	dmsec (500);
    IT0 = 1;                      // int0 falling edge
    EX0 = 1;                      // enable int0
    EA = 1;
}

void powerdown (void) interrupt 0 {
    LED = 0;
	tmweep ();
    LED = 1;
	while (1);                    // hang wait for power up or watch-dog reset  
}

/********** MAIN **********/

void main (void) {
    unsigned char a,i;
	unsigned int c;
	bit f;

    start ();                          // start  
    tmreep ();                         // read data from ds2430a
    c = MEMBUF[0];		               // display counter
	c = (c << 8) | MEMBUF[1];
	printf ("Counter %d ... ",c);
	f = 0;
	a = c;
    for (i=2;i<=31;i++) {              // display data & check
		printf ("%02bx",MEMBUF[i]);
	    if (MEMBUF[i]!=a++) f = 1;
		MEMBUF[i] = a;
	}
	if (f) printf (" Error\n");        // status
	else printf (" OK\n");
	c++;
	MEMBUF[0] = c >> 8;
	MEMBUF[1] = c;

    while (1) {                        // reset counter 
	    if (KEY1==0) {
		    printf ("Reset !\n"); 
			MEMBUF[0] = 0;
			MEMBUF[1] = 0;
			a = 0;
			for (i=2;i<=31;i++) MEMBUF[i] = a++;
			while (KEY1==0);
			dmsec (100);
		}
    }
}


