Reputation: 29
I am trying to send 10-bit data from a Renesas MCU to another MCU. I am using 10-bit ADC, so the sensor data is in 10-bit format and then it is stored in a 2-byte variable. For example:
variable type:-
uint8_t data[2] = { 97, 65 };
uint8_t g_uart0_rx_buffer = 0U;
uint8_t g_uart0_rx_error = 0U;
volatile uint8_t * gp_uart0_tx_address;
volatile uint16_t g_uart0_tx_count;
uint16_t g_result_buffer = 0U;
uint8_t check;
uint8_t check_new = 8;
int old_var = 0
uint8_t data[2] = {97 , 65}
It's working fine when I'm trying to send the whole two bytes; the code is given below:
R_ADC_Get_Result(&g_result_buffer);
old_var=g_result_buffer;
data[1] = old_var;
data[0] = (old_var >> 8);
g_uart0_tx_end = R_UART0_Send((uint8_t *) data, 2u);
And R_UART0_Send()
is defined as:
MD_STATUS R_UART0_Send(uint8_t * const tx_buf, uint16_t tx_num)
{
MD_STATUS status = MD_OK;
if (tx_num < 1U)
{
status = MD_ARGERROR;
}
else
{
gp_uart0_tx_address = tx_buf; //i.e volatile uint8_t * gp_uart0_tx_address;
g_uart0_tx_count = tx_num; //i.e volatile uint16_t g_uart0_tx_count;
STMK0 = 1U; /* disable INTST0 interrupt */
TXD0 = *gp_uart0_tx_address;
gp_uart0_tx_address++;
g_uart0_tx_count--;
STMK0 = 0U; /* enable INTST0 interrupt */
}
return (status);
}
This part is working fine and I am getting data from UART and doing my further operation.
Now, I want to send minimum data from this MCU so I had implemented some encryption and tried to send only one byte as per my encryption. The idea behind encryption is:
I store the 10-bit data in data[2]
, so eight bits are stored in data[1]
and two bits are stored in data[0]
. data[0]
may be 0
,1
,2
or 3
. There is a condition on data[0]
, that once it is 01
the MCU will transmit only one byte (i.e., data[1]
) instead of sending the whole two bytes all the time. This continues until data[0]
gets any other number (i.e., 0
,2
, or 3
). This I do in the code below.
R_ADC_Get_Result(&g_result_buffer);
old_var=g_result_buffer;
data[1] = old_var;
data[0] = (old_var >> 8);
check = data[0];
if (check!=check_new) // check_new is declared globally with random variable
{
if (check == 01)
{
//check_val = 'i';
//g_uart0_tx_end = R_UART0_Send1((char) check_val, 1u);
g_uart0_tx_end = R_UART0_Send((uint8_t *) data, 2u);
check_new = check;
//memset(data, 0, 2);
}
if (check == 02)
{
//check_val = 'j';
//g_uart0_tx_end = R_UART0_Send1((char) check_val, 1u);
g_uart0_tx_end = R_UART0_Send((uint8_t *) data, 2u);
check_new = check;
//memset(data, 0, 2);
}
if (check == 03)
{
//check_val = 'k';
//g_uart0_tx_end = R_UART0_Send1((char) check_val, 1u);
g_uart0_tx_end = R_UART0_Send((uint8_t *) data, 2u);
check_new = check;
//memset(data, 0, 2);
}
if (check == 00)
{
//check_val = 'l';
//g_uart0_tx_end = R_UART0_Send1((char) check_val, 1u);
g_uart0_tx_end = R_UART0_Send((uint8_t *) data, 2u);
check_new = check;
//memset(data, 0, 2);
}
//g_uart0_tx_end = R_UART0_Send((uint8_t *) data, 2u);
}
else
{
g_uart0_tx_end = R_UART0_Send((uint8_t *) data+1, 1u);
//memset(data, 0, 2);
}
Where R_UART0_Send()
is defined with:
MD_STATUS R_UART0_Send(uint8_t * const tx_buf, uint16_t tx_num)
{
MD_STATUS status = MD_OK;
if (tx_num < 1U)
{
status = MD_ARGERROR;
}
else
{
if (tx_num == 1U){
gp_uart0_tx_address = tx_buf;
g_uart0_tx_count = tx_num;
STMK0 = 1U; /* disable INTST0 interrupt */
TXD0 = *gp_uart0_tx_address;
gp_uart0_tx_address++;
g_uart0_tx_count--;
STMK0 = 0U; /* enable INTST0 interrupt */
}
else if (tx_num == 2U){
gp_uart0_tx_address = tx_buf;
g_uart0_tx_count = tx_num;
STMK0 = 1U; /* disable INTST0 interrupt */
TXD0 = *gp_uart0_tx_address;
gp_uart0_tx_address++;
g_uart0_tx_count--;
STMK0 = 0U; /* enable INTST0 interrupt */
}
}
return (status);
}
Initially the two-byte data is correct, and once it goes under the condition of sending one-byte data it's still correct. But when the condition changes and again it starts sending two-byte data, it starts sending data[0]
incorrectly. Let me explain it with a example:
data[0]
should be 0
,1
,2
, or 3
.
So the first two-byte data is 1,135
, so it stores the data[0]
(i.e., 1
) in the variable check_new
and checks data[0]
for changes. Until then, it will only send data[1]
.
Now initially it sends 1,135
and then it sends only data[1]
, for example, 145
, 225
, 254
, 65
, 69
, 67
, etc.
Now if it gets some data[0] = 2
, it should send 2,data[1]
, but at that time it starts sending data[0] = 65
or data[0] = 165
, or something else but not 0
,1
,2
, or 3
. Can you please help me that what I am doing wrong in this. Maybe I'm not incrementing the address or something else.
Upvotes: 1
Views: 575