Drivrutin för Grafisk display [AVR]

Berätta om dina pågående projekt.
Användarvisningsbild
Korken
Inlägg: 2230
Blev medlem: 3 februari 2006, 19:19:36
Ort: Luleå, Porsön

Drivrutin för Grafisk display [AVR]

Inlägg av Korken »

Godagens!

Tänkte lägga upp min kod för en grafisk KS0108B display på 128x64 pixlar.
Den kan rita rektanglar/kvadrater, cirklar, linjer och text.

Exempel bilder:
Bild 1
Bild 2

Det fungerar iaf men kan säkert putsas lite också.
Just den här delen av koden ser ni på den första bilden, sen är det bara att kolla på Info delen för hur allt fungerar.
den här kan folk i alla fall använda om en grund så de slipper börja om från början, jag orkade bara kommentera det första men koden är rätt så straight forward, så här har ni koden:

Kod: Markera allt

/*
Info - How to use the commands.

1. LCD_Fill_Screen();
This command will fill the screen with pixels.

2. LCD_Clear_Screen();
This command will clear the screen of pixels.

3. LCD_Draw_Dot(x, y);
This command will draw a dot at the specified x, y location.

4. LCD_Clear_Dot(x, y);
This command will clear a dot at the specified x, y location.

5. LCD_Draw_Line(x1, y1, x2, y2, clr);
This command will draw a line from the specified cordinates (x1, y1) to the specified cordinates (x2, y2).
If clr is 1 it will clear all pixels of the line and if it is 0 it will draw a line.

6. LCD_Draw_Rect(x1, y1, x2, y2, fill, clr);
This command will draw a box from the specified cordinates (x1, y1) to the specified cordinates (x2, y2).
if fill is 1 it will fill the box and if it is 0 it will just make the outline of a box.
If clr is 1 it will clear all pixels of the box and if it is 0 it will draw the box.

7. LCD_Draw_Circle(x, y, R);
This command will draw a circle at the specified cordinates (x, y) with the radius 'R'.

8. LCD_Write_Char(x, y, 'char');
This command will draw a character ('char') at the specified cordinates (x, y), the x and y cordinates determins the character's top-left pixel.

9. LCD_Write_String(x, y, "string");
This command will draw a string ("string") at the specified cordinates (x, y), the x and y cordinates determins the string's top-left pixel.
*/

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

// ---- LCD Definitions ----
#define LCD_DATA_PORT			 PORTD
#define LCD_DATA_PIN			 PIND
#define LCD_DATA_DDR			 DDRD
#define LCD_CONTROL_PORT		 PORTC
#define LCD_CONTROL_PIN		 PINC
#define LCD_CONTROL_DDR		 DDRC
#define LCD_RS				 0
#define LCD_RW				 1
#define LCD_ENABLE			 2
#define LCD_CS1_PIN			 3
#define LCD_CS2_PIN			 4
#define LCD_RESET			5

#define DISPLAY_ON			0b00111111
#define DISPLAY_OFF			0b00111110
#define DISPLAY_STATUS_BUSY		0b10000000
#define DISPLAY_STATUS_ON_OFF		0b00100000
#define DISPLAY_STATUS_RESET		0b00010000
#define DISPLAY_LINE			0b11000000
#define ADDRESS_X			0b01000000
#define ADDRESS_Y			0b10111000

#define SET_CS1()			 (LCD_CONTROL_PORT |= (1<<LCD_CS1_PIN))
#define CLEAR_CS1()			 (LCD_CONTROL_PORT &= ~(1<<LCD_CS1_PIN))
#define SET_CS2()			 (LCD_CONTROL_PORT |= (1<<LCD_CS2_PIN))
#define CLEAR_CS2()			 (LCD_CONTROL_PORT &= ~(1<<LCD_CS2_PIN))
#define SET_ENABLE()			(LCD_CONTROL_PORT |= (1<<LCD_ENABLE))
#define CLEAR_ENABLE()			(LCD_CONTROL_PORT &= ~(1<<LCD_ENABLE))
#define SET_RW()			 (LCD_CONTROL_PORT |= (1<<LCD_RW))
#define CLEAR_RW()			 (LCD_CONTROL_PORT &= ~(1<<LCD_RW))
#define SET_RS()			 (LCD_CONTROL_PORT |= (1<<LCD_RS))
#define CLEAR_RS()			 (LCD_CONTROL_PORT &= ~(1<<LCD_RS))
// ------------

