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

BeagleBone Serial Communication

$
0
0

I have a problem seeing characters written in /dev/ttyS8 on my PC terminal with this C code:

void main (void)
{
    int file, i;
    unsigned char receive[100]; // declare a buffer for receiving data
    char buf[20];
    size_t nbytes;
    ssize_t bytes_written;

    if ((file = open("/dev/ttyS8", O_RDWR))<0)
    {
        printf("UART: Failed to open the file.n");
        return;
    }

    //
    struct termios options; // the termios structure is vital
    tcgetattr(file, &options); // sets the parameters associated with file

    // Set up the communications options:
    // 9600 baud, 8-bit, enable receiver, no modem control lines
    options.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
    options.c_iflag = IGNPAR | ICRNL; // ignore partity errors, CR -> newline
    tcflush(file, TCIFLUSH); // discard file information not transmitted
    tcsetattr(file, TCSANOW, &options); // changes occur immmediately

    strcpy(buf, "This is a testn");
    nbytes = strlen(buf);

    while (1)
    {
        bytes_written = write(file, buf, nbytes);
        sleep(10);
    }
    close(file);
}

I tested if maybe type of serial cable is a problem, but it behaves the same.
/dev/ttyS8 is my UART output, but I cant determine what I’m doing wrong. Any idea what to test will be helpful, or on what to pay attention when trying to do something like this.

Thanks for your help!


Is UART necessary to send AT-commands to Bluetooth module?

$
0
0

I’m looking to alert a connected device over Bluetooth when an accelerometer detects motion. I’m thinking of using the HM-10 Bluetooth module and an ATtiny for controlling the accelerometer.

The doc says that AT-commands can only be sent via UART. Does that mean the ATTiny will need to have an UART port? Could an ATtiny45 be used instead?

Thanks

Input and UART error in PIC32

$
0
0

I have a PIC32 starter kit and I am making a program such that when I apply input 1, MCU will send this data to my PC using UART and INPUT 1 ON will be displayed on terminal, same goes for input 2 & 3. These inputs are coming from opto couplers and these are basically 3.1v. So whenever I apply these inputs, MCU pin become high, data is send to PC using UART and INPUT 1 ON or INPUT 2 ON is displayed.

But the problem is let say, I have applied input 1 and terminal is displaying INPUT 1 ON and at the same time if I apply input 2, INPUT 1 ON goes off and INPUT 2 ON started displaying. I want that both INPUT 1 ON INPUT 2 ON should be displayed and if I apply input 3 at the same time INPUT 3 ON should also be displayed along with INPUT 1 ON INPUT 2 ON. I dont know how to make logic for this. Can anyone just help me with this. Thanks

CODE:

    if(PORTAbits.RA6 == 0)
    {
        putsUART2(">>Input: 1 ONrn");

    }
    if(PORTAbits.RA6 == 1)
    {
        putsUART2(">>Input: 1 OFFrn");

    }
    if(PORTAbits.RA7 == 0)
    {
        putsUART2(">>Input: 2 ONrn");

    }
    if(PORTAbits.RA7 == 1)
    {
        putsUART2(">>Input: 2 OFFrn");

    }
    if(PORTGbits.RG13 == 0)
    {
        putsUART2(">>Input: 3 ONrn");

    }
    if(PORTGbits.RG13 == 1)
    {
        putsUART2(">>Input: 3 OFFrn");

    }

EDIT

Right now what I am trying to do is if(PORTAbits.RA6 == 0) the it should display Input: 1 ON and if if(PORTAbits.RA6 == 1) then it should display Input: 1 OFF and same for other inputs. But what’s happening is when there is no input Input: 1 OFF Input: 2 OFF Input: 3 OFF is displayed but when if(PORTAbits.RA6 == 0) comes it shows Input: 1 ON for just one time and then continuously displays Input: 1 OFF Input: 2 OFF Input: 3 OFF. I dont know why its behaving like this.

Make Arduino and 89C52 talk to each other

$
0
0

I am trying to create a project in which I am required to make Arduino and Atmel 89C52 talk to each other.

I know this can be achieved via UART but the problem is my Arduino is already using its TX/RX ports. Is there a way to make them talk by any other means? Like I2C or SPI interface? Would soft UART suffice? The whole project depends on then talking to each other effectively.

Regards

USART RX interrupt racing condition

$
0
0

I am designing an RS-485 interface with STM32F0xx series MCU.

The scheme uses USART RX interrupt.

I am wondering, if I set up the baud rate as certain value(for example, 9600), is there possibility that the interrupt fire more often than every one character time due to noise or some other disturbance in the RS-485 interface?

I am worried that if above happen, it might lock up the MCU and prevent it from doing other tasks.

Need a robust way to change uart lines

$
0
0

I’m working on a project where I am sending data from an msp430 to an atmega328 through regular tx/rx lines. My board will also need the capability to perform software changes on the atmega through FTDI, which needs to be connected to the same tx/rx lines. So when the FTDI is connected, I’ll need to break the lines from the MSP. My first thought was just to put a PMOS between the two lines and have the FTDI VCC connected to it. But I am not sure this would be the most robust solution. Can any one point me to the right direction?

Thanks,
Micah

RS422 to uart interfacing

$
0
0

I am trying to interface an RS422 to the UART on my MCU. Now, RS422 is differential. So, I understand that I need to have a differential to single ended converter IC.
This is followed by a voltage level shifter to match Voh,Vol,Vih and Vil voltage levels of the MCU UART.

Other than this do I need any buffers or protocol convertyers ?
The data is sent at standard baud rates of 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800 bps.
Also, the UART standard rates are in baud, right ?
How will it impact the bps. Because baud means symbols. For eg 9600baud = 9600x8bps.

UART DTE with multiple stop bits

$
0
0

I have a system (call it a DTE system). It sends me data (UART) which I receive at a DCE(LPC4337). Now, DTE transmits with 2 stop bits where-as it receives with just 1 stop bit. How is that possible.
It would mean I need to interface the to 2 different UARTs in my DCE, right ?
Also, supposing I go for full modem(RTS and CTS control), how would this work out ? As I would be using 2 different UARTs to communicate with the same DTE.


Xbee UART small signal drop

$
0
0

I have designed a circuit for wireless communication using an Xbee 900HP and a PIC16F1847 using UART. The circuit works and successfully sends data over UART, however when I scope the UART RX and TX lines I noticed some strange signals which are not large enough to cause any problems but are are none the less concerning as I have no explanation for them. Below I have attached images of the TX line of my PIC (RX line of the Xbee) with and without the Xbee attached to the circuit.

As you can see when the Xbee is attached there are some voltage “drops” in the TX line preceding my intended data signal. These drops look like digital data but i cannot be sure. Has anyone else experienced this or know of a possible reason?

For your reference I hav eattached a schematic of how the Xbee is wired to the PIC.

enter image description here
TX signal from PIC (UART) without Xbee attached

enter image description here
TX signal from PIC (UART) WITH Xbee attached

enter image description here

FIFO-related data transmission problems between microcontroller and PC

$
0
0

I have a situation in which a microcontroller is to perform a large number of ADC conversions and format the results into commands (or data packages) and send these to a PC using the UART. In order to measure continuously while sending data, I created a circular buffer (queue/FIFO-buffer) to store the pending packages, and the microcontroller will then (ideally) empty this queue as fast as the PC will allow.

The microcontroller automatically sends the next command (if any) after an ACK-character (in this case a ‘!’) has been received. The UART Interrupt Handler is like this:

if (ch == '!') // ch is the received character
{
    cmd_rx_ack = '!';
    cmd_tx_pop_cmd();
}

The function cmd_tx_pop_cmd() is taking one package out of the circular buffer and putting the bytes int a cmd_tx[] array and the length of the package in cmd_tx_length.

In the main loop:

if (cmd_rx_ack == ‘!’)
{
    cmd_rx_ack = 0; // Reset ack status

    // Transmit the command
    if (cmd_tx_length > 0)
    {
        cmd_tx_transmit();
    }
}

The function cmd_tx_transmit() is simply transmitting the bytes in cmd_tx[] one by one (this is done in the main loop since it takes too long time to be done in the UART-handler).

This works fine, if the consumer of the queue (in this case the UART-handler) has higher priority than the producer (a timer that periodically makes the ADC convert and push a data package with the result onto the queue). Earlier I had concurrency problems (see Concurrency issues with circular buffer in embedded program) but now I have another problem:

I want the microcontroller to send the next package in the queue not only when an ACK has been received, but also in the situations listed below:

  1. The first package to be transmitted
  2. If the measurements are performed at a speed slower than the time it takes to send a package.

In the first situation, the package is never transmitted since no ACK is received after the package is pushed onto the queue.

In the second situation, the packages are never popped out of the queue for the same reason as in the first situation. (Assume the first package is ACKed and the UART-handler wants to transmit the next one, but the queue is empty at this point in time). In this case, the queue is just getting bigger and bigger and no packages are transmitted.

I can solve the first one by transmitting the first package manually (without the use of the queue) and put the remaining onto the queue. Not a pretty solution, though – but it works… I still need to solve the second problem.

So, I think that the UART-handler is not the right place to do the queue-popping since it cannot take account for the two situations I just mentioned. But I cannot put it in the main loop since it has lowest priority and then concurrency issues become a problem.

What would be a good way to implement this?

Thanks in advance :-)

BTW 1: An ACK is transmitted to the microcontroller if the PC accepted the transmitted package. I have made a special encoding of the packages so their length can be determined. This works just fine and cmd_tx_pop_cmd() will only pop the first package available in the queue. I have tested all this many, many times and it works perfectly, so please do not focus on this part as it is not the problem.

BTW 2: The microcontroller is a Tiva C series TM4C123GH6 as found on the Tiva C Series Launchpad. I am using gcc-arm-none-eabi.

How to make UART-UART cable

$
0
0

I am software developer and I need to make cable to connect this http://lib.chipdip.ru/036/DOC001036112.pdf usb adapter (which I connect to USB port of my PC) to UART port of my A13 Olinuxino.

As I understand I must connect them this way:

UART(ADAPTER) : UART (A13)
ground(-) : ground(-)
plus(+) : plus(+)
RX : TX
TX : RX

Am I right?

How to calculate the TH1 TL1 value for baud rate 38400 at 11.059mhz?

$
0
0

Hc-05 is needed to be access in AT command mode using Microcontroller .as per datasheet default baudrate is 38400.so TH1 TL1 TMOD AND SCON value is need to access it.

Intel 8051 UART mode 0 doesn't work in Proteus (clock impulses on TXD occur constantly, MCU recieves bytes itself)

$
0
0

I think that I’ve read everything about 8051′s UART but I can’t understand what goes wrong even in this simpliest example. According to literature in mode 0 MCU transmits byte only when we write it into SBUF, and only there 8 clock impulses occur in TXD line; MCU reads byte when TXD voltage changes.

In this example (only indicator is connected to P1) I write nothing to SBUF, but pediodically TI, then RI interrpupt occur, TXD voltage constantly changes and I constantly recieve 0.
Clock frequency is 12 MHz. What can be a reason?

Thank everyone for any effort!

;====================================================================
; MCU: 80C51
; Compiler:  ASEM-51 (Proteus)
;====================================================================

$NOMOD51
$INCLUDE (8051.MCU)

;====================================================================
; RESET AND INTERRUPT VECTORS
;====================================================================

      ; Reset vector
      org   0000h
      jmp   Init

      ; Serial port interrupt vector
      org   0023h
      call  SERIAL_PORT_HANDLER
      reti

;====================================================================
; MAIN CODE BLOCK
;====================================================================

      org   0150h
Init:   
      setb  ES
      setb  EA
      mov   SCON,       #00010000b ; UART Mode 0,
                       ; accept messages with any 9th bit,
                       ; enable recieving,
                       ; don't recieve 9th bit, 
                       ; clear interrupt flags

Loop:
      jmp   Loop

;====================================================================
; SERIAL PORT INTERRUPT HANDLER
;====================================================================      

SERIAL_PORT_HANDLER:
   jb       TI,     _SENT

_RECEIVED:
   mov      P1, SBUF ; Move to indicator
   clr      RI
   jmp      _AFTER_SERIAL_PORT_HANDLING

_SENT:
   clr      TI

_AFTER_SERIAL_PORT_HANDLING:
   ret

;====================================================================
      END

Not able to perform operations based on the data received to MCU through UART?

$
0
0

I have been trying to do a simple operation based on the data byte received by the MCU(pic18F) from the PC terminal through UART. The operation is turning ON an LED depending on the content of the received data byte. So here is the code for the logic:

char data = 0;
unsigned char UARTReadByte()
{
   while(!PIR1bits.RCIF); //Wait until the receive buffer is full 
   return RCREG; //Return received byte 
}

void main()
{
   TRISAbits.TRISA1 = 0
   UARTInit(); //Initialize UART
   LCDInit(); //Initialize LCD
   while(1)
  {
     data = UARTReadByte(); //The received data from the virtual terminal (UART) is stored in another variable char 
     Write_Command(0x80); 
     Write_data(data); //Display the received data on the LCD
     if(data==1)  //check if the data is 1, if 1 turn ON the LED
     PORTAbits.RA0 = 1;
 }
}

The received number is displayed on the LCD, however the LED is not turning ON. I presume the MCU is not able to check and validate the received byte due to issues in the data format. Can anyone help me with solving this problem. Thank you all in advance.

Getting [VTERM] Parity Error when simulating USART connection in proteus

$
0
0

I’m trying to simulate USART connection between atmega32 and a virtual terminal.
But when I push the button to start the transmission, I always get

[VTERM] Parity Error

I’ve set even parity from the code and set the virtual terminal to have even parity as well.

Here’s the circuit:

enter image description here

And the terminal settings:

enter image description here

Here’s my code:

#include <avr/io.h>

void init();
void transmit(unsigned char);
unsigned char recieve();

int main(void)
{
    DDRA &= ~(1 << PA0);
    init();
    while (1) 
    {
        if(PINA & (1 << PA0)){
            transmit('a');
            while(PINA & (1 << PA0)){}
        }
    }
}

void init()
{
    //Set baud
    UBRRH = (unsigned char) (6 >> 8);
    UBRRL = (unsigned char) 6;

    //Enable TX & RX
    UCSRB |= (1 << TXEN) | (1 << RXEN);

    //Set parity EVEN
    UCSRC = (1 << URSEL) | (1 << UPM1);

    //Set stop bits (2 bits)
    UCSRC |= (1 << USBS);

    //Set data size 8bit
    UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
}

void transmit(unsigned char data)
{
    while(! (UCSRA & (1 << UDRE))){}
    UCSRB &= ~(1 << TXB8);
    if(data & 0x0100) {
        UCSRB |= (1 << TXB8);
    }
    UDR = data;
}

AVR Atmega164P UART / ISP programmer problem

$
0
0

I am having a problem with the USART on an Atmega164P. When the ISP programmer is connected everything works fine. If I disconnect the programmer then all I get is garbled serial and the chip won’t respond to characters that I send. Here’s the code

void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}

void sendString ( unsigned char str[16] )
{
    int i=0;
    for ( i = 0 ; i < len ( str ) ; i ++ )
    {
        USART_Transmit ( str [ i ] );
    }
}

Assorted functions call sendString and they all work fine until the programmer is disconnected. Then nothing works.

The connection to the PC is a USB->TTL Serial adapter.

On the electrical side I’ve checked for common ground / ground loops, different voltages on the programming pins, and other inputs that might be affecting the programming pins. Nothing is connected to the programming pins except the programmer itself.

We also tried plugging the programmer into a USB wall charger. This made the chip function as expected, as did connecting it to a laptop running only on battery.

We also connected the RESET line to VCC both with and without a pullup resistor, neither of those tests yielded successful results (still received garbled serial).

STM32 UART reliability with high baud rate

$
0
0

