OLED control via SPI from AVR

So I have a dev board with an XMega128A1 and the newest version of the 128x128 OLED Carrier board. I am trying to control it via SPI. I have check my wiring and have SCK - D0, MOSI - D1 and 2 other GPIOs connected to D/C and Reset. I have clocked the MCU way down to 32KHz and even put some small for loops in to slow the progress between specific steps. I have done some logic checks and the right pins go high and low when they need to (i.e. the D/C is driven low when sending a command and driven high when sending data). It seems that I can only send commands; no data. When I send a command that does not require data (e.g. wake up, sleep or start scroll) it seems to work just fine. If I try to send a command that does require data, like clear screen, nothing happens. I have tried playing around with the different data modes on the SPI as well and that did not seem to make a difference.

Has anyone gotten this thing to work in SPI mode or does anyone have any idea what might be going on here?

Quick update. I have tried communicating with the OLED Carrier board in 68XX-parallel (8-bit) mode, 4-wire SPI mode and 3-wire SPI mode. In each case, I can send it Command bytes but not Data bytes. It just seems like the data bytes are not latching. (I have done a bunch of logic checks on D/C). Has anyone had any experience with the new version of this board?

On more update.

I am really surprised that there were no other replies to this at all. Is no one else using this board?

Anyway. It seems as if the new version of this board is not compatible with the old version. The display driver chip on the old board was an SSD1339 and the new one is an SSD1351. These boards seem identical in pin-out but are not %100 command compatible. There does not appear to be any kind of Draw commands or clear window command. Also the documentation that you can currently find on the net about it is a bit flawed (referring to the pdf datasheet from the manufacturer themselves). I believe it is a very early version of the document. I have requested a new datasheet but have not received a response yet.

It should be known that the link to thdatasheet for the new Sparkfun OLED carrier board is actually the datasheet for the old carrier board and that datasheet still has a link to the old SSD1339 datasheet.

I have contacted Sparkfun tech support about it and did my best to explain the issue but I have not heard a response back from them.

This sounds like exactly the same problem I’ve had with the SSD1339 and been unable to resolve. (I was starting to doubt my ability to program an AVR until I finally hooked up a logic analyzer and saw what I expected to see.)

Assuming the newer chip still returns a status byte, do you find the value of that byte to be different after you’ve tried to send data?

I find that under normal operation the status byte is 0x01 but after trying to send data it becomes 0x05.

I’ll be watching to see how you resolve the issue with the newer driver chip. Maybe it’ll help me fix my problem.

To be honest with you, since I had started out using SPI to communicate with it, I never tried reading from it. Not sure that it would have saved me any time. But since I currently still have it hooked up in parallel mode, I’ll read the status byte and let you know what I find.

It looks like I never actually mentioned that I was able to communicate with the display and draw to it. The only way it appears you can do this is by actually writing to the display RAM. You can still pick your starting and ending columns and rows and writing to the RAM still auto-increments the address pointer. I have not yet found a status byte though. Maybe I just missed it in the documentation. I’ll take a look at the old SSD1339 doc and see what you needed to do to read it there, though I doubt it will work on the new SSD1351.

Two issues I have found with the documentation for the SSD1351 are as follows.

First, it states that you can use an 8-bit color mode, though this does not seem to work at all. It still just seems to wait for two bytes for the full 16 bit color.

Second, and this actually seems like more of a hardware issue, has to do with the bits you set for the colors in 16 bit parallel mode. In section 8.3.2 (page 22) of the currently available datasheet (v0.10) for the SSD1351, it shows, for the 8 bits/Serial (which I assume to mean 8 bit parallel or serial), 65k color, that the first 5 MSBs of the first Byte being sent are for the 5-bit Red (maybe Magenta) color, then the last 3 LSBs of the first Byte and the first 3 MSBs of the second Byte combine to determine a 6-bit Green color, and finally, the last 5 LSBs of the second Byte make up the 5-bit Blue (maybe Cyan) color. This follows a standard 5-6-5 16-bit color schema. There are some anomalies it seems though. First, the red and blue seem to be switched even though I have set it to read as RGB in the “Set Re-map / Color Depth (0xA0)” command, not BGR. Second, it seems that if the first MSB of the second Byte is set (1), the display gets screwed up. It does this on two different boards.