const unsigned char char_array[26][5] PROGMEM =
{
	{0b01111110, 0b10010000, 0b10010000, 0b10010000, 0b01111110}, //A
	{0b11111110, 0b10010010, 0b10010010, 0b10010010, 0b01101100}, //B
	{0b01111100, 0b10000010, 0b10000010, 0b10000010, 0b01000100}, //C
	{0b11111110, 0b10000010, 0b10000010, 0b01000100, 0b00111000}, //D
	{0b11111110, 0b10010010, 0b10010010, 0b10010010, 0b10000010}, //E
	{0b11111110, 0b10010000, 0b10010000, 0b10010000, 0b10000000}, //F
	{0b01111100, 0b10000010, 0b10010010, 0b10010010, 0b01011100}, //G
	{0b11111110, 0b00010000, 0b00010000, 0b00010000, 0b11111110}, //H
	{0b10000010, 0b11111110, 0b10000010, 0, 0},                   //I
	{0b00001100, 0b00000010, 0b00000010, 0b00000010, 0b11111100}, //J
	{0b11111110, 0b00010000, 0b00101000, 0b01000100, 0b10000010}, //K
	{0b11111110, 0b00000010, 0b00000010, 0b00000010, 0b00000010}, //L
	{0b11111110, 0b01000000, 0b00100000, 0b01000000, 0b11111110}, //M
	{0b11111110, 0b01000000, 0b00111000, 0b00000100, 0b11111110}, //N
	{0b01111100, 0b10000010, 0b10000010, 0b10000010, 0b01111100}, //O
	{0b11111110, 0b10010000, 0b10010000, 0b10010000, 0b01100000}, //P
	{0b01111000, 0b10000100, 0b10000100, 0b10000110, 0b01111010}, //Q
	{0b11111110, 0b10010000, 0b10011000, 0b10010100, 0b01100010}, //R
	{0b01100100, 0b10010010, 0b10010010, 0b10010010, 0b01001100}, //S
	{0b10000000, 0b10000000, 0b11111110, 0b10000000, 0b10000000}, //T
	{0b11111100, 0b00000010, 0b00000010, 0b00000010, 0b11111100}, //U
	{0b11100000, 0b00011100, 0b00000010, 0b00011100, 0b11100000}, //V
	{0b01111100, 0b00000010, 0b00011100, 0b00000010, 0b01111100}, //W
	{0b10000010, 0b01101100, 0b00010000, 0b01101100, 0b10000010}, //X
	{0b11100000, 0b00010000, 0b00011110, 0b00010000, 0b11100000}, //Y
	{0b10000110, 0b10001010, 0b10010010, 0b10100010, 0b11000010}  //Z
};

const unsigned char schar_array[26][5] PROGMEM =
{
	{0b00011110, 0b00100100, 0b00100100, 0b00011110, 0},			//a
	{0b00111110, 0b00101010, 0b00101010, 0b00010100, 0},			//b
	{0b00011100, 0b00100010, 0b00100010, 0b00100010, 0},			//c
	{0b00111110, 0b00100010, 0b00100010, 0b00011100, 0},			//d
	{0b00111110, 0b00101010, 0b00101010, 0b00100010, 0},			//e
	{0b00111110, 0b00101000, 0b00101000, 0b00100000, 0},			//f
	{0b00011100, 0b00100010, 0b00101010, 0b00101100, 0},			//g
	{0b00111110, 0b00001000, 0b00001000, 0b00111110, 0},			//h
	{0b00100010, 0b00111110, 0b00100010, 0, 0},				 		//i
	{0b00000100, 0b00100010, 0b00100010, 0b00111100, 0},			//j
	{0b00111110, 0b00001000, 0b00010100, 0b00100010, 0},			//k
	{0b00111110, 0b00000010, 0b00000010, 0b00000010, 0},			//l
	{0b00111110, 0b00010000, 0b00001000, 0b00010000, 0b00111110},	//m
	{0b00111110, 0b00010000, 0b00001000, 0b00000100, 0b00111110},	//n
	{0b00011100, 0b00100010, 0b00100010, 0b00100010, 0b00011100},	//o
	{0b00111110, 0b00101000, 0b00101000, 0b00010000, 0},			//p
	{0b00011000, 0b00100100, 0b00100110, 0b00011010, 0},			//q
	{0b00111110, 0b00101000, 0b00101000, 0b00010110, 0},			//r
	{0b00010010, 0b00101010, 0b00101010, 0b00100100, 0},			//s
	{0b00100000, 0b00100000, 0b00111110, 0b00100000, 0b00100000},	//t
	{0b00111100, 0b00000010, 0b00000010, 0b00111100, 0},			//u
	{0b00110000, 0b00001100, 0b00000010, 0b00111100, 0},			//v
	{0b00111100, 0b00000010, 0b00001100, 0b00000010, 0b00111100},	//w
	{0b00100010, 0b00010100, 0b00001000, 0b00010100, 0b00100010},	//x
	{0b00110000, 0b00001000, 0b00000110, 0b00001000, 0b00110000},	//y
	{0b00100110, 0b00101010, 0b00101010, 0b00110010, 0}			 	//z
};

const unsigned char num_array[10][5] PROGMEM =
{
	{0b01111100, 0b10100010, 0b10010010, 0b10001010, 0b01111100}, //0
	{0b01000010, 0b11111110, 0b00000010, 0, 0},				 	  //1
	{0b01001110, 0b10001010, 0b10010010, 0b10010010, 0b01100010}, //2
	{0b01000100, 0b10000010, 0b10010010, 0b10010010, 0b01101100}, //3
	{0b11110000, 0b00010000, 0b00010000, 0b00010000, 0b11111110}, //4
	{0b11110010, 0b10010010, 0b10010010, 0b10010010, 0b10001100}, //5
	{0b01111100, 0b10010010, 0b10010010, 0b10010010, 0b01001100}, //6
	{0b10000000, 0b10000000, 0b10001110, 0b10010000, 0b11100000}, //7
	{0b01101100, 0b10010010, 0b10010010, 0b10010010, 0b01101100}, //8
	{0b01100000, 0b10010000, 0b10010000, 0b10010000, 0b01111110}  //9
};

const unsigned char sign_array[22][5] PROGMEM =
{
	{0b01110000, 0b10001010, 0b01110000, 0, 0},					  // !
	{0b11000110, 0b11001000, 0b00010000, 0b00100110, 0b11000110}, // %
	{0b00000010, 0b00001100, 0b00110000, 0b11000000, 0},		  // /	
	{0b01111100, 0b10000010, 0, 0, 0},                            // (
	{0b10000010, 0b01111100, 0, 0, 0},					 		  // )
	{0b11111110, 0b10000010, 0, 0, 0},					 		  // [
	{0b10000010, 0b11111110, 0, 0, 0},					 		  // ]
	{0b00010000, 0b01101100, 0b10000010, 0, 0},				 	  // {
	{0b10000010, 0b01101100, 0b00010000, 0, 0},				 	  // }
	{0b00101000, 0b00101000, 0b00101000, 0, 0},				 	  // =
	{0b01100000, 0b10000000, 0b10001010, 0b10010000, 0b01100000}, // ?
	{0b00010000, 0b00111000, 0b00010000, 0, 0},				  	  // +
	{0b00010000, 0b00010000, 0b00010000, 0, 0},				 	  // -
	{0b00000010, 0, 0, 0, 0},						 			  // .
	{0b00100100, 0, 0, 0, 0},						 			  // :
	{0b00000010, 0b00000010, 0b00000010, 0b00000010, 0b00000010}, // _
	{0b11000000, 0, 0, 0, 0},						 			  // ´
	{0b11111110, 0, 0, 0, 0},						 			  // |
	{0b01000000, 0b10100000, 0b01000000, 0, 0},					  // *
	{0b00100000, 0b01000000, 0b10000000, 0b01000000, 0b00100000}, // ^
	{0b00010000, 0b00101000, 0b01000100, 0, 0},					  // <
	{0b01000100, 0b00101000, 0b00010000, 0, 0}					  // >
};

