Quantcast
Channel: Question and Answer » uart
Viewing all 57 articles
Browse latest View live

I can't get out from interrupt driven serial communication

$
0
0

program works correctly but after receiving ‘#’ it has to come out from while loop and print “Exit” on lcd but it doesn’t.program still in while loop.

#define F_CPU 14745600
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
char data;
int ser=1;
void uart2_init(void)
{
   UCSR2B = 0x00; //disable while setting baud rate
   UCSR2A = 0x00;
   UCSR2C = 0x06;
   UBRR2L = 0x5F; //set baud rate lo
   UBRR2H = 0x00; //set baud rate hi
   UCSR2B = 0x98;
}

ISR(USART2_RX_vect)         // ISR for receive complete interrupt
{   
  data = UDR2;
  lcd_wr_char(data);    

  if(data=='#')
  { 
    ser=0;
  } 
}

void init_devices()
{
   cli(); //Clears the global interrupts

   lcd_port_config();
   uart2_init();

   sei();   //Enables the global interrupts
 }

//Main Function
int main(void)
{
  init_devices();
  lcd_set_4bit();
  lcd_init();
  while(ser);
  lcd_string("Exit");
}

communication gets disconnected on msp 430f54x

$
0
0

My msp gets disconnected while running a routine that is using port 10 for one-run 4X4 keypad.
Is there any connection between the two?

I’ve already tried using different computers and serial ports and other cables.

at start, the coms works perfectly, but when a key on the keypad is being pressed, then the coms gets disconnected.

here is the part of the code where it gets disconnected:

   P10OUT = 0x02;
                for(m=0;m<250;m++);       //delay <- gets disconnected here
            if (( P10IN & BIT7 ) == 128){
                    for(m=0;m<950;m++);       //delay
            P10OUT  = 0x0f;     
            return 1;
                }  

where m is a local var.

Sending multiple electrical signal to PC via Serial port

$
0
0

I am doing a project in which I want to control small toy car using EMG signals from my forearm. I want to send 4 electrical signals to the computer. I have made a processing circuit which contains amplifier, filter and ADC.

My question is,

  1. How can I send 4 signals to the computer using serial to usb cable?
  2. Is there any alternative with which I can send these signals to the computer more easily?

What happens on STM32 when multiple (UART) Interrupts are triggered at the same time? is there an interrupt stack?

$
0
0

I’m implementing a uart daisy-chain communication scheme with a Cortex M4.
When a node receives a byte over one UART, an interrupt is generated (RXNE) and the byte is written out over another UART – since I use a send and receive buffer, this places the byte in the send buffer and enables the TXE interrupt which should be triggered subsequently.

This means for every byte received, two interrupts are triggered.

Now both UARTs can receive and transmit, and it is very much possible that both UARTs do receive a byte at the same time – now both will get a RXNE interrupt and trigger a TXE interrupt on the other UART.

But only one ISR can be processed at the same time – what happens with the other ones?

Is there an interrupt stack that can get full or interrupts simply called until all corresponding interrupt bits are cleared?

The thing is that my application tends to lock up under the situation described above (with multiple bytes being received). Not however when the UARTs are set to a lower speed (this seems counter intuitive) or when there is only one side connected.

trying to understand a baud rate generator (module m counter)

$
0
0

I would like to understand a baud rate generator (module m counter).

module mod_m_counter
   #(
    parameter N=4, // number of bits in counter
              M=10 // mod-M
   )
   (
    input wire clk, reset,
    output wire max_tick,
    output wire [N-1:0] q
   );

   //signal declaration
   reg [N-1:0] r_reg;
   wire [N-1:0] r_next;

   // body
   // register
   always @(posedge clk, posedge reset)
      if (reset)
         r_reg <= 0;
      else
         r_reg <= r_next;

   // next-state logic
   assign r_next = (r_reg==(M-1)) ? 0 : r_reg + 1;
   // output logic
   assign q = r_reg;
   assign max_tick = (r_reg==(M-1)) ? 1'b1 : 1'b0;

endmodule

I know that using N=4 and M=10 (having a 50Mhz clock), I’m generating a baud rate of 19200 and I would like to understand this so that I can set another baud rates.

Thanks in advance for your help.
God bless

Bluetooth communication issue avr-pc

$
0
0

In the last few days I was messing around with avr <-> pc communication.
Thanks to the help that I received here I managed to establish a good connection using pl2303 ttl-to-usb converter.

Now, when I try to use a bluetooth module (bluetooth mate silver, from sparkfun) I encounter a new problem: Despite the fact that I connected the bluetooth module the same way as pl2303 (RX to TX, TX to RX) and I connect to it (via rfcomm), I don’t get anything from it. I also tried to send data to the mcu trough bluetooth, but I didn’t have any more success.

If I connect the RX and TX of the bluetooth module one to another the data echoes back, if I send something.

Connecting in parallel the pl2303 and the bluetooth (RX w/ RX, TX w/ TX) and then to the mcu I can see clearly that the one is receiving something, and the other one does not.

In this print-screen is easy to see both terminals and the bluetooth connection (the ‘connected’ LED lights up on the module).

enter image description here

Concerning the code, I used this library. I initialize the connection baud at 9600 (and cutecom at 1200 due to some prescaler) and 8N1 (default).

I hope I was specific enough and coherent describing this issue.

UART connection using PL 2303. Weird characters [closed]

$
0
0

I took a while to errors when playing movies from the player (lock screen, reboots).
After updating the firmware, still not working.( Same reboots, lock screen)

I open the media player and I have seen that has a UART port.

Pinout:

1 Ground
2 UART_RX0
3 UART_TX0
4 UART_RX1
5 UART_TX1
6 VDC 3.3v

Image below.

I tried to connect the port UART with connector Sitecom CN-104 v4002

sitecomlearningcentre.com/es/products/cn-104v4002/usb-to-serial-cable-0-6m/downloads

This mounts a PL2303 chip.
I had problems with windows 7 installing driver. I tried Windows XP and GNU/ Linux. In all cases the same problem (Weird characters) in Putty program.

I connected:

UART             DB9

GND              PIN5 (GND)

UART0_TX0        PIN2 (RXD)

UART0_RX0        PIN3 (TXD)

I connected with putty.

Speed: 115200
Data bits: 8
Parity: None
Flow Control: None

I have turned off the mediaplayer and then I have started and only appear strange characters. Dmesg not the system nor the bootloader appears.

I tried different bauds. But nothing.

I have also tried to use the UART1 has and turn the wires, check the cables but not touching.

I tried to connect it directly to the DB9 port without passing through the converter. But exactly the same problem occurs.

Any help on how I can display the data sent from the port UART correctly.

Image with connected cables

UART Port

Connect Schneider EM6400 to ESP8266 and call REST API [closed]

$
0
0

I have a Schneider EM6400 meter whose data I need to send to a web-service.
I dont know much about electronics so here it is-

How do I do it? My idea is to use an ESP8266.
But from what I know, the meter sends data from RS485. Hence direct connection is not possible.

Can I use RS485 to TTL bi-directional Converter?

Also, I think the output can be obtained from USB. So, Can I connect that USB to USB 2.0 to TTL UART Serial Converter or FT232RL 3.3V 5V FTDI USB to TTL Converter?

NOTE: I know the ESP programing part. Just need to know about the conversion.


Trouble enabling UART on Beaglebone Black

$
0
0

I’m having trouble getting UART enabled. I’ve gone through many different directions on how to enable and troubleshoot from updating the uEnv.txt file to updating the kernel.
I’ve come to conclude that it may be an issue with using a different kernel than the instructions, but I’m not sure?
I don’t know very much about the Beaglebone and I’m still learning.
Whenever I try to test UART by using Python and typing the following:

import Adafruit_BBIO.UART as UART

UART.setup(“UART1″)

I get the error:
File “stdin”, line 1, in module

RuntimeError: Unable to export UART channel.

I’m also starting to worry that I simply don’t have capemgr. When I run the command :

