SE3 PSE Binome2023-7
Manette
Photos
Programmation AVR
Allumage de LEDS
Code 1
#include<avr/io.h> #include<stdio.h> #include<stdlib.h> void config(unsigned int bit, unsigned char port, unsigned int io) { if (io == 1) { if (port == 'B') DDRB |= (io<<bit); if (port == 'C') DDRC |= (io<<bit); if (port == 'D') DDRD |= (io<<bit); } else if (io == 0) { if (port == 'B') DDRB &= ~(1<<bit); if (port == 'C') DDRC &= ~(1<<bit); if (port == 'D') DDRD &= ~(1<<bit); } } void put_bit(unsigned int bit, unsigned char port, unsigned int io) { if (io == 1) { if (port == 'B') PORTB |= (io<<bit); if (port == 'C') PORTC |= (io<<bit); if (port == 'D') PORTD |= (io<<bit); } else if (io == 0) { if (port == 'B') PORTB &= ~(1<<bit); if (port == 'C') PORTC &= ~(1<<bit); if (port == 'D') PORTD &= ~(1<<bit); } } int get_bit(unsigned int bit, unsigned char port) { if (port == 'B') return 1 & (PINB>>bit); if (port == 'C') return 1 & (PINC>>bit); if (port == 'D') return 1 & (PIND>>bit); return -1; } int main(void) { //Setup config(2,'C',1); config(4,'C',1); put_bit(2,'C',1); put_bit(4,'C',1); while (1); return 0; }
Code 2
#include <stdio.h>
#include <stdint.h>
#include <avr/io.h>
#define OUTPUT 0
#define INPUT 1
#define INPUT_PULLUP 2
#define HIGH 1
#define LOW 0
#define PIN_BUTTON1 1
#define PIN_LED1 0 void pinModed(uint8_t pin,uint8_t mode,uint8_t portchoix) {
volatile char *ddr;
if(portchoix == 'B') ddr = &DDRB;
if(portchoix == 'C') ddr = &DDRC;
if(portchoix == 'D') ddr = &DDRD;
volatile char *port;
if(portchoix == 'B') port = &PORTB;
if(portchoix == 'C') port = &PORTC;
if(portchoix == 'D') port = &PORTD; if(mode==OUTPUT) {
*ddr |= (1<<pin);
}
else if(mode==INPUT)
{
*ddr &= ~(1<<pin);
}
else if(mode==INPUT_PULLUP)
{
*ddr &= ~(1<<pin);
*port |= ~(1<<pin);
}
} void digitalWrited(uint8_t pin ,uint8_t valeur ,uint8_t portchoix ) {
volatile char *port;
if(portchoix == 'B') port = &PORTB;
if(portchoix == 'C') port = &PORTC;
if(portchoix == 'D') port = &PORTD; if(valeur == HIGH) {
*port |= (1<<pin);
}
else
{
*port &= ~(1<<pin);
}
} int digitalread(uint8_t pin, uint8_t portchoix) {
volatile char *pinbcd;
if(portchoix == 'B') pinbcd = &PINB;
if(portchoix == 'C') pinbcd = &PINC;
if(portchoix == 'D') pinbcd = &PIND; return *pinbcd & (1<<pin) ? HIGH : LOW ; }
int main(){
pinModed(PC2,OUTPUT,'C');
pinModed(PC4,OUTPUT,'C');
digitalWrited(PC2,HIGH,'C');
digitalWrited(PC4,HIGH,'C');
while(1){}
}