A lot of changes. UART works. Timer function work but sei() fucks shit up.

This commit is contained in:
Marco 2022-05-11 20:38:02 +02:00
parent f2d128bfcd
commit 609deed11e
6 changed files with 139 additions and 21 deletions

19
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"/usr/avr/include",
"${workspaceFolder}/**"
],
"defines": [
"__AVR_ATmega32U4__"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"io.h": "c",
"sfr_defs.h": "c"
}
}

View File

@ -2,9 +2,9 @@ TARGETNAME = togglePin
BUILDFOLDER = ./build/
TARGET = $(BUILDFOLDER)$(TARGETNAME)
INC = -Isrc/include -I/usr/lib/avr/include/
CFLAGS = -Wall -Wpedantic -Wextra -g -Os -mmcu=atmega328p
CPPFLAGS = -DF_CPU=16000000UL
INC = -Isrc/include -I/usr/avr/include
CFLAGS = -Wall -Wpedantic -Wextra -Os -mmcu=atmega32u4
CPPFLAGS = -DF_CPU=8000000UL
SRC = $(wildcard src/*.c)
@ -17,7 +17,7 @@ $(TARGET): $(SRC)
rm $@.elf
flash:
avrdude -p m328p -P /dev/ttyUSB0 -c arduino -b 57600 -Uflash:w:"$(TARGET).hex":i
avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 -Uflash:w:"$(TARGET).hex":i
clean:
rm -fr ./src/*.o ./build/

110
src/inverter.c Normal file
View File

@ -0,0 +1,110 @@
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
void togglePin(uint8_t port, uint8_t pin);
void initCtcTimer0(void);
void initOverflowTimer0(void);
void initOverflowTimer1(void);
void initUart(unsigned int baud);
void uart_sendByte(uint8_t byte);
int uart_printf(char var, FILE *stream);
static FILE stream = FDEV_SETUP_STREAM(uart_printf, NULL, _FDEV_SETUP_WRITE);
uint8_t sec = 0;
int main()
{
stdout = &stream;
/* Make PB1 & PB2 an output pin */
DDRB |= (1 << DDB1);
DDRB |= (1 << DDB2);
/* With this frequency, it is 38,4k baud */
initUart(12);
while (1) {
_delay_ms(1000);
printf("Seconds passed: %u\n\r", sec++);
}
return 0;
}
void initCtcTimer0(void)
{
/* Initialize counter 0 */
TCNT0 = 0;
/* Enable Counter0 Compare Match A Interrupt */
TIMSK0 |= (1 << OCIE0A);
/* Select clock. Prescaler of 0*/
TCCR0B |= (1 << CS00);
/* Use CTC Mode */
TCCR0A |= (1 << WGM01);
/*
* OCR0A contains TOP value for counter:
*/
OCR0A = 200;
}
void initOverflowTimer0(void)
{
/* Prescaler = 0 */
TCCR0B |= (1<<CS00);
/* Enable Timer Overflow Interrupt */
TIMSK0 |= (1<<TOIE0);
}
void initOverflowTimer1(void)
{
TCCR1B |= (1<<CS11);
TIMSK1 |= (1<<TOIE1);
}
ISR(TIMER0_COMPA_vect)
{
PORTB ^= (1 << PB2);
}
void initUart(unsigned int baud)
{
/* Set baud rate */
UBRR1H = (unsigned char)(baud>>8);
UBRR1L = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSR1B = (1<<RXEN1)|(1<<TXEN1);
/* Set frame format */
UCSR1C |= (3<<UCSZ10); // 8 data bits
// UCSR1C &= ~(1<<USBS1); // 1 stop bit
}
void uart_sendByte(uint8_t byte)
{
/* Wait for empty transmit buffer */
while (!(UCSR1A & (1<<UDRE1)))
{
}
/* Put data into buffer, sends the data */
UDR1 = byte;
}
int uart_printf(char var, FILE *stream)
{
uart_sendByte(var);
return 0;
}

View File

@ -1,17 +0,0 @@
#include <avr/io.h>
#include <util/delay.h>
int main()
{
DDRB |= 1 << PB2;
while (1) {
PORTB &= ~(1 << PB2);
_delay_ms(10);
PORTB |= 1 << PB2;
_delay_ms(10);
}
return 0;
}