/*****************************************************/
/*
NHD-12232AZ.c
Program for writing to Newhaven Display 122x32 AZ series Graphic LCD

(c)2008 Curt Lagerstam - Newhaven Display International, LLC. 

 	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
*/
/*****************************************************/

#include <msp430xG46x.h>
#include "pic.h"

/*#define A0  P2^0
#define R_W  P2^7
#define E  P2^6 
#define CS1  P2^2       // Active low
#define CS2  P2^3       // Active low
#define RST  P2^1*/     // Active low

void delay(unsigned int n);
void init();
void Comleft(char i);
void Comright(char i);
void Writeright(char i);
void Writeleft(char i);
void graphic(unsigned char *img);
void bothSides(char i); 
/*****************************************/
void delay(unsigned int n)
{
	unsigned int i,j;
	for (i=0;i<n;i++)
  		for (j=0;j<350;j++)
  			{;}
}
/*****************************************/
void Comleft(char i)
{
 P6OUT =i;
 P2OUT = 0x40;           //R_W = 0  A0 = 0 , E = 1 CS1=0
 delay(2);
 P2OUT = 0x04;            //CS1 = 1 E=0;
}

void Comright(char i)
{
 P6OUT = i;
 P2OUT = 0x40;             //R_W = 0 A0 = 0 E = 1 CS2=0;
 delay(2);
 P2OUT = 0x08;             // CS2=1 E=0
}

void Writeleft(char i)
{
 P6OUT = i;
 P2OUT = 0x41;             //R_W = 0  A0 = 1  E = 1 CS1=0;
 delay(2);
 P2OUT = 0x04;              //CS1 = 1 E =0;
}

void Writeright(char i)
{
 P6OUT = i;
 P2OUT = 0x41;                //R_W = 0  A0 = 1  E = 1 CS2=0;
 delay(2);
 P2OUT = 0x08;                //CS2= 1 E=0;
}
/*****************************************/
void bothSides(char i)
{
 Comleft(i);
 Comright(i);
}
/*****************************************/

void init()
{
 P6DIR = 0xFF;
 P2SEL = 0xF1;
 P2DIR = 0xF1;
 //FLL_CTL1 = 0x03;                        // crystal input is selected and ACLK divider is 8;
 UCB0CTL1 = UCSSEL_2 + UCSWRST;            // Use SMCLK, keep SW reset
 UCB0BR0 = 0x0A;                             // fSCL = SMCLK/11 = 95.3kHz
 UCB0BR1 = 0x02;
 
 P2OUT = 0x00;          //  RST = 0;	 	Reset RST
 delay(1);
 P2OUT = 0x02;	       //RST = 1; Reset RST= M68 Interface
 delay(10);
 P2OUT = 0xC0;         //A0 = 0 CS1 = 0 CS2 = 0 R_W = 1 E=1;
 
 bothSides(0xE2);
 delay(10);
 bothSides(0xA4);
 bothSides(0xA9);
 bothSides(0xA0);
 bothSides(0xEE);
 bothSides(0xC0);
 bothSides(0xAF);
}
/*****************************************/
void Graphic(unsigned char *img)
{
	int m, n;
	char page = 0xB8;
	for(m=0;m<4;m++)
	{
		bothSides(page);
		bothSides(0x00);

		for(n=0;n<61;n++)
		{
			Writeleft(*img);
			img++;
		}

		for(n=0;n<61;n++)
		{
			Writeright(*img);
			img++;
		}

		page++;
	}

}
/*****************************************/	
int main(void)
{
  //unsigned char *picture = c;
	while(1)
	{
		init();
		delay(30);
		Graphic(picture);
		delay(2000);
	}
}
/*****************************************/
//copy & paste this into pic.h

/*****************************************/
