Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned."— Presentation transcript:

1 Programming Microcontrollers in C Lecture L7.1

2 C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes-32768 – 32727 unsigned int2 bytes0 – 65535 long int4 bytes-2,147,483,648 – 2,147,483,647

3 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

4 Special C Operators OperatorExampleEquivalent 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;

5 Conditional Statements if(expression) statement; if(expression) statement_1; else statement_2; if(expression_1) statement_1; else if(expression_2) statement_2; else statement_3;

6 Case Statement switch(expression) { case 0: statement_0; break; case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; }

7 Loop Statements while(expression) statement; do statement; while (expression); for (expression_1; expression_2; expression_3) statement;

8 Conditonal Expression Operators &&AND ||OR !NOT >Greater than <Less than >=Greater than or equal <=Less than or equal ==Equal to !=Not equal to

9 Pointers char *ptr/* p points to a char */ void main(void) { char *ptr static char message[] = “Hello World!”; ptr = message (or ptr = &message[0];) printf(“%s\n”; ptr); }

10 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;

11 #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)

12 #include // Serial Peripheral Interface SPI void delay10ms(void); void delay50ms(void); void delay50ms(void); void SPIINIT(void) { SPICR2 = 0x10;// Enable /SS SPIBR = 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

13 // Send character out the SPI port void SENDSPI(unsigned char c) { while(!SPI_TXempty());// Wait for TX buffer empty SPIDR = c;// Send char while(!SPIDONE());// Wait till sent c = 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.)

14 // Write 4-bit instruction void instr4( unsigned char n) { n = n & 0x0F; SENDSPI(n);// EN LO, RS LO SENDSPI(n | 0x80); SENDSPI(n);// EN LO } // Write 8-bit instruction void instr8(unsigned char n) { instr4((n >> 4)); instr4(n) ; } // Write 4-bit data void data4(unsigned char c) { c = c & 0x0F; SENDSPI( c | 0x40); SENDSPI( c | 0xC0); SENDSPI( c | 0x40);// EN LO } // Write 8-bit data void data8(unsigned char n) { data4((n >> 4)); data4(n); } LCD.c (cont.)

15 // Initialize 4-bit wide void initlcd(void) { SPIINIT();// Initialize the SPI delay50ms(); instr4(3);// Function set delay50ms(); instr4(3);// Function set delay50ms(); instr4(3);// Function set delay50ms(); instr4(2);// Cursor to Home delay50ms(); instr8(0x2C);// 4-bits, 2 lines delay10ms(); instr8(6);// Cursor Increment, Shift off delay10ms(); instr8(0x0F);// Display, Cursor, and Cursor Blink off delay10ms(); instr8(1);// Clear Display, Cursor to Home delay20ms(); instr8(0x80);// Set address to 0 delay10ms(); SENDSPI(0);// Turn off all signals } LCD.c (cont.)

16 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.)

17 #include // Hex keypad decoding // Axiom keypad -- CMLS12C32 // Function Prototypes unsigned char keypad(void); void debounce(void); void initkey(void); unsigned char getkey(void); void wait_keyup(void); //Variables unsigned char key; unsigned char ptr; unsigned char dly ; // Initialize keypad Port P void initkey(void) { DDRP = 0xF0;// PP0-PP3 inputs PPSP = 0x0F;// set for pull downs PERP = 0x0F;// enable pull downs } keypad.c

18 // Keycode table const 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.)

19 // keypad: return 0 if key not pressed // return 1 if key pressed. Ascii code in key unsigned char keypad(void) { for(ptr=0;ptr 9) key = ptr + 0x37; else key = ptr + 0x30; debounce(); return(1); } } debounce(); return(0); } keypad.c (cont.)

20 // Delay a little void debounce() { dly = 255; while(dly--); } // Wait to press a key, then return key ascii code unsigned char getkey(void) { while(!keypad()); debounce(); return(key); } // Wait to release key void wait_keyup(void) { while(keypad()); debounce(); } keypad.c (cont.)

21 #include 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


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

Similar presentations


Ads by Google