I am using STM32F4 (bare metal with HAL library) as an HTTP Server. I don’t implement TCP layer, because that is done for me by the WiFi232 D2 module – all I receive in the uC through UART is a string with pure HTML request (and all I send is a string with pure HTML response). The requirement of the application is to send one large response with SPA web page (almost 300k characters) and several tiny responses as AJAX (~200 chars). With 57600bps all works fine, but the big response takes 50s to load on the client, so obviously I have to raise the baud rate.

That was the scenery introduction, now the main play, where the problem begins: with everything above 57600bps, I loose message characters on the way to the browser. I loose them randomly – it’s usually a row of contiguous characters; sometimes several, sometimes above hundred of them. Initially I played with blocking UART transceiving. When I noticed the problem, I changed for DMA and it made absolutely no change. I tested both cases sending through UART to FTDA -> USB -> Termite terminal instead of the WiFi module, and saw the same symptoms. Since every single simulation lead to the aftermath of lost data, I went to the extent of even crossing STM Tx with Rx and checking if everything works okay on the shortest of possible circuits and… it of course worked perfectly :) So the uC is excluded from the suspects.

So is it even possible to achieve reliable UART transmission? Do you have any clue for how to send HTTP msgs by UART at high baud rates? I feel that I exhausted all the possibilities, but it seems unprobable that 115kbps is to much, not even mentioning Mbps… Maybe I’m missing something simple? Applying hardware flow control corrects the transmission only a tiny bit, I still get errors on 115kbps (although less frequently then without it).

*Note, that I keep talking about HTTP msgs, because of their particular nature – I can’t implement any framing nor software flow control algorithm, because I don’t have power on the browser side of this communication chain.


EDIT: Some more observations:

  • With RST/CST flow control I can see a pattern in transmission (@230kbps): contiguous ~45056 OR ~28672 characters are sent correctly, then I loose a couple of characters, and then again – ~45056 OR ~28672, then loose couple of chars etc. Note, that the number of contiguous correct characters is always one of the two mentioned (+/- a couple).
  • Without flow control I (as anticipated) get the following pattern: transmit EXACTLY 8191@115kbps or 4095@230kbps contiguous correct characters and then loose around 90@115kbps or 110@230kbps characters. What is strange though, is that I don’t loose characters in any other spot…

In accordance to those observations, I prefer to use no hwfc and simply add some delay at the well-known spots (after every 8191st or 4095th char, depending on the baud rate). However this is very hackish, I hate this solution and I hope there still is some better way to solve that problem.

Transmitting PWM binary at 400kbps (or 800kbps)

$
0
0

I need to send a string of bits to a chained set of RGB LEDs, each of which have their own controller IC. The datasheet for that IC, the WS2811, is here.

The protocol is quite simple: each bit, both 1 and 0, are sent as a high and a low, with different mark-space ratios, and are then strung together.

At 400kbps, the mark-space is as follows:

  • Binary 1: 1.2us high followed by 1.3us low (each +/- 150ns)
  • Binary 0: 0.5us high followed by 2.0us low (each +/- 150ns)

The data can either be sent at 400kbps or 800kbps. I’m not bothered which one I use, but I expect 400kbps will be easier to implement. (At 800kbps, the above times are halved.)

My question: How could this best be implemented? I’ll probably be using a 20MHz (5MIPs) 8-bit PIC MCU to determine what data is to be sent, and I’m looking for the equivalent of a USART IC that the MCU could rely on to transmit data with the above encoding.

Possible solutions

There are three ways I envisage sending that kind of data at that speed:

  1. Finding an IC / ASIC which is dedicated to this purpose.
  2. Using a second MCU dedicated to receiving data from the primary MCU, and converting it directly to the above PWM encoding.
  3. Building a dedicated transmitter out of TTL/CMOS logic ICs or similar.

For point 1, I can’t find any IC that will do it. Do they exist?

For point 2, I have ideas for how it might work, but no solutions so far. One solution: Bit-banging would be very hard, having to be written in assembler given timing requirements. Another solution, although not capable of achieving the 100ns resolution for timing of the PWM signal to match the specs in the datasheet, could configure the USART on a PIC MCU to transmit synchronously – except each 4 bits transmitted would just be a hacky way of sending one PWM bit. So to send 400kbps PWM data, the USART would have to be capable of 400 x 4 = 1.6Mbps. Then, to send a PWM binary 1, the USART would send HHLL, and to send a PWM binary 0, the USART would send HLLL. This would only barely satisfy the +/-150ns timing requirements, a margin of 25ns. And I haven’t found a PIC that can transmit that fast serially anyway.

