110 likes | 343 Views
Example 10 ASCII String to Binary Conversion. Lecture L4.3. ASCII number string to binary conversion. 34671 = 3 x 104 + 4 x 103 + 6 x 102 + 7 x 10 + 1 = 1 + 10(7 + 10(6 + 10(4 + 10(3)))). Algorithms to convert a double number to an ASCII string. char* ptr; // pointer to keypad buffer
E N D
Example 10 ASCII String to Binary Conversion Lecture L4.3
ASCII number string to binary conversion 34671 = 3 x 104 + 4 x 103 + 6 x 102 + 7 x 10 + 1 = 1 + 10(7 + 10(6 + 10(4 + 10(3))))
char* ptr; // pointer to keypad buffer char kbuf[12]; // keypad buffer ptr = kbuf; // point to keypad buffer
Example 10 // Example 10: Calculator with ASCII string to number conversion #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { long op1, op2; // 32-bit operands char* ptr; // pointer to keypad buffer char* plus; // string pointers char* equals; char* blanks; char kbuf[12]; // keypad buffer ptr = kbuf; plus = "+ "; equals = "= "; blanks = " "; char c, a; int i;
Example 10 (cont.) // Example 10: (cont.) PLL_init(); // set system clock frequency to 24 MHz lcd_init(); // enable lcd keypad_enable(); // enable keypad set_lcd_addr(0x00); // display on 1st line i = 0; // kbuf index = 0 while(1) { c = getkey(); // read keypad a = hex2asc(c); // convert to ascii kbuf[i] = a; // and store in kbuf if(c < 10){ // if 0 - 9 data8(a); // display on LCD wait_keyup(); // wait to release key i++; // inc index } else { switch(c){
Example 10 (cont.) // Example 10: (cont.) case 0xE: // if enter (*) key op1 = number(ptr); // convert to binary set_lcd_addr(0x42); // display on 2nd line write_long_lcd(op1); set_lcd_addr(0x00); // clear 1st line type_lcd(blanks); wait_keyup(); // wait to release key i = 0; // reset kbuf index set_lcd_addr(0x00); // display on 1st line break;
Example 10 (cont.) // Example 10: (cont.) case 0xA: // if key A set_lcd_addr(0x42); // display op1 on 2nd line write_long_lcd(op1); op2 = number(ptr); // convert to binary set_lcd_addr(0x14); // display on 3rd line type_lcd(plus); write_long_lcd(op2); op1 = op1 + op2; // compute sum set_lcd_addr(0x54); // display on 4th line type_lcd(equals); write_long_lcd(op1); set_lcd_addr(0x00); // clear 1st line type_lcd(blanks); wait_keyup(); // wait to release key i = 0; // reset kbuf index set_lcd_addr(0x00); // display on 1st line break;
Example 10 (cont.) // Example 10: (cont.) case 0xF: // if clear (#) key clear_lcd(); // clear lcd display wait_keyup(); // wait to release key i = 0; // reset kbuf index break; default: break; } } } }