Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 11:17:36 23:17


Login with username, password and session length


Pages: [1]
Print
Author Topic: req: CODE FOR BINAY TO DECIMEL  (Read 5943 times)
0 Members and 1 Guest are viewing this topic.
fire
Junior Member
**
Offline Offline

Posts: 56

Thank You
-Given: 29
-Receive: 23


« on: January 28, 2008, 11:05:55 11:05 »


 I AM DOING A TEMPERATURE INDICATOR CIRCUIT USING PIC16F877A AND LM35; HOW CAN I CONVERT 10 BIT DATA 
 (AFTER CONVERSION ) TO DECIMEL FOR DISPLAYING SAME ON LCD DISPLAY.
 
 PLZ  GIVE THE CODE ALSO(IN MICRO C OR ASSEMBLY)...
Logged
setoy
Newbie
*
Offline Offline

Posts: 20

Thank You
-Given: 10
-Receive: 11


« Reply #1 on: January 28, 2008, 12:25:27 12:25 »

use sprintf(..)  function
Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #2 on: January 28, 2008, 02:22:50 14:22 »

This is what I made back in 2004 with the proton compiler. I've only translated it to English. The LCD You have to set up for Yourself!

'****************************************************************
'*  Name     : 10-bitA/Dconv.BAS                                 *
'*  Author   : robban                                      *
'*  Notice   : Compiled with Proton+ compiler              *
'*             : All Rights Reserved                               *
'*  Date     : 2008-01-28                                        *
'*  Version : 1.3                                               *
'*  Notes   :                                                   *
'*            :                                                   *
'****************************************************************
' Program to show temperature with 10-bit resolution.
'
'Let channel 0 (RA0) be analog input.
device 16f877
xtal = 20


adval  var word      'Create "adval" for temporary result

symbol B7 = portb.7
define ADC_SAMPLEUS   1000

TRISA = %11111111     ' PORTA input
ADCON1 = %10000010    ' PORT A being analog and RIGHT-justify result
ADCON0 = %11000001   ' Configure and turn on A/D-module
Pause 500          ' Let it heat up


loop:    ADCON0.2 = 1   'Start conversion

notdone:   pause 5
if ADCON0.2 = 1 Then notdone   'Wait for bit 2 in ADCON0 going low, done!

adval.highbyte = ADRESH   'Move HIGHbyte of result to adval
adval.lowbyte = ADRESL   'Move LOWbyte of result to adval

serout B7,84,["Temp: ", DEC (adval/10)," deg C",10,13]   'Show decimal result  on RS232

Pause 100          'Wait 0.1 Sec.

Goto loop          'Loop forever

End


Posted on: January 28, 2008, 02:56:57 14:56 - Automerged

//This is a AD conversion sample for the CCS compiler and a ds1621 conv. chip displayed on a standard LCD

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#include <lcd.c>          //Change this for Yr. need
#include <ds1621.c>    // Change this for Yr. need

int current_temp, max_temp, min_temp;


void reset_temp()  {

   current_temp = read_temp();
   min_temp=current_temp;
   max_temp=current_temp;
}


void main() {

   init_temp();
   lcd_init();
   delay_ms(6);

   reset_temp();

   while(TRUE)
   {
      current_temp = read_temp();

      if(input(RESET_BUTTON)==0)
         reset_temp();
      else if(current_temp>max_temp)
         max_temp=current_temp;
      else if(current_temp<min_temp)
         min_temp=current_temp;

      printf(lcd_putc,"\fCurrent Temp: %U F\nMin: %U Max: %U",                        current_temp,min_temp,max_temp);
      delay_ms(500);
   }
}
Logged

Code Warrior
Kombinator
Junior Member
**
Offline Offline

Posts: 82

Thank You
-Given: 38
-Receive: 32



« Reply #3 on: January 28, 2008, 10:32:07 22:32 »

      resulthi=ADRESH;    //load result into temporary variable
      resultlo=ADRESL;
 
      temp=resulthi<<8;
      temp=temp|resultlo;       //combine the resultant registers and store it in variable temp




void digit_to_ascii(long input, char *str, char numdigits)
{
  char digit;

  for (digit=numdigits; digit > 0; digit--) {
    str[digit-1] = (input % 10) + '0';
    input = input / 10;
  }
  str[numdigits] = 0;    // null-terminate the string
}
Logged
vovchik02
Junior Member
**
Offline Offline

Posts: 62

Thank You
-Given: 43
-Receive: 13



« Reply #4 on: February 04, 2008, 10:08:02 10:08 »

Microchip
h__p://ww1.microchip.com/downloads/en/AppNotes/00526.zip
bin 2 BCD 8 and 16bit convert. ASM
Logged
marcegoncba
Newbie
*
Offline Offline

Posts: 18

Thank You
-Given: 83
-Receive: 0


« Reply #5 on: February 04, 2008, 01:49:32 13:49 »

For CCS C compiler (sorry, the comments are in spanish)
Code:
//Rutina para convertir la variable de intensidad de binario a BCD
//From: http://www.telesys.ru/wwwboards/mcontrol/20/messages/261.shtml
//A simple algorithm to convert binary to BCD is:
//1. Add 3 to every BCD nibble that's >=5
//2. Shift left
//Repeat until binary portion is all 0000

//convierte el dato binario en decimal
void convert2BCD(void){
int i;
bin_2_BCD._byte.BCDbyteA = 0;
bin_2_BCD._byte.BCDbyteB = 0;
bin_2_BCD._byte.BCDbyteC = 0;
for(i=0; i<16; i++){
if (bin_2_BCD._nibble._nibbleA > 4) bin_2_BCD._nibble._nibbleA += 3;
if (bin_2_BCD._nibble._nibbleB > 4) bin_2_BCD._nibble._nibbleB += 3;
if (bin_2_BCD._nibble._nibbleC > 4) bin_2_BCD._nibble._nibbleC += 3;
if (bin_2_BCD._nibble._nibbleD > 4) bin_2_BCD._nibble._nibbleD += 3;
if (bin_2_BCD._nibble._nibbleE > 4) bin_2_BCD._nibble._nibbleE += 3;
shift_left(&bin_2_BCD,5,0);
}
}

The structures:
Code:
//Estructuras para el convertidor
//de binario a BCD
struct _NIBBLES{
unsigned int _nibbleLongA:4;
unsigned int _nibbleLongB:4;
unsigned int _nibbleLongC:4;
unsigned int _nibbleLongD:4;
unsigned int _nibbleA:4;
unsigned int _nibbleB:4;
unsigned int _nibbleC:4;
unsigned int _nibbleD:4;
unsigned int _nibbleE:4;
};

struct BCD_BYTES{
unsigned int _binLbyte;
unsigned int _binHbyte;
unsigned int BCDbyteA;
unsigned int BCDbyteB;
unsigned int BCDbyteC;
};

union bin2BCD{
unsigned long _bin16;
struct _NIBBLES _nibble;
struct BCD_BYTES _byte;
};

//variable global de 5 bytes para la conversión
//binaria a BCD (hasta 65535) es una union que
//permite direccionar cada uno de los bytes y
//los nibbles para poder efectuar el algoritmo
//descripto
union bin2BCD bin_2_BCD;

and the use of this function
Code:
bin_2_BCD._bin16 = 65535;
convert2BCD();
//after the conversion
//bin_2_BCD._nibble._nibbleLongE = 6;
//bin_2_BCD._nibble._nibbleLongD = 5;
//bin_2_BCD._nibble._nibbleLongC = 5;
//bin_2_BCD._nibble._nibbleLongB = 3;
//bin_2_BCD._nibble._nibbleLongA = 5;
Logged
setoy
Newbie
*
Offline Offline

Posts: 20

Thank You
-Given: 10
-Receive: 11


« Reply #6 on: February 22, 2008, 08:39:47 08:39 »

I don't use microC, but the follow code must be working:

Code:

int a;
char d[4];

sprintf(d,"%d",a); //in a d you will get formatted ascii to put pthem on LCD later


or

Code:

int a;
char d[4];

d[0]=a%10; // LSB
a/=10;
d[1]=a%10;
a/=10;
...........


undestand?
Logged
TigerX
Newbie
*
Offline Offline

Posts: 12

Thank You
-Given: 4
-Receive: 1


