Files
42_EXT_03_42chips/module00/ex01/main.c
hugogogo 37325f2b16 fix ex01
2025-03-04 13:25:50 +01:00

85 lines
2.5 KiB
C

#include <avr/io.h>
// #include <util/delay.h>
#define SET(REGISTER, BIT) REGISTER |= 1 << BIT
#define CLEAR(REGISTER, BIT) REGISTER &= ~(1 << BIT)
#define TEST(REGISTER, BIT) REGISTER & 1 << BIT
#define TOGGLE(REGISTER, BIT) REGISTER ^= 1 << BIT
#define D1 0
#define D2 1
#define D3 2
#define D4 4
int main()
{
// registers :
// DDRx : (Data Direction Register) Controls whether each pin is an input or output
// - 1 = output (pull-up off)
// - 0 = input
// PORTx : (Port Data Register)
// if DDRx = 1 (output mode) : Controls the output state of a pin when it's configured as an output
// - 1 = HIGH (5V?) (VCC)
// - 0 = LOW (0V) (GND)
// if DDRx = 0 (input mode) : Controls the internal pull-up resistor when the pin is an input
// - 1 = pull-up on
// - 0 = pull-up off (pin floats)
// PINx : (Pin Input Register) Reads the current logical state of a pin
// Reflects the actual voltage on the pin
// Special feature: Writing 1 to PINxn toggles the corresponding PORTxn bit (regardless of DDRx)
// turns on led 1
SET(DDRB, D1); // make PB0 as OUTPUT (pullup is off)
SET(PORTB, D1); // make PB0 as HIGH (LED turns ON)
// SET(DDRB, D2); // make PB1 as OUTPUT (pullup is off)
// CLEAR(PORTB, D2); // make PB1 as LOW (LED turns OFF)
// CLEAR(DDRB, D3); // make PB2 as INPUT
// SET(PORTB, D3); // make PB2 pullup on
// CLEAR(DDRB, D4); // make PB4 as INPUT
// CLEAR(PORTB, D4); // make PB4 pullup off
////////////////////////////////
// TEST weird behavior of PIN register :
// in initial tests, using PIN register to toggle ON two leds only worked for the second one
// after some tests, this behavior is not anymore
//
// test 1 : only led 3 turns on
// SET(DDRB, D1);
// CLEAR(PORTB, D1);
// SET(PINB, D1);
// SET(DDRB, D3);
// CLEAR(PORTB, D3);
// SET(PINB, D3);
//
// test 2 : only led 3 turns on
// SET(DDRB, D1);
// CLEAR(PORTB, D1);
// SET(DDRB, D3);
// CLEAR(PORTB, D3);
// SET(PINB, D1);
// SET(PINB, D3);
//
// test 3 : both leds turns on
// SET(DDRB, D1);
// CLEAR(PORTB, D1);
// SET(DDRB, D3);
// CLEAR(PORTB, D3);
// PINB = (1 << D1) | (1 << D3);
//
// test 4 : both leds turns on
// SET(DDRB, D1);
// CLEAR(PORTB, D1);
// SET(DDRB, D3);
// CLEAR(PORTB, D3);
// SET(PINB, D1);
// _delay_us(500);
// SET(PINB, D3);
//
////////////////////////////////
return 0;
}