void LCD_Init(void);
void LCD_Wait(void);
void LCD_RTS(void);
void LCD_Fill_Screen(void);
void LCD_Clear_Screen(void);
void LCD_Send_Cmd(unsigned char);
void LCD_Send_Data(unsigned char);
void LCD_Set_Pos(unsigned char, unsigned char);
void LCD_Draw_Dot(unsigned char, unsigned char);
void LCD_Clear_Dot(unsigned char, unsigned char);
void LCD_Draw_Line(char, char, char, char, unsigned char);
void LCD_Draw_Rect(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);
void LCD_Draw_Circle(unsigned char, unsigned char, unsigned char);
void LCD_Write_Char(unsigned char, unsigned char, char);
void LCD_Write_String(unsigned char, unsigned char, char *);
unsigned char LCD_Read_Data(void);
unsigned int abs(int);

volatile unsigned char lcd_x = 0, lcd_y = 0;

int main(void)
{	
	LCD_Init();
	LCD_RTS();
	
	while(1);
	return 0;
}

void LCD_RTS(void)
{
	LCD_Clear_Screen();
	
	LCD_Draw_Line(1, 0, 126, 0, 0);
	LCD_Draw_Line(1, 63, 126, 63, 0);
	LCD_Draw_Line(0, 1, 0, 62, 0);
	LCD_Draw_Line(127, 1, 127, 62, 0);
	
	LCD_Draw_Dot(1, 1);
	LCD_Draw_Dot(1, 62);
	LCD_Draw_Dot(126, 1);
	LCD_Draw_Dot(126, 62);

	LCD_Draw_Line(2, 10, 125, 10, 0);
	LCD_Draw_Line(1, 9, 2, 9, 0);
	LCD_Draw_Line(1, 11, 2, 11, 0);
	LCD_Draw_Line(125, 9, 126, 9, 0);
	LCD_Draw_Line(125, 11, 126, 11, 0);
	
	LCD_Draw_Line(2, 31, 125, 31, 0);
	LCD_Draw_Line(1, 30, 2, 30, 0);
	LCD_Draw_Line(1, 32, 2, 32, 0);
	LCD_Draw_Line(125, 30, 126, 30, 0);
	LCD_Draw_Line(125, 32, 126, 32, 0);
	
	LCD_Draw_Line(2, 52, 125, 52, 0);
	LCD_Draw_Line(1, 51, 2, 51, 0);
	LCD_Draw_Line(1, 53, 2, 53, 0);
	LCD_Draw_Line(125, 51, 126, 51, 0);
	LCD_Draw_Line(125, 53, 126, 53, 0);
	
	LCD_Clear_Dot(0, 10);
	LCD_Clear_Dot(0, 31);
	LCD_Clear_Dot(0, 52);
	LCD_Clear_Dot(127, 10);
	LCD_Clear_Dot(127, 31);
	LCD_Clear_Dot(127, 52);

	LCD_Write_String(13, 2, "Pb Charger by Korken");
	LCD_Write_String(4, 13, "Battery Info:");
	LCD_Write_String(4, 22, "Charge Completed!");
	LCD_Write_String(4, 34, "Timer: 03:41:27");
	LCD_Write_String(4, 43, "Stat: Tickle Charge...");
	LCD_Write_String(10, 54, "elektronikforumet.com");
}

void LCD_Init(void)
{
	LCD_CONTROL_DDR = 0xFF;
	LCD_DATA_DDR = 0xFF;
	LCD_CONTROL_PORT &= ~(1<<LCD_RESET);
	_delay_loop_2(20);
	LCD_CONTROL_PORT |= (1<<LCD_RESET);
	
	SET_CS1();
	CLEAR_CS2();
	LCD_Send_Cmd(DISPLAY_ON);
	
	SET_CS2();
	CLEAR_CS1();
	LCD_Send_Cmd(DISPLAY_ON);
	SET_CS1();
	SET_CS2();
}

