yefj
Advanced Member level 5
Hello if have built the following code which sends with delay 5 bytes one after the other ans shown bellow.
to connect to matlab i use the shown bellow commands with the shown bellow callback function.
In reality Matlab is not reacting to the streamed data from the controller.
it should have shown me data the moment it saw LF terminator which is 0A.
Instead i get 10 second delay and a huge block of data pored on the terminal as shown bellow.
Where did i go wrong?
Thanks.
to connect to matlab i use the shown bellow commands with the shown bellow callback function.
In reality Matlab is not reacting to the streamed data from the controller.
it should have shown me data the moment it saw LF terminator which is 0A.
Instead i get 10 second delay and a huge block of data pored on the terminal as shown bellow.
Where did i go wrong?
Thanks.
Code:
sObject=serial('COM4','BaudRate',115200,'TimeOut',10,'Terminator','LF')
sObject.BytesAvailableFcn={@callbackSerial};
fopen(sObject)
Code:
function callbackSerial(ser,~)
global time;
%val=fread(ser);
val=fscanf(ser,'%x');
%val(1,1);
dec2hex(uint8(val))
end
Code:
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "em_chip.h"
#include <stdint.h>
#include <stdbool.h>
#include "em_emu.h"
#include "bsp.h"
#include "bsp_trace.h"
uint8_t received_flag=0;//
// Receive data buffer
uint8_t buffer;
uint8_t tx_buffer=0x6F;
uint8_t tx_buffer_new=0x06;
void USART0_RX_IRQHandler(void)
{
received_flag=1;
// Get the character just received
buffer = USART0->RXDATA;
switch (buffer)
{
case 'a':
tx_buffer_new=0x4F;
break;
case 'b':
tx_buffer_new=0x62;
break;
case 'c':
tx_buffer_new=0x63;
break;
default:
break;
}//end switch
// Clear the requesting interrupt before exiting the handler
USART_IntClear(USART0, USART_IF_RXDATAV);
}
int main(void)
{
uint32_t i;
// Chip errata
CHIP_Init();
CMU_ClockEnable(cmuClock_GPIO, true);
CMU_ClockEnable(cmuClock_USART0, true);
//EFR32fg14 LOC2 page 157 data sheet TX P0
GPIO_PinModeSet(gpioPortA,2, gpioModePushPull, 1);
//EFR32fg14 LOC2 page 157 data sheet RX P2
GPIO_PinModeSet(gpioPortA,3, gpioModeInput, 0);
GPIO_PinModeSet(gpioPortA, 5, gpioModePushPull, 1);
// Default asynchronous initializer (115.2 Kbps, 8N1, no flow control)
USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;
// Configure and enable USART0
USART_InitAsync(USART0, &init);
//datasheet page 157 location2 rx portA pin3(P2) ,tx port A pin2(P0)
USART0->ROUTELOC0 = USART_ROUTELOC0_RXLOC_LOC2 | USART_ROUTELOC0_TXLOC_LOC2;
USART0->ROUTEPEN |= USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN;
// Enable NVIC USART sources
NVIC_ClearPendingIRQ(USART0_RX_IRQn);
NVIC_EnableIRQ(USART0_RX_IRQn);
//enabling TX interrupt made problems
//NVIC_ClearPendingIRQ(USART0_TX_IRQn);
//NVIC_EnableIRQ(USART0_TX_IRQn);
while (1)
{
for(i=0;i<10000;i++)
{
GPIO_PinOutSet(gpioPortA,5);
GPIO_PinOutClear(gpioPortA,5);
}
if (received_flag==1)
{
USART_Tx(USART0,0x2F);
USART_Tx(USART0,tx_buffer_new);
USART_Tx(USART0,0x8F);
USART_Tx(USART0,0x3F);
USART_Tx(USART0,'\n');
tx_buffer=tx_buffer_new;
received_flag=0;
}
else
{
USART_Tx(USART0,0x2F);
USART_Tx(USART0,tx_buffer_new);
USART_Tx(USART0,0x8F);
USART_Tx(USART0,0x3F);
USART_Tx(USART0,'\n');
}
for(i=0;i<10000;i++)
{
GPIO_PinOutSet(gpioPortA,5);
GPIO_PinOutClear(gpioPortA,5);
}
// Enable receive data valid interrupt
USART_IntEnable(USART0, USART_IEN_RXDATAV);
}
}