0% found this document useful (0 votes)
13 views4 pages

ES M2-Note2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

DATA CONVERSION PROGRAMS

In microcontrollers, certain information is stored in the form of packed BCD. A packed BCD
contains two decimal digits in a byte. Eg: 34, 76 etc. An unpacked BCD contains one decimal digit
in a byte. Eg: 05, 07 etc. To display these information, we have to convert it into ASCII. ASCII
codes of the digits 0 to 9 are 30 to 39 respectively. It means that to display the digit 7, we have to
first convert it into its ASCII code 37 (ie. 30 is added). Similarly to get the numerical value of
ASCII code, we have to subtract 30 from it.

Packed BCD to ASCII conversion:


To convert packed BCD to ASCII, follow the steps:
1. Convert it into unpacked BCD
2. Add 30 to each digit.
Example: Packed BCD 0x29 (00101001)
Unpacked BCD 0x02 and 0x09 (00000010 and 00001001)
Adding 0x30 0x32 and 0x39 (00110010 and 00111001)

ASCII to packed BCD conversion:


To convert ASCII to packed BCD, follow the steps:
1. Convert it to unpacked BCD
2. Combine the digits
Example: Let the digits are ‘4’ and ‘7’ which are represented as 0x34 and 0x37. We have to combine
it to a single byte as 47.
Converting 0x34 to unpacked BCD gives 0x04
Converting 0x37 to unpacked BCD gives 0x07
Combining it to packed BCD gives 0x47

Programming Example:
Binary (Hex) to Decimal conversion:
Hexadecimal format is a convenient method to represent binary data. The binary data 00–FFH
converted to decimal will give us 000 to 255. One method of converting it to decimal is dividing it
by 10 and keeping the remainder.
Example: Covert 0xFD to decimal. Its decimal value is 253.
DATA SERIALIZATION
Sometimes we need to send a byte of data one bit at a time through a single pin of a microcontroller.
There are two ways to send data serially:
1. Using serial port
2. Data serialization.
Using serial port method, the programmer has very limited control over the sequence of data
transfer. In data serialization, the data bits can be send under the program control. So the
programmer can determine the timing and bit sequence of the data.

Programming Examples:

If we want to send the MSB first, then the following changes can be made in the for loop:
MEMORY ALLOCATION
We can store predefined fixed data in any of the following three spaces in AVR microcontroller:
• SRAM
• EEPROM
• Flash (code ROM)
The size of these spaces depends on the microcontroller. To allocate space for data in EEPROM and
Flash, different C compilers use different directives or built-in functions. Following is one of the
method to allocate space in EEPROM and Flash.

You might also like