Quantcast
Channel: Microcontrollers
Viewing all articles
Browse latest Browse all 221097

Forum Post: RE: TM4C123GH6PM: Read and Writing from Individual Pins

$
0
0
simple: 1) write a set of macros that set / clear a pin, or read a pin: IO_SET(port, pin), IO_CLR(port, pin) and IO_GET(port, pin), or whatever names you may wish to have. 2) write a function that writes to those two ports and another to read it back, like this: //write a number void dat_write(uint8_t dat) { if (dat & (1<<0)) IO_SET(PORT1, PIN0); else IO_CLR(PORT1, PIN0); if (dat & (1<<1)) IO_SET(PORT1, PIN1); else IO_CLR(PORT1, PIN1); ... if (dat & (1<<6)) IO_SET(PORT2, PIN6); else IO_CLR(PORT2, PIN6); if (dat & (1<<7)) IO_SET(PORT2, PIN7); else IO_CLR(PORT2, PIN7); } //read a number uint8_t dat_read(void) { uint8_t tmp=0; if (IO_GET(PORT1, PIN0)) tmp |= (1<<0); if (IO_GET(PORT1, PIN1)) tmp |= (1<<1); ... if (IO_GET(PORT2, PIN6)) tmp |= (1<<6); if (IO_GET(PORT2, PIN7)) tmp |= (1<<7); return tmp; } you can implement IO_SET(), IO_CLR() and IO_GET() macros however you wish, depending on the framework you are coding within. the code has two advantages: 1) it is hardware independent: should you move it to a different platform, simply link in the appropriate IO_SET(), IO_CLR() and IO_GET() macros and you are ready to go; 2) it is inherently consistent: if you wish to change pin / port assignment, simply modify the definitions of PORT1/2, or PIN0..7, hit recompile and you know that your code will work. anything more complicated than that, you are doing something wrong.

Viewing all articles
Browse latest Browse all 221097

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>