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.
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
#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(); // add callback functions radio sctimer_setStartFrameCb(cb_startFrame); sctimer_setEndFrameCb(cb_endFrame); // setup UART uart_setCallbacks(cb_uartTxDone,cb_uartRxCb); // prepare radio radio_rfOn(); radio_setFrequency(CHANNEL); // switch in RX radio_rxEnable(); radio_rxNow(); while (1) { // sleep while waiting for at least one of the rxpk_done to be set app_vars.rxpk_done = 0; while (app_vars.rxpk_done==0) { board_sleep(); } // if I get here, I just received a packet //===== send notification over serial port // led leds_error_on(); leds_radio_on(); // format frame to send over serial port app_vars.uart_txFrame[0] = app_vars.rxpk_buf[0]; // baecon type #1 app_vars.uart_txFrame[1] = app_vars.rxpk_buf[1]; // beacon type app_vars.uart_txFrame[2] = app_vars.rxpk_buf[17]; // ASN# 1 app_vars.uart_txFrame[3] = app_vars.rxpk_buf[18]; // ASN# 2 app_vars.uart_txFrame[4] = app_vars.rxpk_buf[19]; // ASN# 3 app_vars.uart_txFrame[5] = app_vars.rxpk_buf[20]; // ASN# 4 app_vars.uart_txFrame[6] = app_vars.rxpk_buf[21]; // ASN# 5 app_vars.uart_txFrame[7] = 0xff; // closing flag app_vars.uart_done = 0; app_vars.uart_lastTxByte = 0; // send app_vars.uart_txFrame over UART uart_clearTxInterrupts(); uart_clearRxInterrupts(); uart_enableInterrupts(); uart_writeByte(app_vars.uart_txFrame[app_vars.uart_lastTxByte]); while (app_vars.uart_done==0); // busy wait to finish uart_disableInterrupts(); // led leds_error_off(); } } //=========================== callbacks ======================================= //===== radio void cb_startFrame(PORT_TIMER_WIDTH timestamp) { // update debug stats app_dbg.num_startFrame++; } void cb_endFrame(PORT_TIMER_WIDTH timestamp) { // update debug stats app_dbg.num_endFrame++; // indicate I just received a packet app_vars.rxpk_done = 1; leds_sync_on(); // get packet from radio radio_getReceivedFrame( app_vars.rxpk_buf, &app_vars.rxpk_len, sizeof(app_vars.rxpk_buf), &app_vars.rxpk_rssi, &app_vars.rxpk_lqi, &app_vars.rxpk_crc ); // led leds_sync_off(); } //===== uart void cb_uartTxDone(void) { uart_clearTxInterrupts(); // prepare to send the next byte app_vars.uart_lastTxByte++; if (app_vars.uart_lastTxByte<sizeof(app_vars.uart_txFrame)) { uart_writeByte(app_vars.uart_txFrame[app_vars.uart_lastTxByte]); } else { app_vars.uart_done=1; } } void cb_uartRxCb(void) { // uint8_t byte; uart_clearRxInterrupts(); // toggle LED leds_debug_toggle(); }
/** \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; } void uart_clearTxInterrupts(){ IFG2 &= ~UTXIFG1; } void uart_writeByte(uint8_t byteToWrite){ U1TXBUF = byteToWrite; } uint8_t uart_readByte(){ return U1RXBUF; } //=========================== private ========================================= //=========================== interrupt handlers ============================== kick_scheduler_t uart_tx_isr() { uart_clearTxInterrupts(); // TODO: do not clear, but disable when done uart_vars.txCb(); return DO_NOT_KICK_SCHEDULER; } kick_scheduler_t uart_rx_isr() { uart_clearRxInterrupts(); // TODO: do not clear, but disable when done uart_vars.rxCb(); return DO_NOT_KICK_SCHEDULER; }
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
#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)); // initialize board board_init(); // add callback functions radio sctimer_setStartFrameCb(cb_startFrame); sctimer_setEndFrameCb(cb_endFrame); // setup UART uart_setCallbacks(cb_uartTxDone,cb_uartRxCb); // prepare radio radio_rfOn(); radio_setFrequency(CHANNEL); // switch in RX radio_rxEnable(); radio_rxNow(); while (1) { // sleep while waiting for at least one of the rxpk_done to be set app_vars.rxpk_done = 0; while (app_vars.rxpk_done==0) { board_sleep(); } // if I get here, I just received a packet //===== send notification over serial port // led leds_error_on(); leds_radio_on(); // format frame to send over serial port app_vars.uart_txFrame[0] = app_vars.rxpk_buf[0]; // baecon type #1 app_vars.uart_txFrame[1] = app_vars.rxpk_buf[1]; // beacon type app_vars.uart_txFrame[2] = app_vars.rxpk_buf[17] ; // ASN# 1 app_vars.uart_txFrame[3] = app_vars.rxpk_buf[18]; // ASN# 2 app_vars.uart_txFrame[4] = app_vars.rxpk_buf[19]; // ASN# 3 app_vars.uart_txFrame[5] = app_vars.rxpk_buf[20]; // ASN# 4 app_vars.uart_txFrame[6] = app_vars.rxpk_buf[21]; // ASN# 5 app_vars.uart_txFrame[7] = 0xff; // closing flag app_vars.uart_done = 0; app_vars.uart_lastTxByte = 0; // send app_vars.uart_txFrame over UART uart_clearTxInterrupts(); uart_clearRxInterrupts(); uart_enableInterrupts(); uart_writeByte(app_vars.uart_txFrame[app_vars.uart_lastTxByte]); while (app_vars.uart_done==0); // busy wait to finish uart_disableInterrupts(); // led leds_error_off(); } } //=========================== callbacks ======================================= //===== radio void cb_startFrame(PORT_TIMER_WIDTH timestamp) { // update debug stats app_dbg.num_startFrame++; } void cb_endFrame(PORT_TIMER_WIDTH timestamp) { // update debug stats app_dbg.num_endFrame++; // indicate I just received a packet app_vars.rxpk_done = 1; leds_sync_on(); // get packet from radio radio_getReceivedFrame( app_vars.rxpk_buf, &app_vars.rxpk_len, sizeof(app_vars.rxpk_buf), &app_vars.rxpk_rssi, &app_vars.rxpk_lqi, &app_vars.rxpk_crc ); // led leds_sync_off(); } //===== uart void cb_uartTxDone(void) { uart_clearTxInterrupts(); // prepare to send the next byte app_vars.uart_lastTxByte++; if (app_vars.uart_lastTxByte<sizeof(app_vars.uart_txFrame)) { uart_writeByte(app_vars.uart_txFrame[app_vars.uart_lastTxByte]); } else { app_vars.uart_done=1; } } void cb_uartRxCb(void) { // uint8_t byte; uart_clearRxInterrupts(); // toggle LED leds_debug_toggle(); } //====Conversion int hexadecimalToDecimal(app_vars.uart_txFrame[app_vars.uart_lastTxByte]){ uint8_t myBuffer = app_vars.uart_txFrame[app_vars.uart_lastTxByte]; //// Initializing base value to 1, i.e 16^0 int base = 1; int dec_val = 0; // Extracting characters as digits from last character for (int i=myBuffer-1; i>=0; i--) { // if character lies in '0'-'9', converting // it to integral 0-9 by subtracting 48 from // ASCII value. if (app_vars.uart_txFrame[app_vars.uart_lastTxByte]>='0' && app_vars.uart_txFrame[app_vars.uart_lastTxByte]<='9') { dec_val += (app_vars.uart_txFrame[app_vars.uart_lastTxByte] - 48)*base; // incrementing base by power base = base * 16; } // if character lies in 'A'-'F' , converting // it to integral 10 - 15 by subtracting 55 // from ASCII value else if (app_vars.uart_txFrame[app_vars.uart_lastTxByte]>='A' && app_vars.uart_txFrame[app_vars.uart_lastTxByte]<='F') { dec_val += (app_vars.uart_txFrame[app_vars.uart_lastTxByte] - 55)*base; // incrementing base by power base = base*16; } } return dec_val; }
/** \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; } void uart_clearTxInterrupts(){ IFG2 &= ~UTXIFG1; } void uart_writeByte(uint8_t byteToWrite){ U1TXBUF = byteToWrite; } uint8_t uart_readByte(){ return U1RXBUF; } //=========================== private ========================================= //=========================== interrupt handlers ============================== kick_scheduler_t uart_tx_isr() { uart_clearTxInterrupts(); // TODO: do not clear, but disable when done uart_vars.txCb(); return DO_NOT_KICK_SCHEDULER; } kick_scheduler_t uart_rx_isr() { uart_clearRxInterrupts(); // TODO: do not clear, but disable when done uart_vars.rxCb(); return DO_NOT_KICK_SCHEDULER; }
**Attention** This is a public forum