void LCD_Wait(void)
{
	LCD_DATA_DDR = 0x00;		// Set DDR to input.
	CLEAR_RS();					// Set Instruction/Data pin to low
	SET_RW();					// Set Read/Write to Read (high)
	SET_ENABLE();
	
	while(LCD_DATA_PIN & DISPLAY_STATUS_BUSY);	 // Do loop until LCD isn't busy anymore
	
	CLEAR_ENABLE();
}

void LCD_Send_Cmd(unsigned char cmd)
{
	LCD_Wait();			 // Wait for LCD to be ready for commands
	CLEAR_RS();			 // Set Instruction/Data pin to low
	CLEAR_RW();			 // Set Read/Write to Write (low)
	
	LCD_DATA_DDR = 0xFF;		// Set DDR to output
	LCD_DATA_PORT = cmd;		// Send the data
	
	SET_ENABLE();			// Pulse Enable
	_delay_loop_2(20);	
	CLEAR_ENABLE();
}

void LCD_Send_Data(unsigned char data)
{
	LCD_Wait();			 // Wait for LCD to be ready for commands
	SET_RS();				 // Set Instruction/Data pin to high
	CLEAR_RW();			 // Set Read/Write to Write (low)
	
	LCD_DATA_DDR = 0xFF;		 // Set DDR to output
	LCD_DATA_PORT = data;		// Send data
	
	SET_ENABLE();			// Pulse Enable
	_delay_loop_2(20);	
	CLEAR_ENABLE();
}

unsigned char LCD_Read_Data(void)
{
	LCD_Wait();				 // Wait for LCD to be ready for commands
	LCD_DATA_DDR = 0x00;			 // Set DDR to output
	
	SET_RS();				 // Set Instruction/Data pin to high
	SET_RW();				 // Set Read/Write to Read (high)
	SET_ENABLE();				// Set Enable high
	_delay_loop_2(20);	
	CLEAR_ENABLE();				// Set Enable low
	
	unsigned char data = LCD_DATA_PIN;	// Get data
	
	return data;
}

void LCD_Set_Pos(unsigned char x, unsigned char y)
{
	lcd_x = x;
	lcd_y = y;
	
	unsigned char tmp = 0;
	
	if (lcd_x > 63)
	{
		SET_CS2();
		CLEAR_CS1();
		tmp = (lcd_x - 64);
	}
	
	else
	{
		SET_CS1();
		CLEAR_CS2();
		tmp = lcd_x;
	}
	
	LCD_Send_Cmd(ADDRESS_X | tmp);
	LCD_Send_Cmd(ADDRESS_Y | lcd_y);

	SET_CS2();
	CLEAR_CS1();
	LCD_Send_Cmd(DISPLAY_LINE);
	
	SET_CS1();
	CLEAR_CS2();
	LCD_Send_Cmd(DISPLAY_LINE);
	
	if (lcd_x > 63)
	{
		SET_CS2();
		CLEAR_CS1();
	}
	
	else
	{
		SET_CS1();
		CLEAR_CS2();
	}	
}

void LCD_Draw_Dot(unsigned char x, unsigned char y)
{
	LCD_Set_Pos(x, y/8);
	unsigned char tempdata = LCD_Read_Data();
	tempdata = LCD_Read_Data();
	LCD_Set_Pos(x, y/8);
	LCD_Send_Data(tempdata | (1<<(y % 8)));
}

void LCD_Clear_Dot(unsigned char x, unsigned char y)
{
	LCD_Set_Pos(x, y/8);
	unsigned char tempdata = LCD_Read_Data();
	tempdata = LCD_Read_Data();
	LCD_Set_Pos(x, y/8);
	LCD_Send_Data(tempdata & ~(1<<(y % 8)));

}

void LCD_Fill_Screen(void)
{
	for(unsigned char y = 0; y < 8; y++)
	{
		for(unsigned char x = 0; x < 128; x++)
		{
			LCD_Set_Pos(x, y);
			LCD_Send_Data(0xFF);
		}
	}
}

void LCD_Clear_Screen(void)
{
	for(unsigned char y = 0; y < 8; y++)
	{
		for(unsigned char x = 0; x < 128; x++)
		{
			LCD_Set_Pos(x, y);
			LCD_Send_Data(0x00);
		}
	}
}