here is a (bad) picture of the board.

http://i28.photobucket.com/albums/c206/ … 44-900.jpg

What I am displaying here is a small string and then all of the 95 standard displayable ascii characters.

Two things I feel I should mention here is that if I left CS or R/W floating instead of tying them to ground, I got unpredictable results. Also, when the board is powered up, it should be held in reset (RES = 0) for a few uS else the results again become unpredictable.

would you mind writing down the “exact” pin to pin connection between your board and oled board, and what jumpers you set on oled? (JP1, JP2, BS1, BS2)

I’m in the “pulling my hair” mode for 2 days already as I can’t get a peep out of the darn thing

Hey. Sorry it took me so long to reply. I guess I am not getting a notification when this thread gets a post.

Anyway, I will bring it in tomorrow and give you the pin out.

Are you trying to use SPI or Parallel?

OK. So for Parallel, this is what I have for pinout.

XMega - OLED

PC0-7 D0-7

PD0 R/W

PD1 ERD

PD2 D/C

PD4 RES

PD5 CS

Then of course Vcc to +3.3 and GND to Ground

On this board, I did not change any of the solder jumpers and I did not solder in any headers to the JP1 and JP2 vias.

I did solder in some headers to BS1 and BS2. For 6800 Parallel mode, I have both BS jumpers in the on position. In other words, they are both jumping 3.3v to the center pin (with the board oriented so that the SparkFun silkscreening at the top is horizontal reading right to left, the jumpers are on the bottom two pins).

Here also is some code. It’s ugly, not well commented and inefficient but it works.

Some notes about it: Basically it prints out two sentences then prints out every character. Next it prints out what it is reading from port F (which happens to have a small joystick attached to it on the green MXega proto board that you see in the pic). In the loop that is reading port F it also draws the cake at the coord that are supplied as arguments to the DrawCake function. The only difference in cake1 and cake2 is the orientation of the flame so, without using any interrupts to draw the cake, the little flame flickers very very quickly. For the text, I just use a 40 bit array for the character (5x8). The color of the font is then controlled within the function itself not the font arrays. And, yes, the PrintChar function could have had two loops in it but I expanded them to save a few cycles. A quick few words about the strings. This has no fancy string handling nor the ability to put number vars in it (%d). It does recognize home (\1), new line and line feed (\n) and tab (\t). All strings should end with a null character (\0) or you will be printing out everything in memory.

#include <avr\io.h>
#include <avr/interrupt.h>

#define CHR_W 5
#define CHR_H 8
#define CHR_S 1
#define LINE_S 1
#define CLR_SHIFT 3
#define TAB_W 4
#define PIN_IN_CTRL 0b00011000

