metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« on: February 11, 2013, 08:28:02 08:28 » |
|
I have a quick question: Suppose I have the following code: // All output TRISA = 0; PORTA = 0x00; Now, I have a code that does some task and according to some conditions, the following line might be executed: The 5th pin of PORTA is already 0, or low, whatever, when I write 0 to this pin, which is already 0, what will happen? is it going to write 0 again, or just the PIC ignores it and keeps it 0? Should I test the pin status before I write an existing status, don't laugh at me, but really this question has been on my head for a long time. edit: I forgot to mention, I am using PIC16F..
|
|
« Last Edit: February 11, 2013, 08:27:10 20:27 by metal »
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #1 on: February 11, 2013, 08:59:29 08:59 » |
|
Look into datasheet at IO ports and there you can find how is implement a I/O pin register. But, in your situation , the instruction is executed and write 0 to D input form flipflopD, after rising clock, Q from the flipflop copy D value. On pin is not change, because is the same value, but the microcontroler execute the instruction for port writing.
For example in Atmel MCU if you write "1" in PIN register (register for reading port) the pin output toggle, but microchip use the same register for output and input so, this can't be happened on pic MCU.
|
|
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #2 on: February 11, 2013, 10:25:18 10:25 » |
|
I don't really care if instruction is executed or not in the PIC, what matters to me is the pin status. If I write "0" to the pin while the pin is already "0" and nothing happens, then I am just fine..
Do you mean in AVR that if the pin is "1" and I wrote "1" to that pin, and then that pin will toggle and become "0"? Are you sure? Interesting....
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #3 on: February 11, 2013, 10:41:59 10:41 » |
|
On pic nothing else is happened if write the same value what already is there. On atmel are 2 registers PORT and PIN , PORT is for output and PIN for input, but if one pin is set to be output and you write "1" in corresponding bit for PIN register (for reading port) then the output status on this pin is toggle : // .... DDRA = 0x01; // PA0 output PORTA = 0x00;
while(1) { PINA = 0x01; _delay_ms(1); }
// ...
with this code the PA0 pin toggle after every 1 ms , but only on atmel mcu, I am not sure if on 8051 family is the same effect but can be
|
|
|
Logged
|
|
|
|
Gallymimu
Hero Member
Offline
Posts: 704
Thank You
-Given: 152
-Receive: 214
|
|
« Reply #4 on: February 11, 2013, 03:32:11 15:32 » |
|
I don't really care if instruction is executed or not in the PIC, what matters to me is the pin status. If I write "0" to the pin while the pin is already "0" and nothing happens, then I am just fine..
Do you mean in AVR that if the pin is "1" and I wrote "1" to that pin, and then that pin will toggle and become "0"? Are you sure? Interesting....
Hi Metal, To answer you question directly. Yes you'll be fine. If you write a value to a PIC port that is the same as the existing value nothing will happen on the output of the pin. It will stay at the same level and you will not see any perturbation. That said be careful with using PORT vs LAT when writing and reading. Typically you should write to the LAT and read from the PORT. You can have some funny race conditions if you write to the PORT because of how the pic does read/modify/write to the port bits.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #5 on: February 11, 2013, 03:49:06 15:49 » |
|
Metal use a pic16F and this family has no LAT register, but what you have right, when is needed read-modify-write capability need to be careful how use PORT - LAT registers
|
|
|
Logged
|
|
|
|
Sideshow Bob
Cracking Team
Hero Member
Offline
Posts: 1003
Thank You
-Given: 231
-Receive: 988
|
|
« Reply #6 on: February 11, 2013, 04:09:46 16:09 » |
|
It is correct as pointed out. Most of the 16F PICs do have not a LATx register. And as PICs use a read-modify-write approach. You can in some cases end up with unwanted situations. For example if the pin has a capacetive load. Many beginners included my self. Have once pullud out their hair then trying to make a simple LED blinking program. Then they forget that pins with analog functions. By default will be analog inputs. And not digital I/O pins
|
|
|
Logged
|
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum
|
|
|
LithiumOverdosE
Senior Member
Offline
Posts: 361
Thank You
-Given: 383
-Receive: 588
|
|
« Reply #7 on: February 11, 2013, 04:46:28 16:46 » |
|
RMW approach definitely has its merits when output of pin is used to drive loads that will prolong rise/fall time or if it is uncertain what's the real status of the output pin. Also, you can use LAT to read last value written to it regardless of the physical state of the output (it really saved my ass at few occasions). Rule of thumb is that LAT is used for setting pin state and PORT for reading input (with TRIS set to input). Basically when LAT is set you can forget about it as output will try to follow and spend processor time doing something else (especially good if using high clock speed and multi-tasking with a bit slower external components).
That being said I don't have any real practical experience with AVR so I'm also interested to know if the output of AVR pin will toggle?
|
|
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #8 on: February 11, 2013, 08:29:13 20:29 » |
|
me too, I have been using AVRs since 2006 and I've never noticed this behavior, I mean I have never tried doing that.. because in the datasheet I read in P.50:
"Three I/O memory address locations are allocated for each port, one each for the Data Register – PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write."
So, if the Port Input Pins I/O location is read only, how can I write to PINx, if not mentioned in the datasheet, is it a stable behavior, or where is it mentioned any way? Also in P.66 of ATmega16 datasheet, when you look at PINx registers, they are denoted with R, which means read only.
For PIC, yes, I mentioned in my first post that I am using PIC16, which away from the enhanced PIC16, doesn't have LATn registers. I am afraid that a member will come and tell us that writing to a PORTn pin while the pin is input will also toggle it in PIC :- )) flo0319, I am not mocking at you, but I am truly surprised, I should try it myself in AVR, may be it will work with PIC too, who knows!
|
|
|
Logged
|
|
|
|
Gallymimu
Hero Member
Offline
Posts: 704
Thank You
-Given: 152
-Receive: 214
|
|
« Reply #9 on: February 11, 2013, 10:01:08 22:01 » |
|
I didn't realize that the lower PIC16s didn't have a LAT. Here is a page on the topic of read modify write issues. http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/rmw.htmIf it is a problem, or a likely problem. Would you be comfortable using a memory location as a shadow register so that you are tracking your expected output state separately from the input state (that's basically what the LAT does)
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #10 on: February 11, 2013, 10:03:54 22:03 » |
|
http://www.societyofrobots.com/robotforum/index.php?topic=11396.0This is just a forum, but I found this tip on atmel site, and I use this tip in my projects (to toggle some pins with a minimal math logic and number of instructions) http://www.atmel.com/Images/doc8272.pdfATmega1284p datasheet page 72: "Three I/O memory address locations are allocated for each port, one each for the Data Register – PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write. However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register. In addition, the Pull-up Disable – PUD bit in MCUCR disables the pull-up function for all pins in all ports when set. "
|
|
« Last Edit: February 12, 2013, 11:29:13 11:29 by flo0319 »
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #11 on: February 12, 2013, 10:04:30 22:04 » |
|
Okay, we are both right then, but what I was trying to say is: this feature is not available on all AVRs.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #12 on: February 13, 2013, 09:21:20 09:21 » |
|
If is not write in datasheet this not means that not work. I am not sure but I suppose that it's a consequence at hardware implementation. I have not tested on all atmel mcu, but I have tested for example on atmega32 where is not mentioned this in datasheet. And also I thing that all atmel mcus has the same hardware implementation for ports.
|
|
|
Logged
|
|
|
|
metal
Global Moderator
Hero Member
Offline
Posts: 2420
Thank You
-Given: 862
-Receive: 678
Top Topic Starter
|
|
« Reply #13 on: February 13, 2013, 02:05:21 14:05 » |
|
damn... it is happening again, then it should work for all AVRs, I did not know you tested it on ATmega32. I am trying to find time to test it, but can't at the moment, sure I will test it sooner or later.
|
|
|
Logged
|
|
|
|
LithiumOverdosE
Senior Member
Offline
Posts: 361
Thank You
-Given: 383
-Receive: 588
|
|
« Reply #14 on: February 13, 2013, 05:49:42 17:49 » |
|
AFAIK those features should be incorporated in Proteus models. Much faster to test such basic features.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #15 on: February 13, 2013, 07:07:14 19:07 » |
|
metal, you can test what I said using the attachment : #include <avr/io.h> #include <util/delay.h> void main(void) { DDRB = 0x01; PORTB = 0x00; while(1) { PINB=0x01; _delay_ms(10); } }
i have put 3 MCUs atmega16 atmega32 and atmega48, if someone has the pleasure to test all models ... go
|
|
|
Logged
|
|
|
|
|