ls sys/devices

I don’t have capemgr listed.

I’m using the latest Debian Image : Jessie Debian 8.3 (2016-01-24)

the Kernel version I’m running is 4.1.17-ti-rt-r46.

and my DTC version is 1.4.1-g1e75ebc9

I would really appreciate any help. I’ve been wrestling with this issue for a few days now. Thank you!

UART Interrupt does not get more than one Char – PIC32MX110F106B

$
0
0

I am really struggling with an UART communication with my PIC32MX110F016. It is the first time I am trying to implement an UART communication with PIC32MX family but I find it difficult and I don’t know why it’s not working properly. So I decided to post all my code in a tutorial way (step-by-step) in order to get some help from you guys. I hope it helps in the future for other people who face the same problem.

So firstly I imported the libraries and declared this microcontoller configs:

#pragma config   FNOSC       = FRCPLL
#pragma config   FPLLIDIV    = DIV_2
#pragma config   FPLLMUL     = MUL_20
#pragma config   FPLLODIV    = DIV_2
#pragma config   FPBDIV      = DIV_1
#pragma config   ICESEL      = ICS_PGx2
#pragma config   WDTPS       = PS16384

#define GetSystemClock()        (40000000ul)
#define GetPeripheralClock()    (GetSystemClock())
#define BaudRate   115200

Then I implemented my uartconfig() function:

void uartconfig(void){
PPSUnLock;                        // Allow PIN Mapping for BLE
  PPSOutput(1, RPA0, U1TX);     // MAP Tx to RA0 set to digital out
  PPSInput (3, U1RX, RPA2);     // MAP Rx to RA2 set to digital in
  PPSOutput(4, RPA3, U1RTS);
  PPSInput (2, U1CTS,RPA1);
PPSLock;                         // Prevent Accidental Mapping

#define UART1TX TRISAbits.TRISA0
#define UART1RX TRISAbits.TRISA2

#define CMD TRISBbits.TRISB5

UART1TX = 0;//output
UART1RX = 1;//input

DDPCONbits.JTAGEN = 0;

UARTConfigure(UART1, UART_ENABLE_PINS_CTS_RTS);
UARTSetFifoMode(UART1, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART1, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART1, GetPeripheralClock(), BaudRate);
UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

INTEnable(INT_SOURCE_UART_RX(UART1), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART1), INT_PRIORITY_LEVEL_2);
INTSetVectorSubPriority(INT_VECTOR_UART(UART1), INT_SUB_PRIORITY_LEVEL_0);

// configure for multi-vectored mode
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
INTEnableInterrupts();
}

Then I have decided to create a command_print function to send data through UART:

void command_print(char *buffer){ 
while(*buffer != (char)0)
{
  while(!UARTTransmitterIsReady(UART1));
  UARTSendDataByte(UART1, *buffer++);
}
UARTSendDataByte(UART1, 'r');
while(!UARTTransmissionHasCompleted(UART1));
}

I have decided to create a struct in order to create a buffer for the TX/RX pin (from now I am just using the inputBuffer):

typedef volatile struct UARTBuffer {
char outputBuffer[ UART_INPUT_BUFFER_SIZE ];
char inputBuffer[ UART_OUTPUT_BUFFER_SIZE ];
unsigned short int inputReaderPos;
unsigned short int inputWriterPos;
unsigned short int outputReaderPos;
unsigned short int outputWriterPos;
} UARTBuffer;

UARTBuffer UARTBuffer1;

In my interrupt service routine ISR I decided to ignore the U1TX part and clear the flag always. For the U1RX I defined the as:

