pic programming input? - Page 1
Forum section: electronics / general

author: matise
author_rank: Contributor
timestamp: January 25, 2014, 03:09:28 pm
content: 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

author: FrankBuss
author_rank: Unknown
timestamp: January 25, 2014, 04:27:04 pm
content: 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.

author: matise
author_rank: Contributor
timestamp: January 25, 2014, 04:57:35 pm
content: yes of course, bits.Ra... sorry.Tnx for answer, i will try it that way...

author: NadAngel
author_rank: Contributor
timestamp: February 07, 2014, 08:51:41 pm
content: 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 255pin0 = PORTAbits.RA0;Sorry for the late reply.

author: kony
author_rank: Regular Contributor
timestamp: February 10, 2014, 10:26:40 am
content: Quote from: NadAngel on February 07, 2014, 08:51:41 pmNow, 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 supportsbitdatatype. And unsigned integer is 2 bytes long by defalut, FYI.http://ww1.microchip.com/downloads/en/DeviceDoc/52053B.pdfCode:bit pin0; //now only 1 bit wide, as neededpin0= PORTAbits.RA0;

author: mariush
author_rank: Super Contributor
timestamp: February 10, 2014, 10:35:30 am
content: 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.

author: NadAngel
author_rank: Contributor
timestamp: February 12, 2014, 02:15:07 pm
content: Quote from: kony on February 10, 2014, 10:26:40 amThat'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://type declarationtypedef 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 definitiontChar bitData;//access individual bitsbitData.byte=0b11111111;bitData.bits.b5=0;Thanx for the correction on the int size though - don't know what I was thinking:)

author: dannyf
author_rank: Super Contributor
timestamp: February 12, 2014, 10:12:25 pm
content: I generally avoid bitfields: no standard, endien-dependent and not fast.instead, I use bit masks.

author: macboy
author_rank: Super Contributor
timestamp: February 18, 2014, 02:13:14 pm
content: Quote from: NadAngel on February 07, 2014, 08:51:41 pmThis 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.