void LCD_Draw_Line(char x0, char y0, char x1, char y1, unsigned char clr) 
{
	int dy = y1 - y0;
	int dx = x1 - x0;
	int stepx, stepy;

	if (dy < 0)
	{
		dy = -dy;
		stepy = -1;
	}
	else
	{
		stepy = 1;
	}

	if (dx < 0)
	{
		dx = -dx;
		stepx = -1;
	}
	else
	{
		stepx = 1;
	}

	dy <<= 1;
	dx <<= 1;

	if (clr) LCD_Clear_Dot(x0, y0);
	else LCD_Draw_Dot(x0, y0);

	if (dx > dy)
	{
		int fraction = dy - (dx >> 1);
		while (x0 != x1)
		{
			if (fraction >= 0)
			{
				y0 += stepy;
				fraction -= dx;
			}
			x0 += stepx;
			fraction += dy;
			if (clr) LCD_Clear_Dot(x0, y0);
			else LCD_Draw_Dot(x0, y0);
		}
	}
	else
	{
		int fraction = dx - (dy >> 1);
		while (y0 != y1) 
		{
			if (fraction >= 0)
			{
				x0 += stepx;
				fraction -= dy;
			}
			y0 += stepy;
			fraction += dx;
			if (clr) LCD_Clear_Dot(x0, y0);
			else LCD_Draw_Dot(x0, y0);;
		}
	}
}

void LCD_Draw_Rect(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char fill, unsigned char clr)
{
	if (fill != 0)
	{
		for (unsigned char i = y1; i <= y2; i++)
			LCD_Draw_Line(x1, i, x2, i, clr);
	}
	
	else
	{
		LCD_Draw_Line(x1, y1, x2, y1, clr);
		LCD_Draw_Line(x1, y2, x2, y2, clr);
		LCD_Draw_Line(x2, y1, x2, y2, clr);
		LCD_Draw_Line(x1, y1, x1, y2, clr);
	}
}

void LCD_Draw_Circle(unsigned char x1, unsigned char y1, unsigned char radius)
{
	int f = 1 - radius;
	int ddF_x = 0;
	int ddF_y = -2 * radius;
	int x = 0;
	int y = radius;

	LCD_Draw_Dot(x1, y1 + radius);
	LCD_Draw_Dot(x1, y1 - radius);
	LCD_Draw_Dot(x1 + radius, y1);
	LCD_Draw_Dot(x1 - radius, y1);

	while(x < y)
	{
		if(f >= 0)
		{
			y--;
			ddF_y += 2;
			f += ddF_y;
		}
	
		x++;
		ddF_x += 2;
		f += ddF_x + 1;	
		LCD_Draw_Dot(x1 + x, y1 + y);
		LCD_Draw_Dot(x1 - x, y1 + y);
		LCD_Draw_Dot(x1 + x, y1 - y);
		LCD_Draw_Dot(x1 - x, y1 - y);
		LCD_Draw_Dot(x1 + y, y1 + x);
		LCD_Draw_Dot(x1 - y, y1 + x);
		LCD_Draw_Dot(x1 + y, y1 - x);
		LCD_Draw_Dot(x1 - y, y1 - x);
	}
}

unsigned int abs(int num)
{
	if (num >= 0)
		return num;
	
	else
		return -num;
}

void LCD_Write_Char(unsigned char x, unsigned char y, char ch)
{	
	if (ch == ' ')
	{
		lcd_x += 4;
		return;
	}

	unsigned char ch_data[5] = {0, 0, 0, 0, 0};

	if((ch >= 'A') && (ch <= 'Z'))
	{
		ch_data[0] = pgm_read_byte(&char_array[ch - 'A'][0]);
		ch_data[1] = pgm_read_byte(&char_array[ch - 'A'][1]);
		ch_data[2] = pgm_read_byte(&char_array[ch - 'A'][2]);
		ch_data[3] = pgm_read_byte(&char_array[ch - 'A'][3]);
		ch_data[4] = pgm_read_byte(&char_array[ch - 'A'][4]);
	}
	
	else if((ch >= 'a') && (ch <= 'z'))
	{
		ch_data[0] = pgm_read_byte(&schar_array[ch - 'a'][0]);
		ch_data[1] = pgm_read_byte(&schar_array[ch - 'a'][1]);
		ch_data[2] = pgm_read_byte(&schar_array[ch - 'a'][2]);
		ch_data[3] = pgm_read_byte(&schar_array[ch - 'a'][3]);
		ch_data[4] = pgm_read_byte(&schar_array[ch - 'a'][4]);
	}
	
	else if((ch >= '0') && (ch <= '9'))
	{
		ch_data[0] = pgm_read_byte(&num_array[ch - '0'][0]);
		ch_data[1] = pgm_read_byte(&num_array[ch - '0'][1]);
		ch_data[2] = pgm_read_byte(&num_array[ch - '0'][2]);
		ch_data[3] = pgm_read_byte(&num_array[ch - '0'][3]);
		ch_data[4] = pgm_read_byte(&num_array[ch - '0'][4]);
	}
	
	else
	{
		unsigned char i = 0xFF;
	
		switch(ch)
		{	
			case '!': i = 0; break;
			case '%': i = 1; break;
			case '/': i = 2; break;
			case '(': i = 3; break;
			case ')': i = 4; break;
			case '[': i = 5; break;
			case ']': i = 6; break;
			case '{': i = 7; break;
			case '}': i = 8; break;
			case '=': i = 9; break;
			case '?': i = 10; break;
			case '+': i = 11; break;
			case '-': i = 12; break;
			case '.': i = 13; break;
			case ':': i = 14; break;
			case '_': i = 15; break;
			case '´': i = 16; break;
			case '|': i = 17; break;
			case '*': i = 18; break;
			case '^': i = 19; break;
			case '<': i = 20; break;
			case '>': i = 21; break;
	
			default: break;
		}
	
		if (i != 0xFF)
		{
			ch_data[0] = pgm_read_byte(&sign_array[i][0]);
			ch_data[1] = pgm_read_byte(&sign_array[i][1]);
			ch_data[2] = pgm_read_byte(&sign_array[i][2]);
			ch_data[3] = pgm_read_byte(&sign_array[i][3]);
			ch_data[4] = pgm_read_byte(&sign_array[i][4]);
		}
	}

	if (ch_data[0] != 0)
	{
		for(unsigned char a = 0; a < sizeof(ch_data); a++)
		{
			for(unsigned char i = 0; i < 8; i++)
			{
				if ((ch_data[a] == 0) || ((x + a) > 127))
					break;
	
				if (ch_data[a] & 0x80)
					LCD_Draw_Dot((x + a), (y + i));
	
				ch_data[a] <<= 1;
			}
		}
	}
}

void LCD_Write_String(unsigned char x, unsigned char y, char *s)
{
	unsigned char i = 0;
	
	while(s[i])
	{
		LCD_Write_Char(x, y, s[i]);
		x = (lcd_x + 2);
	
  		if (lcd_x > 127)
			return;
	
		i++;
	}
}
Om inte koden lockar så kan kanske alla tecken jag har gjort vara till nytta. :)

//Emil
Senast redigerad av Korken 13 januari 2011, 12:56:12, redigerad totalt 3 gånger.
Användarvisningsbild
maha
EF Sponsor
Inlägg: 1685
Blev medlem: 22 november 2005, 09:47:02
Ort: Jakobstad, Finland

Inlägg av maha »

Tack för ett trevligt bidrag! Skriv gärna för vilken kompilator koden är gjord så man inte behöver fundera ut det själv.
Användarvisningsbild
Korken
Inlägg: 2230
Blev medlem: 3 februari 2006, 19:19:36
Ort: Luleå, Porsön

Inlägg av Korken »

Ja, självklart!

Det är med avr-gcc :)

//Emil
Mindmapper
Inlägg: 7009
Blev medlem: 31 augusti 2006, 16:42:43
Ort: Jamtland

Inlägg av Mindmapper »

