/MCU
Last active
May 14, 2018 00:09
MCU sketchs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
volatile uint16_t s = 0; //volatile запрещает компилятору оптимизировать переменную (так как ее значение может изменяться вне кода программы, а по перрыванию) | |
ISR(TIMER1_OVF_vect) {//обработчик прерывания по переполнению max(timer1) = 65535 | |
s++; | |
if (s == 490) {//примерно раз в секунду (490 ~ 999-1000 ms, 491 ~ 1001-1002 ms) | |
s = 0; | |
Serial.println(millis()); | |
} | |
} | |
void setup() { | |
Serial.begin(9600); | |
TIMSK1 |= (1 << TOIE1);//разрешает прерывание по переполнению | |
} | |
void loop() { | |
delay(1000);//никак не влияет на работу таймера | |
Serial.println("#"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <conio.h> | |
#include <iostream> | |
using namespace std; | |
HANDLE hSerial; | |
int main(int argc, char* argv[]) { | |
LPCTSTR sPortName = "COM4"; | |
hSerial = ::CreateFile(sPortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
if (hSerial == INVALID_HANDLE_VALUE) { | |
if (GetLastError() == ERROR_FILE_NOT_FOUND) | |
cout << "serial port does not exist.\n"; | |
cout << "some other error occurred.\n"; | |
} | |
DCB dcbSerialParams = {0}; | |
dcbSerialParams.DCBlength = sizeof(dcbSerialParams); | |
if (!GetCommState(hSerial, &dcbSerialParams)) | |
cout << "getting state error\n"; | |
dcbSerialParams.BaudRate = CBR_9600; | |
dcbSerialParams.ByteSize = 8; | |
dcbSerialParams.StopBits = ONESTOPBIT; | |
dcbSerialParams.Parity = NOPARITY; | |
if (!SetCommState(hSerial, &dcbSerialParams)) | |
cout << "error setting serial port state\n"; | |
char data[] = " "; | |
DWORD dwBytesWritten; | |
DWORD iSize; | |
char sReceivedChar; | |
while (true) { | |
if (kbhit()) { | |
data[0] = getch(); | |
WriteFile(hSerial, data, 1, &dwBytesWritten, NULL); | |
cout << data[0]; | |
} | |
ReadFile(hSerial, &sReceivedChar, 1, &iSize, 0); | |
if (iSize > 0) | |
cout << sReceivedChar; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - | Mega | - | - | * | - | TFT | ILI9486 | - | | |
| ---- | ---- | ---- | ----- | ---- | ---- | ---- | ------- | ----- | | |
| vcc | 5v | 5v | vcc | * | t_cs | 42 | 52 | clk | | |
| 0/8 | 22 | 23 | 9/1 | * | pen | int | 43 | f_cs | | |
| 2/10 | 24 | 25 | 11/3 | * | miso | 50 | 51 | mosi | | |
| 4/12 | 26 | 27 | 13/5 | * | gnd | gnd | - | - | | |
| 6/14 | 28 | 29 | 15/7 | * | vdd | 3.3 | gnd | gnd | | |
| 15/7 | 30 | 31 | 6/14 | * | bl | 3.3 | 3.3 | vdd | | |
| 13/5 | 32 | 33 | 4/12 | * | 15 | 29 | 53 | sd_cs | | |
| 11/3 | 34 | 35 | 2/10 | * | 13 | 27 | 28 | 14 | | |
| 9/1 | 36 | 37 | 0/8 | * | 11 | 25 | 26 | 12 | | |
| rs | 38 | 39 | wr | * | 9 | 23 | 24 | 10 | | |
| cs | 40 | 41 | rst | * | 7 | 30 | 22 | 8 | | |
| ?rd | 42 | 43 | - | * | 5 | 32 | 31 | 6 | | |
| - | 44 | 45 | f_cs | * | 3 | 34 | 33 | 4 | | |
| - | 46 | 47 | - | * | 1 | 36 | 34 | 2 | | |
| - | 48 | 49 | - | * | rst | 41 | 37 | 0 | | |
| miso | 50 | 51 | mosi | * | wr | 39 | 3.3 | rd | | |
| clk | 52 | 53 | sd_cs | * | cs | 40 | 38 | rs | | |
| gnd | gnd | gnd | gnd | * | - | - | - | - | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define switchPinA 11 // кнопка джойстика A | |
#define pinAX A0 // Ось X джойстика A | |
#define pinAY A1 // Ось Y джойстика A | |
#define ledPin 13 | |
void setup() { | |
pinMode(ledPin, OUTPUT); | |
pinMode(switchPinA, INPUT); | |
digitalWrite(switchPinA, HIGH); // включаем встроенный подтягивающий резистор | |
Serial.begin(9600); | |
while (!Serial){}; | |
} | |
void loop() { | |
Serial.print(analogRead(pinAX)); | |
Serial.print(" - "); | |
Serial.println(analogRead(pinAY)); | |
if (digitalRead(switchPinA) == LOW) | |
Serial.println("SWITCH"); | |
delay(32); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sketchs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
Serial.begin (9600); | |
pinMode(13, OUTPUT); | |
randomSeed(analogRead(0)); | |
} | |
int cmd=0; | |
void loop() { | |
if ( Serial.available()) { | |
cmd = Serial.read(); | |
digitalWrite(13, HIGH); | |
delay(16); | |
digitalWrite(13, LOW); | |
delay(16); | |
} else { | |
if (random(1024) < 2) | |
Serial.write(42); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment