Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

BCD Wierd

Status
Not open for further replies.

AtomSoft

Well-Known Member
Hey im using a DS1306 and im supposed to convert from BCD to binary.
I know the max i can get from the seconds is 59 which in BCD would be:

0 - tens- ones
------------------
0 - 101 - 1001 = 5 - 9 = 59

The issue is sometimes i get a 1100(12) in the ones field. any thoughts?

Thanks again!

I searched on the forum and tried a bunch of code and could not get none to work.
Some just skipped like 1,2,3,4,6,12, etc...
and some showed me 255,254,253 etc...
 
When do you get 12 in the ones field? If it is occasionally then I would suggest it is a communication or power glitch problem. If it is predictable then you need to post the conditions when it happens.

Mike.
 
OK i do have resistors on it even tho i think its not needed i tried with and without. This is data i collected. I made a buffer and had it store the values so i can show how its incrementing. Be aware since this is timed it may have doubles since its not in sync with pic. You know... :D

Code:
 Address  Symbol Name   Value    Hex  Decimal  Binary    Char

   203   buff        """##$$(())**++,,"                              
   203   [0]                 '"' 0x22      34  00100010    '"'
   204   [1]                 '"' 0x22      34  00100010    '"'
   205   [2]                 '#' 0x23      35  00100011    '#'
   206   [3]                 '#' 0x23      35  00100011    '#'
   207   [4]                 '$' 0x24      36  00100100    '$'
   208   [5]                 '$' 0x24      36  00100100    '$'
   209   [6]                 '(' 0x28      40  00101000    '('
   20A   [7]                 '(' 0x28      40  00101000    '('
   20B   [8]                 ')' 0x29      41  00101001    ')'
   20C   [9]                 ')' 0x29      41  00101001    ')'
   20D   [10]                '*' 0x2A      42  00101010    '*'
   20E   [11]                '*' 0x2A      42  00101010    '*'
   20F   [12]                '+' 0x2B      43  00101011    '+'
   210   [13]                '+' 0x2B      43  00101011    '+'
   211   [14]                ',' 0x2C      44  00101100    ','
   212   [15]                ',' 0x2C      44  00101100    ','[b]
   213   [16]                '.' 0x00       0  00000000    '.'
   214   [17]                '.' 0x00       0  00000000    '.'
   215   [18]                '.' 0x01       1  00000001    '.'
   216   [19]                '.' 0x01       1  00000001    '.'
   217   [20]                '.' 0x02       2  00000010    '.'
   218   [21]                '.' 0x02       2  00000010    '.'
   219   [22]                '.' 0x03       3  00000011    '.'
   21A   [23]                '.' 0x03       3  00000011    '.'
   21B   [24]                '.' 0x04       4  00000100    '.'
   21C   [25]                '.' 0x04       4  00000100    '.'
   21D   [26]                '.' 0x08       8  00001000    '.'
   21E   [27]                '.' 0x08       8  00001000    '.'
   21F   [28]                '.' 0x09       9  00001001    '.'
   220   [29]                '.' 0x09       9  00001001    '.'
   221   [30]                '.' 0x0A      10  00001010    '.'
   222   [31]                '.' 0x0A      10  00001010    '.'
   223   [32]                '.' 0x0B      11  00001011    '.'
   224   [33]                '.' 0x0B      11  00001011    '.'
   225   [34]                '.' 0x0C      12  00001100    '.'
   226   [35]                '.' 0x0C      12  00001100    '.'
   227   [36]                '.' 0x10      16  00010000    '.'
   228   [37]                '.' 0x10      16  00010000    '.'[/b] 
   229   [38]                '.' 0x11      17  00010001    '.'
   22A   [39]                '.' 0x11      17  00010001    '.'

The BOLD above is a full 10 seconds or 9 w/e
EDIT:
Easier view:
Code:
   213   [16]                '.' 0x00       0  00000000    '.'
   215   [18]                '.' 0x01       1  00000001    '.'
   217   [20]                '.' 0x02       2  00000010    '.'
   219   [22]                '.' 0x03       3  00000011    '.'
   21B   [24]                '.' 0x04       4  00000100    '.'
   21D   [26]                '.' 0x08       8  00001000    '.'
   21F   [28]                '.' 0x09       9  00001001    '.'
[B]   221   [30]                '.' 0x0A      10  00001010    '.'
   223   [32]                '.' 0x0B      11  00001011    '.'
   225   [34]                '.' 0x0C      12  00001100    '.'[/B]
   227   [36]                '.' 0x10      16  00010000    '.'
   229   [38]                '.' 0x11      17  00010001    '.'

bad in bold
 
Last edited:
i doubt its a power issue as it happens at predictable times. I doubt its a crystal issue because it seems to count right until 9 .

Im sure im not supposed to get a 10.11, or 12. After 9 its supposed to be 0 again right?

bcd-png.24733
 

Attachments

  • bcd.png
    bcd.png
    13.8 KB · Views: 201
Last edited:
What communication mode does the code use and do you have the SERMOD pin set accordingly?

Are these the values you get from reading 0x00 ? It looks like the data values are a simple binary count. The BCD is not there.

You need to post the code.
 
How would i DAW in C18?

Im not a big fan of inline asm.. would it be like:
Code:
char DAWIT(char Var)
{
    char tmp;
_asm

EXTERN  Var
EXTERN  tmp

DAWIT        CODE
    MOVLW   Var
    DAW
    MOVWF   tmp;
    END
_endasm
    return tmp;

}

I know that doesnt work but i tried.
 
SERMOD is tied to VCC for SPI mode.

This is my full code.
Code:
#include <p18cxxx.h>
#include <stdio.h>
#include <delays.h>
#include <spi.h>

#pragma config WDT = OFF, LVP = OFF, OSC = HS
/***********************************
            Main
***********************************/
#define CS LATCbits.LATC2

void main(void){
    unsigned char tmp,tmp2;
    unsigned char buff[20];
    char i;

    ADCON1 = 0x0E;

    TRISCbits.TRISC2 = 0;       //CS is output
    TRISCbits.TRISC3 = 0;       //SCL is output
    TRISCbits.TRISC4 = 1;       //SDA is input
    TRISCbits.TRISC5 = 0;       //SDO is output

    OpenSPI(SPI_FOSC_4,MODE_10,SMPMID);

    CS = 1;
        WriteSPI(0x8F);
        WriteSPI(0x00);
    CS = 0;

    while(1){

    for(i=0;i<20;i++){
        CS = 1;
            WriteSPI(0x00); //sec
            tmp2 = ReadSPI();
            buff[i] = tmp2;
        CS = 0;

        Delay10KTCYx(250); 
        Delay10KTCYx(250);
    }
}
}

heh SMPEND
 
Last edited:
New output :
Code:
 Address  Symbol Name   Value    Hex  Decimal  Binary    Char 

   203   buff        "		"                              
   203   [0]                 '.' 0x03       3  00000011    '.'
   204   [1]                 '.' 0x04       4  00000100    '.'
   205   [2]                 '.' 0x04       4  00000100    '.'
   206   [3]                 '.' 0x05       5  00000101    '.'
   207   [4]                 '.' 0x05       5  00000101    '.'
   208   [5]                 '.' 0x06       6  00000110    '.'
   209   [6]                 '.' 0x06       6  00000110    '.'
   20A   [7]                 '.' 0x07       7  00000111    '.'
   20B   [8]                 '.' 0x07       7  00000111    '.'
   20C   [9]                 '.' 0x08       8  00001000    '.'
   20D   [10]                '.' 0x08       8  00001000    '.'
   20E   [11]                '.' 0x09       9  00001001    '.'
   20F   [12]                '.' 0x09       9  00001001    '.'
   210   [13]                '.' 0x00       0  00000000    '.'
   211   [14]                '.' 0x00       0  00000000    '.'
   212   [15]                '.' 0x01       1  00000001    '.'
   213   [16]                '.' 0x01       1  00000001    '.'
   214   [17]                '.' 0x02       2  00000010    '.'
   215   [18]                '.' 0x02       2  00000010    '.'
   216   [19]                '.' 0x03       3  00000011    '.'
   217   [20]                '.' 0x03       3  00000011    '.'
   218   [21]                '.' 0x04       4  00000100    '.'
   219   [22]                '.' 0x04       4  00000100    '.'
   21A   [23]                '.' 0x05       5  00000101    '.'
   21B   [24]                '.' 0x05       5  00000101    '.'
   21C   [25]                '.' 0x06       6  00000110    '.'
   21D   [26]                '.' 0x06       6  00000110    '.'
   21E   [27]                '.' 0x07       7  00000111    '.'
   21F   [28]                '.' 0x07       7  00000111    '.'
   220   [29]                '.' 0x08       8  00001000    '.'
   221   [30]                '.' 0x08       8  00001000    '.'
   222   [31]                '.' 0x09       9  00001001    '.'
   223   [32]                '.' 0x09       9  00001001    '.'
   224   [33]                '.' 0x00       0  00000000    '.'
   225   [34]                '.' 0x00       0  00000000    '.'
   226   [35]                '.' 0x01       1  00000001    '.'
   227   [36]                '.' 0x01       1  00000001    '.'
   228   [37]                '.' 0x02       2  00000010    '.'
   229   [38]                '.' 0x02       2  00000010    '.'
   22A   [39]                '.' 0x03       3  00000011    '.'

using new code:
Code:
#include <p18cxxx.h>
#include <stdio.h>
#include <delays.h>
#include <spi.h>

#pragma config WDT = OFF, LVP = OFF, OSC = HS
/***********************************
            Main
***********************************/
#define CS LATCbits.LATC2

void main(void){
    unsigned char tmp,tmp2;
    unsigned char buff[40];
    char i;

    ADCON1 = 0x0E;

    TRISCbits.TRISC2 = 0;       //CS is output
    TRISCbits.TRISC3 = 0;       //SCL is output
    TRISCbits.TRISC4 = 1;       //SDA is input
    TRISCbits.TRISC5 = 0;       //SDO is output

    OpenSPI(SPI_FOSC_4,MODE_10,SMPEND);         //[b]Had to change to SMPEND[/b]

    CS = 1;
        WriteSPI(0x8F);
        WriteSPI(0x00);
    CS = 0;

    while(1){

    for(i=0;i<40;i++){
        CS = 1;
            WriteSPI(0x00); //sec
            tmp2 = ReadSPI();
            buff[i] = tmp2 & 0x0F;      //[b]So i can get the actual digit.[/b]
        CS = 0;

        Delay10KTCYx(250); 
    }

    CS = 0;     //this was here just so i can place a breakpoint here. 
}
}

This is a edit

changed code to get ASCII digit:
Code:
            tmp2 = ReadSPI() & 0x0F;
            buff[i] = tmp2 + 0x30;      //[b]So i can get the actual digit.[/b]

or

Code:
            tmp2 = (ReadSPI() & 0x0F) + 0x30;       //[b]So i can get the actual digit.[/b]
            buff[i] = tmp2;
 
Last edited:
I am glad it works .

It would be a good idea to start #define'ing your bits to meaningful names like you did with CS.

This is not so good
Code:
    TRISCbits.TRISC2 = 0;       //CS is output
    TRISCbits.TRISC3 = 0;       //SCL is output
    TRISCbits.TRISC4 = 1;       //SDA is input
    TRISCbits.TRISC5 = 0;       //SDO is output

It is a lot easier to read an makes moving the signals to other pins a snap.
 
THANK YOU ALL! i just get lost sometimes. Im going to see if i write some BCD code now. it shouldnt be hard since i need seperate numbers anyway. I just have to & and shift to get each digit.
 
I am glad it works .

It would be a good idea to start #define'ing your bits to meaningful names like you did with CS.

This is not so good
Code:
    TRISCbits.TRISC2 = 0;       //CS is output
    TRISCbits.TRISC3 = 0;       //SCL is output
    TRISCbits.TRISC4 = 1;       //SDA is input
    TRISCbits.TRISC5 = 0;       //SDO is output

It is a lot easier to read an makes moving the signals to other pins a snap.

Better?:
Code:
#include <p18cxxx.h>
#include <stdio.h>
#include <delays.h>
#include <spi.h>

#pragma config WDT = OFF, LVP = OFF, OSC = HS
/***********************************
            Main
***********************************/
#define CS LATCbits.LATC2

#define CS_T  TRISCbits.TRISC2
#define SCL_T TRISCbits.TRISC3
#define SDA_T TRISCbits.TRISC4
#define SDO_T TRISCbits.TRISC5

void main(void){
    unsigned char tmp,tmp2;
    unsigned char buff[40];
    char i;

    ADCON1 = 0x0E;

    CS_T = 0;       //CS is output
    SCL_T = 0;      //SCL is output
    SDA_T = 1;      //SDA is input
    SDO_T = 0;      //SDO is output

    OpenSPI(SPI_FOSC_4,MODE_10,SMPEND);         //[b]Had to change to SMPEND[/b]

    CS = 1;
        WriteSPI(0x8F);
        WriteSPI(0x00);
    CS = 0;

    while(1){

    for(i=0;i<40;i++){
        CS = 1;
            WriteSPI(0x00); //sec
            tmp2 = (ReadSPI() & 0x0F) + 0x30;
            buff[i] = tmp2;
        CS = 0;

        Delay10KTCYx(250); 
        Delay10KTCYx(250); 
    }

    CS = 0;     //this was here just so i can place a breakpoint here. 
}
}

I did at first and i will do it again :D thanks for the tip
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top