+ Reply to Thread
Results 1 to 15 of 15

Thread: BCD Wierd

  1. #1
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default BCD Wierd

    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...
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange


  2. #2
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,776

    Default

    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.

  3. #3
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,463
    Blog Entries
    11

    Default

    Did you forget a resistor ?

    Use the junebug/PICkit2 logic tool to capture the raw data.
    Learn to read the raw bits using the DS data sheet. Then you can better understand the code and fix it.

    3v0
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  4. #4
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,689
    Blog Entries
    5

    Default

    The 18F cores have a DAW instruction. Is that what you're looking for?
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  5. #5
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    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...

    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    ','
       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    '.' 
       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    '.'
       221   [30]                '.' 0x0A      10  00001010    '.'
       223   [32]                '.' 0x0B      11  00001011    '.'
       225   [34]                '.' 0x0C      12  00001100    '.'
       227   [36]                '.' 0x10      16  00010000    '.'
       229   [38]                '.' 0x11      17  00010001    '.'
    
    bad in bold
    Last edited by AtomSoft; 21st December 2008 at 02:57 PM.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  6. #6
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    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?

    Attached Images
    Last edited by AtomSoft; 21st December 2008 at 02:59 PM.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  7. #7
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,463
    Blog Entries
    11

    Default

    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.
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  8. #8
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    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.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  9. #9
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    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 by AtomSoft; 21st December 2008 at 03:43 PM.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  10. #10
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    I think it was the :

    OpenSPI(SPI_FOSC_4,MODE_10,SMPMID);

    Needed to be

    OpenSPI(SPI_FOSC_4,MODE_10,SMPEND);
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  11. #11
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    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);         //Had to change to SMPEND
    
        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;      //So i can get the actual digit.
            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;      //So i can get the actual digit. 
    or

    Code:
                tmp2 = (ReadSPI() & 0x0F) + 0x30;       //So i can get the actual digit.
                buff[i] = tmp2;
    
    Last edited by AtomSoft; 21st December 2008 at 04:03 PM.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  12. #12
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,463
    Blog Entries
    11

    Default

    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.
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  13. #13
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    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.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  14. #14
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,738

    Default

    Quote Originally Posted by 3v0 View Post
    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);         //Had to change to SMPEND
    
        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 thanks for the tip
    Last edited by AtomSoft; 21st December 2008 at 04:10 PM.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  15. #15
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,463
    Blog Entries
    11

    Default

    Better?:
    Yes it is,
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

+ Reply to Thread

Similar Threads

  1. wierd problem
    By timothyjackson in forum Micro Controllers
    Replies: 2
    Latest: 22nd February 2005, 12:16 PM
  2. Help in my wierd problem...
    By ym2k in forum Micro Controllers
    Replies: 19
    Latest: 21st February 2005, 11:38 AM
  3. wierd problem with lcd
    By jijita in forum General Electronics Chat
    Replies: 0
    Latest: 29th August 2004, 08:02 PM
  4. wierd tv problem
    By daviddoria in forum General Electronics Chat
    Replies: 3
    Latest: 8th June 2004, 11:44 PM
  5. wierd problem with 16f628
    By eeanma in forum Micro Controllers
    Replies: 2
    Latest: 1st November 2003, 11:43 AM

Tags for this Thread