Advertisement
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"
- uint32_t pwm_motor = 0x0000; //entspricht Drehzahl
- //Kommentare mit ! aus Atmel Beispielcode
- /*! Defining slave address. */
- #define SLAVE_ADDRESS 0x55 //nur Beispiel
- /*! Defining number of bytes in buffer. */
- #define NUM_BYTES 8
- /*! 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. */
- TWI_Slave_t twiSlave; /*!< TWI slave module. */
- /*! Buffer with test data to send.*/
- uint8_t sendBuffer[NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD};
- /*! Simple function that invert the received value in the sendbuffer. This
- * function is used in the driver and passed on as a pointer to the driver.
- */
- void TWIC_SlaveProcessData(void)
- {
- uint8_t bufIndex = twiSlave.bytesReceived;
- twiSlave.sendData[bufIndex] = (~twiSlave.receivedData[bufIndex]);
- }
- /* TWIC Master Interrupt vector. */
- ISR(TWIC_TWIM_vect)
- {
- TWI_MasterInterruptHandler(&twiMaster);
- }
- /*! TWIC Slave Interrupt vector. */
- ISR(TWIC_TWIS_vect)
- {
- TWI_SlaveInterruptHandler(&twiSlave);
- }
- 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
- PORTC.DIR |= (1<<PIN0); //Motor 1
- PORTC.DIR |= (1<<PIN1); //Motor 2
- PORTC.DIR |= (1<<PIN2); //Motor 3
- PORTC.DIR |= (1<<PIN3); //Motor 4
- }
- void timer_init(void)
- {
- //Timer E0 initialisieren
- TCE0.CTRLA = TC_CLKSEL_DIV1024_gc; //Prescaler auf 1024
- TCE0.CTRLB = 0x00; //Timer im "Normalmode"
- TCE0.INTCTRLA = 0x03; //Interrupt hat nun höchste Priorität
- TCE0.PER = 0xff00; //Periode = Timer_Topwert = Taktfrequenz / Prescaler
- //Timer D0 initialisieren
- TCD0.CTRLA = TC_CLKSEL_DIV1024_gc; //Prescaler auf 1024
- TCD0.CTRLB = 0x00; //Timer im "Normalmode"
- TCD0.INTCTRLA = 0x03; //Interrupt hat nun höchste Priorität
- TCD0.PER = 0x0500; //Periode = Timer_Topwert = Taktfrequenz / Prescaler
- //Timer C0 als PWM initialisieren
- TCC0.PER = 0xffff; //ganzer Zählerbereich wird verwendet
- TCC0.CTRLA = 0x02; //Taktfrequenz der PWM
- TCC0.CTRLB = 0xf3; //Single Slope und PWM an allen PWM Ausgängen freischalten
- }
- void set_pwmDutyCycle(void)
- {
- //Drehzahlen der Motoren festlegen
- TCC0.CCA = pwm_motor; //DutyCycle für Motor 1
- TCC0.CCB = pwm_motor; //Motor 2
- TCC0.CCC = pwm_motor; //Motor 3
- TCC0.CCD = pwm_motor; //Motor 4
- }
- int main (void)
- {
- //Initialisierungen
- clock_init();
- sei();
- ports_init();
- timer_init();
- //Interruptlevel freigeben
- PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
- /*! /brief Example code
- *
- * Example code that reads the key pressed and show a value from the buffer,
- * sends the value to the slave and read back the processed value which will
- * be inverted and displayed after key release.
- */
- /* Initialize TWI master. */
- TWI_MasterInit(&twiMaster,
- &TWIC,
- TWI_MASTER_INTLVL_LO_gc,
- TWI_BAUDSETTING);
- /* Initialize TWI slave. */
- TWI_SlaveInitializeDriver(&twiSlave, &TWIC, TWIC_SlaveProcessData);
- TWI_SlaveInitializeModule(&twiSlave,
- SLAVE_ADDRESS,
- TWI_SLAVE_INTLVL_LO_gc);
- /*! Auswählen welches Zeichen gesendet werden soll*/
- uint8_t BufPos = 1;
- TWI_MasterWriteRead(&twiMaster,
- SLAVE_ADDRESS,
- &sendBuffer[BufPos],
- 1,
- 1);
- while (twiMaster.status != TWIM_STATUS_READY) {
- PORTA.OUTSET |= (1<<PIN5); //Wenn LED an -> Warten bis Übertragung fertig
- }
- PORTA.OUTCLR |= (1<<PIN5); //LED löschen
- /* Show the sent byte received and processed on LEDs. */
- uint8_t val;
- val = (twiMaster.readData[0]); //mit Breakpoint überprüfen
- //Hauptschleife
- while(1)
- {
- set_pwmDutyCycle();
- _delay_ms(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement