ELEG3924 Microprocessor Ch.7 Programming In C



Similar documents
Keil C51 Cross Compiler

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Useful Number Systems

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Number Systems. Introduction / Number Systems

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Lecture 22: C Programming 4 Embedded Systems

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

8051 hardware summary

Lecture 11: Number Systems

Binary Representation

Technical Support Bulletin Nr.18 Modbus Tips

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Embedded C Programming

Nemo 96HD/HD+ MODBUS

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Dear Reader. The latest tutorial will be available from now on from the website of my consulting company Argenox Technologies:

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

CPEN Digital Logic Design Binary Systems

PFB366 Profibus-DP Gateway User Manual

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

2.2: Bitwise Logical Operations

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Interface and Simulation of a LCD Text Display

MODBUS APPLICATION PROTOCOL SPECIFICATION V1.1b3 CONTENTS

Flash Microcontroller. Architectural Overview. Features. Block Diagram. Figure 1. Block Diagram of the AT89C core

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

3. Convert a number from one number system to another

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

MODBUS APPLICATION PROTOCOL SPECIFICATION V1.1b CONTENTS

Definitions and Documents

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Informatica e Sistemi in Tempo Reale

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Application Note: AN00141 xcore-xa - Application Development

C programming for embedded microcontroller systems.

Flash Microcontroller. Memory Organization. Memory Organization

Numbering Systems. InThisAppendix...

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

AN617. Fixed Point Routines FIXED POINT ARITHMETIC INTRODUCTION. Thi d t t d ith F M k Design Consultant

Table 1 below is a complete list of MPTH commands with descriptions. Table 1 : MPTH Commands. Command Name Code Setting Value Description

Machine Architecture and Number Systems. Major Computer Components. Schematic Diagram of a Computer. The CPU. The Bus. Main Memory.

Binary, Hexadecimal, Octal, and BCD Numbers

Lecture N -1- PHYS Microcontrollers

The programming language C. sws1 1

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Caml Virtual Machine File & data formats Document version: 1.4

MACHINE ARCHITECTURE & LANGUAGE

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

RS-485 Protocol Manual

The string of digits in the binary number system represents the quantity

Positional Numbering System

The Answer to the 14 Most Frequently Asked Modbus Questions

University of Portsmouth Faculty of Technology Department of Electronic and Computer Engineering. Content

Modbus RTU Communications RX/WX and MRX/MWX

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Memory is implemented as an array of electronic switches

SRF08 Ultra sonic range finder Technical Specification

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Chapter 5 Real time clock by John Leung

Computer Science 281 Binary and Hexadecimal Review

WIZnet S2E (Serial-to-Ethernet) Device s Configuration Tool Programming Guide

8085 MICROPROCESSOR PROGRAMS

Java Interview Questions and Answers

Serial Communications Protocol. Data 2179 Revision 4. For Use With Cooper Power Systems Apparatus Controls: Form 6 Recloser Control

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

Comp151. Definitions & Declarations

Simple Image File Formats

Object Oriented Software Design

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

Introduction to MIPS Assembly Programming

Bluetooth HID Profile

8-Bit Flash Microcontroller for Smart Cards. AT89SCXXXXA Summary. Features. Description. Complete datasheet available under NDA

CSI 333 Lecture 1 Number Systems

8051 MICROCONTROLLER COURSE

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials

LABORATORY MANUAL EE0310 MICROPROCESSOR & MICROCONTROLLER LAB

Dolphin In-Circuit programming Updating Firmware in the field

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

=

Consult protocol, Nissan Technical egroup, Issue 6

Number and codes in digital systems

Chapter 4: Computer Codes

Binary Numbers. Binary Octal Hexadecimal

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

AN135 CACHE OPTIMIZATIONS FOR C8051F12X. Overview. Relevant Devices. Introduction. Key Points

Microprocessor or Microcontroller?

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

AN1754 APPLICATION NOTE

Compiler Construction

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Transcription:

Department of Electrical Engineering University of Arkansas ELEG3924 Microprocessor Ch.7 Programming In C Dr. Jingxian Wu wuj@uark.edu

OUTLINE 2 Data types and time delay I/O programming and Logic operations Data conversion programs Accessing code ROM space

DATA TYPE: C V.S. ASSEMBLY 3 C V.S. Assembly language Advantage of C It s easier to program in C compared to assembly. C code can be easily ported to other microcontroller, while assembly language can usually be used for 1 type of microcontroller There are lots of function libraries written in C Advantage of assembly language The hex file generated by assembly is usually smaller Code efficiency is higher (faster) C programming C has become the standard for embedded system programming Object oriented language (C++, Java, C#) are usually not as efficient as C C is flexible One of the main concerns in C programming for embedded system is to generate a hex file that is small in size We need to pay attention to the size of variables during programming

DATA TYPE 4 Data types unsigned char, signed char, unsigned int, signed int, sbit, bit and sfr unsigned char 8-bit. Most popular data type, matches most registers. Represent ASCII code or integers in the range of 0 ~ 255. Example: write a C program to send the ASCII code of 0, 1, 2, A, B, C to port 1 (demo) #include <reg51.h> // the definition of registers void main (void) unsigned char mynum[] = 012ABC ; unsigned char z; for (z = 0; z <=5; z++) P1 = mynum[z]; //port 1 is denoted as P1 Example: write a C program to toggle all bits of P2 continuously #include <reg51.h> void main(void) for ( ; ; ) P1 = 0x55; P1 = 0xAA; // infinite loop // hex number: 0x55

DATA TYPE 5 signed char an 8-bit number with the most significant bit representing sign (+ or -) Range: -128 ~ 127 (range of unsigned char: 0 ~ 255) Example: write a C program to send -3 to 3 to port 1 #include <reg51.h> void main(void) char mynum[] = -3,-2,-1,0,1,2,3; unsigned char z; for (z = 0; z<=6; z++) P1 = mynum[z]; // signed char unsigned int 16-bit (needs to registers), range: 0 ~ 65535 (FFFFH) E.g.: unsigned int a; signed int 16-bit, MSB represents sign. Range: -32768 ~ 32767 E.g.: int a; Since int requires twice as many memory space as char, use unsigned or signed int only when the number cannot be represented with unsigned or signed char.

DATA TYPE 6 bit Used to access single bit of bit-addressable RAM (20H 2FH) Example: bit mybit = 0; // the compiler will assign a RAM space to a //automatically sbit Single bit, used to access single-bit addressable registers Example: write a C program to toggle bit D0 of P1 50,000 times #include <reg51.h> sbit MYBIT = P1^0; void main(void) unsigned int z;; 50000 times, cannot use char for (z=0; z<=50000;z++) MYBIT = 0; MYBIT = 1; sfr Used to access special function registers example: sfr ACC = 0xE0; // the address of reg. A

DATA TYPE: TIME DELAY 7 Time delay Two methods to achieve time delay: Timer (Ch. 9) Loop When using loops in C, it s difficult to determine the exact time delay by means of calculation For the same C code, different compilers usually will generate different assembly codes Thus the same C codes with different compilers might generate different delays The only way to know the exact delay is through oscilloscope. #include <reg51.h> void main(void) unsigned int x; while(1) P1 = 0x55; for (x=0;x<40000;x++); //delay P1=0xAA; for (x=0;x<40000;x++); //delay

OUTLINE 8 Data types and time delay I/O programming and Logic operations Data conversion programs Accessing code ROM space

I/O PROGRAMMING 9 Byte size I/O Example: write a program to get a byte of data from P0. If it s less than 100, send it to P1; otherwise send it to P2 Self study: examples 7-9, 7-10

I/O PROGRAMMING 10 Bit-addressable I/O Programming Example: write a program to monitor bit P1.5. If it is high, send 55H to P0; otherwise send AAH to P2

I/O PROGRAMMING: SFR REGISTERS 11 Access SFR register Use sfr or sbit Example: read P0, send the result to P1; read in the value of P2.6 sfr rega = 0xE0; sfr P0 = 0x80; sfr P1 = 0x90; sbit inbit = 0xA6; bit mybit; void main(void) unsigned int z; P0 = 0xff; // bit address for P2^6 // compiler will automatically allocate memory to mybit // input mode inbit = 1; //input mode for (z = 0; z<50000; z++) rega = P0; P1 = rega; mybit = inbit; //read P2^6 to mybit

LOGIC 12 logic operators in C && (and), (or),! (not) Example: if (var1 < 3 && var2 == 1) if (!(var > 5)) Bit-wise logic operators Bit-by-bit logical operations: & (and), (or), ^ (xor), ~ (not) Shifting: << (shift to left) >> (shift to right): NOT cyclic shift! Example #include <reg51.h> void main(void) P0=0x35 & 0x0F; P1=0x04 0x68; P2=0x04 ^ 0x78; P3=~0x55; P0 = 0x35 << 3;

LOGIC: DATA SERIALIZATION 13 Data serialization 1. using serial port (Ch. 10) 2. using shifting operators Example: write a C program to bring in a byte of data serially one bit at a time via P1.0. LSB should come first #include <reg51.h> sbit P1b0 = P1^0; sbin ACCMSB = ACC^7; void main(void) unsigned char x; for (x=0; x<8; x++) ACCMSB = P1B0; ACC = ACC >> 1;

OUTLINE 14 Data types and time delay I/O programming and Logic operations Data conversion programs Accessing code ROM space

DATA CONVERSION: PACKED BCD TO ASCII 15 Packed BCD to ASCII Recall: packed BCD unpacked BCD ASCII Example: Write a C program to convert packed BCD 0x29 to ASCII, send the result to P1 and P2 #include <reg51.h> void main(void) unsigned char x, y, z; unsigned char packedbcd = 0x29; x = packedbcd & 0x0F; P1 = x 0x30; x = packedbcd & 0xF0; y = x>>4; P2 = y 0x30; // extract low nibble // unpacked BCD ASCII //extract high nibble // shift it to low nibble //unpacked BCD ASCII

DATA CONVERSION: ASCII TO BCD 16 ASCII to packed BCD ASCII unpacked BCD packed BCD Example: Write C program to convert 47 to packed BCD

DATA CONVERSION: BINARY TO ASCII 17 Binary (or decimal) to ASCII iteratively divided by 10 and keep the remainder Example: convert 0xFD = 1111 1101 = 253D to ASCII: 2, 5, 3 #include <reg51.h> void main(void) unsigned char quotient, remainder, binbyte; binbyte = 0xFD; //0xFD=253 quotient = binbyte/10; // quotient = 25 remainder = binbyte % 10; // remainder = 3 P0 = remainder 0x30; // P0 = 3 binbyte = quotient; // binbyte = 25; quotient = binbyte/10; // quotient = 2 remainder = binbyte%10; // remainder = 5 P1 = remainder 0x30; // P1 = 5 P2 = quotient 0x30; // P1 = 2

OUTLINE 18 Data types and time delay I/O programming and Logic operations Data conversion programs Accessing code ROM space

RAM AND ROM 19 In 8051, data can be stored in RAM (e.g. MOV) ROM (e.g. MOVC) External ROM (e.g. MOVX) RAM Compiler will automatically allocate RAM space for declared variables R0 R7: bank 0 (00H 07H) Variables (including array): address 08 and beyond stack: address right after variables.

RAM AND ROM 20 Accessing ROM in C To require the compiler store data in ROM, use the code keyword Without code keyword, all the data will be stored in RAM The compiler will automatically allocate ROM space for the variables. Example #include <reg51.h> void main(void) code unsigned char mynum[] = ABCDEF ; unsigned char z; for (z = 0; z<6; z++) P1 = mynum[z];