CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Button.c

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
honeytree



Joined: 13 Nov 2005
Posts: 8

View user's profile Send private message

Button.c
PostPosted: Mon Nov 14, 2005 9:11 pm     Reply with quote

I ran the button.c code as listed on the board, yet I do not receive any output to screen as suggested. How is the RS232 being used?

--------------------------------------------------------
#include <16F877.H>
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// These are the "Bvar" variables.
int button1 = 0; // For the button on pin A4
int button2 = 0; // For the button on pin B0

#include "button.c"

/* Function parameters:

button(pin, DownState, Delay, Rate, BVar, Action)

Returns: The value of the Action variable (normally
set = 1) is returned when the button is
pressed. When the button isn't pressed,
*/ the opposite value is returned (normally 0).

void main()
{
printf("\n\rStart: ");

set_tris_a(0x10); // Set Pin A4 as input
set_tris_b(0x01); // Set Pin B0 as input


// When button on RB0 is pressed, "B" is displayed on the
// LCD. When the RA4 button is pressed, "A"
// is displayed. If you hold the buttons down for more
// than 1/2 second, they will auto-repeat 10 times per
// second.

while(1)
{
if(button(PIN_B0, 0, 50, 10, button2, 1))
printf("B");

if(button(PIN_A4, 0, 50, 10, button1, 1))
printf("A");

// You must use a delay statement in the loop.
// A good value to use is 10 ms.
delay_ms(10);
}

}
---------------------------------
// button.c file
//******************************************************

/* Pin: such as PIN_B0.
Downstate:
This is the logic level of the button when it's pressed.
For a circuit with a Normally-Open switch and a pull-up
resistor, this parameter will be 0.

Delay:
This is the initial delay before auto-repeat begins.
Example: A Delay value of 50 means a 500 ms auto-repeat
delay.

Rate:
This is the auto-repeat interval.
Example: A Rate value of 10 gives a 100 ms interval,
which means an auto-repeat rate of 10 keys per second.

Bvar:
This an 8-bit variable that must be declared in the main
// program, and it must be initialized to zero. There must
// be a separate variable for each button that you use.
// Example: Declare the variable for pin B0 as int8 B0 = 0;
// See the demo program for more examples. Note that Bvar
// is a "reference variable" (it has a "&" in front of it).
// This is intentional. It's done so the button function
// can directly access the Bvar variable which is declared
// outside the function, without having to pass a pointer
// to it. This keeps the function interface simple.
//====================================

// Action:
// This is the value that's returned when the button is
// pressed. Normally you set this = 1.

// Return value:
// When the button is pressed, the value of the Action
// parameter will be returned. When the button is not
// pressed, the opposite value will be returned.
// Normally, you set the Action parameter = 1, and so
// if the button is pressed, the function will return 1.
// If it's not pressed, it will return 0. So you just
// poll the function every 10 ms in a loop, and check the
// return value with an if() statement. If it's non-zero
// then the button is pressed and you can take appropriate
// action.
*/


//=====================================
// The following macro is used by the Button function.
#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;

// Read the button pin.
pin_value = read_bit_var(pin);

// Check if the button is pressed. It's pressed if the
// pin value is the same as the "downstate". If it's not
// pressed, then zero the Bvar and return "Not pressed".
if(pin_value != downstate)
{
Bvar = 0;
return(!action);
}

// The button is pressed. Check to see if it's a new
// keypress. We can tell if it's a new keypress by
// checking if BVar = 0. If so, load the counter with
// the initial auto-repeat delay and return "Pressed".
// (If the delay has been set to 0, then load a non-zero
// value to allow the function to operate properly).
if(Bvar == 0)
{
if(delay == 0)
Bvar = 255;
else
Bvar = delay;

return(action);
}

// Decrement the auto-repeat counter.
Bvar--;

// Check if we just counted down to 0. If so, then load
// the counter with the auto-repeat interval and return
// "Pressed". If the delay is set to 0 or 255, it means
// that auto-repeat is disabled, so fall through and
// return "Not Pressed".
if(BVar == 0)
{
BVar = rate;

if((delay != 0) && (delay != 255))
return(action);
}

// If the counter is positive, then it means an auto-repeat
// is still pending, so return "Not Pressed".
return(!action);

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 14, 2005 10:31 pm     Reply with quote

You don't have to keep posting the code over and over again.
I posted that code for people who had previously used PicBasicPro
and know how to use it, and just want a quick way to move over
to CCS.

I suggest you start with a simple "Hello World" program, that
just displays text on a terminal window in a PC. Example:

Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//===========================
void main()
{

printf("Hello World");
   
while(1);
}
honeytree



Joined: 13 Nov 2005
Posts: 8

View user's profile Send private message

PostPosted: Mon Nov 14, 2005 10:56 pm     Reply with quote

Thank you for your help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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