So at the moment, option 3 is my only option: build dedicated hardware out of logic ICs. For example:

  • Construct a 25-bit shift register to output the PWM, required for a single bit. Then circuitry which fills the register accordingly depending on whether a binary 1 or 0 is to be transmitted. And since this would still be very intensive on the MCU, I would add perhaps an 8-bit latch which the MCU could write to, and allow further circuitry to process the 8 bits one at a time, for each one encoding the bit into the 25 bit shift register as a PWM cycle.
  • As above, but replace the 25 bit shift register with a monostable that outputs either a long or a short pulse, and another which loads a new bit every 2.5us.
  • Using a memory IC which acts as a lookup table, so perhaps (with extra circuitry) could be given a byte to transmit from the MCU and return data that a circuit could translate into serial data as the PWM output.

In short, I can’t find a good solution! It seems like an awful lot of work for such a simple problem. How would you do it?

Can i directly connect a gsm shield to a computer without using arduino?

$
0
0

Ok, i’ve bought a gsm900 in alexan, and what i bought was a gsm shield thinking that it is really just the same only that shield can be placed directly in an arduino.
The problem is that, im trying to do a computer system that utilizes gsm module to automatically send sms notifs, but the gsm shield does not have rs 232 port like some of the gsm module available in the internet. And Im thinking would it be possible to connect my gsm shield to an arduino then connect the arduino to the pc, would the at commands coming from my computer system be able to pass through the arduino and send it to the gsm? Or if it is possible how should i do the programming? Im using visual studio 2012 and c# as my language.

Ps. My gsm shield have small male pins namely rx tx gnd and i cant remember the other one, (my gsm shield was with my friend at this time). And i have read that rs 232 are composed of that pins. Correct me if im wrong, can i just buy/creata/modify an rs 232 cable so it can be connected through those pins? I still have very limited knowloedge bout gsm.

Thanks!

Serial Communication Rx ISR logic design

$
0
0

If im not wrong, an ISR is supposed to do minimal processing when it receives a data serially(via UART).
Im planning on implementing such a protocol for communication between 2 systems via uart.
This is a rough situation of how i plan on coding the ISR.
Assume 2 system A & B where A is sending a message to B:
Keyword is used to indicate start of message(Established-data/length cannot be the keyword).
ChannelOK,Length of data,RxLength,RxData,Packet received flag in B’s process are default 0.Length of data=no. of(all data)in bytes(ex:if all data=1,Length of data=1)

A's Process                               
 Send Keyword
 Send Length of data
 Send all data                

B's Process
 Rx Interrupt
 enter ISR
 ISR: if(Received Byte == Keyword && !ChannelOK)
      {
        Set ChannelOK 
      }

      if(ChannelOK && RxLength)
      {
       Set Length of data=received byte 
       ChannelOK=0
       RxLength=0
      }

      if(Length of data != 0 && RxData)
      {
        Store Data
        --Length of data
        if(Length of data==0)
        {
         Set Packet received flag
         RxData=0
        }
      } 

      if(ChannelOK)
      {
       Set RxLength
      } 

      if(Length of data)
      {
       Set RxData
      }
     Reset to Interrupt Again

My doubt is: B has so many stuff to do in the ISR while A is sending continuously. Asumming A sends data at 7.5Mbps(11 bits per transfer), the ISR has to reset the interrupt ever (11/7.5M) seconds. This seems very very small. Will data be lost if i fail to reset the interrupt on time or will it be stored in the 16 byte FIFO so that an interrupt can be immediately triggered the second i reset the interrupt or must i slow down A Tx process by waiting for an ACK for each byte(slows down a lot)???

Im a newbie to ISR’s.Please do help
Any ISR designs or protocols for serial data communication would be useful

Thanks

Viewing all 57 articles
Browse latest View live