// LCD driver, 128×32 pixels
#include
#include “c12832.H”
#include “string.h”
#include “Small_7.h”
const uint32_t CS1 = 1UL << 0; // chip selection, active low, PTD0
const uint32_t RST = 1UL << 3; // reset, active low, PTD3
const uint32_t A0 = 1UL << 9; // Register selection, PTC9. A0=0: instruction; A0=1: data
const uint32_t SCL = 1UL << 1; // clock, PTD1
const uint32_t SI = 1UL << 2; // data, PTD2
unsigned char buffer[512];
unsigned char* font;
unsigned int char_x;
unsigned int char_y;
void Delay_us(uint32_t nCount)
{
while(nCount--)
{
}
}
/*----------------------------------------------------------------------------
LCD pin config
*----------------------------------------------------------------------------*/
void LCD_Config(void)
{
SIM->SCGC5 |= (1UL << 11) | (1UL << 12); /* Enable Clock to Port C & D */
PORTC->PCR[9] = (1UL << 8); /* Pin PTC9 is GPIO */
PORTD->PCR[0] = (1UL << 8); /* Pin PTD0 is GPIO */
PORTD->PCR[1] = (1UL << 8); /* Pin PTD1 is GPIO */
PORTD->PCR[2] = (1UL << 8); /* Pin PTD2 is GPIO */
PORTD->PCR[3] = (1UL << 8); /* Pin PTD3 is GPIO */
PTD->PDDR = (CS1 | RST | SCL | SI ); /* enable PTD0,1,2,3 as Output */
PTD->PCOR = (CS1 | RST | SCL | SI ); /* output all 0 */
PTC->PDDR = A0; /* enable PTC9 as Output */
PTC->PCOR = A0; /* output 0 */
return;
}
/*—————————————————————————
signal out, data or commmand
*—————————————————————————*/
void LCD_Write(uint8_t x, int8_t i)
{
uint8_t n;
PTD->PCOR = CS1; // CS1 = 0
if(i==0)
PTC->PCOR = A0; // A0 = 0
else
PTC->PSOR = A0; // A0 = 1
for(n=0; n<8; n++){
PTD->PCOR = SCL; // SCL = 0;
if(x&0x80)
PTD->PSOR = SI; // output 1
else
PTD->PCOR = SI; // output 0
// Delay_us(1); // delay xx ms
PTD->PSOR = SCL; // SCL = 1;
x <<= 1; }
PTD->PSOR = CS1; // CS = 1;
}
void locate(int x, int y)
{
char_x = x;
char_y = y;
}
void set_font(unsigned char* f)
{
font = f;
}
/****************************************************
* LCD Initialization *
*****************************************************/
void init_LCD(void)
{
PTD->PSOR = CS1; // CS1 = 1
PTD->PCOR = RST; // RST = 0
Delay_us(50); // delay 50us
PTD->PSOR = RST; // RST = 1
Delay_us(5000); // delay 5ms
LCD_Write(0xAE,0);
LCD_Write(0xA2,0);
LCD_Write(0xA0,0);
LCD_Write(0xC8,0);
LCD_Write(0x22,0);
LCD_Write(0x2F,0);
LCD_Write(0x40,0);
LCD_Write(0xAF,0);
LCD_Write(0x81,0);
LCD_Write(0x17,0);
LCD_Write(0xA6,0);
locate(0,0);
set_font((unsigned char*)Small_7); // standart font
// copy_to_lcd();
}
void copy_to_lcd(void)
{
int i=0;
//page 0
LCD_Write(0x00,0); // set column low nibble 0
LCD_Write(0x10,0); // set column hi nibble 0
LCD_Write(0xB0,0); // set page address 0
for(i=0; i<128; i++) {
LCD_Write(buffer[i],1);
}
// page 1
LCD_Write(0x00,0); // set column low nibble 0
LCD_Write(0x10,0); // set column hi nibble 0
LCD_Write(0xB1,0); // set page address 1
for(i=128; i<256; i++) {
LCD_Write(buffer[i],1);
}
//page 2
LCD_Write(0x00,0); // set column low nibble 0
LCD_Write(0x10,0); // set column hi nibble 0
LCD_Write(0xB2,0); // set page address 2
for(i=256; i<384; i++) {
LCD_Write(buffer[i],1);
}
//page 3
LCD_Write(0x00,0); // set column low nibble 0
LCD_Write(0x10,0); // set column hi nibble 0
LCD_Write(0xB3,0); // set page address 3
FPTD->PCOR = CS1; // CS1 = 0
for(i=384; i<512; i++) {
LCD_Write(buffer[i],1);
}
}
void pixel(int x, int y, int color)
{
// first check parameter
if(x > 128 || y > 32 || x < 0 || y < 0) return;
// if(draw_mode == NORMAL) {
if(color == 0)
buffer[x + ((y/8) * 128)] &= ~(1 << (y%8)); // erase pixel
else
buffer[x + ((y/8) * 128)] |= (1 << (y%8)); // set pixel
/* } else { // XOR mode
if(color == 1)
buffer[x + ((y/8) * 128)] ^= (1 << (y%8)); // xor pixel
}*/
}
void line(int x0, int y0, int x1, int y1, int color)
{
int dx = 0, dy = 0;
int dx_sym = 0, dy_sym = 0;
int dx_x2 = 0, dy_x2 = 0;
int di = 0;
dx = x1-x0;
dy = y1-y0;
// if (dx == 0) { /* vertical line */
// if (y1 > y0) vline(x0,y0,y1,color);
// else vline(x0,y1,y0,color);
// return;
// }
if (dx > 0) {
dx_sym = 1;
} else {
dx_sym = -1;
}
// if (dy == 0) { /* horizontal line */
// if (x1 > x0) hline(x0,x1,y0,color);
// else hline(x1,x0,y0,color);
// return;
// }
if (dy > 0) {
dy_sym = 1;
} else {
dy_sym = -1;
}
dx = dx_sym*dx;
dy = dy_sym*dy;
dx_x2 = dx*2;
dy_x2 = dy*2;
if (dx >= dy) {
di = dy_x2 – dx;
while (x0 != x1) {
pixel(x0, y0, color);
x0 += dx_sym;
if (di<0) {
di += dy_x2;
} else {
di += dy_x2 - dx_x2;
y0 += dy_sym;
}
}
pixel(x0, y0, color);
} else {
di = dx_x2 - dy;
while (y0 != y1) {
pixel(x0, y0, color);
y0 += dy_sym;
if (di < 0) {
di += dx_x2;
} else {
di += dx_x2 - dy_x2;
x0 += dx_sym;
}
}
pixel(x0, y0, color);
}
copy_to_lcd();
}
void rect(int x0, int y0, int x1, int y1, int color)
{
if (x1 > x0) line(x0,y0,x1,y0,color);
else line(x1,y0,x0,y0,color);
if (y1 > y0) line(x0,y0,x0,y1,color);
else line(x0,y1,x0,y0,color);
if (x1 > x0) line(x0,y1,x1,y1,color);
else line(x1,y1,x0,y1,color);
if (y1 > y0) line(x1,y0,x1,y1,color);
else line(x1,y1,x1,y0,color);
copy_to_lcd();
}
void fillrect(int x0, int y0, int x1, int y1, int color)
{
int l,c,i;
if(x0 > x1) {
i = x0;
x0 = x1;
x1 = i;
}
if(y0 > y1) {
i = y0;
y0 = y1;
y1 = i;
}
for(l = x0; l<= x1; l ++) {
for(c = y0; c<= y1; c++) {
pixel(l,c,color);
}
}
copy_to_lcd();
}
void circle(int x0, int y0, int r, int color)
{
int draw_x0, draw_y0;
int draw_x1, draw_y1;
int draw_x2, draw_y2;
int draw_x3, draw_y3;
int draw_x4, draw_y4;
int draw_x5, draw_y5;
int draw_x6, draw_y6;
int draw_x7, draw_y7;
int xx, yy;
int di;
//WindowMax();
if (r == 0) { /* no radius */
return;
}
draw_x0 = draw_x1 = x0;
draw_y0 = draw_y1 = y0 + r;
if (draw_y0 < 32) {
pixel(draw_x0, draw_y0, color); /* 90 degree */
}
draw_x2 = draw_x3 = x0;
draw_y2 = draw_y3 = y0 - r;
if (draw_y2 >= 0) {
pixel(draw_x2, draw_y2, color); /* 270 degree */
}
draw_x4 = draw_x6 = x0 + r;
draw_y4 = draw_y6 = y0;
if (draw_x4 < 128) {
pixel(draw_x4, draw_y4, color); /* 0 degree */
}
draw_x5 = draw_x7 = x0 - r;
draw_y5 = draw_y7 = y0;
if (draw_x5>=0) {
pixel(draw_x5, draw_y5, color); /* 180 degree */
}
if (r == 1) {
return;
}
di = 3 – 2*r;
xx = 0;
yy = r;
while (xx < yy) {
if (di < 0) {
di += 4*xx + 6;
} else {
di += 4*(xx - yy) + 10;
yy--;
draw_y0--;
draw_y1--;
draw_y2++;
draw_y3++;
draw_x4--;
draw_x5++;
draw_x6--;
draw_x7++;
}
xx++;
draw_x0++;
draw_x1--;
draw_x2++;
draw_x3--;
draw_y4++;
draw_y5++;
draw_y6--;
draw_y7--;
if ( (draw_x0 <= 32) && (draw_y0>=0) ) {
pixel(draw_x0, draw_y0, color);
}
if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) {
pixel(draw_x1, draw_y1, color);
}
if ( (draw_x2 <= 32) && (draw_y2 <= 128) ) {
pixel(draw_x2, draw_y2, color);
}
if ( (draw_x3 >=0 ) && (draw_y3 <= 128) ) {
pixel(draw_x3, draw_y3, color);
}
if ( (draw_x4 <= 32) && (draw_y4 >= 0) ) {
pixel(draw_x4, draw_y4, color);
}
if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) {
pixel(draw_x5, draw_y5, color);
}
if ( (draw_x6 <= 32) && (draw_y6 <= 32) ) {
pixel(draw_x6, draw_y6, color);
}
if ( (draw_x7 >= 0) && (draw_y7 <= 32) ) {
pixel(draw_x7, draw_y7, color);
}
}
copy_to_lcd();
}
void fillcircle(int x, int y, int r, int color)
{
int i;
for (i = 0; i <= r; i++)
circle(x,y,i,color);
copy_to_lcd();
}
void cls(void)
{
memset(buffer,0x00,512); // clear display buffer
copy_to_lcd();
}
int columns()
{
return 128 / font[1];
}
int rows()
{
return 32 / font[2];
}
void character(int x, int y, int c)
{
unsigned int hor,vert,offset,bpl,j,i,b;
unsigned char* zeichen;
unsigned char z,w;
if ((c < 31) || (c > 127)) return; // test char range
// read font parameter from start of array
offset = font[0]; // bytes / char
hor = font[1]; // get hor size of font
vert = font[2]; // get vert size of font
bpl = font[3]; // bytes per line
if (char_x + hor > 128) {
char_x = 0;
char_y = char_y + vert;
if (char_y >= 32 – font[2]) {
char_y = 0;
}
}
zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
w = zeichen[0]; // width of actual char
// construct the char into the buffer
for (j=0; j
b = 1 << (j & 0x07);
if (( z & b ) == 0x00) {
pixel(x+i,y+j,0);
} else {
pixel(x+i,y+j,1);
}
}
}
char_x += w;
}
int putc(int value)
{
if (value == '\n') { // new line
char_x = 0;
char_y = char_y + font[2];
if (char_y >= 32 – font[2]) {
char_y = 0;
}
} else {
character(char_x, char_y, value);
copy_to_lcd();
}
return value;
}
void putstr(int y, int x, char *c)
{
uint8_t i,n;
n = strlen(c);
locate((x-1)*5,(y-1)*9+3);
for(i=0;i