Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127...

21
Programming Microcontrollers in C Lecture L7.1

description

C Operators =Assignment +Add -Subtract *Multiply /Divide %Modulus (remainder after division) &Logical bit-by-bit AND |Logical bit-by-bit OR ^Logical bit-by-bit exclusive-OR ~Logical bit-by-bit negation Shift right

Transcript of Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127...

Page 1: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

Programming Microcontrollersin C

Lecture L7.1

Page 2: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

C Data Types

Type Size Range

char 1 byte -128 – 127

unsigned char 1 byte 0 – 255

Int 2 bytes -32768 – 32727

unsigned int 2 bytes 0 – 65535

long int 4 bytes -2,147,483,648 – 2,147,483,647

Page 3: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

C Operators= Assignment+ Add- Subtract* Multiply/ Divide

% Modulus (remainder after division)& Logical bit-by-bit AND| Logical bit-by-bit OR^ Logical bit-by-bit exclusive-OR~ Logical bit-by-bit negation

<< Shift left>> Shift right

Page 4: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

Special C OperatorsOperator Example Equivalent to:

= a = b = c = 0; a=0; b=0; c=0;++ a++ a = a + 1;-- a-- a = a - 1;+= a += 2; a = a + 2;-= a -= 2; a = a - 2;|= a |= 2; a = a | 2;&= a &= 2; a = a & 2;<<= a <<= 3; a = a << 3;>>= a >>= 3; a = a >> 3;

Page 5: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

Conditional Statementsif(expression)

statement;

if(expression)statement_1;

elsestatement_2;

if(expression_1)statement_1;

else if(expression_2)statement_2;

elsestatement_3;

Page 6: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

Case Statement

switch(expression){

case 0: statement_0; break;case 1: statement_1; break;case 2: statement_2; break;case 3: statement_3; break;

}

Page 7: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

Loop Statementswhile(expression)

statement;

do statement;while (expression);

for (expression_1; expression_2; expression_3)statement;

Page 8: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

Conditonal Expression Operators&& AND|| OR! NOT> Greater than< Less than

>= Greater than or equal<= Less than or equal== Equal to!= Not equal to

Page 9: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

Pointers

char *ptr /* p points to a char */

void main(void){char *ptrstatic char message[] = “Hello World!”;ptr = message (or ptr = &message[0];)printf(“%s\n”; ptr);}

Page 10: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

mc9s12c32.h#define _REG_BASE 0#define _ADDR(off) (unsigned char volatile *)(_REG_BASE + off)#define _P(off) *(unsigned char volatile *)(_REG_BASE + off)#define _LP(off) *(unsigned short volatile *)(_REG_BASE + off)

#define PORTA _P(0x00)#define PORTAB _LP(0x00)#define PORTB _P(0x01)#define DDRA _P(0x02)#define DDRAB _LP(0x02)#define DDRB _P(0x03)

DDRA = 0xF0;

PORTA = 0xAE;

New_val = PORTA;

Page 11: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

#define CANRXIDR0 _P(0x160)#define CANRXIDR1 _P(0x161)#define CANRXIDR2 _P(0x162)#define CANRXIDR3 _P(0x163)#define CANRXDSR0 _P(0x164)#define CANRXDSR1 _P(0x165)#define CANRXDSR2 _P(0x166)#define CANRXDSR3 _P(0x167)#define CANRXDSR4 _P(0x168)#define CANRXDSR5 _P(0x169)#define CANRXDSR6 _P(0x16A)#define CANRXDSR7 _P(0x16B)#define CANRXDLR _P(0x16C)

#define CANRXFG _ADDR(0x160)#define CANTXFG _ADDR(0x170)

#define _REG_BASE 0#define _ADDR(off) (unsigned char volatile *)(_REG_BASE + off)#define _P(off) *(unsigned char volatile *)(_REG_BASE + off)#define _LP(off) *(unsigned short volatile *)(_REG_BASE + off)

Page 12: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

#include <mc9s12c32.h>// Serial Peripheral Interface SPI

void delay10ms(void);void delay50ms(void);void delay50ms(void);

void SPIINIT(void){

SPICR2 = 0x10; // Enable /SSSPIBR = 0x00; // 4 MHz (/2)SPICR1 = 0x52; // CPHA = 0, CPO; = 0

}

// Is SPI data sent?unsigned char SPIDONE(){

return(SPISR & 0x80);}

// Is SPI transmit buffer empty?unsigned char SPI_TXempty(){

return(SPISR & 0x20);}

LCD.c

Page 13: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

// Send character out the SPI portvoid SENDSPI(unsigned char c){ while(!SPI_TXempty()); // Wait for TX buffer empty

SPIDR = c; // Send charwhile(!SPIDONE()); // Wait till sentc = SPIDR; // Clear SPIF

}void delay10ms(void){

unsigned long i;for(i=0; i <=225 ; i++);

} void delay20ms(void){

delay10ms(); delay10ms();

}void delay50ms(void){

delay10ms(); delay10ms();delay10ms(); delay10ms(); delay10ms();

}

LCD.c (cont.)

Page 14: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

// Write 4-bit instructionvoid instr4( unsigned char n){

n = n & 0x0F;SENDSPI(n); // EN LO, RS LOSENDSPI(n | 0x80);SENDSPI(n); // EN LO

}// Write 8-bit instructionvoid instr8(unsigned char n){

instr4((n >> 4));instr4(n) ;

}// Write 4-bit datavoid data4(unsigned char c){

c = c & 0x0F;SENDSPI( c | 0x40);SENDSPI( c | 0xC0);SENDSPI( c | 0x40); // EN LO

}// Write 8-bit datavoid data8(unsigned char n){

data4((n >> 4));data4(n);

}

LCD.c (cont.)

Page 15: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

// Initialize 4-bit widevoid initlcd(void){

SPIINIT(); // Initialize the SPIdelay50ms();instr4(3); // Function setdelay50ms();instr4(3); // Function setdelay50ms();instr4(3); // Function setdelay50ms();instr4(2); // Cursor to Homedelay50ms();instr8(0x2C); // 4-bits, 2 linesdelay10ms();instr8(6); // Cursor Increment, Shift off

delay10ms();instr8(0x0F); // Display, Cursor, and Cursor Blink

offdelay10ms();instr8(1); // Clear Display, Cursor to Homedelay20ms();instr8(0x80); // Set address to 0delay10ms();SENDSPI(0); // Turn off all signals

}

LCD.c (cont.)

Page 16: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

void clearlcd(void){

instr8(1); // Clear Display, Cursor to Home

delay10ms();} void lcdout( unsigned char ascii){

data8(ascii); // Send ascii code to LCD}

LCD.c (cont.)

Page 17: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

#include <mc9s12c32.h>// Hex keypad decoding// Axiom keypad -- CMLS12C32

// Function Prototypesunsigned char keypad(void);void debounce(void);void initkey(void);unsigned char getkey(void);void wait_keyup(void);

//Variablesunsigned char key;unsigned char ptr;unsigned char dly ;

// Initialize keypad Port Pvoid initkey(void){

DDRP = 0xF0; // PP0-PP3 inputsPPSP = 0x0F; // set for pull downsPERP = 0x0F; // enable pull downs

}

keypad.c

Page 18: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

// Keycode tableconst unsigned char keycodes[16]={

/* key row col */0x14, /* 0 1 2 */0x88, /* 1 4 1 */0x84, /* 2 4 2 */0x82, /* 3 4 3 */0x48, /* 4 3 1 */0x44, /* 5 3 2 */0x42, /* 6 3 3 */0x28, /* 7 2 1 */0x24, /* 8 2 2 */0x22, /* 9 2 3 */0x81, /* A 4 4 */0x41, /* B 3 4 */0x21, /* C 2 4 */0x11, /* D 1 4 */0x18, /* E/* 1 1 */0x12, /* F/# 1 3 */

};

keypad.c (cont.)

Page 19: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

// keypad: return 0 if key not pressed// return 1 if key pressed. Ascii code in keyunsigned char keypad(void){

for(ptr=0;ptr<=16;ptr++){

PTP = keycodes[ptr];if(PTP == keycodes[ptr]){

if(ptr>9)key = ptr + 0x37;

elsekey = ptr + 0x30;

debounce();return(1);

}}debounce();return(0);

}

keypad.c (cont.)

Page 20: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

// Delay a littlevoid debounce(){

dly = 255;while(dly--);

}

// Wait to press a key, then return key ascii codeunsigned char getkey(void){

while(!keypad());debounce();return(key);

}

// Wait to release keyvoid wait_keyup(void){

while(keypad());debounce();

}

keypad.c (cont.)

Page 21: Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned.

#include <mc9s12c32.h>

void initlcd(void);void lcdout( unsigned char);void initkey(void);unsigned char getkey(void);void wait_keyup(void);

char keypress;

void main(void){ initkey();

initlcd(); while(1) { keypress = getkey();

lcdout(keypress);wait_keyup();

}}

keylcd.c