EEE 204 Lecture8
EEE 204 Lecture8
2
Variables in C
Static and Volatile variables are different.
A static variable is simply a variable that exists for the lifetime of the application. Static
variables can be global: defined outside of a function and accessible everywhere, or
local: defined within a function and only accessible from within that function.
Local static variables are created on the first invocation of the function and remain in
memory for the function to use when next called.
The static keyword enforces the compiler to ensure that the RAM for the variable is
always reserved and not reused for other variables.
So if you need a variable that is only used within a function and only updated by that
function and you need the value of that variable to remain for the next time that you
call the function then you need to define it as a static variable.
static int MyVariable; = 0;
3
Variables in C
A volatile variable is one the can be changed without the compilers knowledge
for example by an interrupt. This means that whenever this variable is accessed
by the program it must be re-read from the actual memory location in case it
has changed, rather than simply read from the cache or registers on the ALU if it
has already been used recently by the program.
volatile int MyVariable; = 0;
A variable can be both static (always present) and volatile (changed from anywhere).
static volatile int MyVariable; = 0;
4
Bitwise Logic operators in C
Operator Description Example
~ Complement each bit of the variable MyVar=~ MyVar;// complements all bits
| OR the variable bit-by-bit MyVar=MyVar | 0b00000001;//Set the last bit
& AND the variable bit-by-bit MyVar=MyVar & 0b11111110;//Clear the last bit
^ XOR the variable bit-by-bit MyVar=MyVar ^ 0b10000000;//Toggle the first
bit
<< Rotate left the variable n times arithmetically MyVar=MyVar<<4; //Rotate left 4 times
>> Rotate right the variable n times arithmetically MyVar=MyVar>>3; //Rotate right 3 times
The table given above summarizes all the Bitwise Logic Operators in C
5
Bitwise Logic Operators in C
Ex: Run the following program and observe the changings e and f variables.
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
int e= 0b1111111111110000;
int f= 0x0001;
while(1)
{
e=~e; // Complement all the bits in e, e=0000 0000 0000 1111
e=e|BIT7;// Set bit 7 by using OR with 0b10000000, e=0000 0000 1000 1111
e=e&~BIT0;// Clear bit 0 by using AND with 0b11111110, e=0000 0000 1000 1110
e=e^BIT4; // Toggle Bit 4 by using XOR with 0b00010000, e=0000 0000 1001 1110
e|=BIT6;// Set bit 6 by using OR with 0b10000000, e=0000 0000 1101 1110
e&=~BIT1;// Clear bit 1 by using AND with 0b11111101, e=0000 0000 1101 1100
e^=BIT3; // Toggle Bit 3 by using XOR with 0b00001000, e=0000 0000 1101 0100
7
Conditional Statements (If)
Ex. Write a C program by using C Language that turns the LED on connected to P4.7 when 2.1 is pressed.
#include <msp430.h>
#define BUTTON P2IN
#define LED P4OUT
int main(void)
{
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P2DIR=0x00;// P2 is input
P4DIR=0xFF;// P4 is output
P4OUT=0x00; // Clear P4
while(1)// Always check!, otherwise it checks once and ends the program
{
if (BUTTON==0xFD)// Means “If button is pressed”, it is connected to P2.1
{
LED |= BIT7; // LED is ON
}
}
return 0;
}
8
Conditional Statements (If)
#include <msp430.h>
* In the previous example, even if #define BUTTON P2IN
the button is not pressed program #define LED P4OUT
does nothing or do not know how int main(void)
to handle and the LED keeps ON.
{
It is also possible to handle if the
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
condition is not satisfied.
P2DIR=0x00;// P2 is input
* Examine the example given
nearby! P4DIR=0xFF;// P4 is output
* In the example, if button is P4OUT=0x00; // Clear P4
pressed LED is ON, if not pressed while(1)// Always check!, otherwise it checks once and ends the program
LED is OFF {
if (BUTTON==0xFD)// If button is pressed, it is connected to P2.1
{
LED |= BIT7; //LED is ON
}
LED &=~BIT7; // LED is OFF if button is not pressed
}
return 0;
}
9
Conditional Statements (If)
#include <msp430.h>
• Since the total lines of code for
#define BUTTON P2IN
if statement is only one. It is
not necessary to use {} #define LED P4OUT
symbols. int main(void)
• Check the same example {
without {} WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P2DIR=0x00;// P2 is input
P4DIR=0xFF;// P4 is output
P4OUT=0x00; // Clear P4
while(1)// Always check!, otherwise it checks once and ends the program
{
if (BUTTON==0xFD)// If button is pressed, it is connected to P2.1
LED |= BIT7; //Condition is satisfied, LED is ON
LED &=~BIT7; // Condition is satisfied, LED is OFF
}
return 0;
}
10
Conditional Statements (If-else)
if(condition) // condition to satisfy * In if-else statement, if the
{ condition is not satisfied, program
flowing continues with the block
. // codes of codes in else statement.
. // to
. // execute
}
else // if the condition is not satisfied
{
. // codes
. // to
. // execute
}
11
Conditional Statements (If-else)
#include <msp430.h>
#define BUTTON P2IN
#define LED2 P4OUT * In if-else statement, if the
#define LED1 P1OUT condition is not satisfied, program
int main(void) flowing continues with the block
{
of codes in else statement.
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P2DIR=0x00; P4DIR=0xFF; P4OUT=0x00; P1DIR=0xFF; P1OUT=0x00;
while(1)// Always check!, otherwise it checks once and ends the program
{
if (BUTTON==0xFD)// If button on P2.1 is pressed
{
LED1 |= BIT0; //LED on P1.0 is ON
LED2 &=~BIT7; //LED on P4.7 is OFF
} * If the number of lines of code to
execute is more than 1 line and
else // If button on P2.1 is NOT pressed
{ the condition is not satisfied,
LED2 |= BIT7; //LED on P1.0 is OFF else statement can really be
LED1 &=~BIT0; //LED on P4.7 is ON useful.
}
}
return 0;
}
12
Conditional Statements (If-else if- else)
if(condition 1) // condition to satisfy
{
. // codes
. // to • In if- else if- else
. // execute
statement, if the condition1 is
}
else if (condition 2)// if the cond. 1 is not but cond.2 is satisfied not satisfied, program flow
{ continues with the next
. // codes condition (else if)
. // to
. // execute
checking and continues in this
} way until a condition is
else if (condition 3)//if cond. 1 and cond.2 are not but cond.3 is satisfied satisfied. Code block of
{
satisfied condition is executed.
. // codes
. // to • If none of the condition is
. // execute satisfied, code block belonging
} to else is executed.
.
.
• Only one condition can be
. satisfied at the same time.
else // If none of the conditions are not satisfied • Conditions MUST NOT overlap!
{
. // codes
. // to
. // execute
}
13
Conditional Statements (If-else if- else)
if(condition 1) // condition to satisfy
{
. // codes
. // to • In if- else if- else
. // execute
statement, if the condition1 is
}
else if (condition 2)// if the cond. 1 is not but cond.2 is satisfied not satisfied, program flow
{ continues with the next
. // codes condition (else if)
. // to
. // execute
checking and continues in this
} way until a condition is
else if (condition 3)//if cond. 1 and cond.2 are not but cond.3 is satisfied satisfied. Code block of
{
satisfied condition is executed.
. // codes
. // to • If none of the condition is
. // execute satisfied, code block belonging
} to else is executed.
.
.
• Only one condition can be
. satisfied at the same time.
else // If none of the conditions are not satisfied • Conditions MUST NOT overlap!
{
. // codes
. // to
. // execute
}
14
Conditional Statements (If-else if- else)
#include <msp430.h>
• Ex: Write a C language program #define BUTTON1 P2IN
#define BUTTON2 P1IN
that.. #define LED2 P4OUT
• Turns ON both LEDs when #define LED1
int main(void)
P1OUT
15
Conditional Statements (while)
while(condition) // condition to satisfy
{
. // codes
. // to
. // execute
}
While condition inside the parantheses is satisfied, the codes between { } are executed.
Otherwise, program flow continues.
16
Conditional Statements (while)
Ex. Write a C program that turns the LED ON connected to P4.7 while the button on P2.1 is not
pressed.
#include <msp430.h> • Like in if statement, if the
#define BUTTON P2IN number of codes to execute is
#define LED P4OUT no more than 1 line, we do not
int main(void) have to use {}
{
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P2DIR=0x00;// P2 is input
P4DIR=0xFF;// P4 is output
P4OUT=0x00; // Clear P4
while(1)// Always check!, otherwise it checks once and ends the program
{
while (BUTTON!=0xFD)// Means “While button is not pressed”, it is connected to P2.1
{
LED |= BIT7; // LED is ON
}
LED &=~BIT7; // LED is OFF
}
return 0;
}
17
Conditional Statements (do while)
do
{ // codes
. // to
. // execute
}
while(condition); // condition to satisfy
In case we need to execute the code once at least or check the condition to satisfy after
the execution of the code. We can employ do-while loop.
18
Conditional Statements (do while)
Ex: Write the C program that toggles LED on P1.0 and P4.7 alternatively with some delay.
#include<msp430.h>
void Delay_Func(void);
void Delay_Func(void)
{
volatile unsigned long i;
i=50000;
do i--;
while(i != 0);
}
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P4DIR = 0x80; // Set P4.7 to output direction
P1DIR = 0x01; // Set P1.0 to output direction
while(1)
{
P4OUT = 0x00; // Toggle P4.7 is OFF
P1OUT = 0x01; // Toggle P1.0 is ON
Delay_Func();
P4OUT = 0x80; // Toggle P4.7 is ON
P1OUT = 0x00; // Toggle P1.0 is OFF
Delay_Func();
}
}
19
Loop Statements (for)
for (initialization; condition; iteration) // condition to satisfy
{
. // codes
. // to
. // execute
}
In for loop, after each iteration, condition is checked if it is satisfied or not. As long
as it is satisfied, codes inside {} are executed. Otherwise, program flow continues
from the end point of the for loop.
20
Conditional Statements (for)
Ex. Write a C program that blinks the LED on connected to P4.7 four times when 2.1 is pressed.
#include <msp430.h>
#define BUTTON P2IN
#define LED P4OUT
void MyDelay(void); • for loop is terminated when
void MyDelay(void) // Delay Function j=8. Because it is expected to
{ blink the LED four times
volatile unsigned long int i;
for(i=1; i<50000; i++); • Even if it is possible to declare
} a variable in for loop, it is not
int main(void)
{
in C!
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P2DIR=0x00;// P2 is input
P4DIR=0xFF;// P4 is output
P4OUT=0x00; // Clear P4
while(1)// Always check!, otherwise it checks once and ends the program
{
char j;
if (BUTTON==0xFD)// Means “If button is pressed”, it is connected to P2.1
{
for (j=0; j<8; j++)
{
LED ^= BIT7;
MyDelay();
}
}
}
return 0;
}
21
Conditional Statements (switch)
switch(choice)
{
case 1: // do something //
...
break;
case 2: // do something else //
...
break;
.
.
.
default: // for all other values //
break;// optional
}
In switch loop, choice is received as a parameter and the corresponding case is executed.
Program execution continues until it sees break. If no case is selected, default case is executed.
22
Conditional Statements (switch) * Same example of else
#include <msp430.h>
#define BUTTON1 P2IN
if with a switch way
switch(m)
#define BUTTON2 P1IN {
#define LED2 P4OUT case 1:
#define LED1 P1OUT LED1 &=~BIT0; //LED on P1.0 is OFF
int main(void) LED2 |= BIT7; //LED on P4.7 is ON
{ break;
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P2DIR=0x00; P4DIR=0xFF; P4OUT=0x00; P1DIR=0xFD; P1OUT=0x00; case 2:
LED1 |= BIT0; //LED on P1.0 is ON
char m; LED2 &=~BIT7; //LED on P4.7 is OFF
while(1)// Always check!, otherwise it checks once and ends the program break;
{
case 3:
if (BUTTON1==0xFD && BUTTON2==0)// If both buttons are
LED1 |= BIT0; //LED on on P1.0 is ON
pressed
LED2 |= BIT7; //LED on on P4.7 is ON
m=3; break;
else if (BUTTON1==0xFD && BUTTON2==2) // If BTN1 is default:
pressed, BTN2 not pressed LED1 &=~BIT0; //LED on P1.0 is OFF
m=2; LED2 &=~BIT7; //LED on P4.7 is OFF
else if (BUTTON1==0xFF && BUTTON2==0) // If BTN2 is }
pressed, BTN1 not pressed
m=1; }
else // If none is pressed return 0;
m=0; }
23
Conditional Statements (switch) * Let’s remove breaks
and see what happens
#include <msp430.h> switch(m)
#define BUTTON1 P2IN {
#define BUTTON2 P1IN case 1:
#define LED2 P4OUT LED1 &=~BIT0; //LED on P1.0 is OFF
#define LED1 P1OUT LED2 |= BIT7; //LED on P4.7 is ON
void MyDelay(void); MyDelay();
void MyDelay(void) // Delay Function
{ case 2:
volatile unsigned long int i; LED1 |= BIT0; //LED on P1.0 is ON
for(i=1; i<50000; i++); LED2 &=~BIT7; //LED on P4.7 is OFF
} MyDelay();
int main(void)
{ case 3:
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer LED1 |= BIT0; //LED on on P1.0 is ON
P2DIR=0x00; P4DIR=0xFF; P4OUT=0x00; P1DIR=0xFD; P1OUT=0x00; LED2 |= BIT7; //LED on on P4.7 is ON
char m; MyDelay();
while(1)// Always check!, otherwise it checks once and ends the program
{ default:
if (BUTTON1==0xFD && BUTTON2==0)// If both buttons are pressed LED1 &=~BIT0; //LED on P1.0 is OFF
m=3; LED2 &=~BIT7; //LED on P4.7 is OFF
else if (BUTTON1==0xFD && BUTTON2==2) // If BTN1 is pressed, BTN2 not pressed MyDelay();
m=2; }
else if (BUTTON1==0xFF && BUTTON2==0) // If BTN2 is pressed, BTN1 not pressed }
m=1; return 0;
else // If none is pressed }
m=0;
24