Извините, я думал понятно из скетча.главное - картинка очень информативная ,
что куда - понятно
pinMode(11, OUTPUT); digitalWrite(11,HIGH);//res
pinMode(12, OUTPUT); digitalWrite(12,HIGH);//dc
pinMode(9, OUTPUT); digitalWrite(9,HIGH);//clk
pinMode(10, OUTPUT);//data
pinMode(13, OUTPUT); digitalWrite(13,HIGH);//cs
// PORTB |= (1 << res);/// Set bits - high
// DDRB |= (1 << res);// Set bits - output
// PORTB &= ~(1 << data);// Clear bit - low
// PORTB |= (1 << cs) | (1 << dc) | (1 << clk);/// Set bits - high
// DDRB |= (1 << data) | (1 << cs) | (1 << dc) | (1 << clk);// Set bits - output
int const clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0
int const data = 1;//10;//D1,SDA,MOSI - Data - PB1
int const res = 2;//11,RES - Reset - PB2//можно сразу к +
int const dc = 3;//12;DC - Data/Command - PB3
int const cs = 4;//13;CS - Chip Select - PB4
// Initialisation sequence for OLED module
int const InitLen = 23;
unsigned char Init[InitLen] = {
0xAE, // Display off
0xD5, // Set display clock
0x80, // Recommended value
0xA8, // Set multiplex
0x3F,
0xD3, // Set display offset
0x00,
0x40, // Zero start line
0x8D, // Charge pump
0x14,
0x20, // Memory mode
0x02, // Page addressing
0xA1, // 0xA0/0xA1 flip horizontally
0xC8, // 0xC0/0xC8 flip vertically
0xDA, // Set comp ins
0x12,
0x81, // Set contrast
0x7F,
0xD9, // Set pre charge
0xF1,
0xDB, // Set vcom detect
0x40,
0xA6 // Normal (0xA7=Inverse)
};
// Character set - stored in program memory
const uint8_t CharMap[96][6] PROGMEM = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00 },
{ 0x00, 0x07, 0x00, 0x07, 0x00, 0x00 },
{ 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00 },
{ 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00 },
{ 0x23, 0x13, 0x08, 0x64, 0x62, 0x00 },
{ 0x36, 0x49, 0x56, 0x20, 0x50, 0x00 },
{ 0x00, 0x08, 0x07, 0x03, 0x00, 0x00 },
{ 0x00, 0x1C, 0x22, 0x41, 0x00, 0x00 },
{ 0x00, 0x41, 0x22, 0x1C, 0x00, 0x00 },
{ 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 0x00 },
{ 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00 },
{ 0x00, 0x80, 0x70, 0x30, 0x00, 0x00 },
{ 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 },
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },
{ 0x20, 0x10, 0x08, 0x04, 0x02, 0x00 },
{ 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00 },
{ 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00 },
{ 0x72, 0x49, 0x49, 0x49, 0x46, 0x00 },
{ 0x21, 0x41, 0x49, 0x4D, 0x33, 0x00 },
{ 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00 },
{ 0x27, 0x45, 0x45, 0x45, 0x39, 0x00 },
{ 0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00 },
{ 0x41, 0x21, 0x11, 0x09, 0x07, 0x00 },
{ 0x36, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x46, 0x49, 0x49, 0x29, 0x1E, 0x00 },
{ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00 },
{ 0x00, 0x40, 0x34, 0x00, 0x00, 0x00 },
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x00 },
{ 0x00, 0x41, 0x22, 0x14, 0x08, 0x00 },
{ 0x02, 0x01, 0x59, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x5D, 0x59, 0x4E, 0x00 },
{ 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 },
{ 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 },
{ 0x3E, 0x41, 0x41, 0x51, 0x73, 0x00 },
{ 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 },
{ 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 },
{ 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 },
{ 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00 },
{ 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 },
{ 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 },
{ 0x26, 0x49, 0x49, 0x49, 0x32, 0x00 },
{ 0x03, 0x01, 0x7F, 0x01, 0x03, 0x00 },
{ 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 },
{ 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 },
{ 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 },
{ 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 },
{ 0x03, 0x04, 0x78, 0x04, 0x03, 0x00 },
{ 0x61, 0x59, 0x49, 0x4D, 0x43, 0x00 },
{ 0x00, 0x7F, 0x41, 0x41, 0x41, 0x00 },
{ 0x02, 0x04, 0x08, 0x10, 0x20, 0x00 },
{ 0x00, 0x41, 0x41, 0x41, 0x7F, 0x00 },
{ 0x04, 0x02, 0x01, 0x02, 0x04, 0x00 },
{ 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x00, 0x03, 0x07, 0x08, 0x00, 0x00 },
{ 0x20, 0x54, 0x54, 0x78, 0x40, 0x00 },
{ 0x7F, 0x28, 0x44, 0x44, 0x38, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x28, 0x00 },
{ 0x38, 0x44, 0x44, 0x28, 0x7F, 0x00 },
{ 0x38, 0x54, 0x54, 0x54, 0x18, 0x00 },
{ 0x00, 0x08, 0x7E, 0x09, 0x02, 0x00 },
{ 0x18, 0xA4, 0xA4, 0x9C, 0x78, 0x00 },
{ 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x00, 0x44, 0x7D, 0x40, 0x00, 0x00 },
{ 0x20, 0x40, 0x40, 0x3D, 0x00, 0x00 },
{ 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00 },
{ 0x00, 0x41, 0x7F, 0x40, 0x00, 0x00 },
{ 0x7C, 0x04, 0x78, 0x04, 0x78, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x38, 0x00 },
{ 0xFC, 0x18, 0x24, 0x24, 0x18, 0x00 },
{ 0x18, 0x24, 0x24, 0x18, 0xFC, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x08, 0x00 },
{ 0x48, 0x54, 0x54, 0x54, 0x24, 0x00 },
{ 0x04, 0x04, 0x3F, 0x44, 0x24, 0x00 },
{ 0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00 },
{ 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00 },
{ 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00 },
{ 0x44, 0x28, 0x10, 0x28, 0x44, 0x00 },
{ 0x4C, 0x90, 0x90, 0x90, 0x7C, 0x00 },
{ 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00 },
{ 0x00, 0x08, 0x36, 0x41, 0x00, 0x00 },
{ 0x00, 0x00, 0x77, 0x00, 0x00, 0x00 },
{ 0x00, 0x41, 0x36, 0x08, 0x00, 0x00 },
{ 0x02, 0x01, 0x02, 0x04, 0x02, 0x00 },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 }
};
int Line, Column, Scroll;
//int thisByte = 33;
char charVar[64];
byte a=0;
boolean c = false;
void setup() {
//clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0
//data = 1;//10;//D1,SDA,MOSI - Data - PB1
//res = 2;//11 - PB2
//dc = 3;//12; //-Data/Command - PB3
//cs = 4;//13;//-Chip Select - PB4
// Define pins
pinMode(11, OUTPUT); digitalWrite(11,HIGH);//res
pinMode(12, OUTPUT); digitalWrite(12,HIGH);//dc
pinMode(9, OUTPUT); digitalWrite(9,HIGH);//clk
pinMode(10, OUTPUT);//data
pinMode(13, OUTPUT); digitalWrite(13,HIGH);//cs
// PORTB |= (1 << res);/// Set bits - high
// DDRB |= (1 << res);// Set bits - output
// PORTB &= ~(1 << data);// Clear bit - low
// PORTB |= (1 << cs) | (1 << dc) | (1 << clk);/// Set bits - high
// DDRB |= (1 << data) | (1 << cs) | (1 << dc) | (1 << clk);// Set bits - output
InitDisplay();
ClearDisplay();
Command(0xAF); // Display on
initUSART();
}
void loop() {
//"ASCII Table"
// if (thisByte < 126){
// Print(thisByte);Print(' ');
// thisByte++;
// }
// delay(1000);
if (c){
if (a==0)a=64;
for (int i=0;i < a;i++) {
USART_Transmit(charVar[i]);//test
Print(charVar[i]);
charVar[i]=0;
}
USART_Transmit('\n');//test
USART_Transmit('\r');//test
}
a=0;
delay(1000);
}
// Write a data byte to the display
void Data(uint8_t d) {
PINB = 1<<cs; // cs low
for (uint8_t bit = 0x80; bit; bit >>= 1) {
PINB = 1<<clk; // clk low
if (d & bit) PORTB = PORTB | (1<<data); else PORTB = PORTB & ~(1<<data);
PINB = 1<<clk; // clk high
}
PINB = 1<<cs; // cs high
}
// Write a command byte to the display
void Command(uint8_t c) {
PINB = 1<<dc; // dc low
Data(c);
PINB = 1<<dc; // dc high
}
void InitDisplay () {
for (uint8_t c=0; c<InitLen; c++) Command(Init[c]);
}
// Character terminal **********************************************
void Clear256() {
uint8_t b = 0;
do {
PINB = 1<<clk; // clk low
b++;
PINB = 1<<clk; // clk high
} while (b != 0);
}
// Optimised for fast scrolling
void ClearLine (int line) {
Command(0xB0 + line);
Command(0x00); // Column start low
Command(0x00); // Column start high
PINB = 1<<cs; // cs low
PORTB = PORTB & ~(1<<data); // data low
Clear256(); Clear256(); Clear256(); Clear256();
PINB = 1<<cs; // cs high
}
void ClearDisplay () {
for (uint8_t p=0; p < 8; p++) ClearLine(p);
Line = Scroll;
Column = 0;
}
// Clears the top line, then scrolls the display up by one line
void ScrollDisplay () {
ClearLine(Scroll);
Scroll = (Scroll + 1) & 0x07;
Command(0xD3);
Command(Scroll << 3);
}
// Plots a character; line = 0 to 7; column = 0 to 20
void PlotChar(char c, int line, int column) {
column = column*6;
Command(0xB0 + ((line + Scroll) & 0x07));
Command(0x00 + (column & 0x0F)); // Column start low
Command(0x10 + (column >> 4)); // Column start high
for (uint8_t col = 0 ; col < 6; col++) {
Data(pgm_read_byte(&CharMap[c-32][col]));
}
}
// Prints a character, handling control characters
void Print(char c) {
c = c & 0x7F; // Ignore top bit
if (c >= 32) {
PlotChar(c, Line, Column++);
if (Column > 20) {
Column = 0;
if (Line == 7) ScrollDisplay(); else Line++;
}
}
// Return character
else if (c == 13) {
Column = 0;
if (Line == 7) ScrollDisplay(); else Line++;
}
}
void initUSART() {
//---------------------
//CodeVisionAVR V2.05.0
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x33;
//---------------------
UCSRB |= (1<<RXCIE) | (1<<TXCIE); // Set bits Разрешение прерывания по завершении приема и передачи.
}
void USART_Transmit(unsigned int data)//test
{
/* Ждать очистки буфера передатчика */
while ( !( UCSRA & (1<<UDRE))) {
};
UDR = data;
}
//прерывания по завершении приема.
ISR(USART_RX_vect){
charVar[a]=UDR;
c=true;
a++;
if(a>63)a=0;
}
ISR(USART_TX_vect){//test
c=false;
}
int thisByte = 33;
void setup() {
Serial.begin(9600);
Serial.println("ASCII Table ~ Character Map");
}
void loop() {
//"ASCII Table"
if (thisByte < 126){
Serial.write(thisByte);
thisByte++;
}
delay(1000);
}
А зачем реле?для полного счастья
надо бы добавить реле
и простейший модуль связи типа NRF2401 или JDY-...
int const clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0
int const data = 1;//10;//D1,SDA,MOSI - Data - PB1
int const res = 2;//11,RES - Reset - PB2//можно сразу к +
int const dc = 3;//12;DC - Data/Command - PB3
int const cs = 4;//13;CS - Chip Select - PB4
// Initialisation sequence for OLED module
int const InitLen = 23;
int const BufSize = 32;
uint8_t Buffer[BufSize];
int BufStart = 0, BufEnd = 0;
unsigned char Init[InitLen] = {
0xAE, // Display off
0xD5, // Set display clock
0x80, // Recommended value
0xA8, // Set multiplex
0x3F,
0xD3, // Set display offset
0x00,
0x40, // Zero start line
0x8D, // Charge pump
0x14,
0x20, // Memory mode
0x02, // Page addressing
0xA1, // 0xA0/0xA1 flip horizontally
0xC8, // 0xC0/0xC8 flip vertically
0xDA, // Set comp ins
0x12,
0x81, // Set contrast
0x7F,
0xD9, // Set pre charge
0xF1,
0xDB, // Set vcom detect
0x40,
0xA6 // Normal (0xA7=Inverse)
};
// Character set - stored in program memory
const uint8_t CharMap[96][6] PROGMEM = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00 },
{ 0x00, 0x07, 0x00, 0x07, 0x00, 0x00 },
{ 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00 },
{ 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00 },
{ 0x23, 0x13, 0x08, 0x64, 0x62, 0x00 },
{ 0x36, 0x49, 0x56, 0x20, 0x50, 0x00 },
{ 0x00, 0x08, 0x07, 0x03, 0x00, 0x00 },
{ 0x00, 0x1C, 0x22, 0x41, 0x00, 0x00 },
{ 0x00, 0x41, 0x22, 0x1C, 0x00, 0x00 },
{ 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 0x00 },
{ 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00 },
{ 0x00, 0x80, 0x70, 0x30, 0x00, 0x00 },
{ 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 },
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },
{ 0x20, 0x10, 0x08, 0x04, 0x02, 0x00 },
{ 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00 },
{ 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00 },
{ 0x72, 0x49, 0x49, 0x49, 0x46, 0x00 },
{ 0x21, 0x41, 0x49, 0x4D, 0x33, 0x00 },
{ 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00 },
{ 0x27, 0x45, 0x45, 0x45, 0x39, 0x00 },
{ 0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00 },
{ 0x41, 0x21, 0x11, 0x09, 0x07, 0x00 },
{ 0x36, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x46, 0x49, 0x49, 0x29, 0x1E, 0x00 },
{ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00 },
{ 0x00, 0x40, 0x34, 0x00, 0x00, 0x00 },
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x00 },
{ 0x00, 0x41, 0x22, 0x14, 0x08, 0x00 },
{ 0x02, 0x01, 0x59, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x5D, 0x59, 0x4E, 0x00 },
{ 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 },
{ 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 },
{ 0x3E, 0x41, 0x41, 0x51, 0x73, 0x00 },
{ 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 },
{ 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 },
{ 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 },
{ 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00 },
{ 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 },
{ 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 },
{ 0x26, 0x49, 0x49, 0x49, 0x32, 0x00 },
{ 0x03, 0x01, 0x7F, 0x01, 0x03, 0x00 },
{ 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 },
{ 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 },
{ 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 },
{ 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 },
{ 0x03, 0x04, 0x78, 0x04, 0x03, 0x00 },
{ 0x61, 0x59, 0x49, 0x4D, 0x43, 0x00 },
{ 0x00, 0x7F, 0x41, 0x41, 0x41, 0x00 },
{ 0x02, 0x04, 0x08, 0x10, 0x20, 0x00 },
{ 0x00, 0x41, 0x41, 0x41, 0x7F, 0x00 },
{ 0x04, 0x02, 0x01, 0x02, 0x04, 0x00 },
{ 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x00, 0x03, 0x07, 0x08, 0x00, 0x00 },
{ 0x20, 0x54, 0x54, 0x78, 0x40, 0x00 },
{ 0x7F, 0x28, 0x44, 0x44, 0x38, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x28, 0x00 },
{ 0x38, 0x44, 0x44, 0x28, 0x7F, 0x00 },
{ 0x38, 0x54, 0x54, 0x54, 0x18, 0x00 },
{ 0x00, 0x08, 0x7E, 0x09, 0x02, 0x00 },
{ 0x18, 0xA4, 0xA4, 0x9C, 0x78, 0x00 },
{ 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x00, 0x44, 0x7D, 0x40, 0x00, 0x00 },
{ 0x20, 0x40, 0x40, 0x3D, 0x00, 0x00 },
{ 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00 },
{ 0x00, 0x41, 0x7F, 0x40, 0x00, 0x00 },
{ 0x7C, 0x04, 0x78, 0x04, 0x78, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x38, 0x00 },
{ 0xFC, 0x18, 0x24, 0x24, 0x18, 0x00 },
{ 0x18, 0x24, 0x24, 0x18, 0xFC, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x08, 0x00 },
{ 0x48, 0x54, 0x54, 0x54, 0x24, 0x00 },
{ 0x04, 0x04, 0x3F, 0x44, 0x24, 0x00 },
{ 0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00 },
{ 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00 },
{ 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00 },
{ 0x44, 0x28, 0x10, 0x28, 0x44, 0x00 },
{ 0x4C, 0x90, 0x90, 0x90, 0x7C, 0x00 },
{ 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00 },
{ 0x00, 0x08, 0x36, 0x41, 0x00, 0x00 },
{ 0x00, 0x00, 0x77, 0x00, 0x00, 0x00 },
{ 0x00, 0x41, 0x36, 0x08, 0x00, 0x00 },
{ 0x02, 0x01, 0x02, 0x04, 0x02, 0x00 },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 }
};
int Line, Column, Scroll;
void setup() {
//clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0
//data = 1;//10;//D1,SDA,MOSI - Data - PB1
//res = 2;//11 - PB2
//dc = 3;//12; //-Data/Command - PB3
//cs = 4;//13;//-Chip Select - PB4
// Define pins
pinMode(11, OUTPUT); digitalWrite(11,HIGH);//res
pinMode(12, OUTPUT); digitalWrite(12,HIGH);//dc
pinMode(9, OUTPUT); digitalWrite(9,HIGH);//clk
pinMode(10, OUTPUT);//data
pinMode(13, OUTPUT); digitalWrite(13,HIGH);//cs
// PORTB |= (1 << res);/// Set bits - high
// DDRB |= (1 << res);// Set bits - output
// PORTB &= ~(1 << data);// Clear bit - low
// PORTB |= (1 << cs) | (1 << dc) | (1 << clk);/// Set bits - high
// DDRB |= (1 << data) | (1 << cs) | (1 << dc) | (1 << clk);// Set bits - output
PORTB |= (1 << 6);// Set bits - pullup
DDRB &= ~(1 << 6);// Clear bit - input
GIMSK |= (1 << PCIE);// Set bits - Разрешение прерывания по изменению состояния выводов 0й группы - PCINT.
//PCMSK Определяют условие генерации прерывания PCI0. Если какойлибо
//бит установлен в 1, то изменение состояния соответствующего вывода
//вызовет генерацию прерывания
PCMSK |= (1 << 6);// Set bits -прерывание по изменению состояния вывода 6
InitDisplay();
ClearDisplay();
Command(0xAF); // Display on
initUSART();
}
void loop() {
char temp = getBuf();
if (temp != -1) Print(temp);
}
// Write a data byte to the display
void Data(uint8_t d) {
PINB = 1<<cs; // cs low
for (uint8_t bit = 0x80; bit; bit >>= 1) {
PINB = 1<<clk; // clk low
if (d & bit) PORTB = PORTB | (1<<data); else PORTB = PORTB & ~(1<<data);
PINB = 1<<clk; // clk high
}
PINB = 1<<cs; // cs high
}
// Write a command byte to the display
void Command(uint8_t c) {
PINB = 1<<dc; // dc low
Data(c);
PINB = 1<<dc; // dc high
}
void InitDisplay () {
for (uint8_t c=0; c<InitLen; c++) Command(Init[c]);
}
// Character terminal **********************************************
void Clear256() {
uint8_t b = 0;
do {
PINB = 1<<clk; // clk low
b++;
PINB = 1<<clk; // clk high
} while (b != 0);
}
// Optimised for fast scrolling
void ClearLine (int line) {
Command(0xB0 + line);
Command(0x00); // Column start low
Command(0x00); // Column start high
PINB = 1<<cs; // cs low
PORTB = PORTB & ~(1<<data); // data low
Clear256(); Clear256(); Clear256(); Clear256();
PINB = 1<<cs; // cs high
}
void ClearDisplay () {
for (uint8_t p=0; p < 8; p++) ClearLine(p);
Line = Scroll;
Column = 0;
}
// Clears the top line, then scrolls the display up by one line
void ScrollDisplay () {
ClearLine(Scroll);
Scroll = (Scroll + 1) & 0x07;
Command(0xD3);
Command(Scroll << 3);
}
// Plots a character; line = 0 to 7; column = 0 to 20
void PlotChar(char c, int line, int column) {
column = column*6;
Command(0xB0 + ((line + Scroll) & 0x07));
Command(0x00 + (column & 0x0F)); // Column start low
Command(0x10 + (column >> 4)); // Column start high
for (uint8_t col = 0 ; col < 6; col++) {
Data(pgm_read_byte(&CharMap[c-32][col]));
}
}
// Prints a character, handling control characters
void Print(char c) {
c = c & 0x7F; // Ignore top bit
if (c >= 32) {
PlotChar(c, Line, Column++);
if (Column > 20) {
Column = 0;
if (Line == 7) ScrollDisplay(); else Line++;
}
}
// Return character
else if (c == 13) {
Column = 0;
if (Line == 7) ScrollDisplay(); else Line++;
}
}
void initUSART() {
//---------------------
//CodeVisionAVR V2.05.0
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x33;
//---------------------
UCSRB |= (1<<RXCIE) | (1<<TXCIE); // Set bits Разрешение прерывания по завершении приема и передачи.
}
//прерывания по завершении приема.
ISR(USART_RX_vect){
putBuf (UDR);
}
ISR(PCINT_vect) {
ClearDisplay();
}
void putBuf (uint8_t byt) {
if ((BufStart+BufSize-BufEnd) % BufSize == 1) return; // Buffer full
Buffer[BufEnd] = byt;
BufEnd = (BufEnd+1) % BufSize;
}
int getBuf () {
if (BufStart == BufEnd) return -1; // Buffer empty
int temp = Buffer[BufStart];
BufStart = (BufStart+1) % BufSize;
return temp;
}
если бы взяли STM8L то при меньшей цене получили бы больше возможностейА зачем реле?
Да и остальное не прокатит
Скетч использует 1994 байт (97%) памяти устройства. Всего доступно 2048 байт.
Глобальные переменные используют 75 байт (58%) динамической памяти, оставляя 53 байт для локальных переменных. Максимум: 128 байт.
Зато можно отлаживать тоже ESP или что то в этом роде.
Окончательный вариант
Код:int const clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0 int const data = 1;//10;//D1,SDA,MOSI - Data - PB1 int const res = 2;//11,RES - Reset - PB2//можно сразу к + int const dc = 3;//12;DC - Data/Command - PB3 int const cs = 4;//13;CS - Chip Select - PB4 // Initialisation sequence for OLED module int const InitLen = 23; int const BufSize = 32; uint8_t Buffer[BufSize]; int BufStart = 0, BufEnd = 0; unsigned char Init[InitLen] = { 0xAE, // Display off 0xD5, // Set display clock 0x80, // Recommended value 0xA8, // Set multiplex 0x3F, 0xD3, // Set display offset 0x00, 0x40, // Zero start line 0x8D, // Charge pump 0x14, 0x20, // Memory mode 0x02, // Page addressing 0xA1, // 0xA0/0xA1 flip horizontally 0xC8, // 0xC0/0xC8 flip vertically 0xDA, // Set comp ins 0x12, 0x81, // Set contrast 0x7F, 0xD9, // Set pre charge 0xF1, 0xDB, // Set vcom detect 0x40, 0xA6 // Normal (0xA7=Inverse) }; // Character set - stored in program memory const uint8_t CharMap[96][6] PROGMEM = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00 }, { 0x00, 0x07, 0x00, 0x07, 0x00, 0x00 }, { 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00 }, { 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00 }, { 0x23, 0x13, 0x08, 0x64, 0x62, 0x00 }, { 0x36, 0x49, 0x56, 0x20, 0x50, 0x00 }, { 0x00, 0x08, 0x07, 0x03, 0x00, 0x00 }, { 0x00, 0x1C, 0x22, 0x41, 0x00, 0x00 }, { 0x00, 0x41, 0x22, 0x1C, 0x00, 0x00 }, { 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 0x00 }, { 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00 }, { 0x00, 0x80, 0x70, 0x30, 0x00, 0x00 }, { 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 }, { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, { 0x20, 0x10, 0x08, 0x04, 0x02, 0x00 }, { 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00 }, { 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00 }, { 0x72, 0x49, 0x49, 0x49, 0x46, 0x00 }, { 0x21, 0x41, 0x49, 0x4D, 0x33, 0x00 }, { 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00 }, { 0x27, 0x45, 0x45, 0x45, 0x39, 0x00 }, { 0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00 }, { 0x41, 0x21, 0x11, 0x09, 0x07, 0x00 }, { 0x36, 0x49, 0x49, 0x49, 0x36, 0x00 }, { 0x46, 0x49, 0x49, 0x29, 0x1E, 0x00 }, { 0x00, 0x00, 0x14, 0x00, 0x00, 0x00 }, { 0x00, 0x40, 0x34, 0x00, 0x00, 0x00 }, { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, { 0x14, 0x14, 0x14, 0x14, 0x14, 0x00 }, { 0x00, 0x41, 0x22, 0x14, 0x08, 0x00 }, { 0x02, 0x01, 0x59, 0x09, 0x06, 0x00 }, { 0x3E, 0x41, 0x5D, 0x59, 0x4E, 0x00 }, { 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 }, { 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 }, { 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 }, { 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00 }, { 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 }, { 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 }, { 0x3E, 0x41, 0x41, 0x51, 0x73, 0x00 }, { 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 }, { 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 }, { 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 }, { 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 }, { 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 }, { 0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00 }, { 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 }, { 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 }, { 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 }, { 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 }, { 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 }, { 0x26, 0x49, 0x49, 0x49, 0x32, 0x00 }, { 0x03, 0x01, 0x7F, 0x01, 0x03, 0x00 }, { 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 }, { 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 }, { 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 }, { 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 }, { 0x03, 0x04, 0x78, 0x04, 0x03, 0x00 }, { 0x61, 0x59, 0x49, 0x4D, 0x43, 0x00 }, { 0x00, 0x7F, 0x41, 0x41, 0x41, 0x00 }, { 0x02, 0x04, 0x08, 0x10, 0x20, 0x00 }, { 0x00, 0x41, 0x41, 0x41, 0x7F, 0x00 }, { 0x04, 0x02, 0x01, 0x02, 0x04, 0x00 }, { 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 }, { 0x00, 0x03, 0x07, 0x08, 0x00, 0x00 }, { 0x20, 0x54, 0x54, 0x78, 0x40, 0x00 }, { 0x7F, 0x28, 0x44, 0x44, 0x38, 0x00 }, { 0x38, 0x44, 0x44, 0x44, 0x28, 0x00 }, { 0x38, 0x44, 0x44, 0x28, 0x7F, 0x00 }, { 0x38, 0x54, 0x54, 0x54, 0x18, 0x00 }, { 0x00, 0x08, 0x7E, 0x09, 0x02, 0x00 }, { 0x18, 0xA4, 0xA4, 0x9C, 0x78, 0x00 }, { 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00 }, { 0x00, 0x44, 0x7D, 0x40, 0x00, 0x00 }, { 0x20, 0x40, 0x40, 0x3D, 0x00, 0x00 }, { 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00 }, { 0x00, 0x41, 0x7F, 0x40, 0x00, 0x00 }, { 0x7C, 0x04, 0x78, 0x04, 0x78, 0x00 }, { 0x7C, 0x08, 0x04, 0x04, 0x78, 0x00 }, { 0x38, 0x44, 0x44, 0x44, 0x38, 0x00 }, { 0xFC, 0x18, 0x24, 0x24, 0x18, 0x00 }, { 0x18, 0x24, 0x24, 0x18, 0xFC, 0x00 }, { 0x7C, 0x08, 0x04, 0x04, 0x08, 0x00 }, { 0x48, 0x54, 0x54, 0x54, 0x24, 0x00 }, { 0x04, 0x04, 0x3F, 0x44, 0x24, 0x00 }, { 0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00 }, { 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00 }, { 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00 }, { 0x44, 0x28, 0x10, 0x28, 0x44, 0x00 }, { 0x4C, 0x90, 0x90, 0x90, 0x7C, 0x00 }, { 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00 }, { 0x00, 0x08, 0x36, 0x41, 0x00, 0x00 }, { 0x00, 0x00, 0x77, 0x00, 0x00, 0x00 }, { 0x00, 0x41, 0x36, 0x08, 0x00, 0x00 }, { 0x02, 0x01, 0x02, 0x04, 0x02, 0x00 }, { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 } }; int Line, Column, Scroll; void setup() { //clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0 //data = 1;//10;//D1,SDA,MOSI - Data - PB1 //res = 2;//11 - PB2 //dc = 3;//12; //-Data/Command - PB3 //cs = 4;//13;//-Chip Select - PB4 // Define pins pinMode(11, OUTPUT); digitalWrite(11,HIGH);//res pinMode(12, OUTPUT); digitalWrite(12,HIGH);//dc pinMode(9, OUTPUT); digitalWrite(9,HIGH);//clk pinMode(10, OUTPUT);//data pinMode(13, OUTPUT); digitalWrite(13,HIGH);//cs // PORTB |= (1 << res);/// Set bits - high // DDRB |= (1 << res);// Set bits - output // PORTB &= ~(1 << data);// Clear bit - low // PORTB |= (1 << cs) | (1 << dc) | (1 << clk);/// Set bits - high // DDRB |= (1 << data) | (1 << cs) | (1 << dc) | (1 << clk);// Set bits - output PORTB |= (1 << 6);// Set bits - pullup DDRB &= ~(1 << 6);// Clear bit - input GIMSK |= (1 << PCIE);// Set bits - Разрешение прерывания по изменению состояния выводов 0й группы - PCINT. //PCMSK Определяют условие генерации прерывания PCI0. Если какойлибо //бит установлен в 1, то изменение состояния соответствующего вывода //вызовет генерацию прерывания PCMSK |= (1 << 6);// Set bits -прерывание по изменению состояния вывода 6 InitDisplay(); ClearDisplay(); Command(0xAF); // Display on initUSART(); } void loop() { char temp = getBuf(); if (temp != -1) Print(temp); } // Write a data byte to the display void Data(uint8_t d) { PINB = 1<<cs; // cs low for (uint8_t bit = 0x80; bit; bit >>= 1) { PINB = 1<<clk; // clk low if (d & bit) PORTB = PORTB | (1<<data); else PORTB = PORTB & ~(1<<data); PINB = 1<<clk; // clk high } PINB = 1<<cs; // cs high } // Write a command byte to the display void Command(uint8_t c) { PINB = 1<<dc; // dc low Data(c); PINB = 1<<dc; // dc high } void InitDisplay () { for (uint8_t c=0; c<InitLen; c++) Command(Init[c]); } // Character terminal ********************************************** void Clear256() { uint8_t b = 0; do { PINB = 1<<clk; // clk low b++; PINB = 1<<clk; // clk high } while (b != 0); } // Optimised for fast scrolling void ClearLine (int line) { Command(0xB0 + line); Command(0x00); // Column start low Command(0x00); // Column start high PINB = 1<<cs; // cs low PORTB = PORTB & ~(1<<data); // data low Clear256(); Clear256(); Clear256(); Clear256(); PINB = 1<<cs; // cs high } void ClearDisplay () { for (uint8_t p=0; p < 8; p++) ClearLine(p); Line = Scroll; Column = 0; } // Clears the top line, then scrolls the display up by one line void ScrollDisplay () { ClearLine(Scroll); Scroll = (Scroll + 1) & 0x07; Command(0xD3); Command(Scroll << 3); } // Plots a character; line = 0 to 7; column = 0 to 20 void PlotChar(char c, int line, int column) { column = column*6; Command(0xB0 + ((line + Scroll) & 0x07)); Command(0x00 + (column & 0x0F)); // Column start low Command(0x10 + (column >> 4)); // Column start high for (uint8_t col = 0 ; col < 6; col++) { Data(pgm_read_byte(&CharMap[c-32][col])); } } // Prints a character, handling control characters void Print(char c) { c = c & 0x7F; // Ignore top bit if (c >= 32) { PlotChar(c, Line, Column++); if (Column > 20) { Column = 0; if (Line == 7) ScrollDisplay(); else Line++; } } // Return character else if (c == 13) { Column = 0; if (Line == 7) ScrollDisplay(); else Line++; } } void initUSART() { //--------------------- //CodeVisionAVR V2.05.0 // USART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART Mode: Asynchronous // USART Baud Rate: 9600 UCSRA=0x00; UCSRB=0x18; UCSRC=0x06; UBRRH=0x00; UBRRL=0x33; //--------------------- UCSRB |= (1<<RXCIE) | (1<<TXCIE); // Set bits Разрешение прерывания по завершении приема и передачи. } //прерывания по завершении приема. ISR(USART_RX_vect){ putBuf (UDR); } ISR(PCINT_vect) { ClearDisplay(); } void putBuf (uint8_t byt) { if ((BufStart+BufSize-BufEnd) % BufSize == 1) return; // Buffer full Buffer[BufEnd] = byt; BufEnd = (BufEnd+1) % BufSize; } int getBuf () { if (BufStart == BufEnd) return -1; // Buffer empty int temp = Buffer[BufStart]; BufStart = (BufStart+1) % BufSize; return temp; }