Tack! Har tänkt att börja med grafisk display men inte kommit mig för. Få se om minnet är så långt att jag kommer ihåg koden när det blir dags.
Användarvisningsbild
Landrash
Inlägg: 366
Blev medlem: 6 mars 2007, 18:29:07
Ort: Stockhom

Inlägg av Landrash »

Kanske nånting för wikin?
Användarvisningsbild
mri
Inlägg: 1165
Blev medlem: 15 mars 2007, 13:20:50
Ort: Jakobstad, Finland
Kontakt:

Inlägg av mri »

Välpolerad och snygg kod! Ser ut att kräva C++ kompilator fastän det egentligen är C kod.
Användarvisningsbild
Korken
Inlägg: 2230
Blev medlem: 3 februari 2006, 19:19:36
Ort: Luleå, Porsön

Inlägg av Korken »

Tackar! :)
Orginal kompilatorn i WinAVRs Programmers Notepad funkar och det är vanliga avr-gcc.

Om Wikin vill ha den så är det inga problem. :)

//Emil
Användarvisningsbild
MadModder
Co Admin
Inlägg: 31166
Blev medlem: 6 september 2003, 13:32:07
Ort: MadLand (Enköping)
Kontakt:

Inlägg av MadModder »

Snyggt!

Fast jag måste påpeka att det borde stå "powered" på första bilden ;)
Användarvisningsbild
mri
Inlägg: 1165
Blev medlem: 15 mars 2007, 13:20:50
Ort: Jakobstad, Finland
Kontakt:

Inlägg av mri »

Korken: Jag känner inte till avr-gcc, men följande kod skall generera "syntax error" med en standard C kompilator:

deklarerig av variabel efter kod i ett block:

{
LCD_Set_Pos(x, y/8 );
unsigned char tempdata = LCD_Read_Data();

deklarering av variabel i for-loop:

for(unsigned char y = 0; y < 8; y++)
Användarvisningsbild
gunne
Inlägg: 2088
Blev medlem: 17 juni 2004, 15:00:31
Ort: sthlm
Kontakt:

Inlägg av gunne »

Schysst.

Stöd för åäö? Kan man byta ut några mindre använda symboler?
Användarvisningsbild
Korken
Inlägg: 2230
Blev medlem: 3 februari 2006, 19:19:36
Ort: Luleå, Porsön

Inlägg av Korken »

MadModder:
Inte så petigt! ;)

mri:
Hum, sant.
Fungerar gör det iaf, kanske avr-gcc kompilatorn som känner igen det.

gunne:
Jadå, de går.
Jag la in för engelsk layout men de går säkert att fixa svensk. :)
Dock så måste åäö vara special tecken då de inte passar in i ekvationen för bokstäver som är där, men de är inga problem.

//Emil
Användarvisningsbild
exile
EF Sponsor
Inlägg: 496
Blev medlem: 21 oktober 2005, 23:32:07

Inlägg av exile »

mri: det beror på vilka argument du ger kompilatorn.....

exempel:
gnu99 tillåter att man deklarerar variabeln i for loopen,
medans c89 (ansi c) tillåter inte det....
Användarvisningsbild
Korken
Inlägg: 2230
Blev medlem: 3 februari 2006, 19:19:36
Ort: Luleå, Porsön

Inlägg av Korken »

En lite grej bara, den där kodsnutten är inte i en loop.
Kodsnutten du tog är från "LCD_Draw/Clear_Dot(unsigned char x, unsigned char y)".
Och om själv funktionen används i en loop spelar ju ingen roll väll?

//Emil
Användarvisningsbild
karlstedt
EF Sponsor
Inlägg: 966
Blev medlem: 14 oktober 2003, 16:55:23
Ort: Lund
Kontakt:

Inlägg av karlstedt »

Jag skulle nog vilja slå ett slag för att standarden som avr-gcc kör default är c99. Där tillåts man deklarera variabler efter "compound block".

gnu99, är det nåt hittepå? :)
Användarvisningsbild
Korken
Inlägg: 2230
Blev medlem: 3 februari 2006, 19:19:36
Ort: Luleå, Porsön

Inlägg av Korken »

Gnu99 är antagligen Linux versionen.

//Emil
Skriv svar