unsigned char font[95][5] = 
{
{0b00000000 //(space)
,0b00000000
,0b00000000
,0b00000000
,0b00000000},

{0b00100001 //!
,0b00001000
,0b01000010
,0b00000000
,0b00000100},

{0b01010010 //"
,0b10010100
,0b00000000
,0b00000000
,0b00000000},

{0b01010010 //#
,0b10111110
,0b10100101
,0b01111101
,0b01001010},

{0b00100011 //$
,0b10100010
,0b11000001
,0b01000101
,0b11000100},

{0b11001110 //%
,0b01000100
,0b01000010
,0b00100010
,0b01110011},

{0b01100100 //&
,0b10100101
,0b01000100
,0b01010110
,0b01101111},

{0b00100001 //'
,0b00001000
,0b00000000
,0b00000000
,0b00000000},

{0b00010001 //(
,0b00010000
,0b10000100
,0b00100000
,0b10000010},

{0b01000001 //)
,0b00000100
,0b00100001
,0b00001000
,0b10001000},

{0b00100011 //*
,0b10001000
,0b10100000
,0b00000000
,0b00000000},

{0b00000001 //+
,0b00001001
,0b11110010
,0b00010000
,0b00000000},

{0b00000000 //,
,0b00000000
,0b00000000
,0b00000000
,0b11001100},

{0b00000000 //-
,0b00000001
,0b11110000
,0b00000000
,0b00000000},

{0b00000000 //.
,0b00000000
,0b00000000
,0b00000000
,0b11000110},

{0b00001000 ///
,0b01000100
,0b01000010
,0b00100010
,0b00010000},

{0b01110100 //0
,0b11101011
,0b01011010
,0b11010111
,0b00101110},

{0b00100011 //1
,0b00001000
,0b01000010
,0b00010000
,0b10001110},

{0b01110100 //2
,0b01000010
,0b00010001
,0b00010001
,0b00011111},

{0b01110100 //3
,0b01000010
,0b11100000
,0b10000110
,0b00101110},

{0b10010100 //4
,0b10100101
,0b11110001
,0b00001000
,0b01000010},

{0b11111100 //5
,0b00100001
,0b11100000
,0b10000110
,0b00101110},

{0b01110100 //6
,0b01100001
,0b11101000
,0b11000110
,0b00101110},

{0b11111000 //7
,0b01000100
,0b01000100
,0b00100010
,0b00010000},

{0b01110100 //8
,0b01100010
,0b11101000
,0b11000110
,0b00101110},

{0b01110100 //9
,0b01100011
,0b00010111
,0b10000110
,0b00101110},

{0b00000001 //:
,0b10001100
,0b00000000
,0b00011000
,0b11000000},

{0b00000001 //;
,0b10001100
,0b00000000
,0b00011000
,0b11001100},

{0b00000000 //<
,0b10001000
,0b10001000
,0b00100000
,0b10000010},
  
{0b00000000 //=
,0b00000001
,0b11110000
,0b01111100
,0b00000000},

{0b00000010 //>
,0b00001000
,0b00100000
,0b10001000
,0b10001000},

{0b01110100 //?
,0b01000010
,0b00100010
,0b00010000
,0b00000100},

{0b01110100 //@
,0b01100111
,0b01011011
,0b01000010
,0b00101110},
 
{0b00100001 //A
,0b00010100
,0b10100111
,0b01000110
,0b00110001},

{0b11110100 //B
,0b01100011
,0b11101000
,0b11000110
,0b00111110},

{0b01110100 //C
,0b01100001
,0b00001000
,0b01000010
,0b00101110},

{0b11100100 //D
,0b10100011
,0b00011000
,0b11000110
,0b01011100},

{0b11111100 //E
,0b00100001
,0b11101000
,0b01000010
,0b00011111},

{0b11111100 //F
,0b00100001
,0b11101000
,0b01000010
,0b00010000},

{0b01110100 //G
,0b01100001
,0b00001011
,0b01000110
,0b00101110},

{0b10001100 //H
,0b01100011
,0b11111000
,0b11000110
,0b00110001},

{0b11111001 //I
,0b00001000
,0b01000010
,0b00010000
,0b10011111},

{0b01111000 //J
,0b10000100
,0b00100001
,0b00001010
,0b01001100},

{0b10001100 //K
,0b10101001
,0b10001010
,0b01001010
,0b00110001},

{0b10000100 //L
,0b00100001
,0b00001000
,0b01000010
,0b00011111},

{0b10001110 //M
,0b11101011
,0b01011000
,0b11000110
,0b00110001},

{0b10001110 //N
,0b01110011
,0b01011010
,0b11001110
,0b01110001},

{0b01110100 //O
,0b01100011
,0b00011000
,0b11000110
,0b00101110},

{0b11110100 //P
,0b01100011
,0b00011111
,0b01000010
,0b00010000},

{0b01110100 //Q
,0b01100011
,0b00011000
,0b11010110
,0b01001111},

{0b11110100 //R
,0b01100011
,0b00011111
,0b01010010
,0b01010001},

{0b01110100 //S
,0b01100000
,0b11100000
,0b10000110
,0b00101110},

{0b11111001 //T
,0b00001000
,0b01000010
,0b00010000
,0b10000100},

{0b10001100 //U
,0b01100011
,0b00011000
,0b11000110
,0b00101110},

{0b10001100 //V
,0b01100010
,0b10100101
,0b00101000
,0b10000100},

{0b10001100 //W
,0b01100011
,0b00011010
,0b11010111
,0b01110001},

{0b10001010 //X
,0b10010100
,0b01000010
,0b00101001
,0b01010001},

{0b10001100 //Y
,0b01010100
,0b10100010
,0b00010000
,0b10000100},

{0b11111000 //Z
,0b10000100
,0b01000010
,0b00100001
,0b00011111},

{0b01110010 //[
,0b00010000
,0b10000100
,0b00100001
,0b00001110},

{0b10000100 //(\)
,0b00010000
,0b01000010
,0b00001000
,0b00100001},

{0b01110000 //]
,0b10000100
,0b00100001
,0b00001000
,0b01001110},

{0b00100010 //^
,0b10100010
,0b00000000
,0b00000000
,0b00000000},

{0b00000000 //_
,0b00000000
,0b00000000
,0b00000000
,0b00011111},

{0b01000001 //`
,0b00000100
,0b00000000
,0b00000000
,0b00000000},

{0b00000000 //a
,0b00000000
,0b11100000
,0b10111110
,0b00101111},

{0b10000100 //b
,0b00100001
,0b11101000
,0b11000110
,0b00101110},

{0b00000000 //c
,0b00000000
,0b11101000
,0b11000010
,0b00101110},

{0b00001000 //d
,0b01000010
,0b11111000
,0b11000110
,0b00101110},

{0b00000000 //e
,0b00000000
,0b11101000
,0b11111010
,0b00001110},

{0b00000001 //f
,0b10010010
,0b10001111
,0b00100001
,0b00001000},

{0b00000000 //g
,0b00011101
,0b00011000
,0b10111100
,0b00101110},

{0b00000010 //h
,0b00010000
,0b11100100
,0b10100101
,0b00101001},

{0b00000000 //i
,0b00001000
,0b00000010
,0b00010000
,0b10000100},

{0b00000000 //j
,0b00000010
,0b00000000
,0b10000101
,0b00100110},

{0b00000100 //k
,0b00100001
,0b00101010
,0b01100010
,0b10010010},

{0b00000001 //l
,0b00001000
,0b01000010
,0b00010000
,0b10000110},

{0b00000000 //m
,0b00000000
,0b10101010
,0b11010110
,0b00110001},

{0b00000000 //n
,0b00000000
,0b11100100
,0b10100101
,0b00101001},

{0b00000000 //o
,0b00000000
,0b01100100
,0b10100101
,0b00100110},

{0b00000000 //p
,0b00001100
,0b10010100
,0b10111001
,0b00001000},

{0b00000000 //q
,0b00001100
,0b10010100
,0b10011100
,0b00100001},

{0b00000000 //r
,0b00000000
,0b11100100
,0b10100001
,0b00001000},

{0b00000000 //s
,0b00000000
,0b11111000
,0b00111000
,0b00111110},

{0b00000000 //t
,0b00010001
,0b11100100
,0b00100001
,0b00100110},

{0b00000000 //u
,0b00000000
,0b10010100
,0b10100101
,0b00100110},

{0b00000000 //v
,0b00000001
,0b00011000
,0b10101001
,0b01000100},

{0b00000000 //w
,0b00000001
,0b00011000
,0b11010110
,0b10101010},

{0b00000000 //x
,0b00000001
,0b00010101
,0b00010001
,0b01010001},

{0b00000000 //y
,0b00000000
,0b10010100
,0b10011100
,0b00101110},

{0b00000000 //z
,0b00000001
,0b11110001
,0b00010001
,0b00011111},

{0b00010001 //{
,0b00001000
,0b01000100
,0b00010000
,0b10000010},

{0b00100001 //|
,0b00001000
,0b01000010
,0b00010000
,0b10000100},

{0b01000001 //}
,0b00001000
,0b01000001
,0b00010000
,0b10001000},

{0b00000000 //~
,0b00000000
,0b10011011
,0b00000000
,0b00000000}
};

/*
unsigned char cake1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 
0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00};

unsigned char cake2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66,
0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00};
*/

unsigned char chrX = 0;
unsigned char chrY = 0;
char vals[6];

int main();
void Init();
void OLEDCmd(unsigned char);
void OLEDData(unsigned char);
void PrintChar(unsigned char);
void PrintS(char[]);
void DrawCake(unsigned char[], unsigned char, unsigned char);
void IntToString(unsigned int);
void InitTimer();
void InitDAC();

int main()
{

	char i = 0;

	PORTF_DIR = 0;
	PORTE_DIR = 0b11111111;
	PORTF_PIN0CTRL = PIN_IN_CTRL;
	PORTF_PIN1CTRL = PIN_IN_CTRL;
	PORTF_PIN2CTRL = PIN_IN_CTRL;
	PORTF_PIN3CTRL = PIN_IN_CTRL;
	PORTF_PIN4CTRL = PIN_IN_CTRL;
	PORTF_PIN5CTRL = PIN_IN_CTRL;
	PORTF_PIN6CTRL = PIN_IN_CTRL;
	PORTF_PIN7CTRL = PIN_IN_CTRL;


	//Enable low priority interrupts
	PMIC_CTRL = 0b00000001;

	PORTE_OUT = 1;
	Init();	
	PORTE_OUT = 2;

	cli(); //disable interrupts
	
	InitDAC();
	InitTimer();

	sei(); //enable the interrupts

    TCC0_CTRLA = 0b00000001; //pre = 1 and starts the timer Using this with a Period of 1000 sould give us a 32K clock.



	PrintS("The cake is a lie!\n\0");

	PrintS("I am ");
	IntToString(PORTE_OUT);
	PrintS(vals);
	PrintS(" years old\n\0");

	for (i = 32; i < 127; i++)
	{
		PrintChar(i);
	}
	PrintS("\n\0");


	PORTE_OUT = 3;


	while(1)
	{
		DrawCake(cake1, 48, 70);

		IntToString(PORTF_IN);
		PrintS(vals);
		PrintS("\1\0");

		DrawCake(cake2, 48, 70);

	}

	return 0;
}

