Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // structure to allow bit field operations, name conversions: PORTA.0 -> PORT_A.b0 PORTB.7 -> PORT_B.b7 typedef struct{ uint8_t b0:1; uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1; uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; } bits; // define all the ports of your microcontroller #define PORT_A (* (volatile bits *) &PORTA) #define PIN_A (* (volatile bits *) &PINA) #define DDR_A (* (volatile bits *) &DDRA) #define PORT_B (* (volatile bits *) &PORTB) #define PIN_B (* (volatile bits *) &PINB) #define DDR_B (* (volatile bits *) &DDRB) // then you can use specific bits like this to write or read specific bits PORT_A.b0=1; // write value to PORTA bit 0 PIN_B.b4=0; // write value to PINB bit 4
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // add this defines #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT)) #define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) #define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT)) #define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) #define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) (CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT)) // and then you can use SETBIT(PORTX,2); // set bit2 of portx (set to 1) SETBIT(PORTY,0); // set bit0 of porty CLEARBIT(PORTX,5); // clear bit5 of portx (set to 0) FLIPBIT(PORTX,2); // invert bit2 of portX, if it was 1 it becomes 0, and if 0 becomes 1 // example for portx 0x10101010 the result is 0x10101110 if (CHECKBIT(PORTX,4)) {} else {} // result is true if bit4 of portx is 1, false if it is 0 if (!CHECKBIT(PORTX,4)) {} else {} // result is true if bit4 of portx is 0, false if it is 1 WRITEBIT(PORTX,0,PORTY,2); // copy the bit0 of portx to bit2 of porty
Code C - [expand] 1 2 if(bit_is_set(PINC,0)) //this can check if a bit is 1 if(bit_is_clear(PINC,0)) // this can check if a bit is 0
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 typedef struct{ uint8_t b0:1; uint8_t bit1:1; uint8_t bit2:1; uint8_t bit3:1; uint8_t bit4:1; uint8_t bit5:1; uint8_t bit6:1; uint8_t bit7:1; } bits; // define all the ports of your microcontroller #define APORT (* (volatile bits *) &PORTA) #define APIN (* (volatile bits *) &PINA) #define ADDR (* (volatile bits *) &DDRA)
Code C - [expand] 1 2 3 4 5 6 7 8 9 if (PIN_B.b4==0) PORTD=0xFF; //works fine in the AVRstudio debbuger if bit_is_clear(PORTC,0) {}; // you haven't added the code to be executed when this is true PORTA=0xFF; // this is always executed // you probably wanted to do if bit_is_clear(PORTC,0) {PORTA=0xFF;}; //or if bit_is_clear(PORTC,0) PORTA=0xFF;
As i said in the previous post you can't use this way with AVRstudio, and this doesn't compile in my AVRstudio4, I get an error ../test3.c:36: error: called object '1' is not a functionWhen i use the below code and try it doesnt give a output.The best part is that the compiler neither shows an error.
if PINC.0==1
PORTB=0xff;
i am not sure if the PINC.0 statement is right .plz help...
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT)) #define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) #define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) if CHECKBIT(PINC,1) // check if bit1 of portC is 1, or use !CHECKBIT(PINC,1) to check if it is 0 { SETBIT(PORTA,7); // I assume that you want to set one bit only and not all bits of portA } else { CLEARBIT(PORTA,7); }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?