将触摸屏的arduino库转换为通用c库

时间:2016-03-11 09:55:56

标签: arduino

我一直在使用带有arduino的adafruit 3.5英寸触摸屏触控板。它有arduino库可用于此目的。我已经附上了下面的图书馆。我试图让这款触摸屏在没有带有atmega16的arduino板的情况下工作。我相应地将库更改为c文件。我编写了一个程序来改变屏幕颜色,但这也没有发生。我不明白问题出在哪里。有人可以帮我吗。请!!! 这是在c文件中编辑后的代码。

#define SS PINB4
#define MOSI PINB5
#define MISO PINB6
#define SCK PINB7
#define CS PIND0
#define DC PIND1
#define RST PIND6

void delay(uint8_t a)
{
unsigned int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<1024;j++);
}
}

 void Adafruit_HX8357() {
_cs = CS;
_dc = DC;
_rst = RST;
_mosi = MOSI;
_sck = SCK;

}

void Adafruit_GFX(int16_t w, int16_t h)

{

_width    = w;
_height   = h;
rotation  = 0;
cursor_y  = cursor_x    = 0;
textsize  = 1;
textcolor = textbgcolor = 0x001F;

}

void setRotation(uint8_t m) {

writecommand(HX8357_MADCTL);
rotation = m % 4; // can't be higher than 3
switch (rotation) {
case 0:
writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
_width  = HX8357_TFTWIDTH;
_height = HX8357_TFTHEIGHT;
break;
case 1:
writedata(MADCTL_MV | MADCTL_MY | MADCTL_RGB);
_width  = HX8357_TFTHEIGHT;
_height = HX8357_TFTWIDTH;
break;
case 2:
writedata( MADCTL_RGB);
_width  = HX8357_TFTWIDTH;
_height = HX8357_TFTHEIGHT;
break;
case 3:
writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
_width  = HX8357_TFTHEIGHT;
_height = HX8357_TFTWIDTH;
break;
}

}

void writecommand(uint8_t c) {
PORTD &= ~(1<<PIND1);//
//dcport &= ~dcpinmask;
//digitalWrite(_dc, LOW);
PORTB &= ~(1<<PINB7);//clkport &= ~clkpinmask;
//digitalWrite(_sclk, LOW);
PORTD &= ~(1<<PIND0);//*csport &= ~cspinmask;
//digitalWrite(_cs, LOW);

spiwrite(c);
//Serial.print("Command 0x"); Serial.println(c, HEX);

PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);

}

void writedata(uint8_t c) {
PORTD |= (1<<PIND1);//
//dcport |= dcpinmask;
//digitalWrite(_dc, HIGH);
PORTB &= ~(1<<PINB7);//clkport &= ~clkpinmask;
//digitalWrite(_sclk, LOW);
PORTD &= ~(1<<PIND0);// *csport &= ~cspinmask;
//digitalWrite(_cs, LOW);

spiwrite(c);
//Serial.print("Data 0x"); Serial.println(c, HEX);

//digitalWrite(_cs, HIGH);
PORTD |= (1<<PIND0);//*csport |= cspinmask;

}

void spiwrite(uint8_t c) {

SPDR = c;
while(!(SPSR & (1<<SPIF) ));

            }

void init()
{
DDRB = (1<<PINB5)|(1<<PINB7);
SPCR = 0x50;
SPSR = 0x00;

}

