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 CCS Technical Support

replacement output_low/high(VARIABLE) code issues...

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







replacement output_low/high(VARIABLE) code issues...
PostPosted: Thu Jan 05, 2006 9:48 pm     Reply with quote

Hi all,

I was looking for a way to replace the "output_x(CONST)" methods with something that can take a variable. I was given code to do this by someone from this board (thanks if I forgot to thank you).

I have changed the code to make work for the pic18f452 chip (possibly all the 18's?).

I have expanded the one method to 2, where one is for specific pin changes, and one that is for the whole port. Saves me from goiing thru each pin, and instead just providing a 8 bit value to be ALL the pins for a given port.

The issue that I am having is that when I do a PORT update (all 8 bits), the highest bit, RC7, in my case is ALWAYS active/on. If I do a single pin deactive of RC7 RIGHT after the port update it's okay....

Here's the code...

Code:


#include <18F452.h>

#device *=16
#device adc=10
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 4000000)
//#use rs232(stream=SERIAL, baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)
//#zero_ram
#case
#fill_rom 0xdead
#id   checksum

#define PORT_A  PIN_A0
#define PORT_B  PIN_B0
#define PORT_C  PIN_C0
#define PORT_D  PIN_D0
#define PORT_E  PIN_E0
#define TRIS_OFFSET 0x12

// 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 addreses 0xf80 ~ 0xf84, and the
// corresponding TRIS registers are known to be at
// 0xf92 ~ f96, respectively.
int8 do_pin_io(int16 ccs_pin, int8 action)
{
    int16 io_port;
    int16 io_tris;

    int16 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+TRIS_OFFSET;            // 0x12 is the offset

    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 do_port_io(int16 port, unsigned int8 value)
{
    unsigned int16 io_port;
    unsigned int16 io_tris;

    io_port=port >> 3;                 // Get the i/o port address     
    io_tris=io_port+TRIS_OFFSET;       // 0x12 is the offset

    *io_tris = ~value;
    *io_port = value;   
}

void testLEDs()
{
    int digit;
    int LEDCount=8; // base 1
    int8 x;
    int8 y=0;
    //                     1   2    3    4    5    6    7    8
    int8 const mask[] = {0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f};

    setup_adc_ports(NO_ANALOGS);

    while(1)
    {
        for(x=0; x<LEDCount; x++)
        {
            do_port_io(PORT_C, mask[x]);    // Set the segments
//            do_pin_io(PIN_C7, 0);           // unless I do this, C7 is ALWAYS active!
            do_pin_io(PIN_D0+x, 0);         // turn on digit (using Common Cathode LED's)
            delay_ms(500);                 
            do_pin_io(PIN_D0+x, 1);         // turn off digit
        }
    }
}


void main()           
{
    testLEDs();

#ignore_warnings 203
    while(1);
#ignore_warnings none
}



Any thoughts? Replacement code?

~Kam (^8*
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