Author Topic: pic programming input?  (Read 4598 times)

0 Members and 1 Guest are viewing this topic.

Offline matiseTopic starter

  • Contributor
  • Posts: 28
pic programming input?
« on: January 25, 2014, 03:09:28 pm »
if i have an input like PORTA.bitsRA0=1;
And want to "save" digital inputs on lets say;
int pin0;
Is it just to write the code like this?

pin0 = PORTA.bitsRA0;   

Or is it Another way to do it?  :)

tnx
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: pic programming input?
« Reply #1 on: January 25, 2014, 04:27:04 pm »
I guess you mean PORTAbits.RA0, but yes, this should work. The direction register is input by default, so no need to do anything for it, but if it is one of those PICs with analog inputs, usually you have to configure it for digital input (ANSEL register), because default is analog input.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline matiseTopic starter

  • Contributor
  • Posts: 28
Re: pic programming input?
« Reply #2 on: January 25, 2014, 04:57:35 pm »
yes of course, bits.Ra...  sorry.

Tnx for answer, i will try it that way...  :)
 

Offline NadAngel

  • Contributor
  • Posts: 12
  • Country: si
Re: pic programming input?
« Reply #3 on: February 07, 2014, 08:51:41 pm »
When doing embedded programming you should conserve your resources and use best programming practises. So if you would only like to save the state of one input, which is represented with a bit (0 or 1), you don't need an integer which takes 4 bytes of RAM. Instead, you only need a char variable which takes only one byte. In fact, you can save the state of the whole PORTA in one unsigned char variable.

unsigned char pin0; //0 to 255

pin0 = PORTAbits.RA0; 


Sorry for the late reply.
 

Offline kony

  • Regular Contributor
  • *
  • Posts: 242
  • Country: cz
Re: pic programming input?
« Reply #4 on: February 10, 2014, 10:26:40 am »
When doing embedded programming you should conserve your resources and use best programming practises. So if you would only like to save the state of one input, which is represented with a bit (0 or 1), you don't need an integer which takes 4 bytes of RAM. Instead, you only need a char variable which takes only one byte. In fact, you can save the state of the whole PORTA in one unsigned char variable.

unsigned char pin0; //0 to 255

pin0 = PORTAbits.RA0; 

Now, this is still waste of 7 bits of RAM ! I can't imagine how do I would fit in smaller 16F devices, when doing all flags and bit variables this way.
XC8 compiler supports bit datatype. And unsigned integer is 2 bytes long by defalut, FYI.
http://ww1.microchip.com/downloads/en/DeviceDoc/52053B.pdf

Code: [Select]
bit pin0; //now only 1 bit wide, as needed

pin0= PORTAbits.RA0;
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5029
  • Country: ro
  • .
Re: pic programming input?
« Reply #5 on: February 10, 2014, 10:35:30 am »
page 145 in that datasheet.  8 "bit" variables are packed into an "unsigned char". Each time you assign or read value from a "bit" variable, several instructions are executed. This takes more time and produces a slightly larger binary compared to just using "unsigned char" variable from the start, which may hurt if the pic has low memory for programs.
 
It only makes sense to use the "bit" type  if he has more than one or two "bit" variables.
 

Offline NadAngel

  • Contributor
  • Posts: 12
  • Country: si
Re: pic programming input?
« Reply #6 on: February 12, 2014, 02:15:07 pm »
When doing embedded programming you should conserve your resources and use best programming practises. So if you would only like to save the state of one input, which is represented with a bit (0 or 1), you don't need an integer which takes 4 bytes of RAM. Instead, you only need a char variable which takes only one byte. In fact, you can save the state of the whole PORTA in one unsigned char variable.

unsigned char pin0; //0 to 255

pin0 = PORTAbits.RA0; 

Now, this is still waste of 7 bits of RAM ! I can't imagine how do I would fit in smaller 16F devices, when doing all flags and bit variables this way.
XC8 compiler supports bit datatype. And unsigned integer is 2 bytes long by defalut, FYI.
http://ww1.microchip.com/downloads/en/DeviceDoc/52053B.pdf

Code: [Select]
bit pin0; //now only 1 bit wide, as needed

pin0= PORTAbits.RA0;

That's why I said you can save the state of the whole PORTA register in a single unsigned char variable. You can later access the individual bits by macros or use a type previously defined to access individual bits. There is a nice paragraph on this subject on page 51 of the xc8 user's guide. I prefer to use the type union method to access bits:

Code: [Select]
//type declaration
typedef union {
    unsigned char byte;
    struct {
        unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
    }bits;
} tChar;
//variable definition
tChar bitData;

//access individual bits
bitData.byte=0b11111111;
bitData.bits.b5=0;

Thanx for the correction on the int size though - don't know what I was thinking:)
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: pic programming input?
« Reply #7 on: February 12, 2014, 10:12:25 pm »
I generally avoid bitfields: no standard, endien-dependent and not fast.

instead, I use bit masks.
================================
https://dannyelectronics.wordpress.com/
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2254
  • Country: ca
Re: pic programming input?
« Reply #8 on: February 18, 2014, 02:13:14 pm »
When doing embedded programming you should conserve your resources and use best programming practises. So if you would only like to save the state of one input, which is represented with a bit (0 or 1), you don't need an integer which takes 4 bytes of RAM. Instead, you only need a char variable which takes only one byte. In fact, you can save the state of the whole PORTA in one unsigned char variable.

unsigned char pin0; //0 to 255

pin0 = PORTAbits.RA0; 


Sorry for the late reply.
This is a little pedantic of me, but for both PICC18 and XC8 compilers for 8 bit micros, an "int" is 16 bits (2 bytes), not 32 bits.
You point is still completely valid though, don't use a bigger data type, especially since setting a multi-byte data type on a 8 bit micro requires extra instructions (wasting time and FLASH space) in addition to extra RAM.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf