alpha91
Full Member level 3
- Joined
- Sep 23, 2011
- Messages
- 168
- Helped
- 1
- Reputation
- 2
- Reaction score
- 2
- Trophy points
- 1,298
- Activity points
- 2,625
void main() {
TRISA = 0b1111;
TRISB = 0b0000;
while (1)
{
if (PORTA.F0 = 1)
{
PORTB = 0b0001;
delay_ms(500);
PORTB = 0b0000;
delay_ms(500);
}
else {
PORTB = 0b1011;
delay_ms(500);
PORTB = 0b1000;
delay_ms(500);
}
}
}
Desired function is, when button is pressed, only one LED is blinking, when released, two LEDs should blinking.
Yo didn't say what doesn't work or what compiler you are using. The code is wrong anyway, the line "if(PORTA.F0 = 1)" is an assignment of '1' to PORTA, you need to change to "==" to make it test the state of PORTA.F0.
Brian.
Assuming that you fixed the error that Brian pointed out, and you connected the LEDs to pins 0 and 1 of PortB, the code above works exactly as you described.
It's true that I'm rusty with my pic14, but I think it's needed to turn off the analog comparator before using some pins of PORTA as inputs...
See example 5.1 in the data sheet.
The easiest way is to add "CMCON = 0x07;" as the first line in main().
Also check:
1. you have MCLR pulled high to VDD through a suitable resistor (~10K)
2. you have a capacitor of ~100nF wired directly across the VSS and VDD pins
3. you have current limiting resistors (~330 Ohms) in series with each LED.
4. if your button connects pin 17 to ground, make sure you have a pull-up resistor from pin 17 to VDD (~10K) *OR* if the switch is wired between pin 17 and VDD, make sure you have a pull down resistor from pin 17 to ground.
5. make sure your clock oscillator is running!
Brian.
Hi,
Consider switch bouncing.
Klaus
The first circuit is wrong. Pin 17 is permanently grounded, regardless of the switch position. The second one is correct for detecting pin 17 high when the switch is closed ad low when it is open. If you want the opposite to happen, use the second circuit but swap the switch and resistor.
The bouncing Klaus refers to is a normal effect due to the mechanical action of a switch. As the spring action of the contacts closing is one hard surface against another, there tends to be some 'bounce' where for a short time (maybe 1/1000 second) the contacts may open and close rapidly. The PIC runs fast enough that it thinks you are opening and closing the switch several times so you may see it responding more than once. There are several ways to fix the problem, we call it 'de-bouncing', most work on the principle of waiting as soon as the first switch action is seen then checking again to confirm it after a short delay.
It isn't the ideal solution but if switch bounce is causing you problems, try fitting a capacitor of 100nF between pin 17 and ground and also add a resistor of 100 Ohms in series with the switch. It makes the rise and fall of the voltage take a little longer so the short pulses from the contact bounce are reduced in size.
Brian.
Please post your current code and schematic. You have made several changes to both so it would be useful to be updated on the current design.
Brian.
void main() {
TRISA = 0b1111;
TRISB = 0b0000;
while (1)
{
if (PORTA.F0 == 1)
{
PORTB = 0b0001;
delay_ms(500);
PORTB = 0b0000;
delay_ms(500);
}
else {
PORTB = 0b1011;
delay_ms(500);
PORTB = 0b1000;
delay_ms(500);
}
}
}
Hi, actually i think the main problem is I didnt switch off the comparator. which is CMCON 0x07.It works for me. I have added debounce delay. The only problem was there was no debounce delay. I made a video and the compressed size is 130 KB but the forum is not allowing me to attach the video.
Alpha91, you need a capacitor (~100nF) with short wires connected across VSS and VDD, as close to the PIC as possible. Without it the PIC may not run at all or it may misbehave!
Also check you have the configuration bits set properly for XT or HS oscillator and both LVP and the watchdog timer disabled.
Brian.
Think of it like this:
Current flowing through a resistance causes a voltage drop and to high frequencies, inductance looks like a resistance.
The PIC contains hundreds of thousands of little switches turning on and off at high speed, each changing the current it draws slightly. This means if there is any resistance in the supply to the PIC, it's supply voltage will go up and down in sympathy with those switches operating. The resistance will partly come from the copper in the connecting wires but mostly from the inductance of those wires. If the supply voltage changes it will have an adverse effect on the internal oscillator circuits (voltage changes shift the frequency slightly) and anything else needing a stable voltage to operate it.
The capacitor works like a reservoir, it is kept topped up by the incoming supply but being close to the PIC, it can provide instant power when needed, without the resistance/inductance being in-line. It 'evens out' the current, keeping the voltage far more stable. You can imagine it to be a rechargeable battery wired directly across VSS and VDD, giving power when the PIC demands it and recharging from the incoming supply during times of low demand.
Brian.
I see.. so it is use to stabilize the supply votlage, reduce the voltage ripple, am i right?
Yes.
Microcontrollers can be particularly susceptible to noise on their power supply rails.
The capacitor in question is commonly referred to as either a decoupling or bypass capacitor and it purpose is to essential filter out high frequency noise which may exist on the supply rail. In applications where noise is a particular issue, you will often see a parallel arrangement of several different values and types of capacitors, as each value and type are particular suited for filtering a noise frequency range or serve another purpose such as providing a reservoir of sorts for rapid changes in current.
Analog Devices - Decoupling Techniques
Application Manual for Power Supply Noise Suppression and Decoupling for Digital ICs
BigDog
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?