Skip to content

Commit

Permalink
Fixed and added explanations to first lab, 17 variant.
Browse files Browse the repository at this point in the history
Added HEX inspector;
Added ports pin mapping for Arduino Mega2560;
Changed includePath for mega controller;
Changed comments extension;
  • Loading branch information
VladVanyuk committed Feb 25, 2024
1 parent d97016c commit d066c2a
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"configuration": "cpu=atmega2560",
"board": "arduino:avr:mega",
"sketch": "mc_labs\\mc_lab_02\\lab_test\\lab_test.ino"
"sketch": "mc_labs\\mc_lab_01\\17_Senkiv\\17_Senkiv.ino"
}
7 changes: 5 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"includePath": [
"${workspaceFolder}/**",
"C:\\Users\\${username}\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\**",
"C:\\Users\\${username}\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\mega\\*",
"C:\\Users\\${username}\\AppData\\Local\\Arduino15\\packages\\arduino\\**",
"${workspaceFolder}/../**"
],
Expand All @@ -18,8 +19,10 @@
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
]
"_UNICODE",
"__AVR_ATmega2560__"
],
"mergeConfigurations": false
},
{
"name": "Arduino",
Expand Down
4 changes: 2 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"vsciot-vscode.vscode-arduino",
"tonybaloney.vscode-pets",
"alefragnani.bookmarks",
"aaron-bond.better-comments",
"ms-vscode.cpptools-themes",
"wayou.vscode-todo-highlight",
"gruntfuggly.todo-tree",
"tomoki1207.pdf",
"kisstkondoros.vscode-gutter-preview"
"kisstkondoros.vscode-gutter-preview",
"mateuszchudyk.hexinspector"
]
}
18 changes: 16 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"workbench.colorCustomizations": {

"activityBar.activeBackground": "#007e80",
"activityBar.background": "#007e80",
"activityBar.foreground": "#e7e7e7",
Expand All @@ -27,11 +26,26 @@
],
"cSpell.words": [
"dylib",
"PORTA",
"PORTB",
"PORTC",
"PORTD",
"PORTF",
"PORTG",
"PORTH",
"PORTJ",
"PORTK",
"PORTL",
"smod"
],
"workbench.editor.autoLockGroups": {
"wokwi.diagram-editor": true
},
"terminal.explorerKind": "both",
"vscode-pets.throwBallWithMouse": false
"vscode-pets.throwBallWithMouse": false,
"docwriter.style": "Doxygen",
"docwriter.progress.trackTypes": true,
"docwriter.progress.trackClasses": true,


}
104 changes: 104 additions & 0 deletions extra/docs/PinMapping.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

Pin Number Pin Name Mapped Pin Name
1 PG5 ( OC0B ) Digital pin 4 (PWM)
2 PE0 ( RXD0/PCINT8 ) Digital pin 0 (RX0)
3 PE1 ( TXD0 ) Digital pin 1 (TX0)
4 PE2 ( XCK0/AIN0 )
5 PE3 ( OC3A/AIN1 ) Digital pin 5 (PWM)
6 PE4 ( OC3B/INT4 ) Digital pin 2 (PWM)
7 PE5 ( OC3C/INT5 ) Digital pin 3 (PWM)
8 PE6 ( T3/INT6 )
9 PE7 ( CLKO/ICP3/INT7 )
10 VCC VCC
11 GND GND
12 PH0 ( RXD2 ) Digital pin 17 (RX2)
13 PH1 ( TXD2 ) Digital pin 16 (TX2)
14 PH2 ( XCK2 )
15 PH3 ( OC4A ) Digital pin 6 (PWM)
16 PH4 ( OC4B ) Digital pin 7 (PWM)
17 PH5 ( OC4C ) Digital pin 8 (PWM)
18 PH6 ( OC2B ) Digital pin 9 (PWM)
19 PB0 ( SS/PCINT0 ) Digital pin 53 (SS)
20 PB1 ( SCK/PCINT1 ) Digital pin 52 (SCK)
21 PB2 ( MOSI/PCINT2 ) Digital pin 51 (MOSI)
22 PB3 ( MISO/PCINT3 ) Digital pin 50 (MISO)
23 PB4 ( OC2A/PCINT4 ) Digital pin 10 (PWM)
24 PB5 ( OC1A/PCINT5 ) Digital pin 11 (PWM)
25 PB6 ( OC1B/PCINT6 ) Digital pin 12 (PWM)
26 PB7 ( OC0A/OC1C/PCINT7) Digital pin 13 (PWM)
27 PH7 ( T4 )
28 PG3 ( TOSC2 )
29 PG4 ( TOSC1 )
30 RESE T RESET
31 VCC VCC
32 GND GND
33 XTAL 2 XTAL2
34 XTAL 1 XTAL1
35 PL0 ( ICP4 ) Digital pin 49
36 PL1 ( ICP5 ) Digital pin 48
37 PL2 ( T5 ) Digital pin 47
38 PL3 ( OC5A ) Digital pin 46 (PWM)
39 PL4 ( OC5B ) Digital pin 45 (PWM)
40 PL5 ( OC5C ) Digital pin 44 (PWM)
41 PL6 Digital pin 43
42 PL7 Digital pin 42
43 PD0 ( SCL/INT0 ) Digital pin 21 (SCL)
44 PD1 ( SDA/INT1 ) Digital pin 20 (SDA)
45 PD2 ( RXDI/INT2 ) Digital pin 19 (RX1)
46 PD3 ( TXD1/INT3 ) Digital pin 18 (TX1)
47 PD4 ( ICP1 )
48 PD5 ( XCK1 )
49 PD6 ( T1 )
50 PD7 ( T0 ) Digital pin 38
51 PG0 ( WR ) Digital pin 41
52 PG1 ( RD ) Digital pin 40
53 PC0 ( A8 ) Digital pin 37
54 PC1 ( A9 ) Digital pin 36
55 PC2 ( A10 ) Digital pin 35
56 PC3 ( A11 ) Digital pin 34
57 PC4 ( A12 ) Digital pin 33
58 PC5 ( A13 ) Digital pin 32
59 PC6 ( A14 ) Digital pin 31
60 PC7 ( A15 ) Digital pin 30
61 VCC VCC
62 GND GND
63 PJ0 ( RXD3/PCINT9 ) Digital pin 15 (RX3)
64 PJ1 ( TXD3/PCINT10 ) Digital pin 14 (TX3)
65 PJ2 ( XCK3/PCINT11 )
66 PJ3 ( PCINT12 )
67 PJ4 ( PCINT13 )
68 PJ5 ( PCINT14 )
69 PJ6 ( PCINT 15 )
70 PG2 ( ALE ) Digital pin 39
71 PA7 ( AD7 ) Digital pin 29
72 PA6 ( AD6 ) Digital pin 28
73 PA5 ( AD5 ) Digital pin 27
74 PA4 ( AD4 ) Digital pin 26
75 PA3 ( AD3 ) Digital pin 25
76 PA2 ( AD2 ) Digital pin 24
77 PA1 ( AD1 ) Digital pin 23
78 PA0 ( AD0 ) Digital pin 22
79 PJ7
80 VCC VCC
81 GND GND
82 PK7 ( ADC15/PCINT23 ) Analog pin 15
83 PK6 ( ADC14/PCINT22 ) Analog pin 14
84 PK5 ( ADC13/PCINT21 ) Analog pin 13
85 PK4 ( ADC12/PCINT20 ) Analog pin 12
86 PK3 ( ADC11/PCINT19 ) Analog pin 11
87 PK2 ( ADC10/PCINT18 ) Analog pin 10
88 PK1 ( ADC9/PCINT17 ) Analog pin 9
89 PK0 ( ADC8/PCINT16 ) Analog pin 8
90 PF7 ( ADC7/TDI ) Analog pin 7
91 PF6 ( ADC6/TDO ) Analog pin 6
92 PF5 ( ADC5/TMS ) Analog pin 5
93 PF4 ( ADC4/TCK ) Analog pin 4
94 PF3 ( ADC3 ) Analog pin 3
95 PF2 ( ADC2 ) Analog pin 2
96 PF1 ( ADC1 ) Analog pin 1
97 PF0 ( ADC0 ) Analog pin 0
98 AREF Analog Reference
99 GND GND
100 AVCC VCC

