We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I want to use port 2 of 167 on MCB net board for two peripherals, one for keyboard interface and other for lcd interface. Please suggest some code on how to access the same port in parts. regards Rajesh
Hi Mike Thanks for the suggestion. I would like to know if there is a way to give names to parts of the part. For example P2.0 to P2.7 should be called as LCD and P2.8 to P2.15 should be called as KBD regards Rajesh
To the best of my knowledge there is no way to have a sort of separate variable for writing to a part of a port. Using bytewide write to a port clears the second byte, so it's useless (but using bytewide read from a port, on the other hand, could be quite useful). It seems to me that all that's left is macros and functions. By the way, maybe there is another way of writing to a port. You could create the following sequence of instructions somewhere in RAM: BFLDL reg(=P2), mask(=0xFF), data RET After that, you could fill the 'data' field of the BFLDL instruction and call that sequence to write to the lower byte of the port. Compared to this, the AND-OR approach has one disadvantage: the AND-OR sequence must not be interrupted by other port-accessing code because it would corrupt some of the data written to the port. Everything becomes simple if P2.0-P2.7 are configured as outputs and P2.8-P2.15 as inputs. In this case it's possible to use bytewide access: #define LCD MVAR(unsigned char, (unsigned char*)&P2) #define KBD MVAR(unsigned char, (unsigned char*)&P2 + 1) Sorry if I tried to complicate things. Good luck! Mike
I would like to know if there is a way to give names to parts of the part. Sure. This is what the MACRO capability of the C Programming Language is useful for. For example:
#define SET_LCD(x) {P2 = (P2 & 0xFF00) | ((x) & 0x00FF);} #define SET_KBD(x) {P2 = (P2 & 0x00FF) | (((x) & 0x00FF) << 8);} #define READ_LCD (P2 & 0x00FF) #define READ_KBD ((P2 & >> 8) & 0x00FF)
Hi Jon Thanks for the suggestion. It has definitely helped me. regards Rajesh
Hi Mike Thanks for the suggestion. But it surely sounds complicated for a newbie like me. I will stick to the previous suggestion. It has definitely helped me. regards Rajesh