Commit 0cbb0c29 authored by spookysys's avatar spookysys
Browse files

Dag 2 oppgaver done

parent d52e998a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ int main(void)
	TCA0.SINGLE.CTRLA |= (TCA_SINGLE_ENABLE_bm); //Enable timer
	TCA0.SINGLE.CTRLB |= (0x05 << TCA_SINGLE_WGMODE0_bp); //Set mode to single slope
	
	TCA0.SINGLE.CTRLB |= (TCA_SINGLE_CMP0EN_bm) | (TCA_SINGLE_CMP1EN_bm);
	TCA0.SINGLE.CTRLB |= (TCA_SINGLE_CMP0EN_bm) | (TCA_SINGLE_CMP1EN_bm); //Enable the compare channels
	
	TCA0.SINGLE.PER = 0x0fff; //We set our top to have a sufficiently high frequency (Top at 16 bit (0xffff) ~25Hz, 12 bit (0x0fff) ~400Hz)
	TCA0.SINGLE.CMP0 = 0x0000;
+4 −2
Original line number Diff line number Diff line
@@ -34,7 +34,9 @@ int main(void)
	//Next we Enable timer interupts on overflow 
	TCA0.SINGLE.INTCTRL |= (TCA_SINGLE_OVF_bm);
	

	//Finally we have to set the max value of the timer, the top. 
	//This defines the period of the interrupt (which hints at the register's name.)
	//Note that this is a 16 bit value!
	uint16_t counterTop = 0x4000;
	
	TCA0.SINGLE.PER = counterTop;
@@ -51,6 +53,6 @@ ISR(TCA0_OVF_vect){

	PORTB.OUTTGL = (1 << LED1) | (1 << LED2);
	
	TCA0.SINGLE.INTFLAGS |= ( TCA_SINGLE_OVF_bm); //Clear the interupt flag
	TCA0.SINGLE.INTFLAGS = ( TCA_SINGLE_OVF_bm); //Clear the interupt flag
	
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ void uart_transmit(unsigned char data){
	while (!(USART0.STATUS & (1 << USART_DREIF_bp))){
		//wait for previous transmit to finish
	};
	USART0.TXDATAH = 0x00; //Is needed!
	
	USART0.TXDATAL = data;
}

Session2/PWM/main.c

0 → 100644
+69 −0
Original line number Diff line number Diff line
/*
 * PWM-LF.c
 *
 * Created: 29.01.2017 23:43:39
 * Author : Petter
 */ 

#define F_CPU 3333333UL


#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>


#define LED1 0 // port B, connected to WO0
#define LED2 1 // port B, connected to WO1
#define LED3 4 // Port B
#define SW1 5 // Port A
#define SW2 6 // Port A
#define SW3 7 // Port A



int main(void)
{
	//Set up the leds and buttons. You might want to copy this from previous exercises.
	
	/*We will be using timer 1 in single (not split) mode.
	  It is highly recommended that you read chapter 20.3.3.4 in the datasheet 
	  on timer compare channels.
	  There you will find a sub-capter on the single-slope PWM we will be using.
	*/
	
	//First, enable the timer
	
	
	//Set the mode of the timer to single slope PWM 
	
	
	//We have to override the normal pin opperation so the PWM is pushed directly to the pin
	//Hint: WO0 and WO1 are connected to leds. We need to override them to get the PWM out.
	
	
	
	/*Timer A is a 16 bit timer. This will give us a frequency of 25Hz, we can see it flicker.
	  By lowering the period, (PER) we get higher frequency at the cost of lower resolution.
	*/
	
	//Set the period to 12 bit. (This results in a frequency of ~400Hz.)
	
	
	//We can now control the PWM duty cycle by simply writing values to the CMP0 and CMP1 registers.
	TCA0.SINGLE.CMP0 = 0x0000; //Max brightness (Leds are inverted)
	TCA0.SINGLE.CMP1 = 0x0fff; //Min brightness (Leds are inverted)
	
	
    while(1){
		
		/*Have some fun with the leds. Examples:
		  Have them fade between max and min
		  Have them fade in a pattern (Heartbeat?) 
		  Change the brightness based on buttons

		*/
		
    }
}

Session2/Timer/main.c

0 → 100644
+64 −0
Original line number Diff line number Diff line


#define F_CPU 3333333UL


#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define LED1 0 // port B
#define LED2 1 // port B
#define LED3 4 // Port B


int main(void)
{
	/*	In this exercise we will get LEDs to blink at even intervals using interrupts
		First set up the LEDs like in the previous exercise. 
		You can either copy paste from the previous exercise or start fresh.
	*/

	
	/*We will be using timer A that will trigger an overflow interupt.
	  This is a 16 bit timer that can run in 2 modes
		-sigle mode as 1 16-bit timer
		-dual/split mode as 2 8-bit timers
	  We will be using single mode in this exercise.
	  
	  Hint because the register names can be hard to understand:
	  TCA0.SINGLE.CTRLA adresses the control A register for timer A
	  
	  First we set the prescaler to clk=clk/256 and enable the timer. 
	  This is done by setting the right bits in the control A register.
	*/

	
	//Next we Enable timer interupt for overflow on timer A. 

	
	//Finally we have to set the max value of the timer, the top. 
	//This defines the period of the interrupt (which hints at the register's name.)
	//Note that this is a 16 bit value!

	
	
	//To be able to react to the interrupts from the module we have to enable interrupts globally.
	//This is done in the function sei(), whih is located in the already included header file <avr/interrupt.h>
	sei();
	
    while(1){
		//Do other things?
		
		//Remember to fill in the ISR at the bottom of the file, else nothing will happen!
    }
}

ISR(TCA0_OVF_vect){

	//Do something with the led(s), like toggle.
	
	//Clear the interupt flag.
	//If we do not clear the flag, we will instantly jump back into the ISR again
	
}
 No newline at end of file
Loading