View previous topic :: View next topic |
Author |
Message |
the0xydo
Joined: 10 Mar 2012 Posts: 4
|
PIC 18F2550 setting inputs, counter |
Posted: Sat Mar 10, 2012 1:18 am |
|
|
Hello, I'm kinda new in this programming, so I'd like to know how to set inputs as following.
RA0=analog input
RA1, RA2, RA3=switch (which i think means digital output?)
I'd just need simple program to see if switches are working properly as for example if you press switch 2 the LED on B0 would turn on.
Aswell how do you properly write in program the if function...here's what I mean
Code: |
if (switch2==1)
{
output_high(PIN_B0);
delay_ms(1000);
}
else
{
output_high(PIN_B1);
delay_ms(1000);
}
|
I know I have to define switch2 before main program, like
Code: |
#define switch2 PIN_A2
|
And also how you set like, switch if you press once, then you call first sub-program. You press 2nd time, you call 2nd sub-program, etc.
And yeah I'm using CCS C 3.222 version and PIC 18F2550 and I got real circuit made, so at the end is should work as light show.
Thanks in advance! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Sat Mar 10, 2012 2:35 am |
|
|
Line 3 - no. You need digital _inputs_.
Second, you can't just detect a 'switch'. You need a resistor on each input, so the input is pulled (say) high, when the switch is not made, and the the switch to ground, then pulls the signal low. This way there is a voltage change on the input.
You really need to study the data sheets, and try to work out what has to be done yourself, but the basic settings would be:
Code: |
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_32); //The value needed here will depend on
//your processor clock, but this value should work for most clocks
set_adc_channel(0);
|
Then all you need to do is _read_ the input pins, so:
Code: |
if (input(switch2))
|
Not just directly use 'switch2'.
Doing things 'one after the other', is just down to having some form of counter.
Hopefully enough to get you started.
Best Wishes |
|
|
the0xydo
Joined: 10 Mar 2012 Posts: 4
|
|
Posted: Sat Mar 10, 2012 3:42 pm |
|
|
Yes I've got resistors on all 3 push-switches, and electrically I get correct voltage.
But somehow I can't get switches to work with any kind of program.
If you got any simple program to test switches, and I only have to change PINs (btw I'm using PIN_A2, A3, A4 for digital inputs-push switch, and B0-B5 as digital outputs-LED's). |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Mar 10, 2012 5:10 pm |
|
|
Quote: | If you got any simple program to test switches
|
Push button driver code:
http://www.ccsinfo.com/forum/viewtopic.php?t=23837
Sample push-button circuit for that code:
Code: |
+5v
|
<
> 4.7K
< ___ Switch
To | _|_|_
PIC -----------------o o------
pin |
--- GND
-
|
|
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sat Mar 10, 2012 6:11 pm |
|
|
You also need to "debounce" your switch inputs (unless you want to see how many times the contacts bounce each time you press the switch). Often overlooked, it can cause you lots of issues trying to figure out why when you press the switch, you get 7 counts or some such.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Mar 10, 2012 8:37 pm |
|
|
The button.c driver does debounce the switches. |
|
|
the0xydo
Joined: 10 Mar 2012 Posts: 4
|
|
Posted: Sun Mar 11, 2012 4:16 am |
|
|
i've tried to modify that code for my 18F2550 but when i'm compiling there's error in button.c program, it seems that there is Bvar = 0 is not defined . Geez only a simple push switch is making me so much work and getting on my nerves. I don't know anything else what i could try to make it work... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1346
|
|
Posted: Sun Mar 11, 2012 7:03 am |
|
|
Post the code that you tried to get to work (the complete program, not just the functions). Don't include any CCS example or driver files though (against the forum rules).
Also, did you make the adjustment to the hardware switches that PCM Programmer and Ttelmah suggested (with the resistor)? |
|
|
the0xydo
Joined: 10 Mar 2012 Posts: 4
|
|
Posted: Sun Mar 11, 2012 8:01 am |
|
|
I've had resistors there on right spot from beginning
This is main program.
Code: |
#include <18F2550.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=48000000)
#pragma case
#build (reset=0x800, interrupt=0x808)
#org 0, 0x7FF {}
#define LCD_DB4 PIN_C2
#define LCD_DB5 PIN_B7
#define LCD_DB6 PIN_C6
#define LCD_DB7 PIN_C7
#define LCD_RS PIN_C0
#define LCD_E PIN_C1
#define lcd_type 2
#include "flex_lcd.c"
#include <Button.c>
int8 A2 = 0;
void main()
{
set_tris_a(0x10); // Set Pin A2 as input
while(1)
{
if(button(PIN_A2, 0, 50, 10, A2, 1))
output_high(PIN_B0);
delay_ms(1000);
}
} |
and button.c sub-program
Code: |
#define read_bit_var(x) bit_test(*(int8 *)(x >> 3), x & 7)
int8 button(int16 pin, int8 downstate, int8 delay,
int8 rate, int8 &BVar, int8 action)
{
int8 pin_value;
pin_value = read_bit_var(pin);
if(pin_value != downstate)
{
Bvar = 0;
return(!action);
}
if(Bvar == 0)
{
if(delay == 0)
Bvar = 255;
else
Bvar = delay;
return(action);
}
Bvar--;
if(BVar == 0)
{
BVar = rate;
if((delay != 0) && (delay != 255))
return(action);
}
return(!action);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 11, 2012 6:47 pm |
|
|
The reason for the Bvar error is because you have "#pragma case"
in your code, and it caught a problem where Bvar is sometimes
spelled "BVar" or as "Bvar". If you want to use #case, then edit the
Bvar names so they are all the same. |
|
|
|