Displaying a Numerical Value on HyperTerminal (or equ.)



Similar documents
C Programming Structure of a C18 Program

Programming the PIC18/ XC8 Using C- Coding Dr. Farahmand

Embedded C Programming

Using Xbee in Serial Communication

USART and Asynchronous Communication

Parallel IO. Serial IO. Parallel vs. Serial IO. simplex vs half-duplex vs full-duplex. Wires: Full Duplex. Wires: Simplex, Half-duplex.

CONCEPT1 RS232 COMMUNICATION

Getting Started with PIC24F/PIC24H Programming and Interfacing in C

PFB366 Profibus-DP Gateway User Manual

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

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

Single channel data transceiver module WIZ2-434

Serial Communications

The Answer to the 14 Most Frequently Asked Modbus Questions

[WIR-1186] 865MHz-869MHz Wireless Module(version 3.0) (3.3V)

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

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Using HyperTerminal with Agilent General Purpose Instruments

Technical Support Bulletin Nr.18 Modbus Tips

Display Message on Notice Board using GSM

8051 Serial Port. Crystal TXD. I/O Device RXD. Embedded Systems Peripherals

ELEG3924 Microprocessor Ch.7 Programming In C

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Understanding PIC WEB boards and how to use Microchip's TCP-IP Stack. Rev.A, April Copyright(c) 2008, OLIMEX Ltd, All rights reserved

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

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

Useful Number Systems

Embedded Systems Design Course Applying the mbed microcontroller

2011, The McGraw-Hill Companies, Inc. Chapter 3

HOMEWORK # 2 SOLUTIO

Serial Communications

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

Nemo 96HD/HD+ MODBUS

LTM-1338B. Plus Communications Manual

1. The demonstration code is sending garbage to my terminal program. What are the recommended settings?

The programming language C. sws1 1

Number Representation

CENTRONICS interface and Parallel Printer Port LPT

Lecture 22: C Programming 4 Embedded Systems

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

Tutorial for MPLAB Starter Kit for PIC18F

Lesson 10: Video-Out Interface

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

A RF18 Remote control receiver MODULE

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 1 Introducing Hardware

Virtual Integrated Design Getting started with RS232 Hex Com Tool v6.0

Application Note: AN00121 Using XMOS TCP/IP Library for UDP-based Networking

Keil C51 Cross Compiler

Design and Implementation of Home Monitoring System Using RF Technology

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

Lab 6 Introduction to Serial and Wireless Communication

Solution for Homework 2

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

RS232 Board datasheet

EXERCISE 3: String Variables and ASCII Code

Exam 1 Review Questions

RN-131-PICTAIL & RN-171-PICTAIL Evaluation Boards

µtasker Document FTP Client

Caml Virtual Machine File & data formats Document version: 1.4

SR450 RECEIVER OPERATING INSTRUCTIONS

Field Software Updates Using TPMS LF An example using the Low Frequency radio (LFR) for wireless software updating

Stacks. Linear data structures

2015/02/07 05:41 1/23 WIZ550WEB Users' Guide

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

8051 MICROCONTROLLER COURSE

TURBO PROGRAMMER USB, MMC, SIM DEVELOPMENT KIT

Microcontroller Programming Beginning with Arduino. Charlie Mooney

MCB3101 (Class I) WiRobot Serial Bluetooth Wireless Module User Manual