void InitDAC()
{
	//DAC setup

	DACA_CTRLA = 0b00000100;
	DACA_CTRLB = 0b00000000;
	DACA_CTRLC = 0b00001001;
	DACA_TIMCTRL = 0b01110000;
	DACA_GAINCAL = 0b00000001;
	DACA_OFFSETCAL = 0b00000000;
	DACA_CTRLA = 0b00000101; //enable DAC

}

void InitTimer()
{
	//Timer setup
	TCC0_CTRLA = 0; //timer off for now.
	TCC0_CTRLB = 0;
	TCC0_CTRLC = 0;
	TCC0_CTRLD = 0;
	TCC0_CTRLE = 0;
	TCC0_INTCTRLA = 0b00000001; //Enable overflow int
	TCC0_INTCTRLB = 0b00000000; 
	TCC0_PER = 1016 ; //with no prescaler, 1016 is the number of cycles between each H_BLANK period on a tv screen at 60 FPS (or 60 Hz screen refresh rate) 
					  //at a 32MHz Sys clock
}

void DrawCake(unsigned char cake[], unsigned char x, unsigned char y)
{
	unsigned int i;
	
	OLEDCmd(0x15);
	OLEDData(x);
	OLEDData(x + 31);

	OLEDCmd(0x75);
	OLEDData(y);
	OLEDData(y + 31);
	
	OLEDCmd(0x5C);

	for (i = 0; i < 1024; i++)
	{
		OLEDData(cake[i]);
		OLEDData(0);
	}


}


void PrintS(char string[])
{
	unsigned int i = 0;
	while (string[i] != 0 && i < 65535)
	{
		PrintChar(string[i]);
		i++;
	}

}

void IntToString(unsigned int value)
{
	
	vals[0] = value / 10000;
	value -= vals[0] * 10000;

	vals[1] = value / 1000;
	value -= vals[1] * 1000;

	vals[2] = value / 100;
	value -= vals[2] * 100;

	vals[3] = value / 10;
	value -= vals[3] * 10;

	vals[4] = value;

	vals[0] += 48;
	vals[1] += 48;
	vals[2] += 48;
	vals[3] += 48;
	vals[4] += 48;


	vals[5] = 0;
}