« Reply #7 on: February 22, 2008, 09:05:37 09:05 »

Excuse me setoy...

I am new in C. Because can you explain me sprintf and formatting data..

I mean; What is the function of "%" in  "d[0]=a%10;" and what do you get at the

"int a;
char d[4];

sprintf(d,"%d",a); //in a d you will get formatted ascii to put pthem on LCD later"

This may be simple but I am new. Please excuse me..!
Logged
setoy
Newbie
*
Offline Offline

Posts: 20

Thank You
-Given: 10
-Receive: 11


« Reply #8 on: February 22, 2008, 10:05:01 10:05 »

operator "/" is  division. So, if divide  int type, then 10/3=3 (not 3.33333!). operator "%" is modulus, this mean 10%3=1, remainder of division (10%2=0;10%4=2...etc.). The idea is to divide one 16-bit number to digits and after that need convert them to ascii and send to LCD.

Almost same doing and sprintf(...) function, but in more implementations this is more expensive for RAM/execution time than other ways.
Logged
zuisti
Senior Member
****
Offline Offline

Posts: 409

Thank You
-Given: 242
-Receive: 780


« Reply #9 on: February 22, 2008, 03:03:12 15:03 »

I AM DOING A TEMPERATURE INDICATOR CIRCUIT USING PIC16F877A AND LM35; HOW CAN I CONVERT 10 BIT DATA 
 (AFTER CONVERSION ) TO DECIMEL FOR DISPLAYING SAME ON LCD DISPLAY.
  PLZ  GIVE THE CODE ALSO(IN MICRO C OR ASSEMBLY)...

Hi 'fire',

After conversion you have readed the AD result in an unsigned int (2 bytes) variable.
Its high byte contains the two highest bits only, the low byte contains the 8 bits remained.

You can it display to an LCD (for example) with the standard MikroC functions:

 unsigned int ADresult;
 char adpuf[6];
 .....
 
  // LCD_Init(&PORTD);                   // initialize LCD  (4-bit interface connection)
  //....inic, make the conversion and read the result to an uint var (as usual)
  // eq ADresult  = ADC_read(2);   // get ADC value from 2nd channel

  sprinti(adpuf, "%u", ADresult);    //fill the adpuf with the result in decimal form
  //sprinti is an useful library routine (small code lenght), but it can handle char and int vars only

  LCD_Out(2,1,adpuf);                 // print the string on LCD, 2nd row, 1st column

 Or ...look at to your MikroC installation:
 MikroC_7003\Examples\EasyPic4\P16F877A\ADC_on_LCD\

Hope this helps
zuisti
Logged
fire
Junior Member
**
Offline Offline

Posts: 56

Thank You
-Given: 29
-Receive: 23


« Reply #10 on: February 25, 2008, 07:35:25 07:35 »

  I am very much thankful to all those people who cleared my doubts; especially zuisti...
Logged
jasonix
Newbie
*
Offline Offline

Posts: 12

Thank You
-Given: 48
-Receive: 3


« Reply #11 on: February 29, 2008, 03:02:35 15:02 »

HI, fire
there is a web forum than help you  http://www.piclist.com/techref/microchip/math/radix/index.htm
here you can find you want

greetings from Colombia.

Posted on: February 29, 2008, 03:58:49 15:58 - Automerged

fire, here is the code you want in assembler