void begins(uint8_t type) {
if (_rst > 0) {
DDRD |= (1<<PIND6);//pinMode(_rst, OUTPUT);
_rst = 0;// reset LOW
}

DDRD |= (1<< PIND1);//pinMode(_dc, OUTPUT);
DDRD |= (1<< PIND0);//pinMode(_cs, OUTPUT);
// toggle RST low to reset
if (_rst > 0) {
_rst = 1;//digitalWrite(_rst, HIGH);
delay(100);
_rst = 0;//digitalWrite(_rst, LOW);
delay(100);
_rst = 1;//digitalWrite(_rst, HIGH);
delay(150);
}

writecommand(HX8357_SWRESET);

// setextc
writecommand(HX8357D_SETC);
writedata(0xFF);
writedata(0x83);
writedata(0x57);
delay(300);
// setRGB which also enables SDO
writecommand(HX8357_SETRGB);
writedata(0x80);  //enable SDO pin!
//    writedata(0x00);  //disable SDO pin!
writedata(0x0);
writedata(0x06);
writedata(0x06);

writecommand(HX8357D_SETCOM);
writedata(0x25);  // -1.52V

writecommand(HX8357_SETOSC);
writedata(0x68);  // Normal mode 70Hz, Idle mode 55 Hz

writecommand(HX8357_SETPANEL); //Set Panel
writedata(0x05);  // BGR, Gate direction swapped

writecommand(HX8357_SETPWR1);
writedata(0x00);  // Not deep standby
writedata(0x15);  //BT
writedata(0x1C);  //VSPR
writedata(0x1C);  //VSNR
writedata(0x83);  //AP
writedata(0xAA);  //FS

writecommand(HX8357D_SETSTBA);
writedata(0x50);  //OPON normal
writedata(0x50);  //OPON idle
writedata(0x01);  //STBA
writedata(0x3C);  //STBA
writedata(0x1E);  //STBA
writedata(0x08);  //GEN

writecommand(HX8357D_SETCYC);
writedata(0x02);  //NW 0x02
writedata(0x40);  //RTN
writedata(0x00);  //DIV
writedata(0x2A);  //DUM
writedata(0x2A);  //DUM
writedata(0x0D);  //GDON
writedata(0x78);  //GDOFF

writecommand(HX8357D_SETGAMMA);
writedata(0x02);
writedata(0x0A);
writedata(0x11);
writedata(0x1d);
writedata(0x23);
writedata(0x35);
writedata(0x41);
writedata(0x4b);
writedata(0x4b);
writedata(0x42);
writedata(0x3A);
writedata(0x27);
writedata(0x1B);
writedata(0x08);
writedata(0x09);
writedata(0x03);
writedata(0x02);
writedata(0x0A);
writedata(0x11);
writedata(0x1d);
writedata(0x23);
writedata(0x35);
writedata(0x41);
writedata(0x4b);
writedata(0x4b);
writedata(0x42);
writedata(0x3A);
writedata(0x27);
writedata(0x1B);
writedata(0x08);
writedata(0x09);
writedata(0x03);
writedata(0x00);
writedata(0x01);

writecommand(HX8357_COLMOD);
writedata(0x55);  // 16 bit

writecommand(HX8357_MADCTL);
writedata(0xC0);

writecommand(HX8357_TEON);  // TE off
writedata(0x00);

writecommand(HX8357_TEARLINE);  // tear line
writedata(0x00);
writedata(0x02);

writecommand(HX8357_SLPOUT); //Exit Sleep
delay(150);

writecommand(HX8357_DISPON);  // display on
delay(50);

}

void drawPixel(int16_t x, int16_t y, uint16_t color) {

if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;

setAddrWindow(x,y,x+1,y+1);

//digitalWrite(_dc, HIGH);
PORTD |= (1<<PIND1);// dcport |= dcpinmask;
//digitalWrite(_cs, LOW);
PORTD &= ~(1<<PIND0);//csport &= ~cspinmask;

spiwrite(color >> 8);
spiwrite(color);

PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);
}
void drawFastVLine(int16_t x, int16_t y, int16_t h,uint16_t color) {

// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;

if((y+h-1) >= _height)
h = _height-y;

setAddrWindow(x, y, x, y+h-1);

uint8_t hi = color >> 8, lo = color;

PORTD |= (1<<PIND1);//*dcport |=  dcpinmask;
//digitalWrite(_dc, HIGH);
PORTD &= ~(1<<PIND0);//*csport &= ~cspinmask;
//digitalWrite(_cs, LOW);

while (h--) {
spiwrite(hi);
spiwrite(lo);
}
PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);

}

void drawRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color){
drawFastHLine(x, y, w, color);
drawFastHLine(x, y+h-1, w, color);
drawFastVLine(x, y, h, color);
drawFastVLine(x+w-1, y, h, color);
}

void drawFastHLine(int16_t x, int16_t y, int16_t w,uint16_t color) {

// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((x+w-1) >= _width)  w = _width-x;
setAddrWindow(x, y, x+w-1, y);

uint8_t hi = color >> 8, lo = color;
PORTD |= (1<<PIND1);//*dcport |=  dcpinmask;
PORTD &= ~(1<<PIND0);//*csport &= ~cspinmask;
//digitalWrite(_dc, HIGH);
//digitalWrite(_cs, LOW);
while (w--) {
spiwrite(hi);
spiwrite(lo);
}
PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);

}

void fillScreen(uint16_t color) {
drawRect(0, 0, _width, _height, color);
}

void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,uint16_t y1)
{

writecommand(HX8357_CASET); // Column addr set
writedata(x0 >> 8);
writedata(x0 & 0xFF); // XSTART
writedata(x1 >> 8);
writedata(x1 & 0xFF); // XEND

writecommand(HX8357_PASET); // Row addr set
writedata(y0>>8);
writedata(y0); // YSTART
writedata(y1>>8);
writedata(y1); // YEND

writecommand(HX8357_RAMWR); // write to RAM
}
void setCursor(int16_t x, int16_t y) {
cursor_x = x;
cursor_y = y;
}

void setTextSize(uint8_t s) {
textsize = (s > 0) ? s : 1;
}

void setTextColor(uint16_t c) {

textcolor = textbgcolor = c;
}

int main(void)
{
init();
Adafruit_HX8357();
begins(HX8357D);
Adafruit_GFX(HX8357_TFTWIDTH, HX8357_TFTHEIGHT);

setRotation(3);
fillScreen(HX8357_RED);
return 0;
}

原始图书馆的链接是 https://github.com/adafruit/Adafruit_HX8357_Library https://github.com/adafruit/Adafruit-GFX-Library

0 个答案:

没有答案