void PrintChar(unsigned char chr)
{
	unsigned char i = 0;
	unsigned char c = 0;
	unsigned char cVal = chr - 32;

	switch(chr)
	{
		case 1: //home
			chrX = 0;
			break;
		case 9: //tab
		
			for (i = 0; i < TAB_W; i++)
			{

				chrX += (CHR_W + CHR_S);

				if (chrX > 127)
				{
					chrY += (CHR_H + LINE_S);

					if (chrY > 120)
					{
						chrY = 0;
					}
				}
			}

			break;
		case 10: //new line
		
			chrY += (CHR_H + LINE_S);

			if (chrY > 120)
			{
				chrY = 0;
			}

			chrX = 0;

			break;
		case 13: //line feed (carriage return)
			
			chrY += (CHR_H + LINE_S);

			if (chrY > 120)
			{
				chrY = 0;
			}

			break;
		default: //all other characters
		 	
			if (cVal < 95)
			{

				PORTD_OUT = 0b00010010; 
				PORTC_OUT = 0x15;
				PORTD_OUT = 0b00010000; 

				PORTD_OUT = 0b00010110; 
				PORTC_OUT = chrX;
				PORTD_OUT = 0b00010100; 
				PORTD_OUT = 0b00010110; 
				PORTC_OUT = chrX + (CHR_W - 1);
				PORTD_OUT = 0b00010100;


				PORTD_OUT = 0b00010010; 
				PORTC_OUT = 0x75;
				PORTD_OUT = 0b00010000; 


				PORTD_OUT = 0b00010110; 
				PORTC_OUT = chrY;
				PORTD_OUT = 0b00010100; 
				PORTD_OUT = 0b00010110; 
				PORTC_OUT = chrY + (CHR_H - 1);
				PORTD_OUT = 0b00010100;

				PORTD_OUT = 0b00010010; 
				PORTC_OUT = 0x5C;
				PORTD_OUT = 0b00010000; 


				
				c = font[cVal][0];

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100; 

					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	



				c = font[cVal][1];

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100; 

					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	


				c = font[cVal][2];

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100; 

					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	


				c = font[cVal][3];

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100; 

					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	


				c = font[cVal][4];

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100; 

					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;

					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					c = c << 1;
	
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = (c & 0b10000000);
					PORTD_OUT = 0b00010100; 
					PORTD_OUT = 0b00010110; 
					PORTC_OUT = 0;
					PORTD_OUT = 0b00010100;	
					
				

				chrX += (CHR_W + CHR_S);

				if (chrX > 127 - (CHR_W + CHR_S))
				{
					chrY += (CHR_H + LINE_S);

					if (chrY > 120)
					{
						chrY = 0;
					}

					chrX = 0;

				}
				

			}
			break;
	}		
}


void Init()
{
	
	unsigned int i;

	//Port E setup
	PORTE_DIR = 0b11111111; //Make E all outputs
	PORTE_OUT = 0; //Set them all low
	
	//Port D setuo
	PORTD_DIR = 0b11111111; //Set all Port D pins as outputs
	PORTD_OUT = 0; //Set all port D pins low

	//Port C setup
	PORTC_DIR = 0b11111111; //Set all Port C pins as outputs
	PORTC_OUT = 0; //Set all port C pins low

	OSC_CTRL = 0b00000010; //Enable the 32MHz clock
	PORTE_OUT = OSC_STATUS; //Set the LEDs to see the status
	while ((OSC_STATUS & 0b00000010) != 0b00000010) //Wait until the clock is stable
	{
	}

	//Set the protection byte and then change the system clock
	CCP = 0xD8;
	CLK_CTRL = 0b00000001;

	PORTD_OUT = 0b00000000; //Reset
	for (i = 0; i < 10; i++)
	{
		asm("nop");
	}
	PORTD_OUT = 0b00010010; //Normal

	OLEDCmd(0xFD);
	OLEDData(0xB1);
	
	OLEDCmd(0xA1);
	OLEDData(0);

	OLEDCmd(0xA2);
	OLEDData(0);

	OLEDCmd(0xAF); //Wake Up

	PORTE_OUT = 1;
	
	OLEDCmd(0xA0);
	OLEDData(0b00110000);

	OLEDCmd(0x5C);
	

	for (i = 0; i < 16384; i++)
	{
		OLEDData(0);
		OLEDData(0);
	}

	
}


void OLEDCmd(unsigned char cmd)
{
	
	PORTD_OUT = 0b00010010; 
	PORTC_OUT = cmd;
	PORTD_OUT = 0b00010000; 

}

void OLEDData(unsigned char data)
{

	PORTD_OUT = 0b00010110; 
	PORTC_OUT = data;
	PORTD_OUT = 0b00010100; 
}

ISR(TCC0_OVF_vect)
{
}