How To Send A Message From A Computer To A Computer (Iwea) On A Microsoft Macbook 2.5 (Isoa) To A Microsatellite 2.4 (Ios) On An Unix (Ise

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

SYMETRIX SOLUTIONS: TECH TIP August 2015

Data Transfer between Serial Link and TCP/IP Link Using ez80f91 MCU

L Firmware And FPGA Configuration Upgrade

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

WA Manager Alarming System Management Software Windows 98, NT, XP, 2000 User Guide

BE635 User Manual. Rev. V Bolymin, Inc. All Rights Reserved.

ontroller LSI with Built-in High- Performance Graphic Functions for Automotive Applications

MeshBee Open Source ZigBee RF Module CookBook

RS-485 Protocol Manual

Consult protocol, Nissan Technical egroup, Issue 6

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

LDG Electronics External Meter Serial Communications Protocol Specification

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Keep it Simple Timing

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

Chapter 13. PIC Family Microcontroller

SUDT AccessPort TM Advanced Terminal / Monitor / Debugger Version 1.37 User Manual

Embedded Systems Design Course Applying the mbed microcontroller

Objectives. Basics of Serial Communication. Simplex vs Duplex. CMPE328 Microprocessors (Spring ) Serial Interfacing. By Dr.

In-System Programmer USER MANUAL RN-ISP-UM RN-WIFLYCR-UM

Lecture N -1- PHYS Microcontrollers

AN601 I2C 2.8 Communication Protocol. SM130 SM130 - Mini APPLICATION NOTE

Illustration 1: Diagram of program function and data flow

OPERATOR INTERFACE PRODUCTS APPLICATION NOTE. Xycom 4800/2000 OIL (Operator Interface Language)- Series Terminals: Backup and Restore with ProComm

Harris IRT Enterprises Digital Resistance Tester Model XR

Part Number Description Packages available

Bluetooth for device discovery. Networking Guide

B.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011

Microcontroller-based Wireless Heart Rate Telemonitor for Home Care

Transcription:

(Here, only an integer value is discussed) Objective Sometimes, the result of a calculations or the value of a variable needs to be displayed on a terminal. Points to Consider A numerical value is stored in MCU memory in binary For human interaction the value needs to be displayed in decimal. This requires a conversion from binary to decimal. The terminal requires that the decimal character to be displayed be sent as ASCII characters. This requires that the decimal characters (digits) be converted into equivalent ASCII If the value is negative, the ASCII character for negative sign ( - ) needs to be placed as the leading character. If the transmission of the character string is going to be managed by a function capable of transmitting ASCII character strings of varying length, a Null marker (0x00) needs to be placed at the end of the converted ASCII strings. S Gupta mcjournal.com Page 1

Conversion Binary to (ASCII) Decimal Example 1 int num = 12345; // same as num = 0x3039 or num = 0b0011 0000 0011 1001 Let us assume that it is stored in PIC18F2520 data memory (starting) at address 0x100. Since it is an integer, It will be stored in two (byte) locations PIC18F uses Little Endean format low byte at lower address and high byte at higher address Thus, Low byte 0b00111001 (0x39) will be stored at 0x100, and The High byte 0b00110000 (0x30) will be stored at the next (higher) address 0x101 In order to display it on HyperTerminal in decimal, the above mentioned conversions need to be performed. After the appropriate conversion, the value that should be sent to HyperTerminal will be: 0x31 0x32 0x33 0x34 0x35 Let us assume that after conversion the required string (of ASCII characters) is stored in data memory starting at address 0x200. Address Data Byte ASCII character 0x200 0x31 1 0x201 0x32 2 0x202 0x33 3 0x203 0x34 4 0x204 0x35 5 0x205.. 0x206.. It should be noted that the converted character string does not follow the little Endean format, as the first value that is sent to terminal for display is the most significant character (digit). Thus the converted string needs to be organized in the order, it is going to be displayed on terminal (the leading character first). S Gupta mcjournal.com Page 2

Example 2 int num2 = -12345; Since it is a negative value it will be stored following signed binary convention (2 s complement) // same as num2 = 0xCFC7 or num2 = 0b1100 1111 1100 0111 Let us assume that it is stored in PIC18F2520 data memory (starting) at address 0x110. Since it is an integer, It will be stored in two (byte) locations PIC18F uses Little Endean format low byte at lower address and high byte at higher address Thus, Low byte 0b1100 0111 (0xC7) will be stored at 0x110, and The High byte 0b1100 1111 (0xCF) will be stored at the next (higher) address 0x111 In order to display it on HyperTerminal in decimal, the above mentioned conversions need to be performed, complete with the negative sign. After the conversion, the value that should be sent to HyperTerminal will be: 0x2D 0x31 0x32 0x33 0x34 0x35 Here 0x2D represents the ASCII for - (negative sign) The following shows the converted value (character string) stored in data memory starting at address 0x210. Address Data Byte ASCII character 0x210 0x2D - 0x211 0x31 1 0x212 0x32 2 0x213 0x33 3 0x214 0x34 4 0x215 0x35 5 0x216.. S Gupta mcjournal.com Page 3

Adding Null marker (character) at the end of character strings. Once the starting address of a character string and the length of characters in the string are known, a Null character can be placed after the last character. Fortunately, C18 provides a library function itoa that takes care of all conversion requirements, including the negative sign, if so required. The Null character is automatically placed after the last digit position (character). The following information is taken from Microchip C18 Library Guide itoa Function: Include: Convert a 16-bit signed integer to a string. stdlib.h Prototype: char * itoa(int value, char * string ); Arguments: value A 16-bit signed integer. String Pointer to ASCII string that will hold the result. String must be long enough to hold the ASCII representation, including the sign character for negative values and a trailing null character. Remarks: This function converts the 16-bit signed integer in the argument value to a ASCII string representation. This function is an MPLAB C18 extension of the ANSI required libraries. Return Value: File Name: Pointer to the result string. itoa.asm Note: the itoa function works with signed character values as well. Requirements: Inlcude stdlib (#include <stdlib.h> (Copyright Microchip Technology) Create a RAM character array of sufficient size to hold converted string, including sign and Null character Call itoa () function and pass the name of the integer variable and pointer to string that will hold the converted ASCII characters. S Gupta mcjournal.com Page 4

Example 3 (Example 1 revisited) int num1 = 12345; Here is the final output after the conversion Address Data Byte ASCII character 0x200 0x31 1 0x201 0x32 2 0x202 0x33 3 0x203 0x34 4 0x204 0x35 5 0x205 0x00 Null character 0x206.. Since the numerical value is positive, there is no + character placed as the start, as is conventionally done. The total number of characters required is now 6. Example 4 (Example 2 revisited) int num1 = -12345; Here is the final output after the conversion Address Data Byte ASCII character 0x200 0x2D - 0x201 0x31 1 0x202 0x32 2 0x203 0x33 3 0x204 0x34 4 0x205 0x35 5 0x206 0x00 Null character Since the numerical value is negative, a - character placed as the start. The total number of characters required is now 7. S Gupta mcjournal.com Page 5

Procedure Get the numerical value Type cast it to an integer, if so required Convert the numerical value to equivalent text string (consisting of ASCII characters) use itoa() function Use appropriate function to transmit the message use tx_ram_msg () or equivalent function Example 5 The following shows a code fragment that converts integer and char (byte) values and transmits those. // SG // March 28, 2010 // itoa_1.c // converting integers and a byte values to ASCII #include <P18F2520.h> #include <stdlib.h> // Prototypes void set_uart(void); void tx_char(char); char rx_char(void); void tx_cr_lf(void); void tx_ram_msg(char *); void tx_rom_msg(rom char *); // ==================== Configuration bits ================ #pragma config WDT = OFF #pragma config OSC = INTIO67 #pragma config PWRT = ON #pragma config LVP = OFF #pragma config MCLRE = ON #pragma config BOREN = ON // ======================================================== #define BRG_VAL 12 // For a baud rate of 19200 at 32 MHz char msg1[7]; char msg2[7]; // create ram buffers S Gupta mcjournal.com Page 6

// -------------------- Start of main program ----------------------------- void main(void) int num1 = 12345; // assign some values int num2 = -12345; char cnum1 = 123; char cnum2 = -123; OSCCON = 0b01100000; while (OSCCONbits.IOFS!= 1); itoa(num1, msg1); tx_ram_msg(msg1); itoa(num2, msg2); tx_ram_msg(msg2); itoa(cnum1, msg1); tx_ram_msg(msg1); itoa(cnum2, msg2); tx_ram_msg(msg2); // wait for frequency to stabilize // convert integer value and store equivalent string // display value of num1 on terminal // display value of num1 on terminal // convert integer value and store equivalent string // send cnum1 value // send cnum2 value while(1); // ***************************************************************************** // set_uart() // Set up Uart void set_uart(void) TRISCbits.TRISC7 = 1; // Set Rx pin as an input TRISCbits.TRISC6 = 0; // Set Tx pin as an output TXSTA = 0b10100100; // initialize USART 8-bit, Async, High Speed SPBRG = BRG_VAL; // set the baud rate RCSTA = 0b10010000; - S Gupta mcjournal.com Page 7

S Gupta mcjournal.com Page 8

// tx_ram_msg() // Send a message contained in RAM void tx_ram_msg(char * cp) while(*cp!= 0x00) tx_char(*cp); // read char and tranmit cp++; // bump up the pointer - // tx_rom_msg() // Send a message contained in FLASH void tx_rom_msg(rom char * cp) while(*cp!= 0x00) tx_char(*cp); // read char and tranmit cp++; // bump up the pointer - // tx_char() void tx_char(char c) // Write a byte to the serial port. while (PIR1bits.TXIF!= 1); // wait for the flag to be set TXREG = c; - S Gupta mcjournal.com Page 9

// rx_char() char rx_char(void) // Get a character from the serial port. char c; while (PIR1bits.RCIF!= 1); // wait for receive flag c = RCREG; // read the char return (c); - // tx_cr_lf() void tx_cr_lf(void) // Send a CR and LF tx_char(0x0a); // send LF tx_char(0x0d); // send CR S Gupta mcjournal.com Page 10

Example 6 Refer to the code given in Example 5. Modify the main program so that current values of variables num1 and num2 (as initialized in the program are displayed as follows: Screen display The value of num1: 12345 The value of num2: -12345 Program Terminated Solution The following additional constant strings (character arrays) need to be created to display the messages. These strings can be declared globally (outside of main). rom near *rom_msg1 [] = The value of num1: ; rom near *rom_msg2 [] = The value of num2: ; rom near *rom_msg3 [] = Program Terminated ; The new main function will be Void main (void) OSCCON = 0b01100000; while (OSCCONbits.IOFS!= 1); tx_rom_msg(rom_msg1); itoa(num1, msg1); tx_ram_msg(msg1); tx_cr_lf(); tx_rom_msg(rom_msg2); itoa(num2, msg2); tx_ram_msg(msg2); tx_cr_lf(); tx_rom_msg(rom_msg3); // wait for frequency to stabilize // show The value of num1: on screen // cursor is poised at the end of the message // convert integer value and store equivalent string // display value of num1 on screen // insert <CR and LF> // show The value of num2: on screen // display value of num1 on screen // insert <CR and LF> // show Program Terminated on screen while(1); S Gupta mcjournal.com Page 11

Example 7 Refer to Examples 5 and 6. A global variable voltage holds a value of 3550 representing a voltage reading of 3.550 volts (or 3550 mv). Display the reading on the screen with an appropriate information. Solution A possible screen output for this reading may be as follows: (Screen display) The voltage reading in mv is: 3550 The following additional constant string need to be created to display the leading message. Declaring this string globally rom near *rom_msg4 [] = The voltage reading in mv is: ; The new main function will be modified as follows Void main (void).. tx_rom_msg(rom_msg4); itoa(voltage, msg1); tx_ram_msg(msg1);.. while(1); // show The voltage reading in mv is: on screen // display value of voltage on screen S Gupta mcjournal.com Page 12

Example 8 Refer to Example 7. Modify the code so to the information is displayed as shown below: (Screen display) The voltage reading is: 3550 mv Solution The line of information can be divided in three parts: First part will display the leading message The voltage reading is: The second part will be to display the numerical value 3550 The last part will display the trailing character string mv. It should be noted that a leading space character is present before mv, so that there is a space after the numerical value is displayed information on screen. Declaring these strings rom near *rom_msg5 [] = The voltage reading mv is: ; rom near *rom_msg6 [] = mv ; // there is a trailing space character // there is a leading space character The new main function will be modified as follows Void main (void).. tx_rom_msg(rom_msg5); itoa(voltage, msg1); tx_ram_msg(msg1); // show The voltage reading in mv is: on screen // display value of voltage on screen tx_rom_msg(rom_msg6); while(1); // show mv on screen S Gupta mcjournal.com Page 13