void __ISR(_UART_1_VECTOR, ipl2)UART1HANDLER(void){
char c;
if ( INTGetFlag(INT_U1TX) )
 {
    INTClearFlag(INT_SOURCE_UART_TX(UART1));
}

if ( INTGetFlag(INT_U1RX) )
 {

if( (U1STAbits.OERR == 1) || (U1STAbits.PERR == 1) || (U1STAbits.FERR == 1) ){
    // Ignore
    U1STAbits.OERR = 0;//clears if the Receive buffer has overflowed
    U1STAbits.PERR = 0;//parity error
    U1STAbits.FERR = 0;//framing error
    U1RXREG;
}
else {
    // Check if the buffer is all readed. If so, clear the buffer;
    if( UARTBuffer1.inputWriterPos == UARTBuffer1.inputReaderPos ) {
        UARTBuffer1.inputWriterPos = 0;
        UARTBuffer1.inputReaderPos = 0;
    }
    if (UARTBuffer1.inputWriterPos >= UART_INPUT_BUFFER_SIZE){
        // Buffer overflow
        UARTBuffer1.inputWriterPos = 0;
        UARTBuffer1.inputReaderPos = 0;
    }
    c = U1RXREG;
    UARTBuffer1.inputBuffer[ UARTBuffer1.inputWriterPos++ ] = c;
}
INTClearFlag(INT_SOURCE_UART_RX(UART1));
}
}

Finally my main(void) is defined as:

int32_t main(void) {
__asm__("EI");
UARTBuffer1.inputWriterPos = 0;
SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

INTEnableSystemMultiVectoredInt(); //enable interrupts

uartconfig();

command_print("SF,1");
while (1);
}

Actually my PIC is running normally and my interrupt is being triggered. What I can not understand is the reason why my code never get more than one character in the RXReg Part (namely U1RXREG) inside the ISR. I am clearing the flag by the end of the ISR and I think everything is well configured. Let me know what should I do to get my frame properly and not only the first character.
I know the UART is well designed and if I will try to send another command than "SF,1" the RX register gives me a different char.

EDIT: I am not achieving what I want. I am losing characters in the interrupt so I connect the CTS + RTS but unfortunately I am not getting better results. Is anybody available to help me with the CTS + RTS configuration for the PIC32mx family? I know I have to cross the CTS with the RTS and the RTS with the CTS from the microcontroller to the “other” component. My problem is more related with the functions/configs do I have to do.

TM4C129 Launchpad and XBee

$
0
0

I am using TM4C129 Launchpad launchpad and trying to connect XBee Series 2 (S2C) through UART.

Looking into the pinout of TM4C129, I am connecting like below

XBee TX <—> RX

XBee RX <—> TX

XBee 3.3V <—> pin#41

XBee GND <—> pin#62

I use below code on the LaunchPad at Energia

void setup()
{
  Serial.begin(9600);
  //pinMode(13, OUTPUT);   
}


void loop()
{
  Serial.println('H');
  delay(1000);
  Serial.println('L');
  delay(1000);

}

And the below python code runs on my Mac as a coordinator

#!/usr/bin/python
import serial

ser = serial.Serial('/dev/tty.usbserial-A104IC2U', 9600)

while True:
        incoming = ser.readline().strip()
        print '%s' % incoming

But it doesn’t work. FYI, this code worked on Arduino.

Has anyone tried this before? Any suggestion?
Am I missing some connection? jumper settings? Anything on the code?

HCI UART ? what's the difference with simple UART?

$
0
0

For now, I’m sending bytes from FPGA (verilog) to serial at 115200 bps.

I would like to send at higher speed and connect to a bluetooth module (RN42).

  • UART (SPP or HCI) and USB (HCI only) data connection interfaces.
  • Sustained SPP data rates – 240Kbps (slave), 300Kbps (master)
  • HCI data rates – 1.5Mbps sustained, 3.0Mbps burst in HCI mode

Then, now, I would like to implement a HCI UART in order to get max 3 Mbps.

I didn’t find anything clear about how to implement this.

Could you help me? What’s change? Should I just send my data to higher bps and it’s done?

It’s the first time I’m trying to implement another way to send data than serial.

UART only returns the same character

$
0
0

I am trying to create a program that contains UART using the PIC18f25j50 device. I have attached my creation so far which is a simple program that outputs a single character repeatedly to the console of a PC. The problem is the console displays a different character (þ~) to the selected one. This character stays the same despite the character that is selected changing (e.g “M” -> “þ~”, “g” -> “þ~”).

My thinking is that it something to do with the clock or the baud rate but I can’t see any error. I have a 8MHz crystal connected and the fuses are set so that there is no PLL. The baud rate equation is correct given that I desire the peripheral to be asynchronous, 8-bit and low speed (P.327 of the linked datasheet).

Would anyone have any idea where the mistake is or have ideas to try to fix this?

#include <xc.h>

#pragma config WDTEN = OFF  // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config PLLDIV = 2       // PLL Prescaler Selection bits (Divide by 2 (8 MHz oscillator input))
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset  (Enabled)
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)
#pragma config CPUDIV = OSC1    // CPU System Clock Postscaler (No CPU system clock divide)
#pragma config CP0 = OFF        // Code Protect (Program memory is not code-protected)
#pragma config OSC = HS      // HS
#pragma config T1DIG = ON       // T1OSCEN Enforcement (Secondary Oscillator clock source may be selected)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator (High-power operation)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)
#pragma config DSWDTOSC = INTOSCREF// DSWDT Clock Select (DSWDT uses INTRC)
#pragma config RTCOSC = T1OSCREF// RTCC Clock Select (RTCC uses T1OSC/T1CKI)
#pragma config DSBOREN = OFF    // Deep Sleep BOR (Disabled)
#pragma config DSWDTEN = OFF    // Deep Sleep Watchdog Timer (Disabled)
#pragma config DSWDTPS = G2     // Deep Sleep Watchdog Postscaler (1:2,147,483,648 (25.7 days))

void tx_data(unsigned char);

#define FREQ 8000000    // Frequency = 8MHz
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1)  

void main()
{
    TRISCbits.TRISC6 = 0; // TX pin set as output
    TRISCbits.TRISC7 = 1;   // RX pin set as input

    TXSTAbits.TX9=0;
    RCSTAbits.RX9=0;
    TXSTAbits.SYNC=0;
    TXSTAbits.BRGH=0;
    BAUDCONbits.BRG16=0;
    SPBRG=spbrg_value;     // Fill the SPBRG register to set the Baud Rate
    RCSTAbits.SPEN=1;      // To activate Serial port (TX and RX pins)
    TXSTAbits.TXEN=1;      // To enable transmission
    RCSTAbits.CREN=1;      // To enable continuous reception
while(1)
{
    tx_data('m');        // Transmit the same data back to PC
}


}

void tx_data(unsigned char data1)
{
    TXREG=data1;                      
    while(TXSTAbits.TRMT==0);            
}

MSP430F6736A UART configuration via Code Composer

$
0
0

Using embedded proggraming, Code Composer Studio 6.0.1,

Trying to configurate UART to communicate between MSP430F6736A and my PC. Using USB/RS485 converver(2 vire (A,B)).

For sending data using HERCULES(app for testing communication).

         #include <msp430.h> 
            #include <msp430f6736.h>
            /*
             * main.c
             */

            int main(void) {

                WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer

                P4DIR |= BIT6;
                        P4OUT &= ~BIT6;
                    //clock
                //UCSCTL0 |= DCO_0 | MOD_0;
                //UCSCTL1 |= DCORSEL_0;

                // Config pinov  P2.2 a P2.3 na ich funkciu(Rx a Tx)
                P2SEL |= BIT2 + BIT3; // RX and TX pins

                // config comm
                UCA1CTLW0 |= UCSWRST; // reset UCA1 for config
                UCA1CTLW0 |= UCSSEL_1 ; //  ACLK as source clock
                UCA1MCTLW |= UCBRS0;

                // Baudrate from datasheet
                UCA1BR0 = 1;    // 1200 Baud
                UCA1BR1 = 11;   // 1200 Baud

                UCA1CTLW0 &= ~UCSWRST; // UCA1 back from  reset

                // interrupts enabled for RT and TX
                UCA1IE |= UCTXIE | UCTXIE;

                __bis_SR_register(GIE); // global interupt enabled

            }

