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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Micro-controllers Anurag Dwivedi.
C Examples 1.
Programming In C++ Spring Semester 2013 Lecture 8 Programming In C++, Lecture 8 By Umer Rana.
Input/Output Ports and Interfacing ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
 | bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Embedded Systems 7763B Mt Druitt College of TAFE Electrical Engineering Lesson 2 LCD Display Interfacing.
Programming the HC12 in C. Some Key Differences – Note that in C, the starting location of the program is defined when you compile the program, not in.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
The Serial Peripheral Interface (SPI)
An Embedded C Program 1 Mainly from textbook: Embedded C Programming and the Atmel AVR 2 nd Edition Barnett, Cox and O’Cull.
 C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories. 
Digital Outputs LCD Display
The Serial Peripheral Interface (SPI) Chapter 8 CML9S12-DP256.
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
1 4-Integrating Peripherals in Embedded Systems (cont.)
C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill.
Input/Output Ports and Interfacing
Practical Electronics & Programming
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
CS-280 Dr. Mark L. Hornick 1 Programming for the LCD Display.
UniMAP 1 Interfacing Peripherals. UniMAP 2 Interfacing devices on Embedded Linux In general, to interface to a device connected to an embedded Linux platform.
Parallel Interfacing Chapter 7. Parallel Interfacing Parallel I/O Ports Using Parallel Ports Seven-Segment Displays Keypad Interfacing Liquid Crystal.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
Digital Inputs Interfacing Keypad
Keypad Lecture L4.6. Port Integration Module PIM_9C32 Block Guide V01.06 Reference: S12C32PIMV1.pdf.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Input Interface – Microprocessor
LHO 22 C and the  The Silicon Labs ISE uses the Keil C51 compiler.  The code size is limiter to 2K  C has replaced PL/M (the original Intel high.
UNIT 7 - INTRODUCTION TO I/O INTERFACING. TWO MAJOR TYPES OF I/O INTERFACING ISOLATED I/O - DEDICATED I/O INSTRUCTIONS ARE USED TO ACCESS I/O DEVICES.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Arduino Mega Arduino Mega 2560 Arduino Mega 2560.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
ECE 447: Lecture 12 Keypads ECE 447: Lecture 10. ECE 447: Matrix Keypad.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Examples Lecture L2.2. // Example 1a: Turn on every other segment on 7-seg display #include /* common defines and macros */ #include /* derivative.
Vishwakarma government engineering college Prepare by. Hardik Jolapara( ) LCD Interfacing with ATmega16.
Introduction to Projects using Microcontroller Md. Khalilur Rhaman Credit: Interface Lab.
Sitarambhai Naranjibhai Patel Institute of Technology and Research Centre, Umrakh, Bardoli. A Presentation On “ 16x2 LCD Interfacing with AVR atmega32.
INTERFACING KEYBOARD WITH ATMEGA32 ELECTRONICS & COMMUNICATION TITLE.
Peripherals – Keypad The Keypad provides a simple means of numerical data or control input. The keys can be attributed whatever data or control values.
LCD Interfacing using Atmega 32
4-Integrating Peripherals in Embedded Systems (cont.)
Introduction to C Programming
LCD and Keyboard Interfacing
UNIVERSAL COLLEGE OF ENGINEERING & TECHNOLOGY
GANDHINAGAR INSTITUTE OF TECHNOLOGY
C Short Overview Lembit Jürimägi.
8051 Programming in C rhussin.
Example 10 ASCII String to Binary Conversion
Lecture 5 from (Chapter 4, pages 73 to 96)
LCD and Keyboard Interfacing
Example 6 Hex Keypad Lecture L3.2.
LCD and Keyboard Interfacing
Introduction to Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Example 16 Circular Queue
Example 13 The Serial Peripheral Interface (SPI)
AVR Programming in C Chapter 7
AVR Programming in C Chapter 7
LCD and Keyboard Interfacing
LCD and Keyboard Sepehr Naimi
Homework Homework Continue Reading K&R Chapter 2 Questions?
Code Refresher Test #1 Topics:
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

Programming Microcontrollers in C Lecture L7.1

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

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

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;

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;

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

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

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

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); }

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;

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

#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

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

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

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

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

#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

// Keycode table const unsigned char keycodes[16]={ /* key row col */ 0x14,/* */ 0x88,/* */ 0x84,/* */ 0x82,/* */ 0x48,/* */ 0x44,/* */ 0x42,/* */ 0x28,/* */ 0x24,/* */ 0x22,/* */ 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.)

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

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

#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