View previous topic :: View next topic |
Author |
Message |
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
is there a better way to do this |
Posted: Tue May 02, 2006 5:03 am |
|
|
Hi Guys
is there a better way to do this.
i echo the 4 inputs from port a to 4 outputs on portb a button selects which half of port be the outputs.
Pete
#include <16F627A.h>
#fuses NOWDT,INTRC_IO, NOPUT, NOPROTECT, BROWNOUT, NOMCLR, LVP, NOCPD
#use delay(clock=4000000)
int axis = 0;
int1 but = 0;
zaaxis(void) {
axis = input_a() &0xf;
output_b(axis);
}
xyaxis(void) {
axis = input_a() &0xf;
output_b(axis << 4);
}
butpress(void){
if (input(PIN_A4))
{
while (!input(PIN_A4));
but=1;
delay_us(500);
}
}
butpress1(void){
if (input(PIN_A4))
{
while (!input(PIN_A4));
but=0;
delay_us(500);
}
}
void main(void)
{
while(1)
{
butpress();
butpress1();
delay_us(500);
if (but==0)
{
output_low(PIN_A6);
xyaxis();
}
else
if (but==1)
{
output_high(PIN_A6);
zaaxis();
}
}
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 02, 2006 6:04 am |
|
|
When posting code please use the Code button to preserve the layout of your program. Press this button just before pasting your code and once again after pasting your code.
Code: | #fuses NOWDT,INTRC_IO, NOPUT, NOPROTECT, BROWNOUT, NOMCLR, LVP, NOCPD | Are you sure you want LVP set? 99% of the people on this board use a programmer/ICD that doesn't need this setting. With LVP set you can no longer use RB4 for normal I/O and it can cause unexpected processor behaviour when you have no pull-down resistor on this pin.
Quote: | is there a better way to do this. | There are always better ways to do things. Please describe your problem into more detail, what behaviour do you see in this program that you would like to see different? |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Tue May 02, 2006 6:23 am |
|
|
ok new to all this but am trying. Very some times
when i press the button to select the lower or upper bits on port b it doesnt move the bits.
Pete |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 02, 2006 8:12 am |
|
|
Which compiler version are you using? You can find this in the header of the *.lst file.
Pins on the PIC processor can be used for multiple functions, for example the pins on port A are multiplexed with the A/D converter and comperator modules. Try disabling the other functions by adding the following lines to the start of your main(): Code: | setup_adc_ports(NO_ANALOGS);
setup_comparator(NC_NC_NC_NC); |
Are you absolutely sure your processor is running? In your code you can easily proof this by checking pin A6 to change when you press the button. |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Tue May 02, 2006 8:27 am |
|
|
Hi i am using 3.222
Pete |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 02, 2006 9:46 am |
|
|
Have you tried my suggested modifications?
- Changing LVP fuse to NOLVP
- Adding the code for disabling a/d converter and comparators
- Testing the pin A6 for changing logic level when you press the button.
And what are the results?
Sorry, but we can't read your mind so give as much information as you can. |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Wed May 03, 2006 3:18 am |
|
|
Hi ckielstra
i have done the mods that you stated the 627 has no ADC's only comparators.
the code works like this what ever is on lower 4 bits of portA are echoed to the lower 4 bits of portB if you press the button on port a and release the button the 4 bits should move to portB upper 4 bits.
it does not switch from the lower 4 bits to the upper 4 bits reliably
Pete |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed May 03, 2006 5:24 am |
|
|
Hi Pete,
Pls could you describe the hardware of your circuit ?
- how did you connect the buttons.
- how did you connect the inputs in PA
- how do you read the outputs in PB
- did you tested your hardware with a simple led blinking test to be sure that the micro is alive and running ?
- did you make ALL the suggested changes ?
Humberto |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 03, 2006 5:56 am |
|
|
I looked again at your code and the error is in the way you detect the button pressed. Do some cleaning up because right now your code is very confusing. Try adding some comment and look at the notes I added below.
1)
Code: | butpress(void){
if (input(PIN_A4)) // if user pressed the button then
{
while (!input(PIN_A4)); // This line loops as long as the button is not pressed. Why? We only get here when the button was pressed.
but=1;
delay_us(500); // Why do you have a delay here?
}
} |
2) The function butpress1 is identical to butpress except for the line 'but=1;'. This can't be right, both functions are testing for the user pressing the button. Did you mean one function to test for the button to be pressed and the other function to test for the button to be released? And if so, then why not combine this into a single function? |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Wed May 03, 2006 5:58 am |
|
|
Hi Humberto
my code is in the first post
PA are 5 switch tide low with a 10k resistor pulled to 5v+ when switch pressed and 1 led to tell me if the lower bits or higher bits of PB is selected
PB outputs have 8 leds on them when the port gose high the leds lights
yes all changes have been done
hope this helps
Pete |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Wed May 03, 2006 6:09 am |
|
|
Hi Guys
Here is the code with all mods
Pete
Code: |
#include <16F627A.h>
#fuses NOWDT,INTRC_IO, NOPUT, NOPROTECT, BROWNOUT, NOMCLR, NOLVP, NOCPD
#use delay(clock=4000000)
int axis = 0;
int1 but = 0;
zaaxis(void) {
axis = input_a() &0xf; // strip off upper bits
output_b(axis); // move data from Port A to upper half of portB
}
xyaxis(void) {
axis = input_a() &0xf; // strip off upper bits
output_b(axis << 4); // move data from Port A to Lower half of portB
}
butpress(void){
if (input(PIN_A4)) // if user pressed the button then
{
while (!input(PIN_A4)); // This line loops as long as the button is not pressed. Why? We only get here when the button was pressed.
but=1;
delay_us(500); // Why do you have a delay here?
}
}
void main(void)
{
setup_comparator(NC_NC_NC_NC); //Turn off Comparator
while(1)
{
butpress(); // Check for button press on PortA RA4
if (but==0) // If the button was pressed and the value in but = 0 then
{
output_low(PIN_A6); //turn led off that is conected to portA RA6
xyaxis(); //move data upper half of portB
}
else
if (but==1) //If the button was pressed and the value in but = 1 then
{
output_high(PIN_A6); //turn led on that is conected to portA RA6
zaaxis(); //move data lower half of portB
}
}
}
|
|
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Wed May 03, 2006 6:33 am |
|
|
This code sort of works the only thing with it is the selector button on PortA Ra4 is unreliable.
This button should be one click to select the lower 4 bits and another click to select to upper 4 bits on Port B
Pete
Code: |
#include <16F627A.h>
#fuses NOWDT,INTRC_IO, NOPUT, NOPROTECT, BROWNOUT, NOMCLR, NOLVP, NOCPD
#use delay(clock=4000000)
int axis = 0;
int1 but = 0;
zaaxis(void) {
axis = input_a() &0xf; // strip off upper bits
output_b(axis); // move data from Port A to upper half of portB
}
xyaxis(void) {
axis = input_a() &0xf; // strip off upper bits
output_b(axis << 4); // move data from Port A to Lower half of portB
}
butpress(void){
if (input(PIN_A4)) // if user pressed the button then
{
while(input(PIN_A4)); //wait until button released
but=0; //move the value 0 in to but
}
}
butpress1(void){
if (input(PIN_A4)) // if user pressed the button then
{
while(input(PIN_A4)); //wait until button released
but=1; ////move the value 1 in to but
}
}
void main(void)
{
setup_comparator(NC_NC_NC_NC); //Turn off Comparator
while(1)
{
butpress(); // Check for button press on PortA RA4
butpress1(); // Check for button press on PortA RA4
if (but==0) // If the button was pressed and the value in but = 0 then
{
output_low(PIN_A6); //turn led off that is conected to portA RA6
xyaxis(); //move data upper half of portB
}
else
if (but==1) //If the button was pressed and the value in but = 1 then
{
output_high(PIN_A6); //turn led on that is conected to portA RA6
zaaxis(); //move data lower half of portB
}
}
}
|
|
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Wed May 03, 2006 7:16 am |
|
|
Your method of debouncing the button press seems a bit dodgy.
Quote: | This code sort of works the only thing with it is the selector button on PortA Ra4 is unreliable |
In fact you say so yourself.
Search the archives for debouncing routines. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Wed May 03, 2006 9:27 am |
|
|
Hi Guys
The button PortA works every time but i have to hold it in.
i need 1 press to select lower 4 bits of portb and press the same button again to select the upper 4 bits.
Pete
The state of the code at the minute is
Code: |
#include <16F627A.h>
#fuses NOWDT,INTRC_IO, NOPUT, NOPROTECT, BROWNOUT, NOMCLR, NOLVP, NOCPD
#use delay(clock=4000000)
int button,debounce_counter_button;
int axis = 0;
int but = 0;
zaaxis(void)
{
axis = input_a() &0xf; // strip off upper bits
output_b(axis); // move data from Port A to upper half of portB
}
xyaxis(void)
{
axis = input_a() &0xf; // strip off upper bits
output_b(axis << 4); // move data from Port A to Lower half of portB
}
butpress(void)
{
if ( input(PIN_A4) ) // Check PortA Ra4
{
if ( ++debounce_counter_button > 15 ) // Debounce switch on Ra4
button = 1; //
but = 1; // Make value of but = 1
}
else
{
if ( --debounce_counter_button < -15 ) // Debounce switch on Ra4
button = 0; //
but = 0; // Make value of but = 1
}
}
void main(void)
{
setup_comparator(NC_NC_NC_NC); //Turn off Comparator
while(1)
{
butpress(); // Check for button press on PortA RA4
if (but==0) // If the button was pressed and the value in but = 0 then
{
output_low(PIN_A6); //turn led off that is conected to portA RA6
xyaxis(); //move data upper half of portB
}
else
if (but==1) //If the button was pressed and the value in but = 1 then
{
output_high(PIN_A6); //turn led on that is conected to portA RA6
zaaxis(); //move data lower half of portB
}
}
}
|
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed May 03, 2006 9:28 am |
|
|
Quote: |
PA are 5 switch tide low with a 10k resistor pulled to 5v+ when switch pressed
|
IF the normal state is HIGH while non-pressed switch
THEN this statement:
Code: |
butpress(void){
if (input(PIN_A4)) // if user pressed the button then
{
while(input(PIN_A4)); //wait until button released
but=0; //move the value 0 in to but
}
}
|
Should be:
Code: |
butpress(void){
if (!input(PIN_A4)) // if user pressed the button then
{
while(!input(PIN_A4)); //wait until button released
but=0; //move the value 0 in to but
}
}
|
Humberto
Humberto |
|
|
|