C For Embedded Programming
C For Embedded Programming
Boolean operators used to for conditional expressions (eg: if statement) C does not contain a Boolean data type. Boolean operators yield results of type int, with true and false represented by 1 and 0. Any numeric data type may be used as a Boolean operand. Zero is interpreted as false; any non-zero value is interpreted as true. Bitwise operators operate on individual bit positions within the operands; The result in one bit position is independent of all the other bit positions.
(5 || !3) && 6 = (true OR (NOT true)) AND true = (true OR false) AND true = (true) AND true = true =1
(5 | ~3) & 6 = (00..0101 OR ~00..0011) AND 00..0110 = (00..0101 OR 11..1100) AND 00..0110 = (11..1101) AND 00..0110 = 00..0100 =4
m p m OR p 0 0 0 p 1 1 1 0 1 1 1 1
Interpretation
If bit m of the mask is 0, bit p is passed through to the result unchanged. If bit m of the mask is 1, bit p is set to 1 in the result.
m p m XOR p Interpretation If bit m of the mask is 0, 0 0 bit p is passed through 0 p 1 1 to the result unchanged. If bit m of the mask is 1, 0 1 bit p is passed through 1 ~p 1 0 to the result inverted.
Testing Bits
Form a mask with 1 in the bit position of interest; Bitwise AND the mask with the operand. The result is non-zero if and only if the bit of interest was 1:
if ((bits & 64) != 0) 0x64) /* check to see if bit 6 is set */ /* check to see if bit 6 is set * /* check to see if bit 6 is set */ Same as: if (bits &
b7b6b5b4b3b2b1b0
&
01000000
0b6000000
7
b7b6b5b4b3b 2b 1b 0
10000000
1b6b5b4b3b2b1b 0
Clearing Bits
Clearing a bit to 0 is accomplished with the bitwise-AND operator:
bits &= ~(1 << 7) ; /* clears bit 7 */ (1 << 7) ~(1 << 7) 10000000 01111111
Mask must be as wide as the operand! if bits is a 32-bit data type, the assignment must be 32-bit:
/* clears bit 7 */
Extracting Bits
Bits 15 - 11
Bits 10 - 5
Bits 4 - 0
Hours
Bits 15 - 11
Minutes
Bits 10 - 6
Seconds 2
Bits 5 - 0
?????
Bits 15 - 11
Hours
Bits 10 - 6
Minutes
Bits 5 - 0
00000
00000 Minutes
Minutes
0
10
Inserting Bits
Bits 15 - 11
Bits 10 - 5
Bits 4 - 0
oldtime newtime = oldtime & ~(0x3F << 5) newtime |= (newmins & 0x3F) << 5
Hours
Bits 15 - 11
Old Minutes
Bits 10 - 5
Seconds 2
Bits 4 - 0
Hours
Bits 15 - 11
000000
Bits 10 - 5
Seconds 2
Bits 4 - 0
Hours
New Minutes
Seconds 2
11
Structures in C
#include<stdio.h> struct{ float x; int y; char c; } brett; int main(void) { brett.x=2.345; brett.y=5; brett.c='b'; printf("x,y,c in brett is %f,%d,%c\n",brett.x,brett.y,brett.c); }
12
13
Pointers to Structures
#include<stdio.h> #include<stdlib.h> typedef struct{ float x; int y; char c; } elec3730; int main(void) { elec3730 *brett; brett=malloc(sizeof(elec3730)); if (brett != NULL) { (*brett).x=2.345; brett->y=5; brett->c='b'; printf("x,y,c in brett is %f,%d,%c\n",brett->x,(*brett).y,(*brett).c); free(brett); } } ELEC3730 Callaghan Campus 14
typedef unsigned char typedef struct { BOOL right_shift BOOL left_shift BOOL ctrl ; BOOL alt ; BOOL left_ctrl; BOOL left_alt ; } KEYS ;
; ;
void main(){ KEYS kybd ; do{ /* wait for both shift keys*/ get_keys(&kybd) } while (!kybd.left_shift || !kybd.right_shift) ; }
void get_keys(KEYS { int hit, *addr; *kybd)
addr=0x417; hit=*addr; kybd->right_shift kybd->left_shift kybd->ctrl kybd->alt kybd->left_alt kybd->left_ctrl } = = = = = = (hit (hit (hit (hit (hit (hit & & & & & & (1 (1 (1 (1 (1 (1 << << << << << << 0)) 1)) 2)) 3)) 9)) 8)) != != != != != != 0 0 0 0 0 0 ; ; ; ; ; ;
15
struct { unsigned
:5 , :6 , :5 ;
/* secs divided by 2 */
16
typedef unsigned char typedef struct { unsigned right_shift left_shift ctrl alt left_ctrl left_alt } KYBD_BITS;
void main(){ KEYS kybd ; do{ /* wait for both shift keys*/ get_keys(&kybd) } while (!kybd.left_shift || !kybd.right_shift) ; }
void get_keys(KEYS { int hit, *addr; KYBD_BITS *bits ; *kybd)
addr=0x417; hit=*addr; bits = (KYBD_BITS *) &hit ; kybd->right_shift = bits->right_shift kybd->left_shift = bits->left_shift kybd->ctrl = bits->ctrl kybd->alt = bits->alt kybd->left_alt = bits->left_alt kybd->left_ctrl = bits->left_ctrl } != != != != != != 0 0 0 0 0 0 ; ; ; ; ; ;
17
Given an address:
! !
Cast it as a pointer to data of the desired type, Then dereference the pointer by subscripting. Read or write various parts of an object named operand using:
((char *) &operand)[k]
18
typedef unsigned char typedef struct { char lo ; char hi ; short both ; } KEYS;
void main(){ KEYS kybd ; do{ /* wait for both shift keys*/ get_keys(&kybd) } while (!kybd.left_shift || !kybd.right_shift) ; }
void get_keys(KEYS { int hit, *addr; *kybd)
addr=0x417; hit=*addr; kybd->both = ((short *) &hit)[0] ; kybd->lo = ((char *) &hit)[0] ; kybd->hi = ((char *) &hit)[1] ; }
19
union { 31 0 unsigned long dd ; dd unsigned short dw[2] ; dw[1] dw[0] unsigned char db[4] ; db[3] db[2] db[1] db[0] 31 24 23 16 15 87 0 };
20
typedef unsigned char typedef union { char b[2] ; short int w ; } VARIANT ;
void main(){ KEYS kybd ; do{ /* wait for both shift keys*/ get_keys(&kybd) } while (!kybd.left_shift || !kybd.right_shift) ; }
void get_keys(KEYS { VARIANT *hit; hit=0x417; kybd->both = hit->w ; kybd->lo = hit->b[0] ; kybd->hi = hit->b[1] ; } *kybd)
21