Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Simulating PORT on ATMega328 DDR register not working as expected

3 posters

Go down  Message [Page 1 of 1]

StefanoLenzi



Dear All,

It seems that the simulation of PORT on ATMega328 does not work as expected. In particular, I have noticed that regardless of the DDRx register the PORTx i can READ from the PORTx. The expected behavior is that READ operation is working only when DDRx is configured as INPUT.

I have attached the SimulIDE circuit and the Firmware SourceCode, as you can read in the comment whtever you comment line 13 the code works

Am i doing something wrong or there is an issue with SimulIDE simulating the ATMega328 ?

Ciao,
Stefano
Attachments
Simulating PORT on ATMega328 DDR register not working as expected Attachmentmain.zip
Assembly source code
You don't have permission to download attachments.
(1 Kb) Downloaded 2 times
Simulating PORT on ATMega328 DDR register not working as expected AttachmentAccendiScale.zip
SimulIDE circuit
You don't have permission to download attachments.
(2 Kb) Downloaded 2 times

arcachofo

arcachofo

Hi Stefano.

You are connecting PINC0 directly to Vcc, this would probably cause damage to the device in real life.
In the simulation it just forces the pin High.
Note that MCU pins have certain impedance and can't drive very low impedance loads.
To set a pin High you should use a resistance in the order of kΩ.

StefanoLenzi



arcachofo wrote:

You are connecting PINC0 directly to Vcc, this would probably cause damage to the device in real life.
Thank you for the info, I will update the circuit so that if my student try that with real device they won't burn the device Smile
arcachofo wrote:
In the simulation it just forces the pin High.
That was the goal Smile

Nevertheless, the issue with the simulator is that when I program the MCU so that PINC0 in configured as Output, I can read the value from PINC0. If you have time to look at ASM code you will notice the issue.

If you need I can try to replicate the issue with a minimal codebase.

I wonder if the simulation of DDRx registers and all the PORTx and PINx is expected to work or not.

arcachofo

arcachofo

Nevertheless, the issue with the simulator is that when I program the MCU so that PINC0 in configured as Output, I can read the value from PINC0.
That is the expected behavior as far as I know: reading PINx register you read the actual state of the pin regardless of the direction configured at DDRx.

I wonder if the simulation of DDRx registers and all the PORTx and PINx is expected to work or not.
Yes, it should work.

StefanoLenzi



arcachofo wrote:
Nevertheless, the issue with the simulator is that when I program the MCU so that PINC0 in configured as Output, I can read the value from PINC0.
That is the expected behavior as far as I know: reading PINx register you read the actual state of the pin regardless of the direction configured at DDRx.

I have understood that DDRx is controlling the behavior of the PORT, as you can read from the following sources:
1. Explorer Embedded Guide
2. Microchip Studio Guide
but it seems that it is more complex, in fact by reading the ATMel Guide

Can u help me to elaborate a bit more the concept? Does it means that if DDRx configure the PORTx as Output I can read the value of the PINx register but it has "the last read value till the PORTx was configured as INPUT" or the PINx is expected to contain the "the current value and the PINx is constantly updated according to the signal regardless of DDRx"

In the former case, what's the meaning of DDRx ?

Thank you for the GREAT JOB!

Ciao,
Stefano

P.S.: As soon as I have time I will try on real device

arcachofo

arcachofo

At the end is somhow simple:
With DDRx you set the direction of the pin.
With PINx you read the state of the pin regardless of the direction.

KerimF

KerimF

And, if we read PORTx, we get the 'internal' state of the port (its bits as programmed the last time), not the 'external' hardware state of the port (which could be read by PINx).

StefanoLenzi likes this post

StefanoLenzi



It seems that I found the holy bible for the meaning of DDRx, PORTx, and PINx

According to the ATMega328P schema the DDRx.n when is set to ZERO it MUST avoid that the value from PORTx.n reach the "external pin"

On the other hand setting DDRx.n to 1 it the value of PORTx.n reaches directely PINx.n, and if the "external pin" is connected to a device that sends signal to the ATMega328 that value will mess up with the value coming from PORTx.n but there is no safeguard from the microchip for the PINx.n

I think that the following video explain the actual behavoir very very well https://www.youtube.com/watch?v=_btHOwC3CDI&t=201s

I am going to check again the code and the circuit according to the video and will let you know if there is any bug on SimulIDE.

Sponsored content



Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum