sshahryiar
Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh
|
Waveshare BD663474 2.2 Inch 320x240 TFT Touch LCD Library |
Posted: Sat Aug 13, 2016 10:58 pm |
|
|
font.c
Code: |
static const unsigned char font[96][5] =
{
{0x00, 0x00, 0x00, 0x00, 0x00} // 20
,{0x00, 0x00, 0x5F, 0x00, 0x00} // 21 !
,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
,{0x14, 0x7F, 0x14, 0x7f, 0x14} // 23 #
,{0x24, 0x2A, 0x7F, 0x2A, 0x12} // 24 $
,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
,{0x00, 0x1C, 0x22, 0x41, 0x00} // 28 (
,{0x00, 0x41, 0x22, 0x1C, 0x00} // 29 )
,{0x14, 0x08, 0x3E, 0x08, 0x14} // 2a *
,{0x08, 0x08, 0x3E, 0x08, 0x08} // 2b +
,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
,{0x3E, 0x51, 0x49, 0x45, 0x3E} // 30 0
,{0x00, 0x42, 0x7F, 0x40, 0x00} // 31 1
,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
,{0x21, 0x41, 0x45, 0x4B, 0x31} // 33 3
,{0x18, 0x14, 0x12, 0x7F, 0x10} // 34 4
,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
,{0x3C, 0x4a, 0x49, 0x49, 0x30} // 36 6
,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
,{0x06, 0x49, 0x49, 0x29, 0x1E} // 39 9
,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
,{0x32, 0x49, 0x79, 0x41, 0x3E} // 40 @
,{0x7E, 0x11, 0x11, 0x11, 0x7E} // 41 A
,{0x7F, 0x49, 0x49, 0x49, 0x36} // 42 B
,{0x3E, 0x41, 0x41, 0x41, 0x22} // 43 C
,{0x7F, 0x41, 0x41, 0x22, 0x1C} // 44 D
,{0x7F, 0x49, 0x49, 0x49, 0x41} // 45 E
,{0x7F, 0x09, 0x09, 0x09, 0x01} // 46 F
,{0x3E, 0x41, 0x49, 0x49, 0x7A} // 47 G
,{0x7F, 0x08, 0x08, 0x08, 0x7F} // 48 H
,{0x00, 0x41, 0x7F, 0x41, 0x00} // 49 I
,{0x20, 0x40, 0x41, 0x3F, 0x01} // 4a J
,{0x7F, 0x08, 0x14, 0x22, 0x41} // 4b K
,{0x7F, 0x40, 0x40, 0x40, 0x40} // 4c L
,{0x7F, 0x02, 0x0C, 0x02, 0x7F} // 4d M
,{0x7F, 0x04, 0x08, 0x10, 0x7F} // 4e N
,{0x3E, 0x41, 0x41, 0x41, 0x3E} // 4f O
,{0x7F, 0x09, 0x09, 0x09, 0x06} // 50 P
,{0x3E, 0x41, 0x51, 0x21, 0x5E} // 51 Q
,{0x7F, 0x09, 0x19, 0x29, 0x46} // 52 R
,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
,{0x01, 0x01, 0x7F, 0x01, 0x01} // 54 T
,{0x3F, 0x40, 0x40, 0x40, 0x3F} // 55 U
,{0x1F, 0x20, 0x40, 0x20, 0x1F} // 56 V
,{0x3F, 0x40, 0x38, 0x40, 0x3F} // 57 W
,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
,{0x00, 0x7F, 0x41, 0x41, 0x00} // 5b [
,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c ?
,{0x00, 0x41, 0x41, 0x7F, 0x00} // 5d ]
,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
,{0x7F, 0x48, 0x44, 0x44, 0x38} // 62 b
,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
,{0x38, 0x44, 0x44, 0x48, 0x7F} // 64 d
,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
,{0x08, 0x7E, 0x09, 0x01, 0x02} // 66 f
,{0x0C, 0x52, 0x52, 0x52, 0x3E} // 67 g
,{0x7F, 0x08, 0x04, 0x04, 0x78} // 68 h
,{0x00, 0x44, 0x7D, 0x40, 0x00} // 69 i
,{0x20, 0x40, 0x44, 0x3D, 0x00} // 6a j
,{0x7F, 0x10, 0x28, 0x44, 0x00} // 6b k
,{0x00, 0x41, 0x7F, 0x40, 0x00} // 6c l
,{0x7C, 0x04, 0x18, 0x04, 0x78} // 6d m
,{0x7C, 0x08, 0x04, 0x04, 0x78} // 6e n
,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
,{0x7C, 0x14, 0x14, 0x14, 0x08} // 70 p
,{0x08, 0x14, 0x14, 0x18, 0x7C} // 71 q
,{0x7C, 0x08, 0x04, 0x04, 0x08} // 72 r
,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
,{0x04, 0x3F, 0x44, 0x40, 0x20} // 74 t
,{0x3C, 0x40, 0x40, 0x20, 0x7C} // 75 u
,{0x1C, 0x20, 0x40, 0x20, 0x1C} // 76 v
,{0x3C, 0x40, 0x30, 0x40, 0x3C} // 77 w
,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
,{0x0C, 0x50, 0x50, 0x50, 0x3C} // 79 y
,{0x44, 0x64, 0x54, 0x4C, 0x44} // 7a z
,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
,{0x00, 0x00, 0x7F, 0x00, 0x00} // 7c |
,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e ?
,{0x78, 0x46, 0x41, 0x46, 0x78} // 7f ?
};
|
BD663474.h
Code: |
#define TFT_CS pin_A1
#define TFT_RS pin_A2
#define TFT_RST pin_D0
#define White 0xFFFF
#define Black 0x0000
#define Grey 0xF7DE
#define Grey2 0x8430
#define Light_Grey 0xC618
#define Dark_Grey 0x8410
#define Purple 0xF81F
#define Grey_Blue 0x5458
#define Blue 0x001F
#define Dark_Blue 0x01CF
#define Light_Blue 0x051F
#define Light_Blue_2 0x7D7C
#define Red 0xF800
#define Green 0x07E0
#define Cyan 0x7FFF
#define Yellow 0xFFE0
#define Orange 0xFC08
#define SQUARE 0
#define ROUND 1
#define NO 0
#define YES 1
#define ON 1
#define OFF 0
#define DAT 1
#define CMD 0
#define MAX_X 240
#define MAX_Y 320
void TFT_IO_init();
void TFT_reset();
void TFT_write_word(unsigned long value);
void TFT_write(unsigned long value, short mode);
void TFT_write_RAM_prepare();
void TFT_write_reg(unsigned long cmd_value, unsigned long data_value);
void TFT_init();
void TFT_set_display_address_window(signed long xs, signed long ys, signed long xe, signed long ye);
void TFT_set_cursor(signed long x_pos, signed long y_pos);
void TFT_fill(unsigned long colour);
unsigned long TFT_BGR2RGB(unsigned long colour);
unsigned long RGB565_converter(unsigned char r, unsigned char g, unsigned char b);
void swap_values(signed long *a, signed long *b);
void Draw_Pixel(signed long x_pos, signed long y_pos, unsigned long colour);
void Draw_Line(signed long x1, signed long y1, signed long x2, signed long y2, unsigned long colour);
void Draw_V_Line(signed long x1, signed long y1, signed long y2, unsigned colour);
void Draw_H_Line(signed long x1, signed long x2, signed long y1, unsigned colour);
void Draw_Triangle(signed long x1, signed long y1, signed long x2, signed long y2, signed long x3, signed long y3, short fill, unsigned long colour);
void Draw_Rectangle(signed long x1, signed long y1, signed long x2, signed long y2, short fill, unsigned char type, unsigned long colour, unsigned long back_colour);
void Draw_H_Bar(signed long x1, signed long x2, signed long y1, signed long bar_width, signed long bar_value, unsigned long border_colour, unsigned long bar_colour, unsigned long back_colour, unsigned char border);
void Draw_V_Bar(signed long x1, signed long y1, signed long y2, signed long bar_width, signed long bar_value, unsigned long border_colour, unsigned long bar_colour, unsigned long back_colour, unsigned char border);
void Draw_Circle(signed long xc, signed long yc, signed long radius, short fill, unsigned long colour);
void Draw_Font_Pixel(unsigned long x_pos, unsigned long y_pos, unsigned long colour, unsigned char pixel_size);
void print_char(unsigned long x_pos, unsigned long y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, char ch);
void print_str(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, char *ch);
void print_C(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, signed long value);
void print_I(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, signed long value);
void print_D(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, unsigned long value, unsigned char points);
void print_F(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, float value, unsigned char points);
void Draw_BMP(unsigned long x_pos, unsigned long y_pos, unsigned long H, unsigned long W, unsigned char *bitmap);
|
BD663474.c
Code: |
#include "BD663474.h"
#include "font.c"
void TFT_IO_init()
{
set_TRIS_A(0xE9);
set_TRIS_C(0xD7);
set_TRIS_D(0xFE);
output_high(TFT_CS);
setup_SPI(SPI_MASTER | SPI_XMIT_L_TO_H | SPI_SCK_IDLE_HIGH | SPI_SAMPLE_AT_MIDDLE);
}
void TFT_reset()
{
output_low(TFT_RST);
delay_us(3);
output_high(TFT_RST);
delay_us(3);
}
void TFT_write_word(unsigned long value)
{
SPI_write(value >> 8);
SPI_write(value);
}
void TFT_write(unsigned long value, short mode)
{
output_bit(TFT_RS, mode);
output_low(TFT_CS);
TFT_write_word(value);
output_high(TFT_CS);
}
void TFT_write_RAM_prepare()
{
TFT_write(0x0202, CMD);
}
void TFT_write_reg(unsigned long cmd_value, unsigned long data_value)
{
TFT_write(cmd_value, CMD);
TFT_write(data_value, DAT);
}
void TFT_init()
{
TFT_IO_init();
TFT_reset();
TFT_write_reg(0x000, 0x0001);
delay_ms(10);
TFT_write_reg(0x100, 0x0000);
TFT_write_reg(0x101, 0x0000);
TFT_write_reg(0x102, 0x3110);
TFT_write_reg(0x103, 0xE200);
TFT_write_reg(0x110, 0x009D);
TFT_write_reg(0x111, 0x0022);
TFT_write_reg(0x100, 0x0120);
delay_ms(20);
TFT_write_reg(0x100, 0x3120);
delay_ms(80);
TFT_write_reg(0x001, 0x0100);
TFT_write_reg(0x002, 0x0000);
TFT_write_reg(0x003, 0x1238);//
TFT_write_reg(0x006, 0x0000);
TFT_write_reg(0x007, 0x0101);
TFT_write_reg(0x008, 0x0808);
TFT_write_reg(0x009, 0x0000);
TFT_write_reg(0x00B, 0x0000);
TFT_write_reg(0x00C, 0x0000);
TFT_write_reg(0x00D, 0x0018);
TFT_write_reg(0x012, 0x0000);
TFT_write_reg(0x013, 0x0000);
TFT_write_reg(0x018, 0x0000);
TFT_write_reg(0x019, 0x0000);
TFT_write_reg(0x203, 0x0000);
TFT_write_reg(0x204, 0x0000);
TFT_write_reg(0x210, 0x0000);
TFT_write_reg(0x211, 0x00EF);
TFT_write_reg(0x212, 0x0000);
TFT_write_reg(0x213, 0x013F);
TFT_write_reg(0x214, 0x0000);
TFT_write_reg(0x215, 0x0000);
TFT_write_reg(0x216, 0x0000);
TFT_write_reg(0x217, 0x0000);
TFT_write_reg(0x300, 0x5343);
TFT_write_reg(0x301, 0x1021);
TFT_write_reg(0x302, 0x0003);
TFT_write_reg(0x303, 0x0011);
TFT_write_reg(0x304, 0x050A);
TFT_write_reg(0x305, 0x4342);
TFT_write_reg(0x306, 0x1100);
TFT_write_reg(0x307, 0x0003);
TFT_write_reg(0x308, 0x1201);
TFT_write_reg(0x309, 0x050A);
TFT_write_reg(0x400, 0x4027);
TFT_write_reg(0x401, 0x0000);
TFT_write_reg(0x402, 0x0000);
TFT_write_reg(0x403, 0x013F);
TFT_write_reg(0x404, 0x0000);
TFT_write_reg(0x200, 0x0000);
TFT_write_reg(0x201, 0x0000);
TFT_write_reg(0x100, 0x7120);
TFT_write_reg(0x007, 0x0103);
delay_ms(10);
TFT_write_reg(0x007, 0x0113);
}
void TFT_set_display_address_window(signed long xs, signed long ys, signed long xe, signed long ye)
{
TFT_write_reg(0x210, xs);
TFT_write_reg(0x211, xe);
TFT_write_reg(0x212, ys);
TFT_write_reg(0x213, ye);
}
void TFT_set_cursor(signed long x_pos, signed long y_pos)
{
TFT_write_reg(0x200, x_pos);
TFT_write_reg(0x201, y_pos);
TFT_write_RAM_prepare();
}
void TFT_fill(unsigned long colour)
{
unsigned long long index = (MAX_X * MAX_Y);
TFT_set_display_address_window(0, 0, 239, 319);
TFT_set_cursor(0, 0);
while(index)
{
TFT_write(colour, DAT);
index--;
};
}
unsigned long TFT_BGR2RGB(unsigned long colour)
{
unsigned long r = 0x0000;
unsigned long g = 0x0000;
unsigned long b = 0x0000;
unsigned long rgb_colour = 0x0000;
b = ((colour >> 0) & 0x1F);
g = ((colour >> 5) & 0x3F);
r = ((colour >> 11) & 0x1F);
rgb_colour = ((b << 11) + (g << 5) + r);
return rgb_colour;
}
unsigned long RGB565_converter(unsigned char r, unsigned char g, unsigned char b)
{
return (((((unsigned long)r) >> 3) << 11) | ((((unsigned long)g) >> 2) << 5) | (((unsigned long)b) >> 3));
}
void swap_values(signed long *a, signed long *b)
{
signed long temp = 0x0000;
temp = *b;
*b = *a;
*a = temp;
}
void Draw_Pixel(signed long x_pos, signed long y_pos, unsigned long colour)
{
unsigned char i = 2;
if((x_pos >= MAX_X) || (y_pos >= MAX_Y) || (x_pos < 0) || (y_pos < 0))
{
return;
}
TFT_set_display_address_window(x_pos, y_pos, (x_pos + 1), (y_pos + 1));
TFT_set_cursor(x_pos, y_pos);
while(i)
{
TFT_write(colour, DAT);
i--;
}
}
void Draw_Line(signed long x1, signed long y1, signed long x2, signed long y2, unsigned long colour)
{
signed long dx = 0x0000;
signed long dy = 0x0000;
signed long stepx = 0x0000;
signed long stepy = 0x0000;
signed long fraction = 0x0000;
dy = (y2 - y1);
dx = (x2 - x1);
if(dy < 0)
{
dy = -dy;
stepy = -1;
}
else
{
stepy = 1;
}
if(dx < 0)
{
dx = -dx;
stepx = -1;
}
else
{
stepx = 1;
}
dx <<= 0x01;
dy <<= 0x01;
Draw_Pixel(x1, y1, colour);
if(dx > dy)
{
fraction = (dy - (dx >> 1));
while(x1 != x2)
{
if(fraction >= 0)
{
y1 += stepy;
fraction -= dx;
}
x1 += stepx;
fraction += dy;
Draw_Pixel(x1, y1, colour);
}
}
else
{
fraction = (dx - (dy >> 1));
while(y1 != y2)
{
if (fraction >= 0)
{
x1 += stepx;
fraction -= dy;
}
y1 += stepy;
fraction += dx;
Draw_Pixel(x1, y1, colour);
}
}
}
void Draw_V_Line(signed long x1, signed long y1, signed long y2, unsigned colour)
{
if(y1 > y2)
{
swap_values(&y1, &y2);
}
while(y2 > (y1 - 1))
{
Draw_Pixel(x1, y2, colour);
y2--;
}
}
void Draw_H_Line(signed long x1, signed long x2, signed long y1, unsigned colour)
{
if(x1 > x2)
{
swap_values(&x1, &x2);
}
while(x2 > (x1 - 1))
{
Draw_Pixel(x2, y1, colour);
x2--;
}
}
void Draw_Triangle(signed long x1, signed long y1, signed long x2, signed long y2, signed long x3, signed long y3, short fill, unsigned long colour)
{
signed long a = 0;
signed long b = 0;
signed long sa = 0;
signed long sb = 0;
signed long yp = 0;
signed long last = 0;
signed long dx12 = 0;
signed long dx23 = 0;
signed long dx13 = 0;
signed long dy12 = 0;
signed long dy23 = 0;
signed long dy13 = 0;
switch(fill)
{
case YES:
{
if(y1 > y2)
{
swap_values(&y1, &y2);
swap_values(&x1, &x2);
}
if(y2 > y3)
{
swap_values(&y3, &y2);
swap_values(&x3, &x2);
}
if(y1 > y2)
{
swap_values(&y1, &y2);
swap_values(&x1, &x2);
}
if(y1 == y3)
{
a = b = x1;
if(x2 < a)
{
a = x2;
}
else if(x2 > b)
{
b = x2;
}
if(x2 < a)
{
a = x3;
}
else if(x3 > b)
{
b = x3;
}
Draw_H_Line(a, (a + (b - (a + 1))), y1, colour);
return;
}
dx12 = (x2 - x1);
dy12 = (y2 - y1);
dx13 = (x3 - x1);
dy13 = (y3 - y1);
dx23 = (x3 - x2);
dy23 = (y3 - y2);
sa = 0,
sb = 0;
if(y2 == y3)
{
last = y2;
}
else
{
last = (y2 - 1);
}
for(yp = y1; yp <= last; yp++)
{
a = (x1 + (sa / dy12));
b = (x1 + (sb / dy13));
sa += dx12;
sb += dx13;
if(a > b)
{
swap_values(&a, &b);
}
Draw_H_Line(a, (a + (b - (a + 1))), yp, colour);
}
sa = (dx23 * (yp - y2));
sb = (dx13 * (yp - y1));
for(; yp <= y3; yp++)
{
a = (x2 + (sa / dy23));
b = (x1 + (sb / dy13));
sa += dx23;
sb += dx13;
if(a > b)
{
swap_values(&a, &b);
}
Draw_H_Line(a, (a + (b - (a + 1))), yp, colour);
}
break;
}
default:
{
Draw_Line(x1, y1, x2, y2, colour);
Draw_Line(x2, y2, x3, y3, colour);
Draw_Line(x3, y3, x1, y1, colour);
break;
}
}
}
void Draw_Rectangle(signed long x1, signed long y1, signed long x2, signed long y2, short fill, unsigned char type, unsigned long colour, unsigned long back_colour)
{
signed long i = 0x0000;
signed long xmin = 0x0000;
signed long xmax = 0x0000;
signed long ymin = 0x0000;
signed long ymax = 0x0000;
switch(fill)
{
case YES:
{
if(x1 < x2)
{
xmin = x1;
xmax = x2;
}
else
{
xmin = x2;
xmax = x1;
}
if(y1 < y2)
{
ymin = y1;
ymax = y2;
}
else
{
ymin = y2;
ymax = y1;
}
for(; xmin <= xmax; ++xmin)
{
for(i = ymin; i <= ymax; ++i)
{
Draw_Pixel(xmin, i, colour);
}
}
break;
}
default:
{
Draw_V_Line(x1, y1, y2, colour);
Draw_V_Line(x2, y1, y2, colour);
Draw_H_Line(x1, x2, y1, colour);
Draw_H_Line(x1, x2, y2, colour);
break;
}
}
if(type != SQUARE)
{
Draw_Pixel(x1, y1, back_colour);
Draw_Pixel(x1, y2, back_colour);
Draw_Pixel(x2, y1, back_colour);
Draw_Pixel(x2, y2, back_colour);
}
}
void Draw_H_Bar(signed long x1, signed long x2, signed long y1, signed long bar_width, signed long bar_value, unsigned long border_colour, unsigned long bar_colour, unsigned long back_colour, unsigned char border)
{
switch(border)
{
case YES:
{
Draw_Rectangle((x1 + 1), (y1 + 1), (x1 + bar_value), (y1 + bar_width - 1), YES, SQUARE, bar_colour, back_colour);
Draw_Rectangle((x2 - 1), (y1 + 1), (x1 + bar_value + 1), (y1 + bar_width - 1), YES, SQUARE, back_colour, back_colour);
Draw_Rectangle(x1, y1, x2, (y1 + bar_width), NO, SQUARE, border_colour, back_colour);
break;
}
default:
{
Draw_Rectangle(x1, y1, (x1 + bar_value), (y1 + bar_width), YES, SQUARE, bar_colour, back_colour);
Draw_Rectangle(x2, y1, (x1 + bar_value), (y1 + bar_width), YES, SQUARE, back_colour, back_colour);
break;
}
}
}
void Draw_V_Bar(signed long x1, signed long y1, signed long y2, signed long bar_width, signed long bar_value, unsigned long border_colour, unsigned long bar_colour, unsigned long back_colour, unsigned char border)
{
switch(border)
{
case YES:
{
Draw_Rectangle((x1 + 1), (y2 - 1), (x1 + bar_width - 1), (y2 - bar_value), YES, SQUARE, bar_colour, back_colour);
Draw_Rectangle((x1 + 1), (y2 - bar_value - 1), (x1 + bar_width - 1), (y1 + 1), YES, SQUARE, back_colour, back_colour);
Draw_Rectangle(x1, y1, (x1 + bar_width), y2, NO, SQUARE, border_colour, back_colour);
break;
}
default:
{
Draw_Rectangle(x1, y2, (x1 + bar_width), (y2 - bar_value), YES, SQUARE, bar_colour, back_colour);
Draw_Rectangle(x1, (y2 - bar_value), (x1 + bar_width), y1, YES, SQUARE, back_colour, back_colour);
break;
}
}
}
void Draw_Circle(signed long xc, signed long yc, signed long radius, short fill, unsigned long colour)
{
signed long a = 0x0000;
signed long b = 0x0000;
signed long p = 0x0000;
b = radius;
p = (1 - b);
do
{
switch(fill)
{
case YES:
{
Draw_Line((xc - a), (yc + b), (xc + a), (yc + b), colour);
Draw_Line((xc - a), (yc - b), (xc + a), (yc - b), colour);
Draw_Line((xc - b), (yc + a), (xc + b), (yc + a), colour);
Draw_Line((xc - b), (yc - a), (xc + b), (yc - a), colour);
break;
}
default:
{
Draw_Pixel((xc + a), (yc + b), colour);
Draw_Pixel((xc + b), (yc + a), colour);
Draw_Pixel((xc - a), (yc + b), colour);
Draw_Pixel((xc - b), (yc + a), colour);
Draw_Pixel((xc + b), (yc - a), colour);
Draw_Pixel((xc + a), (yc - b), colour);
Draw_Pixel((xc - a), (yc - b), colour);
Draw_Pixel((xc - b), (yc - a), colour);
break;
}
}
if(p < 0)
{
p += (0x03 + (0x02 * a++));
}
else
{
p += (0x05 + (0x02 * ((a++) - (b--))));
}
}while(a <= b);
}
void Draw_Font_Pixel(unsigned long x_pos, unsigned long y_pos, unsigned long colour, unsigned char pixel_size)
{
unsigned long i = 0x0000;
TFT_set_cursor(x_pos, y_pos);
for(i = 0x0000; i < (pixel_size * pixel_size); i++)
{
TFT_write(colour, DAT);
}
}
void print_char(unsigned long x_pos, unsigned long y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, char ch)
{
unsigned char i = 0x00;
unsigned char j = 0x00;
unsigned long value = 0x0000;
if(font_size <= 0)
{
font_size = 1;
}
if(x_pos < font_size)
{
x_pos = font_size;
}
for(i = 0x00; i < 0x05; i++)
{
for(j = 0x00; j < 0x08; j++)
{
value = 0x0000;
value = ((font[((unsigned char)ch) - 0x20][i]));
if(((value >> j) & 0x01) != 0x00)
{
Draw_Font_Pixel(x_pos, y_pos, colour, font_size);
}
else
{
Draw_Font_Pixel(x_pos, y_pos, back_colour, font_size);
}
y_pos += font_size;
}
y_pos -= (font_size << 0x03);
x_pos++;
}
}
void print_str(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, char *ch)
{
do
{
print_char(x_pos, y_pos, font_size, colour, back_colour, *ch++);
x_pos += 0x06;
}while((*ch >= 0x20) && (*ch <= 0x7F));
print_char(x_pos, y_pos, font_size, colour, back_colour, 0x20);
}
void print_C(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, signed long value)
{
unsigned char ch[6] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
if(value < 0x00)
{
ch[0] = 0x2D;
value = -value;
}
else
{
ch[0] = 0x20;
}
if((value > 99) && (value <= 999))
{
ch[1] = ((value / 100) + 0x30);
ch[2] = (((value % 100) / 10) + 0x30);
ch[3] = ((value % 10) + 0x30);
}
else if((value > 9) && (value <= 99))
{
ch[1] = (((value % 100) / 10) + 0x30);
ch[2] = ((value % 10) + 0x30);
ch[3] = 0x20;
}
else if((value >= 0) && (value <= 9))
{
ch[1] = ((value % 10) + 0x30);
ch[2] = 0x20;
ch[3] = 0x20;
}
print_str(x_pos, y_pos, font_size, colour, back_colour, ch);
}
void print_I(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, signed long value)
{
unsigned char ch[6] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
if(value < 0)
{
ch[0] = 0x2D;
value = -value;
}
else
{
ch[0] = 0x20;
}
if(value > 9999)
{
ch[1] = ((value / 10000) + 0x30);
ch[2] = (((value % 10000)/ 1000) + 0x30);
ch[3] = (((value % 1000) / 100) + 0x30);
ch[4] = (((value % 100) / 10) + 0x30);
ch[5] = ((value % 10) + 0x30);
}
else if((value > 999) && (value <= 9999))
{
ch[1] = (((value % 10000)/ 1000) + 0x30);
ch[2] = (((value % 1000) / 100) + 0x30);
ch[3] = (((value % 100) / 10) + 0x30);
ch[4] = ((value % 10) + 0x30);
ch[5] = 0x20;
}
else if((value > 99) && (value <= 999))
{
ch[1] = (((value % 1000) / 100) + 0x30);
ch[2] = (((value % 100) / 10) + 0x30);
ch[3] = ((value % 10) + 0x30);
ch[4] = 0x20;
ch[5] = 0x20;
}
else if((value > 9) && (value <= 99))
{
ch[1] = (((value % 100) / 10) + 0x30);
ch[2] = ((value % 10) + 0x30);
ch[3] = 0x20;
ch[4] = 0x20;
ch[5] = 0x20;
}
else
{
ch[1] = ((value % 10) + 0x30);
ch[2] = 0x20;
ch[3] = 0x20;
ch[4] = 0x20;
ch[5] = 0x20;
}
print_str(x_pos, y_pos, font_size, colour, back_colour, ch);
}
void print_D(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, unsigned long value, unsigned char points)
{
unsigned char ch[6] = {0x2E, 0x20, 0x20, 0x20, 0x20, 0x20};
ch[1] = ((value / 1000) + 0x30);
if(points > 1)
{
ch[2] = (((value % 1000) / 100) + 0x30);
if(points > 2)
{
ch[3] = (((value % 100) / 10) + 0x30);
if(points > 3)
{
ch[4] = ((value % 10) + 0x30);
}
}
}
print_str(x_pos, y_pos, font_size, colour, back_colour, ch);
}
void print_F(unsigned char x_pos, unsigned char y_pos, unsigned char font_size, unsigned long colour, unsigned long back_colour, float value, unsigned char points)
{
signed long tmp = 0x0000;
tmp = value;
print_I(x_pos, y_pos, font_size, colour, back_colour, tmp);
tmp = ((value - tmp) * 10000);
if(tmp < 0)
{
tmp = -tmp;
}
if((value >= 10000) && (value < 100000))
{
print_D((x_pos + 36), y_pos, font_size, colour, back_colour, tmp, points);
}
else if((value >= 1000) && (value < 10000))
{
print_D((x_pos + 30), y_pos, font_size, colour, back_colour, tmp, points);
}
else if((value >= 100) && (value < 1000))
{
print_D((x_pos + 24), y_pos, font_size, colour, back_colour, tmp, points);
}
else if((value >= 10) && (value < 100))
{
print_D((x_pos + 18), y_pos, font_size, colour, back_colour, tmp, points);
}
else if(value < 10)
{
print_D((x_pos + 12), y_pos, font_size, colour, back_colour, tmp, points);
if((value) < 0)
{
print_char(x_pos, y_pos, font_size, colour, back_colour, 0x2D);
}
else
{
print_char(x_pos, y_pos, font_size, colour, back_colour, 0x20);
}
}
}
void Draw_BMP(unsigned long x_pos, unsigned long y_pos, unsigned long H, unsigned long W, unsigned char *bitmap)
{
unsigned long index = 0;
unsigned long size = (W * H);
unsigned long *bitmap_ptr = (unsigned long*)bitmap;
TFT_set_display_address_window(x_pos, y_pos, (W - 1), (H - 1));
TFT_write_RAM_prepare();
for(index = 0; index < size; index++)
{
TFT_write(*bitmap_ptr++, DAT);
}
TFT_set_display_address_window(0, 0, 239, 319);
}
|
XPT2046.h
Code: |
#define TP_INT input(pin_A0)
#define TP_CS pin_A4
#define CMD_RDY 0x90
#define CMD_RDX 0xD0
#define error 50.0
#define RL_min 200.0
#define RL_max 3800.0
#define RW_min 200.0
#define RW_max 3800.0
#define res_l 319.0
#define res_w 239.0
unsigned long tpx = 0x0000;
unsigned long tpy = 0x0000;
void tp_init();
void tp_IO_init();
unsigned long TP_read();
void TP_read_coordinates(unsigned long *x_pos, unsigned long *y_pos);
unsigned long filter_data(unsigned long value, unsigned char axis);
float map(float value, float x_min, float x_max, float y_min, float y_max);
float constrain(float value, float value_min, float value_max);
|
XPT2046.c
Code: |
#include "XPT2046.h"
void tp_init()
{
tp_IO_init();
output_high(TP_CS);
setup_SPI(SPI_MASTER | SPI_XMIT_L_TO_H | SPI_SCK_IDLE_HIGH | SPI_SAMPLE_AT_MIDDLE);
}
void tp_IO_init()
{
set_TRIS_A(0xE9);
set_TRIS_C(0xD7);
}
unsigned long TP_read()
{
unsigned char lb = 0x00;
unsigned char hb = 0x00;
unsigned long value = 0x0000;
hb = SPI_read(0);
lb = SPI_read(0);
value = hb;
value <<= 8;
value |= lb;
value >>= 3;
value &= 0x0FFF;
return value;
}
void TP_read_coordinates(unsigned long *x_pos, unsigned long *y_pos)
{
unsigned long temp = 0x0000;
unsigned long avg_x = 0x0000;
unsigned long avg_y = 0x0000;
unsigned char samples = 0x10;
output_low(TP_CS);
while(samples > 0)
{
SPI_write(CMD_RDY);
avg_y += TP_read();
SPI_write(CMD_RDX);
avg_x += TP_read();
samples--;
};
output_high(TP_CS);
temp = (avg_x >> 4);
temp = filter_data(temp, 0);
*x_pos = temp;
temp = (avg_y >> 4);
temp = filter_data(temp, 1);
*y_pos = temp;
}
unsigned long filter_data(unsigned long value, unsigned char axis)
{
float temp[3];
float sum = 0.0;
float min_R = 0.0;
float max_R = 0.0;
float axis_max = 0.0;
unsigned long res = 0x0000;
unsigned char i = 0x00;
switch(axis)
{
case 1:
{
min_R = RL_min;
max_R = RL_max;
axis_max = res_l;
break;
}
default:
{
min_R = RW_min;
max_R = RW_max;
axis_max = res_w;
break;
}
}
temp[0] = map((((float)value) - error), min_R, max_R, 0.0, axis_max);
temp[1] = map((((float)value) + error), min_R, max_R, 0.0, axis_max);
temp[2] = map(((float)value), min_R, max_R, 0.0, axis_max);
for(i = 0; i < 3; i++)
{
sum += temp[i];
}
sum /= 3.0;
res = constrain(sum, 0.0, axis_max);
return res;
}
float map(float value, float x_min, float x_max, float y_min, float y_max)
{
return (y_min + (((y_max - y_min) / (x_max - x_min)) * (value - x_min)));
}
float constrain(float value, float value_min, float value_max)
{
if(value >= value_max)
{
return value_max;
}
else if(value <= value_min)
{
return value_min;
}
else
{
return value;
}
}
|
Example Code
Code: |
#include <18F4520.h>
#device *= 16
#device ADC = 10
#device CONST = ROM
#device CONST = READ_ONLY
#fuses NOWDT, INTRC_IO, FCMEN, IESO, BROWNOUT, BORV27, NODEBUG, CPD, NOWRTB, NOWRTD, PUT
#fuses MCLR, NOPBADEN, NOLPT1OSC, NOLVP, STVREN, NOXINST, NOWRT, PROTECT, NOCPB, EBTR
#use delay (internal = 32MHz)
#include "XPT2046.c"
#include "BD663474.c"
void setup();
void test_colours();
void test_2D_graphics();
void draw_flag();
void test_RGB();
void print_test_data();
void main()
{
unsigned char msg[11] = {"Draw Check"};
setup();
test_RGB();
test_colours();
test_2D_graphics();
draw_flag();
print_test_data();
print_str(10, 10, 2, White, Black, msg);
while(TRUE)
{
TP_read_coordinates(&tpx, &tpy);
Draw_Circle(tpx, tpy, 6, YES, White);
};
}
void setup()
{
disable_interrupts(GLOBAL);
setup_WDT(WDT_off);
setup_oscillator(OSC_32MHZ | OSC_PLL_ON);
port_B_pullups(FALSE);
setup_comparator(NC_NC_NC_NC);
setup_ADC(ADC_off);
setup_ADC_ports(no_analogs);
setup_PSP(PSP_disabled);
setup_SPI(SPI_MASTER | SPI_XMIT_L_TO_H | SPI_SCK_IDLE_HIGH | SPI_SAMPLE_AT_MIDDLE);
setup_SPI2(SPI_disabled | SPI_SS_disabled);
setup_timer_0(T0_off | T0_8_BIT);
set_timer0(0);
setup_timer_1(T1_disabled);
set_timer1(0);
setup_timer_2(T2_disabled, 255, 1);
set_timer2(0);
setup_timer_3(T1_disabled);
set_timer3(0);
setup_CCP1(CCP_off);
setup_CCP2(CCP_off);
TFT_init();
tp_init();
delay_ms(200);
}
void draw_flag()
{
Draw_Rectangle(20, 20, 219, 299, YES, SQUARE, Green, Green);
Draw_Circle(120, 160, 60, YES, Red);
delay_ms(4000);
TFT_fill(Black);
}
void test_RGB()
{
TFT_fill(Red);
delay_ms(600);
TFT_fill(Green);
delay_ms(600);
TFT_fill(Blue);
delay_ms(600);
TFT_fill(Black);
}
void test_colours()
{
Draw_Rectangle(0, 0, 23, 319, YES, SQUARE, Dark_Grey, Dark_Grey);
Draw_Rectangle(24, 0, 47, 319, YES, SQUARE, Light_Grey, Light_Grey);
Draw_Rectangle(48, 0, 71, 319, YES, SQUARE, White, White);
Draw_Rectangle(72, 0, 95, 319, YES, SQUARE, Cyan, Cyan);
Draw_Rectangle(96, 0, 119, 319, YES, SQUARE, Dark_Blue, Dark_Blue);
Draw_Rectangle(120, 0, 143, 319, YES, SQUARE, Purple, Purple);
Draw_Rectangle(144, 0, 167, 319, YES, SQUARE, Red, Red);
Draw_Rectangle(168, 0, 191, 319, YES, SQUARE, Orange, Orange);
Draw_Rectangle(192, 0, 215, 319, YES, SQUARE, Yellow, Yellow);
Draw_Rectangle(216, 0, 239, 319, YES, SQUARE, Green, Green);
delay_ms(2000);
TFT_fill(Black);
}
void test_2D_graphics()
{
signed long i = 0;
TFT_fill(Black);
for(i = 10; i <= 310; i += 10)
{
Draw_V_Line(i, 20, 300, White);
}
for(i = 20; i <= 300; i += 10)
{
Draw_H_Line(10, 230, i, White);
}
delay_ms(3000);
TFT_fill(Black);
for(i = 0; i < 240; i += 5)
{
Draw_Line(0, 0, i, 319, Light_Blue);
}
for(i = 239; i > -1; i -= 5)
{
Draw_Line(239, 319, i, 0, Blue);
}
delay_ms(3000);
TFT_fill(Black);
Draw_Rectangle(9, 9, 230, 310, NO, ROUND, Blue, Black);
Draw_Rectangle(19, 19, 220, 300, YES, SQUARE, Cyan, Black);
delay_ms(3000);
TFT_fill(Black);
Draw_Circle(119, 159, 80, NO, Red);
Draw_Circle(119, 159, 40, YES, Yellow);
delay_ms(3000);
TFT_fill(Black);
Draw_Triangle(10, 310, 120, 10, 230, 310, NO, Green);
delay_ms(900);
Draw_Triangle(10, 310, 120, 10, 230, 310, YES, Grey_Blue);
delay_ms(3000);
TFT_fill(Black);
}
void print_test_data()
{
unsigned char msg1[11] = {"MicroArena"};
unsigned char msg2[36] = {"https://www.facebook.com/MicroArena"};
unsigned char s = 0;
signed char c = -9;
signed long i = -9;
float f = -9.0;
print_str(10, 10, 1, Red, Black, msg1);
print_str(10, 30, 2, Green, Black, msg1);
print_str(10, 60, 3, Blue, Black, msg1);
print_str(10, 90, 3, White, Black, msg2);
for(s = 0; s < 128; s++)
{
print_C(10, 160, 1, Red, Black, c);
print_I(60, 160, 2, Green, Black, i);
print_F(120, 160, 3, Blue, Black, f, 1);
c++;
i++;
f += 0.1;
}
TFT_fill(Black);
}
|
Videos:
https://www.youtube.com/watch?v=TWaYD_7blFQ
https://www.youtube.com/watch?v=HP_tnB31Rug _________________ https://www.facebook.com/MicroArena
SShahryiar |
|