Guest User

I2C_ATXMega32_Test

a guest
Sep 28th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <avr/interrupt.h>
  2. #include <util/delay.h>
  3. #include <avr/io.h>
  4. #include "avr_compiler.h"
  5. #include "twi_master_driver.h"
  6. #include "twi_slave_driver.h"
  7.  
  8.  
  9. //Kommentare mit ! aus Atmel Beispielcode
  10.  
  11. /*! Defining slave address. */
  12. #define SLAVE_ADDRESS    0x69 //Adresse des MPU6050, womöglich linksshift nötig (0x69 >> 1)
  13.  
  14. /*! CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings */
  15. #define CPU_SPEED   32000000
  16. #define BAUDRATE    100000
  17. #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)
  18.  
  19. /* Global variables */
  20. TWI_Master_t twiMaster;    /*!< TWI master module. */
  21.  
  22. /*! Defining number of bytes in buffer. */
  23. #define NUM_BYTES   2
  24. uint8_t sendBuffer[NUM_BYTES];
  25.  
  26. /* TWIE Master Interrupt vector. */
  27. ISR(TWIE_TWIM_vect)
  28. {
  29.     TWI_MasterInterruptHandler(&twiMaster);
  30. }
  31.  
  32. void clock_init(void)
  33. {
  34.     OSC.CTRL |= OSC_RC32MEN_bm; //aktiviert internen 32MHz Oszillator
  35.     while(!(OSC.STATUS & OSC_RC32MRDY_bm)); //warte bis der Oszillator stabil läuft
  36.     CCP = CCP_IOREG_gc; //deaktiviert den Interrupt für 4 Taktzyklen -> Schutz
  37.     CLK.CTRL = 0x01;    //Wähle den Oszillator als Taktquelle aus
  38. }
  39.  
  40. void ports_init(void)
  41. {
  42.     //Ausgänge/Eingänge definieren
  43.     PORTA.DIR |= (1<<PIN5);     //blau an Motor 1 = LED1= PORTA.DIR = 0x20;
  44.     PORTC.DIR |= (1<<PIN6);     //rot an Motor 2 = LED2
  45.     PORTC.DIR |= (1<<PIN7);     //rot an Motor 3 = LED3
  46.     PORTE.DIR |= (1<<PIN2);     //blau an Motor 4 = LED4
  47.  
  48.     //TWI Interface an PORTE initialisieren
  49.     PORTE.DIRSET   |= PIN0_bm;          // Als Ausgang  TWI SDA     // TWI am PortE initialisiert
  50.     PORTE.DIRSET   |= PIN1_bm;          // Als Ausgang  TWI SCL     // TWI am PortE initialisiert
  51. }
  52.  
  53. int main (void)
  54. {
  55.     //Initialisierungen
  56.     clock_init();
  57.     ports_init();
  58.     TWI_MasterInit(&twiMaster, &TWIE, TWI_MASTER_INTLVL_HI_gc, TWI_BAUDSETTING);    // TWI am PortE initialisiert
  59.    
  60.     //Interruptlevel freigeben
  61.     PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
  62.     sei();
  63.    
  64.     sendBuffer[0] = 0x75;
  65.     sendBuffer[1] = 0x25;
  66.    
  67.     uint8_t val1 = 0x00;
  68.  
  69.     if (TWI_MasterWriteRead(&twiMaster, SLAVE_ADDRESS, &sendBuffer[0], 1, 1)) {
  70.         PORTA.OUTSET |= (1<<PIN5);      //blau an Motor 1 = LED1 -> Kommunikation hat funktioniert
  71.     }
  72.    
  73.     val1 = (twiMaster.readData[0]);
  74.  
  75.     _delay_ms(1);
  76.    
  77.     //Hauptschleife
  78.     while(1)
  79.     {
  80.         _delay_ms(1);
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment