PIC16F877, sample coding regarding the use of switch

This is not the expected result and may indicate a problem with the connection or coding. The coding uses TRISA and TRISD to set the direction of the pins, and then assigns values to L1 and L2 based on the values of s1 and s2. It then sets portd to 0 and repeats the process. In summary, the coding seems to be correct but there may be an issue with the connection or initial values of the ports that is causing the LEDs to turn on immediately.
  • #1
lightgreen
14
0

Homework Statement



I'm learning PIC recently. Below is the coding that i have prepared and the attachment is the connection that I used. My question is, why the 2 LEDs will on immediately once i trigger the PIC? This is because the result is not something that should be coming out. What's wrong with my connection or my coding?

Below is the coding part:
define osc 8
TRISA.0 = 1
TRISA.1 = 1
TRISD.0 = 0
TRISD.1 = 0

L1 var portd.0
L2 var portd.1
s1 var porta.0
s2 var porta.1

Main
portd = 0
if s1 = 0 then
L1 = 1
else
L1 = 0
endif
if s2 = 0 then
L2 = 1
else
L2 = 0
endif
pause 500
goto Main
end

Homework Equations





The Attempt at a Solution

 

Attachments

  • Connection.doc
    47.5 KB · Views: 179
Physics news on Phys.org
  • #2
I think the coding is correct, but there is something wrong with the connection. The LEDs will on immediately when i trigger the PIC because the initial value of the port A and port D are both 0. Therefore, if s1=0 and s2=0, L1 and L2 will be 1.
 
  • #3


I would first suggest checking the connection to make sure it is correct and that there are no loose or faulty connections. It is also important to make sure that the PIC is properly programmed and that the code is error-free.

In terms of the coding, I would recommend using more descriptive variable names to make it easier to understand the purpose of each variable. Additionally, it is important to initialize all variables before using them in the code.

One potential issue that I see is that the LEDs are being turned on immediately because the initial state of the switches may be high (1) instead of low (0). This can be fixed by using pull-down resistors to ensure that the initial state of the switches is low.

Another thing to check is the logic of the code. Are the LEDs supposed to turn on when the switches are pressed (logic 0) or when they are released (logic 1)? Make sure the logic is consistent throughout the code.

I would also recommend adding comments to the code to explain what each section is doing, which can help with troubleshooting and understanding the code in the future. Additionally, testing the code step by step and using debugging tools can help identify any errors or issues.

Overall, it is important to carefully check the connection and code to ensure everything is correct and functioning as intended. If there are still issues, it may be helpful to consult with a more experienced programmer or refer to the PIC's datasheet for more information.
 

Related to PIC16F877, sample coding regarding the use of switch

1. What is a PIC16F877 microcontroller?

The PIC16F877 is a popular 8-bit microcontroller manufactured by Microchip Technology. It is widely used in embedded systems and is known for its low cost, low power consumption, and ease of use.

2. How do I program a PIC16F877 microcontroller?

The PIC16F877 can be programmed using various programming languages such as C, Assembly, and BASIC. It requires a programming tool, such as a PIC programmer or a development board, and a software development environment to write and upload code to the microcontroller.

3. What is a switch and how is it used in PIC16F877 coding?

A switch is an electronic component that can be used to open or close an electrical circuit. In PIC16F877 coding, a switch can be connected to one of the microcontroller's input pins and used as a trigger for a specific action or function.

4. Can I use multiple switches with a PIC16F877 microcontroller?

Yes, the PIC16F877 has multiple input pins that can be used to connect multiple switches. Each switch can be programmed to trigger a different action or function, making it a versatile component in microcontroller coding.

5. Are there any sample codes available for using switches with a PIC16F877 microcontroller?

Yes, there are many sample codes and tutorials available online that demonstrate how to use switches with a PIC16F877 microcontroller. These codes can be used as a reference or starting point for your own projects.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
650
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
3K
Back
Top