|
|
View previous topic :: View next topic |
Author |
Message |
keviltran
Joined: 08 Oct 2013 Posts: 1
|
[Help] PIC 16F886 with 9 button program with CCS |
Posted: Tue Oct 08, 2013 10:42 am |
|
|
Anyone can help pls
Hi all. I'm a newbie in PIC and i do this project
Input:
Port A0-A5
Port C0-C2
Output:
PortB
When i push a button form 1-9 Pic will output random on PortB. This is program i have written. I have simulation on Proteus, it working but sometime when i push button, output is blinking, not constant. Is there wrong in this program?
And when I download program to real chip PIC 16F886, it not working. Pls help me make good and clear program. Many thanks !
Code: |
#include <16F886.h>
#use delay(crystal=20000000)
#define sw1 PIN_A0
#define sw2 PIN_A1
#define sw3 PIN_A2
#define sw4 PIN_A3
#define sw5 PIN_A4
#define sw6 PIN_A5
#define sw7 PIN_C0
#define sw8 PIN_C1
#define sw9 PIN_C2
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
void main()
{
while(TRUE)
{
//sw1
if(!input(sw1))
OUTPUT_B(0x70);
///sw2
else if(!input(sw2))
OUTPUT_B(0x0C);
///sw3
else if(!input(sw3))
OUTPUT_B(0x04);
///sw4
else if (!input(sw4))
OUTPUT_B(0x07);
///sw5
else if (!input(sw5))
OUTPUT_B(0x02);
///sw6
else if (!input(sw6))
OUTPUT_B(0x05);
///sw7
else if (!input(sw7))
OUTPUT_B(0x0E);
///sw8
else if (!input(sw8))
OUTPUT_B(0x0B);
///sw9
else if (!input(sw9))
OUTPUT_B(0x0A);
else
OUTPUT_B(0x00);
}
} |
|
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Oct 08, 2013 11:08 am |
|
|
Hi,
You have a number of issues with your code, but one omission is probably preventing the PIC oscillator from starting. Add the following line to your code:
Also, move all the Fuse definitions up directly above the #use delay statement.
You should add an LED to one of your I/O pins, and flash it at startup to be sure your PIC is actually running. Toggle the LED On/Off with a 500mS
On time, and a 500mS Off time. You should add this to all your PIC projects!
Finally, you need to add 'switch debouncing' to your code. When a physical switch is pressed, it will 'chatter' (make and break continuously)
for a short period of time. This will effect the operation of your code, so you need to add some additional code to prevent this from becoming a
problem!
John |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19605
|
|
Posted: Tue Oct 08, 2013 11:29 am |
|
|
You have now learnt the first problem of Proteus. It is not the real chip....
Now forget your code for a moment, and prove you actually have the chip running. The simple 'flash an LED' test, and verify the LED flashes at the correct rate. Proteus 'accepts' what you tell it about your clock, even if the connections are wrong. It also makes the power connections without showing them, and will have a chip happily run, without decoupling etc...
Only once you have the chip working, progress to trying to write code.
You currently have the wrong oscillator selected to run at 20MHz...
Then real switches will exhibit 'bounce', so in the real processor you will see patterns of outputs rapidly changing as the switches change. You'll have to think about this one...
Then how are the input switches connected?. Normally you'd want pull up resistors on all the lines being used for inputs, and the switches just pulling the signals down.
As written, each switch overrides the later ones. Is this what you want?.
Now not a good solution, but shows an alternative to your code:
Code: |
#include <16F886.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES HS
#use delay(crystal=20000000)
int16 pins[] = {PIN_A0,PIN_A1,PIN_A2,PIN_A3,PIN_A4,PIN_A5,PIN_C0,PIN_C1,PIN_C2};
int8 patterns[]={0x70,0xC,4,7,2,5,0xE,0xB,0xA};
void main(void)
{
int8 count;
int1 clear_flag;
while(TRUE)
{
clear_flag=TRUE;
for (count=0;count<sizeof(patterns);count++)
{
if (input(pins[count]==0))
{
output_b(patterns[count]);
clear_flag=FALSE;
break;
}
}
if (clear_flag)
output_b(0x00);
}
}
|
This still has the problem of how switches work. A search here on some of the keyboard code, shows approaches to fixing this.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|