From Bob Gardner in thread
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=41558
One more step:#define LCD_EN_BIT_NUM 0 #define LCD_RS_BIT_NUM 1 #define LCD_EN (1<<LCD_EN_BIT_NUM) #define LCD_RS (1<<LCD_RS_BIT_NUM) #define ENHI() PORTC |= LCD_EN #define ENLO() PORTC &= ~LCD_EN #define ENPULSE() ENHI(); ENLO() //usage PORTD=data; ENPULSE();
I started a new thread because this is OT from the OP in that thread, and think it is more a GCC thing.
I was always concerned with codespace and instructions required with this method. I wasn't sure if the compiler would figgure out to just set or clear that one bit, or if it would actually read the port change the one bit and write the port. So I tried it out and read the dissasembler...
#include#define EN PA7 // Strobe, latch data #define LCD_EN (1<<EN) #define CTRL_PORT PORTA #define ENHI() CTRL_PORT |= LCD_EN #define ENLO() CTRL_PORT &= ~LCD_EN #define ENPULSE() ENHI(); ENLO() int main( void ) { //usage DDRC = 0xFF; ENPULSE(); }
Generated this:
17: ENPULSE(); +0000006B: 9AAF SBI 0x15,7 Set bit in I/O register +0000006C: 98AF CBI 0x15,7 Clear bit in I/O register
That I like, quick and much more atomic than what I get when I use it in the rest of my program. The exact same #defines were used and for some reason it no longer makes use of the CBI instruction.
60: ENPULSE(); +0000033F: 9ADF SBI 0x1B,7 Set bit in I/O register +00000340: E71F LDI R17,0x7F Load immediate +00000341: B38B IN R24,0x1B In from I/O location +00000342: 2381 AND R24,R17 Logical AND +00000343: BB8B OUT 0x1B,R24 Out to I/O location
I could post all the reast of the code in the project, but that would take much space.
Both are compiled at -Os, I have the latest WinAVR and Studio
What is causing this change?
Is there any good way to tell it just to clear the bit, from C, using good programing practice? It would save 3 instructions each time, not that I need more speed or that I'm running out of codespace! AVRs are awsome little devices. There is just a drive in me to minimize codespace and instruction count as long as it doesn't reduce portability or readablity of code.