Intial commit

This commit is contained in:
Jimmy 2020-12-10 16:21:16 +13:00
parent 4bdc66acde
commit 0030fef76e
5 changed files with 94 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin/
.vscode/

31
makefile Normal file
View File

@ -0,0 +1,31 @@
MCU=atmega32
F_CPU=8000000
PROG=dragon_jtag
DEBUGGER = dragon
PORT=/dev/ttyUSB0
CC=avr-g++
OBJCOPY=avr-objcopy
CFLAGS=-Wall -g -mmcu=${MCU} -DF_CPU=${F_CPU} -I.
TARGET=main
SRCS=src/*.cpp
default: build flash
build:
${CC} ${CFLAGS} -o bin/${TARGET}.bin ${SRCS}
${CC} ${CFLAGS} -o bin/${TARGET}.elf ${SRCS}
${OBJCOPY} -j .text -j .data -O ihex bin/${TARGET}.bin bin/${TARGET}.hex
flash:
avrdude -p ${MCU} -c ${PROG} -U flash:w:bin/main.hex
clean:
rm -f bin/*
debug: flash avarice
sleep 1
avr-gdb -ex "target remote :4242" bin/main.elf
avarice:
avarice --file bin/main.elf --part ${MCU} --${DEBUGGER} :4242

24
src/main.cpp Normal file
View File

@ -0,0 +1,24 @@
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "photointerrupter.h"
int main()
{
photointerrupter::init();
DDRD |= (1<<PD6);
while(1);
return 0;
}
PHOTOINTERRUPT {
cli();
PORTD |= (1<<PD6);
_delay_ms(100);
PORTD &= ~(1<<PD6);
GIFR |= (1<<INTF1);
sei();
}

31
src/photointerrupter.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "photointerrupter.h"
namespace photointerrupter {
void init() {
TCCR2 |= (1<<WGM21); //CTC TOP OCR2
TCCR2 |= (1<<COM20); //Toggle OC0
TCCR2 |= (1<<CS20); //No prescale
OCR2 |= 105;
//DDRB |= (1<<PB3);
DDRD |= (1<<PD6)|(1<<PD7);
//TIMSK |= (1<<OCIE0);
MCUCR |= (1<<ISC11)|(1<<ISC10); //INT1 rising edge
GICR |= (1<<INT1);
sei();
}
ISR(TIMER2_COMP_vect) {
volatile static int count;
count++;
if(count == 60) TCCR2 &= ~(1<<CS20);
if(count > 600) {
TCCR2 |= (1<<CS20);
count = 0;
}
}
} // namespace photointerrupter

6
src/photointerrupter.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#define PHOTOINTERRUPT ISR(INT1_vect)
namespace photointerrupter {
void init();
} // namespace photointerrupter