#pragma vector=USCI_A1_VECTOR
__interrupt void    Recieve_ISR(void)
{
    while  (!UCA1IFG)    //  USCI_A0 TX  buffer  ready?
    {
    P4DIR |= BIT6;
    P4OUT &= ~BIT6;
    UCA1RXBUF  =  UCA1TXBUF;    //  TX  -&amp;gt;   RXed    character
    }
}

All I want is when I send data(for eample number 5) from HERCULES(config same: 1200 baud, no parity, 1 stop bit) I want to receive them and turn LED on. But it doesn´t work.

Somewhere in this code is something missing or something bad written. , . I got big head from datasheets so can anyone help me?

AVR UART synchronization problem

$
0
0

I’m learning embedded programming (I’m not an Electrical Engineer).
Sorry if this is not the right place or I’m asking this question incorrectly, but I had a very tough time trying to figure out a solution.
I’m using a ATMega32 with a GPS module and a 16×2 LCD. I just want to read a NMEA sentence over UART and send the parsed data to the LCD. I read the sentence in a buffer and then spit it out to the display.But I get inconsistent data sometimes..
The GPS is always on and the AVR powers up in the middle and waits for the start of a sentence and then continues on receiving.

My question is: how can two devices sync if one is always sending (GPS sends data at 1 Hz rate) and the other receives at random times.
How is UART synchronization achieved? I know about start and stop bits, but once the UART gets a bad packet (raising a framing error for example) how can it recover and synchronize?

please explain what happens at hardware level also or if you can,point me to some online resource, I can’t find what I’m looking for.

Thanks so much!

Luca


Using BILATERAL SWITCH properly?

$
0
0

Summery: I am trying to figure out if I can use a bilateral switch as if it was a tiny relay capable of communicating UART signal, or are there things I am missing. Now, the long explanation.

So, I never used a bilateral switch before. The problem I am trying to solve involves having 2 devices which have one UART port each share one UART output port. Basically I have a UAV drone with a Flight Controller (FC) and an On Screen Display (OSD) units. During flight I want the FC to talk to OSD via the UART. However, on the ground, on occasion, I need to connect to either the FC or the OSD via their respective UART ports to update software, etc.

So basically I am shooting for the following states:

  • S1: FC <-> External Port
  • S2: OSD <-> External Port
  • S3: FC <-> OSD

To do this I figured I could use 8 bilateral switches in this kind of arrangement:

8 bilateral switches layout

I want to use the Texas-Instruments CD4066BPWR (Datasheet: ti.com/lit/ds/symlink/cd4066b.pdf). And this is the way I would like to connect it up:

Physical Layout

What my question boils down to is do I properly understand how bilateral switches in general and this particular IC in particular work. If I substitute the bilateral switches with tiny relays my diagram should work just fine. But with bilateral switches are there any catches I am not seeing, such as for instance if they do not work at high frequencies? I don’t need them to switch quickly, but I do need them to communicate high frequency data signals (standard serial speeds).

Also, I can’t figure out if I need a set of resistors to pull the control pins low to disconnect the connection? And when disconnected, will the IN/OUT pins just float with circuit potential or will they be pulled low or high, or stay in the last known state or something strange like that? I am treating these bilateral switches as if they were relays basically, but am I wrong to do so?

Using UART's TX & RX for Separate Functinality

$
0
0

I am using PIC18F4520.
The project requirement is to interface GSM module with uC as well as receive commands from PC via serial port.

to achieve this I have connected Microcontroller- TX to GSM module- RX, as GSM module is used just for sending SMS. It is working OK with this connection.(this connection is on TTL level.)

but i want to receive command from PC , so I have connected PC- TX to uC -RX (this connection is through MAX232).
when i connect PC-TX to uC-RX the serial terminal s/w (PC) shows garbage values on RX, and uC also receives garbage values (As I am not sending anything but still Receive interrupt routines gets called continuously ) . there is no issue in program.

but meanwhile the GSM module working fine.

Do i need to terminate the TX line?
I am using just 2 wire serial cable for connection to PC.

Thank You

Viewing all 57 articles
Browse latest View live