ATmega328p UART framing error does not occur!

Go To Last Post
31 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a protocole over serial port as following:
1. no signal (idle) is High (+5V).
2. reset signal is a 60 us Low (0v) pulse.
3. data signal is : 250 kbps/8 bits/2 stop/no parity

 

I want to detect reset signal using Frame Error capability of MCU (ATmega328p)

 

250 kbps so 1 bit is 4us,
serial packet is 11 bit = 44 us
the farme format is = start-b0-b1-b2-b3-b4-b5-b6-b7-stop1-stop2
for normal data = 0-x-x-x-x-x-x-x-x-1-1
for reset pulse = 0-0-0-0-0-0-0-0-0-0-0-0-0-0-0

 

According page 245 of the data sheet, if 1st Stop bit of RX data is zero, FE0 (bit4) of UCSR0A register is set by MCU
But it does not occur for me!

 

I am just curious why Frame Error does not occur, whilst the datasheet says it occures!

This topic has a solution.

Majid

Last Edited: Wed. Jul 12, 2017 - 06:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

m.majid wrote:
I am just curious why Frame Error does not occur, whilst the datasheet says it occures!
Most likely because of an error in your software. E.g. did you notice you have to read UCSRA before UDR?

Stefan Ernst

Last Edited: Sat. Jun 24, 2017 - 02:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

here is the code:

automatic code generated by CVAVR 

i changed if else as my design

 

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status;
char data;

status=UCSR0A;
data=UDR0;

if (status & 0x10) // framing error
bReset = 1;
else
ucData[ucIndex++] = data;
}

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Instructions for properly posting source code: https://www.avrfreaks.net/comment... (also how to embed images)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
    char status;
    char data;
    
    status=UCSR0A;
    data=UDR0;
    
    if (status & 0x10) // framing error
        bReset = 1;
    else
        ucData[ucIndex++] = data;
}

 

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

m.majid wrote:
According page 245 of the data sheet, if 1st Stop bit of RX data is zero, FE0 (bit4) of UCSR0A register is set by MCU

But it does not occur for me!

How are you detecting that?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i need my code ignore reset pulse, just buffer input data

for normal data it works nice, every data is correct
but can not handle reset pulse, cause Frame Error not work, 

 

and interprete reset pulse as 2 data : 00, E0 and puts in buffer by mistake,

 

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i use proteus to trace the code step by step , never goes to bReset = 1; bReset ramains 0 forever.

program interprete 60 us break (0V pulse) as 2 normal data :0x00 ans 0xE0

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

m.majid wrote:
i use proteus to trace the code step by step
Wait! Do we actually talk about a simulated Mega328 here?

Then ask the Poteus support why the FE bit is not set.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In your ISR you have

bReset = 1;

 

Is bReset declared as volatile.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hmm , yes its possible,

for now just simulating is available for me, for test on real IC i must wait a few days 

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sternst wrote:
ask the Poteus support why the FE bit is not set.

Before that, does Proteus even claim to simulate framing errors?

 

If it does, are you sure that you have followed the instructions carefully, correctly, and completely?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <mega328p.h>

bit bReset;
unsigned char ucData[256];
unsigned char ucIndex, ucWriteIndex;

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
    char status;
    char data;
    
    status=UCSR0A;
    data=UDR0;
    
    if (status & 0x10) // framing error
        bReset = 1;
    else
        ucData[ucIndex++] = data;
}

#include <stdio.h>

void main(void)
{
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: Off
// Interrupt on any change on pins PCINT16-23: Off
EICRA=0x00;
EIMSK=0x00;
PCICR=0x00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x00;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART0 Mode: Asynchronous
// USART Baud Rate: 250000
UCSR0A=0x00;
UCSR0B=0x98;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x03;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
ADCSRB=0x00;

// Global enable interrupts
#asm("sei")

    bReset = 0;
    ucIndex = 0;
    ucWriteIndex = 0;
    
    while (1)
    {
        if (ucWriteIndex != ucIndex)
        {
            putchar(ucData[ucWriteIndex++]);
        }
        
        if (bReset)
        {
            ucWriteIndex = ucIndex;
            bReset = 0;
        }
    }
}

code is auto generated by CVAVR

I added if else 

it works nice for normal data

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

proteus claims simulating USART

 

I will test the real IC in a few days, share the result!

 

thanks everone

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

m.majid wrote:
proteus claims simulating USART

That wasn't the question!

 

The question was: does it simulate the Framing Error detection?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sternst wrote:
Wait! Do we actually talk about a simulated Mega328 here? Then ask the Poteus support why the FE bit is not set.

+1.  At least one.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Sounds like we're doing dmx512 here. There should be plenty of examples on the internet. Google arduino dmx512.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
The question was: does it simulate the Framing Error detection?
If a 60µs continuous low pulse is received as two bytes (0x00 and 0xE0), then it does not even simulate the start bit detection correctly.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you are simulating then how are you generating the framing error?

"This forum helps those that help themselves."

"How have you proved that your chip is running at xxMHz?" - Me

"If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

-deleted-

Looking at wrong datasheet/

Last Edited: Sun. Jun 25, 2017 - 09:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

blue_z wrote:

The framing error is supposed to be suppressed because you have a break condition. 

Ehh what?!? The framing error is specifically USED to detect break conditions. From the datasheet:

 

Quote:
The FE Flag is zero when the stop bit was correctly read as '1', and the FE Flag will be one
when the stop bit was incorrect (zero). This flag can be used for detecting out-of-sync conditions,
detecting break conditions and protocol handling.

 

Last Edited: Sun. Jun 25, 2017 - 09:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes I also simulating 60us break pulse, 

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes, I exactly want to deploy FE to detect break,

 

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did the test with ATmega88 and ATmega328P 
code works fine on real ICs
same code does not work on simulated models in proteus isis

seems the simulated model does not support FE of USART

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

m.majid wrote:
same code does not work on simulated models in proteus isis

What did Proteus support have to say?

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote them, they didnt reply yet

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got reply from support team,
they have fixed the problem and sent new dll for avr model
my project works fine with new model

problem solved !

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

m.majid wrote:
I got reply from support team

You mean the Proteus support team?

 

So this was a Protues problem - nothing to do with AVR?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes as I mentioned before , I tested on real chip.

project works fine on real chip.

nothing wrong with avr and my code.

 

the other possibilities were:

1. problem in AVR model on Proteus 

2. user problem (me!) in using Proteus

 

they sent me new AVR2.DLL file , so problem established and fixed , it was Proteus model.

 

now my code works fıne on both simulated model on Proteus and real chip of ATmega88 and ATmega328P

 

 

 

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

m.majid wrote:
they sent me new AVR2.DLL file , so problem established and fixed , it was Proteus model.

Excellent.

 

Please provide version details for the benefit of future readers ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

awneil wrote:

m.majid wrote:
they sent me new AVR2.DLL file , so problem established and fixed , it was Proteus model.

Excellent.

 

Please provide version details for the benefit of future readers ...

 

 

of course, here is summary 

 

problem : Framing Error (FE0 flag of UCSR0A register) is not working in simulated model of AVR in Proteus 8.6 SP3 program

 

cause : there is a bug in AVR2.dll file

 

solution : new version (Ver: 8.3.24150.0) of AVR2.dll file must be obtained from https://support.labcenter.com 

and replaced in C:\ProgramData\Labcenter Electronics\Proteus 8 Professional\MODELS

 

good luck!

 

 

Majid