h__p://www.piclist.com/techref/microchip/math/radix/index.htm

  ;======= Bin2Dec10G.asm ============= Sep 04 =========
  ;       Test of 10b binary to 4 digit conversion as
  ;       based on division by powers of 10.
  ;
  ;       E. A., Grens
  ;
  ;       For most PICs - here for 16F628 with 4 MHz
  ;       internal oscillator.
  ;
  ;       Average processor cycles for execution of
  ;       subroutine over the input range 0x000 to 0x3FF
  ;       is 88.  No variables required except the
  ;       input blh, blo and the out BCD digits.
  ;       Subroutine length 38.
  ;======================================================

        list p=16f628
        radix   dec
        #include
        __config 0x3D30

 
  ;-------Set Up RAM Variables---------------------------
  blo   equ     0x20            ;lsbyte of 10b binary
  bhi   equ     0x21            ;msb
  d4    equ     0x23            ;msdigit
  d3    equ     0x24
  d2    equ     0x25
  d1    equ     0x26            ;lsd
  ;-------Program Code--------------------------------
                                         
        org     0x00            ;Effective Reset Vector
  ;
        nop
        goto    Start           ;Jump by interrupt
  ;
  Start
        movlw   0x07
        movwf   CMCON           ;Digital I/O
        movlw   0x03            ;Test input
        movwf   bhi
        movlw   0xFF
        movwf   blo
        movlw   0x04
        call    Bin2decg
  Hold
        goto    Hold            ;Finished

  Bin2decg                      ;10b binaey to BCD
        clrf    d1
        clrf    d2
        clrf    d3
        clrf    d4
        clrc
        rrf     bhi,f           ;N = 4*Q + R
        rrf     blo,f           ;blo = Q
        rrf     d1,f            ;d1 = R as temp, R25
        goto    B2d2            ;repeat
  B2d3
        addwf   blo,f           ;get remainder
                                  ;    after /100, C = 0
        rlf     d1,f            ;*4 and add R back in
        rlf     blo,f
        rlf     d1,f
        rlf     blo,f           ;4*blo + R = N -
                                  ;    1000*d4 - 100*d3
                movlw   10
  B2d4
        subwf   blo,f           ;blo = N - 1000*d4 -
                                  ;     100*d3 - 10*d2
        skpc
        goto    B2d5            ;blo10
        goto    B2d4
  B2d5
        addwf   blo,f           ;put last 10 back, C=0
        movf    blo,w           
        movwf   d1
        return
   
        end

     
**********************

12-bit code

Posted by Peter Hemsley on September 18, 2004 at 17 40 56
Message   ; Bonus 12-Bit version

  ; Leaner and Meaner, 36 instructions
  ; Ideal for displaying A/D values
  Bin2DecFast
        movf    NUMHI,w
        iorlw   0xF0            ;w=H2-16
        movwf   D1              ;D1=H2-16
        addwf   D1,f            ;D1=H2*2-32
        addwf   D1,f            ;D1=H2*3-48
        movwf   D2              ;D2=H2-16
        addlw   -D'5'           ;w=H2-21
        addwf   D2,f            ;D2=H2*2-37 Done!
        addlw   D'41'           ;w=H2+20
        movwf   D0              ;D0=H2+20

        swapf   NUMLO,w
        iorlw   0xF0            ;w=H1-16
        addwf   D1,f            ;D1=H2*3+H1-64
        addwf   D0,f            ;D0=H2+H1+4, C=1
        rlf     D0,f            ;D0=(H2+H1)*2+9, C=0
        comf    D0,f            ;D0=-(H2+H1)*2-10
        rlf     D0,f            ;D0=-(H2+H1)*4-20

        movf    NUMLO,w
        andlw   0x0F            ;w=H0
        addwf   D0,f            ;D0=H0-(H2+H1)*4-20 Done!
        rlf     D1,f            ;C=0, D1=H2*6+H1*2-128 Done!

        movlw   D'5'
        movwf   D3

        movlw   D'10'
  mod0
        addwf   D0,f            Grin(X)=D(X)mod10
        decf    D1,f            Grin(X+1)=D(X+1)+D(X)div10
        skpc
        goto    mod0
  mod1
        addwf   D1,f
        decf    D2,f
        skpc
        goto    mod1
  mod2
        addwf   D2,f
        decf    D3,f
        skpc
        goto    mod2

        return

Logged
tavioman
Active Member
***
Offline Offline

Posts: 151

Thank You
-Given: 14
-Receive: 17



« Reply #12 on: April 07, 2008, 07:01:43 07:01 »

Update

Code:
unsigned char BcdToInt(unsigned char data)
{
   unsigned char i;

   i = data;
   data = (i >> 4) * 10;
   data = data + (i & 0x0F);

   return data;
}


unsigned char IntToBcd(unsigned char data)
{
   unsigned char nibh;
   unsigned char nibl;

   nibh = data / 10;
   nibl = data - (nibh * 10);

   return((nibh << 4) | nibl);
}
« Last Edit: July 20, 2008, 12:01:22 12:01 by tavioman » Logged

- Brain juice -
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC