How to solve the problem of reading, modifying and writing PIC microcontrollers

Publisher:脑洞狂想Latest update time:2020-02-10 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Regarding similar problems, you can make a board and try it yourself. I believe it should not be difficult to simulate I2C communication with IO ports. The implementation method is to connect a pull-up resistor to the port, set the output register to zero, and then change the direction register to realize the conversion between 0 and 1. If it is set to output, it is 0, and if it is set to 1, the port will be pulled high under the action of the pull-up resistor. The other IO ports of the port are used for control. When doing I2C communication, it is found that when the I2C bus outputs 0, the result output is always high. Observe with an oscilloscope and finally find that the problem is that after the output register is set to 0, when writing other IO ports of the port, the entire port value will come, and then modify the written bit, and write the entire port again. Because the IO port of I2C has a pull-up resistor, the value read back each time is 1. When writing the entire port, the value of the output register is rewritten to 1, resulting in the problem that the output cannot be zero. The solution is to set the shadow register of the port. Every time you want to modify the value of any IO port of the port, you first modify the corresponding bit of the shadow register, and then use the value of the shadow register to write the entire port, and the problem is solved. By solving this problem, I have a deeper understanding of the read-modify-write function of the port.


Let's systematically introduce what read-modify-write is, the problems it causes and their solutions:

As long as the final value of the FILE (register, memory, and I/O) processed by the PICmicro command is related to the value before the command is processed, then this command is a so-called read-modify-write command. Because the operation of this type of command can be further divided into three small steps, namely read (READ), modify (MODIFY), and then write (WRITE).


Such as: ADDWF, DECF, IORWF, XORWF, BSF, BCF, etc.


When using this type of command, if the FILE being processed is I/O, you must be especially careful. Because, assuming the task voltage is 5V; and you have connected 8 diodes (perhaps LEDs) to the ground instead of resistors in series on the 8 pins of PORTB. If you previously issued the command "BSF PORTB,0", the LED on the 0th pin of PORTB is on. When you run the command "BSF PORTB,1", although the LED on the 1st pin of PORTB is on, the LED on the 0th pin of PORTB will be off. The reason is that when running "BSF PORTB,1", the entire PORTB value will be read back first, and the 0th pin of PORTB it reads is equivalent to a low potential (pulled to 0.7V by a diode). When it writes the entire PORTB value out again, the 0th pin of PORTB is written as a low potential, and the light is naturally off.


The solution is to add resistors in series or not to directly perform read-modify-write commands on the I/O.


There is another situation that needs attention: when you connect the two commands "BSF PORTB,0" and "BSF PORTB,1" together without idle running, even if you do not have the bad circuit design mentioned in the previous paragraph, sometimes you will find that the high potential of PORTB,0 will disappear. The reason for this phenomenon is: because you may have long wires connected to the outside of these two pins, there are considerable capacitance and inductance that will prevent the change of potential; and, the PIC command operation is "written out" in the last 1/4 cycle and "read in" in the first 1/4 cycle, PORTB,0 has not had time to rise to a high potential and is read back by the next command.


The solution is: usually just insert a NOP command between two commands.

Reference address:How to solve the problem of reading, modifying and writing PIC microcontrollers

Previous article:PIC microcontroller software development method
Next article:PIC16F877A serial port sending string problem

Recommended ReadingLatest update time:2024-11-16 11:43

Solution to the problem of stm8s903k.h: No such file or directory in STVD of stm8s
The key problem is that stm8s903k.h cannot be found So where is stm8s903k.h? Check the STVD installation path C:Program Files (x86)STMicroelectronicsst_toolsetinclude I believe you can find a lot of ST series microcontroller .h files, including stm8s903k.h Add the path of stm8s903k.h, OK!
[Microcontroller]
Solution to the problem of stm8s903k.h: No such file or directory in STVD of stm8s
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号