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

change pins with const map without affect port state[SOLVED]

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



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

change pins with const map without affect port state[SOLVED]
PostPosted: Thu Oct 22, 2015 11:44 am     Reply with quote

First of all, i read here https://www.ccsinfo.com/forum/viewtopic.php?t=54447
http://www.ccsinfo.com/forum/viewtopic.php?t=18949
http://www.ccsinfo.com/forum/viewtopic.php?t=20182
and many other topic, but without success.
I want to change a multiple pins, 2-6 mapped as constant without affect port state. Very simply example:
Code:
BYTE const POSITIONS[4] = {0b01, //01
                           0b11, //11
                           0b10, //10
                           0b00};//00
void test_step(int8 steps)   
{
           static BYTE stepper_state = 0;   
      stepper_state = get_state();
      output_a( POSITIONS[ stepper_state ]);
      
}


or
Code:
void test_step(int8 steps)   
{
      output_a( POSITIONS[ steps ]);   
}

Above code will change the rest of port pins.
Other solution is
Code:
void test_step()   
{
              static BYTE stepper_state = 0;
           switch (stepper_state){
         case 0: output_low(pin_a0);
         output_high(pin_a1);
         break;
         
         case 1: output_high(pin_a0);
         output_high(pin_a1);
         break;
         
         case 2: output_high(pin_a0);
         output_low(pin_a1);
         break;
         
         case 3: output_low(pin_a0);
         output_low(pin_a1);
         break;     
     }   
           stepper_state=(stepper_state+1)&(sizeof(POSITIONS)-1);
}

This code work without change other pins, but i don't want to use, i can't post entire code because is based on PICC example code EX_STEP.C.
But the idea is if i can set a const function just for pin are used avoid output_x() function.


Last edited by wangine on Thu Oct 22, 2015 6:35 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19512

View user's profile Send private message

PostPosted: Thu Oct 22, 2015 1:01 pm     Reply with quote

You do realise, that if you set #use fast_io on your ports, the compiler will _not_ change the port state. It then just becomes a matter of you setting the tris, whenever a state needs to change. output_low(pin_a1), will then code as a single byte instruction....
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Thu Oct 22, 2015 2:30 pm     Reply with quote

yes but the fast_io don't matter if i set individual pin as single byte instruction, the rest of pins will don't change anyway. What i want is to make a constant byte as 01 or 0110 or 010101 like a constant map to access like a function. I made that along time ago but i just don't remember, also i can't find in all my projects. Something like that:

Code:

#define test (PORTA.ra0,1 & PORTA.ra1,1) // same as porta = 3
 byte const [1] = {test};

and because test is not a constant and it cant use or compile. Is difficult to explain, my english is poor, but i think you understand.
Ttelmah



Joined: 11 Mar 2010
Posts: 19512

View user's profile Send private message

PostPosted: Thu Oct 22, 2015 3:01 pm     Reply with quote

The rest of the pins _will_ change.

There are two instructions, to talk to the ports. A _byte_ wide one, that updates all eight pins, and a _bit_ one, that updates just one.

If you use the byte instruction, you set all eight pins.

What you can do, is read the port, 'or' this with the bits you want to set, and write the byte back. All eight will though be written.
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Thu Oct 22, 2015 3:17 pm     Reply with quote

yes i know, it is a solution to read port and use a mask and resend data, is more instructions like than simply set each byte separately. I think i will use a 74HC595 to make that more simply. Thanks for your time.
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Thu Oct 22, 2015 6:33 pm     Reply with quote

i found in my old projects the solution, but now looks like empiric Shocked but working without affect port state. Function its simple with 2 variable, numbers of pins to be writen and data (variable , constant , define dont matter)
Code:

#define pin_1 pin_a0
#define pin_2 pin_a1
#define pin_3 pin_a2
#define pin_4 pin_a3

BYTE const xPOSITIONS[4] = {0b01, //01
                           0b11, //11
                           0b10, //10
                           0b00};//00

void write_port(int8 pins_to_write, int8 data)
{
    unsigned int8 i = 0;
   
    for(i = 0; i < pins_to_write; i++)
        {
        switch (i){
            case 0:
                if(data & pins_to_write)       
                    output_high(pin_1); 
                 else
                    output_low(pin_1);
                break;
            case 1:
                if(data & pins_to_write)       
                    output_high(pin_2); 
                 else
                    output_low(pin_2);
                break;
            case 2:
                if(data & pins_to_write)       
                    output_high(pin_3); 
                 else
                    output_low(pin_3);
                break;
            case 3:
                if(data & pins_to_write)       
                    output_high(pin_4); 
                 else
                    output_low(pin_4);
                break;
                     }
    data <<= 1; //next to MSB
        }
}

and acces like that
Code:
write_port(2, xPOSITIONS[ stepper_state ]);

Function work for maxim 4 pins , can made more but without exced 7 , otherwise no point ,output_x(), is enouth for entire port.
If someone have beter ideea, opinions, are always wellcome, i will move in code library , so many simply and insignifiant functions are lost here and for some reasons when search entire forum its imposible to find or takes to long
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