Advertisement
Guest User

I2C_ATXMega32_Test

a guest
Sep 26th, 2012
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.60 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. uint32_t pwm_motor = 0x0000; //entspricht Drehzahl
  9.  
  10. //Kommentare mit ! aus Atmel Beispielcode
  11.  
  12. /*! Defining slave address. */
  13. #define SLAVE_ADDRESS    0x55 //nur Beispiel
  14.  
  15. /*! Defining number of bytes in buffer. */
  16. #define NUM_BYTES        8
  17.  
  18. /*! CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings */
  19. #define CPU_SPEED   32000000
  20. #define BAUDRATE    100000
  21. #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)
  22.  
  23. /* Global variables */
  24. TWI_Master_t twiMaster;    /*!< TWI master module. */
  25. TWI_Slave_t twiSlave;      /*!< TWI slave module. */
  26.  
  27. /*! Buffer with test data to send.*/
  28. uint8_t sendBuffer[NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD};
  29.  
  30. /*! Simple function that invert the received value in the sendbuffer. This
  31.  *  function is used in the driver and passed on as a pointer to the driver.
  32.  */
  33. void TWIC_SlaveProcessData(void)
  34. {
  35.     uint8_t bufIndex = twiSlave.bytesReceived;
  36.     twiSlave.sendData[bufIndex] = (~twiSlave.receivedData[bufIndex]);
  37. }
  38.  
  39. /* TWIC Master Interrupt vector. */
  40. ISR(TWIC_TWIM_vect)
  41. {
  42.     TWI_MasterInterruptHandler(&twiMaster);
  43. }
  44.  
  45. /*! TWIC Slave Interrupt vector. */
  46. ISR(TWIC_TWIS_vect)
  47. {
  48.     TWI_SlaveInterruptHandler(&twiSlave);
  49. }
  50.  
  51. void clock_init(void)
  52. {
  53.     OSC.CTRL |= OSC_RC32MEN_bm; //aktiviert internen 32MHz Oszillator
  54.     while(!(OSC.STATUS & OSC_RC32MRDY_bm)); //warte bis der Oszillator stabil läuft
  55.     CCP = CCP_IOREG_gc; //deaktiviert den Interrupt für 4 Taktzyklen -> Schutz
  56.     CLK.CTRL = 0x01;    //Wähle den Oszillator als Taktquelle aus
  57. }
  58.  
  59. void ports_init(void)
  60. {
  61.     //Ausgänge/Eingänge definieren
  62.     PORTA.DIR |= (1<<PIN5);     //blau an Motor 1 = LED1= PORTA.DIR = 0x20;
  63.     PORTC.DIR |= (1<<PIN6);     //rot an Motor 2 = LED2
  64.     PORTC.DIR |= (1<<PIN7);     //rot an Motor 3 = LED3
  65.     PORTE.DIR |= (1<<PIN2);     //blau an Motor 4 = LED4
  66.    
  67.     PORTC.DIR |= (1<<PIN0);     //Motor 1
  68.     PORTC.DIR |= (1<<PIN1);     //Motor 2
  69.     PORTC.DIR |= (1<<PIN2);     //Motor 3
  70.     PORTC.DIR |= (1<<PIN3);     //Motor 4
  71. }
  72.  
  73. void timer_init(void)
  74. {
  75.     //Timer E0 initialisieren
  76.     TCE0.CTRLA = TC_CLKSEL_DIV1024_gc;   //Prescaler auf 1024
  77.     TCE0.CTRLB = 0x00;  //Timer im "Normalmode"
  78.     TCE0.INTCTRLA = 0x03;   //Interrupt hat nun höchste Priorität
  79.     TCE0.PER = 0xff00;  //Periode = Timer_Topwert = Taktfrequenz / Prescaler
  80.  
  81.     //Timer D0 initialisieren
  82.     TCD0.CTRLA = TC_CLKSEL_DIV1024_gc;   //Prescaler auf 1024
  83.     TCD0.CTRLB = 0x00;      //Timer im "Normalmode"
  84.     TCD0.INTCTRLA = 0x03;       //Interrupt hat nun höchste Priorität
  85.     TCD0.PER = 0x0500;      //Periode = Timer_Topwert = Taktfrequenz / Prescaler
  86.  
  87.     //Timer C0 als PWM initialisieren
  88.     TCC0.PER = 0xffff;      //ganzer Zählerbereich wird verwendet
  89.     TCC0.CTRLA = 0x02;      //Taktfrequenz der PWM
  90.     TCC0.CTRLB = 0xf3;      //Single Slope und PWM an allen PWM Ausgängen freischalten
  91. }
  92.  
  93. void set_pwmDutyCycle(void)
  94. {
  95.     //Drehzahlen der Motoren festlegen
  96.     TCC0.CCA = pwm_motor;   //DutyCycle für Motor 1
  97.     TCC0.CCB = pwm_motor;   //Motor 2
  98.     TCC0.CCC = pwm_motor;   //Motor 3
  99.     TCC0.CCD = pwm_motor;   //Motor 4
  100. }
  101.  
  102. int main (void)
  103. {
  104.     //Initialisierungen
  105.     clock_init();
  106.     sei();
  107.     ports_init();
  108.     timer_init();
  109.    
  110.     //Interruptlevel freigeben
  111.     PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;  
  112.    
  113.     /*! /brief Example code
  114.      *
  115.      *  Example code that reads the key pressed and show a value from the buffer,
  116.      *  sends the value to the slave and read back the processed value which will
  117.      *  be inverted and displayed after key release.
  118.      */
  119.    
  120.     /* Initialize TWI master. */
  121.     TWI_MasterInit(&twiMaster,
  122.                    &TWIC,
  123.                    TWI_MASTER_INTLVL_LO_gc,
  124.                    TWI_BAUDSETTING);
  125.  
  126.     /* Initialize TWI slave. */
  127.     TWI_SlaveInitializeDriver(&twiSlave, &TWIC, TWIC_SlaveProcessData);
  128.     TWI_SlaveInitializeModule(&twiSlave,
  129.                               SLAVE_ADDRESS,
  130.                               TWI_SLAVE_INTLVL_LO_gc);
  131.    
  132.     /*! Auswählen welches Zeichen gesendet werden soll*/                
  133.     uint8_t BufPos = 1;
  134.    
  135.     TWI_MasterWriteRead(&twiMaster,
  136.                             SLAVE_ADDRESS,
  137.                             &sendBuffer[BufPos],
  138.                             1,
  139.                             1);
  140.                            
  141.     while (twiMaster.status != TWIM_STATUS_READY) {
  142.             PORTA.OUTSET |= (1<<PIN5);  //Wenn LED an -> Warten bis Übertragung fertig
  143.         }
  144.             PORTA.OUTCLR |= (1<<PIN5);  //LED löschen
  145.        
  146.         /* Show the sent byte received and processed on LEDs. */
  147.         uint8_t val;
  148.         val = (twiMaster.readData[0]); //mit Breakpoint überprüfen
  149.    
  150.     //Hauptschleife
  151.     while(1)
  152.     {
  153.         set_pwmDutyCycle();
  154.         _delay_ms(1);
  155.     }
  156.    
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement