Better easyavr

This commit is contained in:
MultiMote 2015-08-07 11:28:31 +03:00
parent 96208e9b2b
commit 451924250c
2 changed files with 23 additions and 28 deletions

View File

@ -4,53 +4,47 @@
// Sets pin of port to 1 // Sets pin of port to 1
// //
// Example for PD2: PIN_ON(D, 2) // Example for PD2: PIN_ON(PORTD, 2)
#define PIN_ON(port,pin) ((PORT ## port) |= _BV(P ## port ## pin)) #define PIN_ON(port,pin) ((port) |= (1 << (pin)))
// Sets pin of port to 0 // Sets pin of port to 0
// //
// Example for PD2: PIN_OFF(D, 2) // Example for PD2: PIN_OFF(PORTD, 2)
#define PIN_OFF(port,pin) ((PORT ## port) &= ~_BV(P ## port ## pin)) #define PIN_OFF(port,pin) ((port) &= ~(1 << (pin)))
// Sets pin of port to value // Sets pin of port to value
// //
// Example for PD2: PIN_SET(D, 2, 1) // Example for PD2: PIN_SET(PORTD, 2, 1)
#define PIN_SET(port,pin,val) (val > 0 ? PIN_ON(port,pin) : PIN_OFF(port,pin)) #define PIN_SET(port,pin,val) (((val) > 0) ? PIN_ON((port),(pin)) : PIN_OFF((port),(pin)))
// Sets pins of port by bitmask
//
// Example for PORTD: PORT_MASK(D, 0b00001111)
#define PORT_SET_MASK(port,mask) ((PORT ## port) = mask)
// Sets all of port pins to OUTPUT mode // Sets all of port pins to OUTPUT mode
// //
// Example for PORTD: PORT_AS_OUTPUT(D) // Example for PORTD: PORT_AS_OUTPUT(PORTD)
#define PORT_AS_OUTPUT(port) ((DDR ## port) = 0xFF) #define PORT_AS_OUTPUT(port) ((port) = 0xFF)
// Sets all of port pins to INPUT mode // Sets all of port pins to INPUT mode
// //
// Example for PORTD: PORT_AS_INPUT(D) // Example for PORTD: PORT_AS_INPUT(PORTD)
#define PORT_AS_INPUT(port) ((DDR ## port) = 0x00) #define PORT_AS_INPUT(port) ((port) = 0x00)
// Sets pin of port to OUTPUT mode // Sets pin of port to OUTPUT mode
// //
// Example for PD1: PORT_AS_OUTPUT(D, 1) // Example for PD1: PORT_AS_OUTPUT(DDRD, 1)
#define PIN_AS_OUTPUT(port,pin) ((DDR ## port) |= _BV(P ## port ## pin)) #define PIN_AS_OUTPUT(ddr,pin) ((ddr) |= (1 << (pin)))
// Sets pin of port to INPUT mode // Sets pin of port to INPUT mode
// //
// Example for PD1: PORT_AS_INPUT(D, 1) // Example for PD1: PORT_AS_INPUT(DDRD, 1)
#define PIN_AS_INPUT(port,pin) ((DDR ## port) &= ~_BV(P ## port ## pin)) #define PIN_AS_INPUT(ddr,pin) ((ddr) &= ~(1 << (pin)))
// Checks pin's value of port // Checks pin's value of port
// Returns 1 or 0 // Returns 1 or 0
// //
// Example for PORTD: CHECK_PIN(D, 2) // Example for PD2: CHECK_PIN(PIND, 2)
#define CHECK_PIN(port,pin) (((PIN ## port) & (1 << (PIN ## port ## pin))) != 0) #define CHECK_PIN(pinreg,pin) (((pinreg) & (1 << (pin))) != 0)
#endif #endif

13
main.c
View File

@ -2,17 +2,18 @@
#include <util/delay.h> #include <util/delay.h>
#include "easyavr.h" #include "easyavr.h"
#define LED_PORT PORTD
#define LED_PIN 0
#define LED_DDR DDRD
int main(void) int main(void)
{ {
DDRD = 0xff; PIN_AS_OUTPUT(LED_DDR, LED_PIN);
while (1) { while (1) {
PIN_ON(D, 0); PIN_ON(LED_PORT, LED_PIN);
_delay_ms(100); _delay_ms(100);
PIN_OFF(D, 0); PIN_OFF(LED_PORT, LED_PIN);
_delay_ms(100); _delay_ms(100);
} }
} }