Bare Metal Programming
Bare Metal Programming
0 0 1 1 0 0 1 0
0 0 32 16 0 0 2 0
50 = 0+0+32+16+0+02+0
0 0 32 0 0 0 2 0
void setup()
pinMode(LED_BUILTIN, OUTPUT);
void loop()
digitalWrite(LED_BUILTIN, HIGH);
Want to see it work? Please access
Circuit in TinkerCad. Select an delay(1000); // Wait for 1000 millisecond(s)
Arduino and review how to program digitalWrite(LED_BUILTIN, LOW);
Pin 13 or the BuiltIn Pin See link
delay(1000); // Wait for 1000 millisecond(s)
}
Bare Metal Programming
Port B has an 8 Bit Register
Port Bin Dec
PortB0 Digital Pin 8
HINT 32
//pinMode(LED_BUILTIN, OUTPUT);
void loop()
PORTB=32; //0010000;
Click here for solution
delay(2000); // Wait for 1000 millisecond(s)
PORTB=0;
}
Bare Metal Programming
Now go ahead and change
the DDR register
variable to suit Pin 9
Bare Metal Programming - memory & Pointers
Bare Metal Programming - memory & Pointers
Bare Metal Programming - memory & Pointers
So how do we
write 32 to Ox25
(PORTB)?
Bare Metal Programming - memory & Pointers
So how do we
write 32 to Ox25
(PORTB)?
We use pointers!
Bare Metal Programming - memory & Pointers
So how do we
write 32 to Ox25
(PORTB)?
We use pointers!
A Pointer in the C
language is a variable
that points to the
memory location of
another variable
memory address.
Bare Metal Programming - memory & Pointers
We use pointers!
Simple Example
int classicVariable = 16; // create a “classic integer variable
int* pointerToClassicVariable = &classicVariable; //create a pointer to “point” to classicVariable or reference the variable
volatile byte* pointerToRegisterB = 0x25; // create a pointer variable called pointerToRegisterB which is pointed to Ox25;
void setup()
{
DDRB = 32;// B010000 sets PB5 as OUTPUT PB5 is 0010000 or 32 );
}
void loop()
{
volatile byte* pointerToRegisterB = 0x25;
*pointerToRegisterB = 32; //PORTB=32;// PORTB is from pinout 32 decimal
volatile byte* pointerToRegisterB = 0x25; // create a pointer variable called pointerToRegisterB which is pointed to Ox25;
volatile byte* pointerToRegisterB = 0x25; // create a pointer variable called pointerToRegisterB which is pointed to Ox25;
volatile byte* pointerToRegisterB = 0x25; // create a pointer variable called pointerToRegisterB which is pointed to Ox25;
volatile byte* pointerToRegisterB = 0x25; // create a pointer variable called pointerToRegisterB which is pointed to Ox25;
volatile byte* pointerToRegisterB = 0x25; // create a pointer variable called pointerToRegisterB which is pointed to Ox25;
void loop()
{
/* volatile byte* memoryPointer = (volatile byte*) 0x25;// we are assigning 0x25 memory to memoryPointer
*memoryPointer=32; */
* ( (volatile byte*) 0x25) = 32; //replaces two lines of the above code
delay(2000); // Wait for 1000 millisecond(s)
* ( (volatile byte*) 0x25) = 0;
delay(1000); // Wait for 1000 millisecond(s)
}
Bare Metal Programming - memory & Pointers
So how do we write 32 to Ox25 (PORTB)?
void loop()
{
* ( (volatile byte*) 0x25) = 32; //replaces two lines of the above code
delay(2000); // Wait for 1000 millisecond(s)
* ( (volatile byte*) 0x25) = 0;
delay(1000); // Wait for 1000 millisecond(s)
}
Ok, Pin 13 is turned on, but the other pins are turned off!
Bare Metal Programming - Quick register reminder
Port B has an 8 Bit Register
Port Bin Dec
PortB0 Digital Pin 8
0 0 1 0 0 0 0 0
Ok, Pin 13 is turned on, but the other pins are turned off!
0 0 0 0 1 0 1 0
OR
0 0 1 0 0 0 0 0
0 0 1 0 1 0 1 0
PORTB = PORTB|32 OR PORTB|=32 - turns on Pin 13 while leaving on Pin 9 and Pin 11
Bare Metal Programming - BIT Masking
We have been using axe, time for a scalpel
AND OPERATION - TURN OFF A PIN
0 0 1 0 1 0 1 0
AND
1 1 0 1 1 1 1 1
0 0 0 0 1 0 1 0
PORTB = PORTB&223 OR PORTB&=233 - turns off Pin 13 while leaving on Pin 9 and Pin 11
Bare Metal Programming - BIT Masking
We have been using axe, time for a scalpel
AND OPERATION - TURN OFF A PIN
PORTB = PORTB&223 OR PORTB&=233 - turns off Pin 13 while leaving on Pin 9 and Pin 11
PORTB = PORTB&223 OR PORTB&=233 - turns off Pin 13 while leaving on Pin 9 and Pin 11
0 0 1 1 1 0 1 0 PORTB
0 1 1 1 0 1 0 0 PORTB <<1
1 1 1 0 1 0 0 0 PORTB <<2
1 1 0 1 0 0 0 0 PORTB <<3
Bare Metal Programming - BIT Masking
Bit Shifting
0 0 0 0 0 0 0 1 PORTB
0 0 0 0 0 0 1 0 PORTB <<1
0 0 0 0 0 1 0 0 PORTB <<2
0 0 0 0 1 0 0 0 PORTB <<3
Bare Metal Programming - BIT Masking
We have been using axe, time for a scalpel
Bit Banging - one bit on
0 0 0 0 1 0 1 0
OR
0 0 1 0 0 0 0 0 1<<5
0 0 1 0 1 0 1 0
PORTB | = 32
PORTB |=(1<<5)
Bare Metal Programming - BIT Masking
Bit Shifting
0 0 0 0 0 0 1 0 1<<2
1 1 1 1 1 1 0 1 ~(1<<2)
Bare Metal Programming - BIT Masking
Bit Shifting
1 0 1 0 1 0 1 0
AND
1 1 0 1 1 1 1 0 ~(1<<5)
1 0 0 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0
1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0
XORR XORR