PWM-LF.c 1.84 KB
Newer Older
Petter Breedveld's avatar
Petter Breedveld committed
1
/*
2
 * PWM-LF.c
Petter Breedveld's avatar
Petter Breedveld committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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>
#include <stdbool.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


bool SW1_pressed,SW2_pressed; //Keeping track of button status

int main(void)
{
	//Set LED pins as output
29
	PORTB.DIRSET = (1 << LED1) | (1 << LED2) | (1 << LED3);
Petter Breedveld's avatar
Petter Breedveld committed
30 31
	
	//Because the LEDs are active low, we invert the output
32 33 34
	PORTB.PIN0CTRL |= (1 << PORT_INVEN_bp);
	PORTB.PIN1CTRL |= (1 << PORT_INVEN_bp);
	PORTB.PIN4CTRL |= (1 << PORT_INVEN_bp);
Petter Breedveld's avatar
Petter Breedveld committed
35 36
	
	//Pullups
37 38 39
	PORTA.PIN5CTRL |= (1 << PORT_PULLUPEN_bp);
	PORTA.PIN6CTRL |= (1 << PORT_PULLUPEN_bp);
	PORTA.PIN7CTRL |= (1 << PORT_PULLUPEN_bp);	
Petter Breedveld's avatar
Petter Breedveld committed
40 41
	
	//Set clock prescaler div256
42 43
	TCA0.SINGLE.CTRLA |= (TCA_SINGLE_ENABLE_bm); //Enable timer
	TCA0.SINGLE.CTRLB |= (0x05 << TCA_SINGLE_WGMODE0_bp); //Set mode to single slope
Petter Breedveld's avatar
Petter Breedveld committed
44
	
45
	TCA0.SINGLE.CTRLB |= (TCA_SINGLE_CMP0EN_bm) | (TCA_SINGLE_CMP1EN_bm);
Petter Breedveld's avatar
Petter Breedveld committed
46
	
47 48 49
	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;
	TCA0.SINGLE.CMP1 = 0x0fff;
Petter Breedveld's avatar
Petter Breedveld committed
50 51 52 53
	
	
    while(1){
		
54
		if (!(PORTA.IN & (1 << SW1))){
Petter Breedveld's avatar
Petter Breedveld committed
55
			if(!SW1_pressed){
56 57
				TCA0.SINGLE.CMP0 = (((TCA0.SINGLE.CMP0 << 1) + 1) & (0x0fff)); //Shift in a 1, and cut off excess to 12 bit
				TCA0.SINGLE.CMP1 >>= 1;
Petter Breedveld's avatar
Petter Breedveld committed
58 59 60 61 62 63 64 65 66 67
				SW1_pressed = true;
			}
		}
		
		else{
			SW1_pressed = false;
		}
		
		if (!(PORTA_IN & (1 << SW2))){
			if(!SW2_pressed){
68 69
				TCA0.SINGLE.CMP1 = (((TCA0.SINGLE.CMP1 << 1) + 1) & (0x0fff)); //Shift in a 1, and cut off excess to 12 bit
				TCA0.SINGLE.CMP0 >>= 1;
Petter Breedveld's avatar
Petter Breedveld committed
70 71 72 73 74 75 76 77 78 79 80
				SW2_pressed = true;
			}
		}
		
		else{
			SW2_pressed = false;
		}
		
    }
}