Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <avr/io.h>
- #include "avr_compiler.h"
- #include "twi_master_driver.h"
- #include "twi_slave_driver.h"
- //Kommentare mit ! aus Atmel Beispielcode
- /*! Defining slave address. */
- #define SLAVE_ADDRESS 0x69 //Adresse des MPU6050, womöglich linksshift nötig (0x69 >> 1)
- /*! CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings */
- #define CPU_SPEED 32000000
- #define BAUDRATE 100000
- #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)
- /* Global variables */
- TWI_Master_t twiMaster; /*!< TWI master module. */
- /*! Defining number of bytes in buffer. */
- #define NUM_BYTES 2
- uint8_t sendBuffer[NUM_BYTES];
- /* TWIE Master Interrupt vector. */
- ISR(TWIE_TWIM_vect)
- {
- TWI_MasterInterruptHandler(&twiMaster);
- }
- void clock_init(void)
- {
- OSC.CTRL |= OSC_RC32MEN_bm; //aktiviert internen 32MHz Oszillator
- while(!(OSC.STATUS & OSC_RC32MRDY_bm)); //warte bis der Oszillator stabil läuft
- CCP = CCP_IOREG_gc; //deaktiviert den Interrupt für 4 Taktzyklen -> Schutz
- CLK.CTRL = 0x01; //Wähle den Oszillator als Taktquelle aus
- }
- void ports_init(void)
- {
- //Ausgänge/Eingänge definieren
- PORTA.DIR |= (1<<PIN5); //blau an Motor 1 = LED1= PORTA.DIR = 0x20;
- PORTC.DIR |= (1<<PIN6); //rot an Motor 2 = LED2
- PORTC.DIR |= (1<<PIN7); //rot an Motor 3 = LED3
- PORTE.DIR |= (1<<PIN2); //blau an Motor 4 = LED4
- //TWI Interface an PORTE initialisieren
- PORTE.DIRSET |= PIN0_bm; // Als Ausgang TWI SDA // TWI am PortE initialisiert
- PORTE.DIRSET |= PIN1_bm; // Als Ausgang TWI SCL // TWI am PortE initialisiert
- }
- int main (void)
- {
- //Initialisierungen
- clock_init();
- ports_init();
- TWI_MasterInit(&twiMaster, &TWIE, TWI_MASTER_INTLVL_HI_gc, TWI_BAUDSETTING); // TWI am PortE initialisiert
- //Interruptlevel freigeben
- PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
- sei();
- sendBuffer[0] = 0x75;
- sendBuffer[1] = 0x25;
- uint8_t val1 = 0x00;
- if (TWI_MasterWriteRead(&twiMaster, SLAVE_ADDRESS, &sendBuffer[0], 1, 1)) {
- PORTA.OUTSET |= (1<<PIN5); //blau an Motor 1 = LED1 -> Kommunikation hat funktioniert
- }
- val1 = (twiMaster.readData[0]);
- _delay_ms(1);
- //Hauptschleife
- while(1)
- {
- _delay_ms(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment