View previous topic :: View next topic |
Author |
Message |
Elie777
Joined: 30 Dec 2010 Posts: 16
|
pic16f84 control output |
Posted: Thu Dec 30, 2010 9:29 am |
|
|
This is a code that i use to drive a car using my computer. I have a light on the car that i want to turn on and off. the red lines below are used to control the light. when the condition is satisfied(10101) the light will turn on but it will turn off alone when the input becomes different than 10101.
Plz can someone give me a hint to correct my code.
#include <16F84A.h>
#device PIC16F84A
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
if((input(pin_A0))&&(!input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B6);
}
if((input(pin_A0))&&(input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_low(pin_B6);
}
while((input(pin_A0))&&(!input(pin_A1))&&(!input(pin_A2))&&(!input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B0);
}
while((!input(pin_A0))&&(input(pin_A1))&&(!input(pin_A2))&&(!input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B2);
}
while((input(pin_A0))&&(input(pin_A1))&&(!input(pin_A2))&&(!input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B0);
output_high(pin_B2);
}
while((!input(pin_A0))&&(!input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B1);
}
while((input(pin_A0))&&(!input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B4);
}
while((!input(pin_A0))&&(input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B2);
}
while((input(pin_A0))&&(input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B0);
output_high(pin_B4);
}
while((!input(pin_A0))&&(!input(pin_A1))&&(!input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B3);
}
while((input(pin_A0))&&(!input(pin_A1))&&(!input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B0);
output_high(pin_B3);
}
while((!input(pin_A0))&&(input(pin_A1))&&(!input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B5);
}
while((input(pin_A0))&&(input(pin_A1))&&(!input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B2);
output_high(pin_B4);
}
while((!input(pin_A0))&&(!input(pin_A1))&&(input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B3);
}
while((input(pin_A0))&&(!input(pin_A1))&&(input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B0);
output_high(pin_B3);
output_high(pin_B4);
}
while((!input(pin_A0))&&(input(pin_A1))&&(input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B0);
output_high(pin_B2);
output_high(pin_B5);
}
while((input(pin_A0))&&(input(pin_A1))&&(input(pin_A2))&&(input(pin_A3))&&(!input(pin_A4)))
{
output_high(pin_B0);
output_high(pin_B3);
output_high(pin_B5);
}
while((!input(pin_A0))&&(!input(pin_A1))&&(!input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B2);
output_high(pin_B5);
}
while((input(pin_A0))&&(!input(pin_A1))&&(!input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B3);
output_high(pin_B4);
}
while((!input(pin_A0))&&(input(pin_A1))&&(!input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B4);
}
while((input(pin_A0))&&(input(pin_A1))&&(!input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B0);
output_high(pin_B5);
}
while((!input(pin_A0))&&(!input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B5);
}
while((!input(pin_A0))&&(input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B1);
output_high(pin_B3);
output_high(pin_B5);
}
output_low(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
output_low(pin_B4);
output_low(pin_B5);
}
Last edited by Elie777 on Sun Jan 02, 2011 6:58 am; edited 1 time in total |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Thu Dec 30, 2010 11:18 am |
|
|
Here's a couple of things to get you started.
Use a more descriptive Subject line for your first post.
Post code using the Code tags in the forum.
Start with a simple program that demonstrates the problem.
So, with that, the first thing that I would do is:
Code: | #include <16F84A.h>
#device PIC16F84A
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
while(true)
{
output_high(pin_B6);
delay_ms(1000);
output_low(pin_B6);
delay_ms(500);
}
}
|
See if you get the light to flash with a long flash and a short off time.
How are you driving the light with the PIC?
Also, you don't have a main loop in your code to allow the PIC to continue to run. |
|
|
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Thu Dec 30, 2010 5:09 pm |
|
|
10x jecottrell for your reply but I think you didn't get what is my problem exactly.
I want to turn on the light using an input of 10101 which is done using
Code: |
if((input(pin_A0))&&(!input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_high(pin_B6);
}
|
and I don't want it to turn off unless the input is 11101 which is done using
Code: |
if((input(pin_A0))&&(input(pin_A1))&&(input(pin_A2))&&(!input(pin_A3))&&(input(pin_A4)))
{
output_low(pin_B6);
}
|
but the light is turning off immediately after changing the input from 10101 and here is my problem. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Thu Dec 30, 2010 5:42 pm |
|
|
The only purpose of my simple program was to test the hardware.
You'll save yourself a lot of time if you work from simple to complex while debugging.
First, use my program to test your hardware and make sure that your hardware will definitely turn the light ON and OFF.
Once that is complete, try this:
Code: | #include <16F84A.h>
#device PIC16F84A
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
while(true)
{
if( (input(pin_A0))&&
(!input(pin_A1))&&
(input(pin_A2))&&
(!input(pin_A3))&&
(input(pin_A4)))
{
output_high(pin_B6);
}
delay_ms(1000);
if( (input(pin_A0))&&
(input(pin_A1))&&
(input(pin_A2))&&
(!input(pin_A3))&&
(input(pin_A4)))
{
output_low(pin_B6);
}
delay_ms(1000);
}
} |
Do you understand that if you don't have a loop your code it will only run once? |
|
|
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Thu Dec 30, 2010 5:57 pm |
|
|
10x again for replying, i really appreciate it.
i tried the code but delay_ms(1000) doesn't work.
error: undefined identifier delay_ms |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Thu Dec 30, 2010 6:36 pm |
|
|
You need to have a complete set of 'setup' commands for your program to work. for the delay to work, I think you need something similar to this:
Code: | #use delay(clock = xxxxx) |
So, you need to provide a bunch more information before anyone can help. That is probably why none of the long time pros have offered up any suggestions. The code I posted was not a complete program, just a picture of what quick test of your hardware might look like in the MAIN loop.
What hardware are you using? Some sort of development board?
What compiler and version?
This gives you an idea of what they need before they can really start to help:
Quote: | 4. When asking for help, please give as much relevant information as you can.
Here is the kind of information that people will find useful:
Version of your compiler
Target PIC Microcontroller (PIC16F877, 18F452, etc)
Standard Run Mode or Debug Mode
Target voltage (5V, 3.3V, etc)
Target oscillator speed, type (20Mhz, RC, Strip Cut, etc)
Any relevant C or ASM source code
Realize that many people on this forum provide their help and expertise in their own free time, so please help them to help you. |
|
|
|
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Thu Dec 30, 2010 6:48 pm |
|
|
I was able to use delay_ms by including #use delay(clock=4000000).
But delay_ms didn't solve the problem because it is keeping the light on for some time and turn it off but i want is to turn off the light using only 11101 and keep it on for any other input. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Thu Dec 30, 2010 7:00 pm |
|
|
The delay statement wasn't included to solve your problems. It is there to slow the program execution down for debugging.
PLEASE, take some time to describe your hardware. What are the inputs for pins A0-A4?
Can you make the inputs be 10101 and 11101 so we can test the program?
If you won't take the time to provide enough information for people to help you, they won't. |
|
|
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Fri Dec 31, 2010 4:00 am |
|
|
OK here is a detailed information:
Target PIC Microcontroller (PIC16F84)
Standard Run Mode
Target voltage (5V)
Target oscillator speed, type (4Mhz,XT oscillator)
I am using dip switches as inputs(A0-A4) and leds as outputs (B0-B6) to be able to test my code on the hardware.
All the code above is working except controlling the light.
10x for your time and help. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Fri Dec 31, 2010 9:46 am |
|
|
Can you describe the circuit for the LEDs? What is the part number? Do you have current limiting resisters in series with them? Do you know most PICs can only source about 25mA per pin?
Try the code I suggested again without the delay_ms lines and see if you can use the pin_A1 dip switch to turn the LED on and off. |
|
|
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Fri Dec 31, 2010 11:28 am |
|
|
The circuit is working 100 %. I used it in many experiments before so there is no problem with the hardware. I am able to use all inputs without any problem and the whole code is tested using my circuit and is working well.
The only prob is how to keep the light on when the input changes from 10101 and turn it off using 11101. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Fri Dec 31, 2010 11:53 am |
|
|
I'm not sure whether I understand the wording of this:
Elie777 wrote: | The only prob is how to keep the light on when the input changes from 10101 and turn it off using 11101. |
If 10101 = light ON and 11101 = light OFF,
then this should do it:
Code: | while(true)
{
if( (input(pin_A0))&&
(!input(pin_A1))&&
(input(pin_A2))&&
(!input(pin_A3))&&
(input(pin_A4)))
{
output_high(pin_B6);
}
if( (input(pin_A0))&&
(input(pin_A1))&&
(input(pin_A2))&&
(!input(pin_A3))&&
(input(pin_A4)))
{
output_low(pin_B6);
}
}
} |
Good luck. |
|
|
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Fri Dec 31, 2010 12:10 pm |
|
|
if 10101 light on and 11101 light off. and for any other input the light should stay on if it was lighted on.
when my input is 10101 the light is turning on but the problem is that when my input becomes different than 10101 the light is turning off. and i want it to turn off only when my input is 11101. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Fri Dec 31, 2010 12:23 pm |
|
|
Elie777 wrote: | when my input is 10101 the light is turning on but the problem is that when my input becomes different than 10101 the light is turning off. and i want it to turn off only when my input is 11101. |
With your code or my code? |
|
|
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Fri Dec 31, 2010 12:35 pm |
|
|
Aren't they the same? I added while (true) to my code.
and the output is the same. |
|
|
|