//https://docs.arduino.cc/retired/hacking/hardware/PinMapping2560/
Binary file added extra/pics/PortsArduinoMega.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
187 changes: 187 additions & 0 deletions mc_labs/mc_lab_01/17_Senkiv/17_Senkiv.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* @file 17_Senkiv.ino
* @addtogroup IP-21
* @paragraph Lab1
* @author Сеньків Олександр Сергійови ([email protected])
* @brief 17 var
* @todo
* Кнопка Світлодіоди Затримка Алгоритм
* PK2 Port-C 1,2 сек. 1
* @attention
* Лінійка з 8-ми одноколірних світлодіодів. При натисканні
* кнопки світлодіоди почергово блимають від 0-виводу порту до 7.
* P0→P1→P2→P3→P4→P5→P6→P7
* @version 0.2
* @date 2024-02-25
*
* @note It is a doxygen style comment
*/

//TODO: 18var?

#define __AVR_ATmega2560__
#include <Arduino.h>

//#define first_led 37 //! It's convention to use uppercase letters when defining a macro
#define FIRST_LED 37
// #define last_led 30 //! Making your macros all upper-case, you can avoid collisions.
#define LAST_LED 30


// int buttonPin = 64;
// const int buttonPin = 64;
// const uint8_t buttonPin = PIN_A10; //! in one style
#define BUTTON_PIN PIN_A10

uint8_t buttonCurrentState = 0; //! todo: make software debounce

// bool btnPressedFlag = false;
bool btnPressedFlag; //* it is preferred for flags to assign basic value in init function

/* a=target variable, b=bit number to act upon 0-n */
#define BIT_SET(a,b) ((a) |= (1ULL<<(b)))
#define BIT_CLEAR(a,b) ((a) &= ~(1ULL<<(b)))
#define BIT_FLIP(a,b) ((a) ^= (1ULL<<(b)))
#define BIT_CHECK(a,b) (!!((a) & (1ULL<<(b)))) // '!!' to make sure this returns 0 or 1
// BITMASK_CHECK(x,y) ((x) & (y)) must be ((x) & (y)) == (y)

#define BITMASK_SET(x, mask) ((x) |= (mask))
#define BITMASK_CLEAR(x, mask) ((x) &= (~(mask)))
#define BITMASK_FLIP(x, mask) ((x) ^= (mask))
#define BITMASK_CHECK_ALL(x, mask) (!(~(x) & (mask))) //!~((~(y))|(x))
#define BITMASK_CHECK_ANY(x, mask) ((x) & (mask))

//-------------------------------------------------------
// LSB P0 P1 P2 P3 P4 P5, P6, P7 MSB
//-------------------------------------------------------
// PORTA = {22, 23, 24, 25, 26, 27, 28, 29};
// PORTF = {A0, A1, A2, A3, A4, A5, A6, A7};
// PORTK = {A8, A9, A10, A11, A12, A13, A14, A15};
// PORTC = {37, 36, 35, 34, 33, 32, 31, 30};
// PORTL = {49, 48, 47, 46, 45, 44, 43, 42};
// PORTB = {53, 52, 51, 50, 10, 11, 12, 13};
// PORTG = {41, 40, 39, xx, xx, 4, xx, xx};
// PORTD = {21, 20, 19, 18, xx, xx, xx, 38};
// PORTE = {0, 1, xx, 5, 2, 3, xx, xx};
// PORTH = {17, 16, xx, 6, 7, 8, 9, xx};
// PORTJ = {15, 14, xx, xx, xx, xx, xx, xx};
//-------------------------------------------------------

#define PORT_PIN_0 (1 << 0) //& 0b00000001
#define PORT_PIN_1 (1 << 1) //& 0b00000010
#define PORT_PIN_2 (1 << 2) //& 0b00000100
#define PORT_PIN_3 (1 << 3) //& 0b00001000
#define PORT_PIN_4 (1 << 4) //& 0b00010000
#define PORT_PIN_5 (1 << 5) //& 0b00100000
#define PORT_PIN_6 (1 << 6) //& 0b01000000
#define PORT_PIN_7 (1 << 7) //& 0b10000000

#define MY_PORT PORTC


void setupSerial()
{
Serial.begin(9600);
delay(200);
Serial.println("");
Serial.println("Setup begin");
}


void setupLedsPort()
{
Serial.println("setupLedsPort begin");
//^ define one PIN on PORTC as output
//# DDRC = 0x80;
// DDRC = 0b10000000;
// DDRB |= 1 << 7
// DDRC |= 1 << DDC7;
// bitSet(DDRC, 7);

//PORTC |= _BV(PC7); // _BV gets the bit value of pin C
//bitRead(PORTC, PC7);

//^ define all PORTC pins as outputs
//# DDRC = 0xFF;
//DDRC = 0b11111111;
//DDRC = 255;

for (uint8_t i = 0; i <= 7; i++)
{
Serial.print("set bit ");
bitSet(DDRC, i);
Serial.println(DDRC, DEC); //BIN
}

//^ set 1 pin to input
// DDRC &= ~(1 << DDC7);
//^ set HIGH
// PORTC |= 1 << PORTC7;

//^ define all PORTC pins as outputs
for (uint8_t i = 0; i <= 7; i++)
{
// Serial.print("Led port:");
// uint8_t printPort = portOutputRegister(digitalPinToPort(30+i));
// Serial.println(printPort);
// uint8_t pin_mask = digitalPinToBitMask(30+i);
// Serial.print("Led mask:");
// Serial.println(pin_mask);

pinMode(30+i, OUTPUT);
digitalWrite(30+i, LOW);
}

Serial.println("setupLedsPort end");
}

void setupBtnPort()
{
Serial.println("setupBtnPort begin");

uint8_t printPort = portOutputRegister(digitalPinToPort(64));
Serial.print("Button port:");
Serial.println(printPort);

uint8_t pin_mask = digitalPinToBitMask(64);
Serial.print("Button mask:");
Serial.println(pin_mask);

pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("setupBtnPort end");

btnPressedFlag = digitalRead(BUTTON_PIN); //? why using reverse flag?
}

void setup()
{
setupSerial();
setupLedsPort();
setupBtnPort();
Serial.println("Setup ended");
}

void loop()
{
//todo: make another function for reading button state.
if (!digitalRead(BUTTON_PIN) && !btnPressedFlag)
{ //todo If btnFlag is active -> it triggers function to read and store digitalRead in <buttonCurrentState> value
Serial.println("Button triggered");
btnPressedFlag = true;
uint32_t time = 0;
for (uint8_t i = FIRST_LED; i >= LAST_LED; i--) // todo: MY_PORT |= (1 << i);
{
digitalWrite(i, HIGH);
delay(240); //todo: configure delay in define
digitalWrite(i, LOW);

time = time + 240;
Serial.println(time); //? 1,2s = 1200
}

}
else if (digitalRead(BUTTON_PIN)) //^ could be just "else"
{
btnPressedFlag = false;
}
}

0 comments on commit d066c2a

Please sign in to comment.