Download presentation
Presentation is loading. Please wait.
Published byCollin McKinney Modified over 8 years ago
1
15-348 Embedded Systems Lecture 4 January 20, 2016
2
Road Map What have we done Some motivation about embedded systems Gotten familiar with embedded hardware What we plan on doing Bit manipulation in C Input and Output using I/O ports
3
Bits and Bytes – A Review The unit of information in computers is a bit 8 bits make one byte How do we represent a number using bits?
4
Data vs Values We can use bits to represent Values Data So what is the difference? Values are numbers where we can do arithmetic Values can be signed or unsigned In Data representation, different bit groups will denote different values
5
What does that mean? We can use 32 bits to represent my Salary Sum of each bit multiplied by appropriate powers of 2 will determine how many dollars I actually get I can add a bonus to my salary by adding a 32 bit Salary to a 32 bit bonus getting a 32 bit dollar amount (with 32 bits and my Salary, there will never be any overflow) But what if the 32 bits represent a color?
6
Data representation A color can be represented with 32 bits as: 0xAARRGGBB AA is an 8 bit value representing alpha (opacity) RR is an 8 bit value representing red component GG is an 8 bit value representing green component BB is an 8 bit value representing blue component What does it mean to add two colors together? 0x0000FFFF + 0x000000FF = 000100FE 0x0000FF00 - 0x00000001 = 0000FEFF
7
How can we work with bits? We want to work with individual bits of a variable instead of the value they represent How do we clear a bit int error_Flags = getflags(); // now clear bit 5? error_Flags &= 0xDF; error_Flags &= ~(0x01 << 5);
8
Bit Manipulations How do we set a bit? int error_Flags = getflags(); // now set bit 5? error_Flags |= 0x20;
9
Good Programming practice Define a macro for set/clear bits #define BitNClear(arg,bit)(arg &= ~(1<<bit)) #define BitNSet(arg,bit) (arg |= (1<<bit)) Define constants for Flags #define IR_NOT_WORKING_FLAG5
10
Use descriptive code #define BitNSet(arg,bit) (arg |= (1<<bit)) #define LED1 5 int main() { int lights = 0; // turn LED 1 on BitNSet(lights,LED1); }
11
What do you think the following do? #define BitNflp(arg,bit) ((arg) ^= BitMsk(bit)) #define BitNtst(arg,bit) ((arg)&BitMsk(bit))
12
Using bit manipulation #define BitNSet(arg,bit)(arg |= (1<<bit)) #define LED1 5 #define BitNTst(arg,bit) (arg & (1<<bit)) int main(){ int lights = 0; // turn LED 1 on BitNSet(lights,LED1); if(BitNTst(lights,LED1)) printf("LED1 is on"); else printf("LED1 is off"); }
13
Defining Bits as part of a struct Bit fields allow packing of data in a struct struct Lights_Status { unsigned int LeftTurnSignal:1; unsigned int RightTurnSignal:1; unsigned int HeadLights:1; unsigned int BrakeLights:1; unsigned int NumLightsOn:3; };
14
Using Bit fields struct Lights_Status carLights; carLights.LeftTurnSignal = 1; carLights.HeadLights = 1; carLights.NumLightsOn = 2;
15
Input/Output in “MC9S12C” We will use pins PA[0-7], PB[0-7], and PP[0-1] as general purpose Input/Output pins. PA[0-7] Pins(odd numbers only) 39-25 PB[0-7] Pins(odd numbers only) 55-41 PP0 Pin 9 and PP1 Pin 11
16
Talk about the IO pins setup Microcontrollers have physical pins that can be configured as input or output Pins are grouped in ports Each port has 8 pins Port have names like A, B, C, etc. Pins are numbered 0, 1, 2, … A0, A1, A2, A3, B0, B1, … A Pin can be configured to be input or output
17
Pin Architecture Microprocessor D D Q Q Write to port address Write to direction register Read from port address
18
Configuring the Pins Each IO pin can be configured to be either input or output Use Data Direction Register DDR to set the direction of the pin. 0 means input, 1 means output. E.g. DDRB = 0x45 DDRB = 01000101 PB0, PB2, and PB6 are outputs PB1, PB3, PB4, PB5, PB7 are inputs
19
Using output pins Let’s start from DDRB = 0x45 If we want to read the value on PB3 if(PORTB_BIT3 == 1) // do something if input is 1 else // do something else if input is 0
20
Writing to IO pins To write a 1 to PB0 PORTB_BIT0 = 1; Following are defined for you: DDRA_BIT[0-7] DDRB_BIT[0-7] PORTB_BIT[0-7] PORTA_BIT[0-7] Use, #include
21
Lets see an example Pin PB4 is connected to one of the LEDs: int i ; DDRB_BIT4 = 1; PORTB_BIT4 = 1; for(i = 0; i <100; i++); PORTB_BIT4 = 0; What would happen in this case?
22
Another Example main( ) { DDRB = 0x00; DDRA = 0xFF; for(;;) { if(PORTB_BIT0 == 0) PORTA_BIT0 = 1; else PORTA_BIT0 = 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.