This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430F1611: Hexadecimal to Decimal Conversion

Part Number: MSP430F1611

I am currently playing around with TelosB modules, and can get it communciated.

Now, I am trying to print these hexadecimal to decimal as this determines the value of sequence number.

If you look at the code, the number I am really interested is from ASN #1 to # 5 and each ASN is stroed as a byte.

Could someone possibly help me out how to convert 5 bytes of hexadecimal to decimal and print on the screen please?

Thank you

2110.01bsp_radio_rx.c
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdint.h"
#include "string.h"
#include "board.h"
#include "radio.h"
#include "leds.h"
#include "uart.h"
#include "sctimer.h"
//=========================== defines =========================================
#define LENGTH_PACKET 125+LENGTH_CRC ///< maximum length is 127 bytes
#define CHANNEL 20
#define LENGTH_SERIAL_FRAME 8 ///< length of the serial frame
//=========================== variables =======================================
typedef struct {
uint8_t num_radioTimerCompare;
uint8_t num_startFrame;
uint8_t num_endFrame;
} app_dbg_t;
app_dbg_t app_dbg;
typedef struct {
// rx packet
volatile uint8_t rxpk_done;
uint8_t rxpk_buf[LENGTH_PACKET];
uint8_t rxpk_len;
uint8_t rxpk_num;
int8_t rxpk_rssi;
uint8_t rxpk_lqi;
bool rxpk_crc;
// uart
uint8_t uart_txFrame[LENGTH_SERIAL_FRAME];
uint8_t uart_lastTxByte;
volatile uint8_t uart_done;
} app_vars_t;
app_vars_t app_vars;
uint8_t count = 25;
//=========================== prototypes ======================================
// radiotimer
//void cb_radioTimerOverflows(void);
// radio
void cb_startFrame(PORT_TIMER_WIDTH timestamp);
void cb_endFrame(PORT_TIMER_WIDTH timestamp);
// uart
void cb_uartTxDone(void);
void cb_uartRxCb(void);
//=========================== main ============================================
/**
\brief The program starts executing here.
*/
int mote_main(void) {
// clear local variables
memset(&app_vars,0,sizeof(app_vars_t));
// initialize board
board_init();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0827.uart.c
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
\brief TelosB-specific definition of the "uart" bsp module.
\author Thomas Watteyne <watteyne@eecs.berkeley.edu>, February 2012.
*/
#include "msp430f1611.h"
#include "uart.h"
#include "board.h"
//=========================== defines =========================================
//=========================== variables =======================================
typedef struct {
uart_tx_cbt txCb;
uart_rx_cbt rxCb;
} uart_vars_t;
uart_vars_t uart_vars;
//=========================== prototypes ======================================
//=========================== public ==========================================
void uart_init() {
P3SEL |= 0xc0; // P3.6,7 = UART1TX/RX
UCTL1 = SWRST; // hold UART1 module in reset
UCTL1 |= CHAR; // 8-bit character
/*
// 9600 baud, clocked from 32kHz ACLK
UTCTL1 |= SSEL0; // clocking from ACLK
UBR01 = 0x03; // 32768/9600 = 3.41
UBR11 = 0x00; //
UMCTL1 = 0x4A; // modulation
*/
// 115200 baud, clocked from 4.8MHz SMCLK
UTCTL1 |= SSEL1; // clocking from SMCLK
UBR01 = 41; // 4.8MHz/115200 - 41.66
UBR11 = 0x00; //
UMCTL1 = 0x4A; // modulation
ME2 |= UTXE1 + URXE1; // enable UART1 TX/RX
UCTL1 &= ~SWRST; // clear UART1 reset bit
}
void uart_setCallbacks(uart_tx_cbt txCb, uart_rx_cbt rxCb) {
uart_vars.txCb = txCb;
uart_vars.rxCb = rxCb;
}
void uart_enableInterrupts(){
IE2 |= (URXIE1 | UTXIE1);
}
void uart_disableInterrupts(){
IE2 &= ~(URXIE1 | UTXIE1);
}
void uart_clearRxInterrupts(){
IFG2 &= ~URXIFG1;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Thank you for your post.

    Trying to use the resources you gave, but I face a bit of difficulties.

    I don't want to convert all the data I receive, I want to convert from a byte A to byte B.

    If you look at this code I attached on here, bsp_radio_rx. I am only interested in the value of ASN#1 to ASN#5 in decimal.

    Could you possibly give me a tip on how to go on about with this?

    Thank you

    8738.01bsp_radio_rx.c
    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    #include "stdint.h"
    #include "string.h"
    #include "board.h"
    #include "radio.h"
    #include "leds.h"
    #include "uart.h"
    #include "sctimer.h"
    //=========================== defines =========================================
    #define LENGTH_PACKET 125+LENGTH_CRC ///< maximum length is 127 bytes
    #define CHANNEL 20
    #define LENGTH_SERIAL_FRAME 8 ///< length of the serial frame
    uint8_t myBuffer[60];
    uint8_t total_bytes;
    //=========================== variables =======================================
    typedef struct {
    uint8_t num_radioTimerCompare;
    uint8_t num_startFrame;
    uint8_t num_endFrame;
    } app_dbg_t;
    app_dbg_t app_dbg;
    typedef struct {
    // rx packet
    volatile uint8_t rxpk_done;
    uint8_t rxpk_buf[LENGTH_PACKET];
    uint8_t rxpk_len;
    uint8_t rxpk_num;
    int8_t rxpk_rssi;
    uint8_t rxpk_lqi;
    bool rxpk_crc;
    // uart
    uint8_t uart_txFrame[LENGTH_SERIAL_FRAME];
    uint8_t uart_lastTxByte;
    volatile uint8_t uart_done;
    } app_vars_t;
    app_vars_t app_vars;
    //=========================== prototypes ======================================
    // radiotimer
    //void cb_radioTimerOverflows(void);
    // radio
    void cb_startFrame(PORT_TIMER_WIDTH timestamp);
    void cb_endFrame(PORT_TIMER_WIDTH timestamp);
    // uart
    void cb_uartTxDone(void);
    void cb_uartRxCb(void);
    //hexadecimal to decimal conversion
    int hexadecimalToDecimal(char hexVal[])
    //=========================== main ============================================
    /**
    \brief The program starts executing here.
    */
    int mote_main(void) {
    // clear local variables
    memset(&app_vars,0,sizeof(app_vars_t));
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    2818.uart.c
    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    /**
    \brief TelosB-specific definition of the "uart" bsp module.
    \author Thomas Watteyne <watteyne@eecs.berkeley.edu>, February 2012.
    */
    #include "msp430f1611.h"
    #include "uart.h"
    #include "board.h"
    //=========================== defines =========================================
    //=========================== variables =======================================
    typedef struct {
    uart_tx_cbt txCb;
    uart_rx_cbt rxCb;
    } uart_vars_t;
    uart_vars_t uart_vars;
    //=========================== prototypes ======================================
    //=========================== public ==========================================
    void uart_init() {
    P3SEL |= 0xc0; // P3.6,7 = UART1TX/RX
    UCTL1 = SWRST; // hold UART1 module in reset
    UCTL1 |= CHAR; // 8-bit character
    /*
    // 9600 baud, clocked from 32kHz ACLK
    UTCTL1 |= SSEL0; // clocking from ACLK
    UBR01 = 0x03; // 32768/9600 = 3.41
    UBR11 = 0x00; //
    UMCTL1 = 0x4A; // modulation
    */
    // 115200 baud, clocked from 4.8MHz SMCLK
    UTCTL1 |= SSEL1; // clocking from SMCLK
    UBR01 = 41; // 4.8MHz/115200 - 41.66
    UBR11 = 0x00; //
    UMCTL1 = 0x4A; // modulation
    ME2 |= UTXE1 + URXE1; // enable UART1 TX/RX
    UCTL1 &= ~SWRST; // clear UART1 reset bit
    }
    void uart_setCallbacks(uart_tx_cbt txCb, uart_rx_cbt rxCb) {
    uart_vars.txCb = txCb;
    uart_vars.rxCb = rxCb;
    }
    void uart_enableInterrupts(){
    IE2 |= (URXIE1 | UTXIE1);
    }
    void uart_disableInterrupts(){
    IE2 &= ~(URXIE1 | UTXIE1);
    }
    void uart_clearRxInterrupts(){
    IFG2 &= ~URXIFG1;
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • You certainly don't have to convert every value in the transmission packet, it is perfectly acceptable to only apply your conversion on the desired bytes. It should be noted that hexadecimal and decimal are intrinsically the same value, what I believe you are looking for is a hexadecimal representation of a decimal number in the terminal. Unfortunately eight bits can be represented as two characters in hex (0xFF) but three characters in (255) which means you will need to split your values into two characters apiece. A better solution would be to handle this logic from the host PC side.

    Regards,
    Ryan

**Attention** This is a public forum