PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 25, 2006 2:19 pm |
|
|
Here is the 18F version, with a demo program that shows how to
call the functions.
Code: | #include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
// do_pin_io() --
// This function will perform an i/o action on a CCS pin.
// The ccs_pin parameter must be a value defined in the .H
// file for your PIC, such as PIN_B0, PIN_C5, etc.
// The CCS pin value and state can be passed in variables.
//
// The action must be as follows:
// Action = 0: Set the specified pin = 0
// Action = 1: Set the specified pin = 1
// Action = 2: Read the specified pin and return
// its value (0 or 1).
// Any action value > 2 will be treated as 2. For actions
// 0 and 1, the return value has no meaning (but 0 is
// returned).
// This function only works for the 18F series PICs, in
// which the i/o ports are at addresses 0xF80 to 0xF84,
// and the corresponding TRIS registers are known to be
// at 0xF92 to 0xF96, respectively.
#define SET_LOW 0
#define SET_HIGH 1
#define READ_PIN 2
int8 do_pin_io(int16 ccs_pin, int8 action)
{
int16 io_port;
int16 io_tris;
int8 bitmask;
int8 retval;
int8 const bitmask_table[] =
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
retval = 0;
io_port = ccs_pin >> 3; // Get the i/o port address
bitmask = bitmask_table[ccs_pin & 7]; // get mask
io_tris = io_port + 0x12; // Get TRIS register address
if(action < 2) // Is this an output action ?
{
*(io_tris) &= ~bitmask; // Set TRIS = output
if(action)
{
*io_port |= bitmask; // Set pin = 1
}
else
{
*io_port &= ~bitmask; // Set pin = 0
}
}
else // If not, we will read the pin (action = 2)
{
*io_tris |= bitmask; // Set TRIS = input
retval = !!(*io_port & bitmask); // Read pin (ret. 0 or 1)
}
return(retval); // This only has meaning if action = 2
}
//=========================================
void main()
{
int8 value_read;
// Blink an LED on pin B0. Also read pin B1 and
// display the value read.
while(1)
{
do_pin_io(PIN_B0, SET_LOW);
delay_ms(500);
do_pin_io(PIN_B0, SET_HIGH);
delay_ms(500);
value_read = do_pin_io(PIN_B1, READ_PIN);
printf("%u ", value_read);
}
} |
|
|