feat(ex01): ex01 is complete
This commit is contained in:
parent
49164a0c18
commit
7eb2daf73f
5 changed files with 129 additions and 0 deletions
43
ex01/Makefile
Normal file
43
ex01/Makefile
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# Makefile
|
||||
MCU=atmega328p
|
||||
F_CPU=16000000
|
||||
CC=avr-gcc
|
||||
OBJCOPY=avr-objcopy
|
||||
CFLAGS=-std=c99 -Wall -Wextra -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -Iinclude
|
||||
TARGET=main
|
||||
SERIAL=-P /dev/ttyUSB0 -b 115200
|
||||
|
||||
|
||||
SRC_DIR=.
|
||||
OBJ_DIR=build
|
||||
|
||||
SRC_FILES=main.c utils.c
|
||||
OBJ_FILES=$(patsubst %.c,%.o,$(SRC_FILES))
|
||||
|
||||
SRC=$(addprefix $(SRC_DIR)/,$(SRC_FILES))
|
||||
OBJ=$(addprefix $(OBJ_DIR)/,$(OBJ_FILES))
|
||||
|
||||
all: flash
|
||||
|
||||
re: fclean all
|
||||
|
||||
fclean: clean
|
||||
clean:
|
||||
rm -rf $(OBJ_DIR)
|
||||
rm -f $(TARGET).hex
|
||||
|
||||
hex: $(TARGET).hex
|
||||
|
||||
flash: hex
|
||||
avrdude -p $(MCU) -c arduino -U flash:w:$(TARGET).hex:i $(SERIAL)
|
||||
|
||||
$(OBJ_DIR)/$(TARGET).bin: $(OBJ)
|
||||
$(CC) $(CFLAGS) $(OBJ) -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
|
||||
mkdir -p $(shell dirname $@)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
|
||||
$(TARGET).hex: $(OBJ_DIR)/$(TARGET).bin
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
15
ex01/include/mystd.h
Normal file
15
ex01/include/mystd.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef MYSTDINT_H
|
||||
#define MYSTDINT_H
|
||||
|
||||
typedef unsigned int uint16_t;
|
||||
typedef signed int int16_t;
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef signed char int8_t;
|
||||
|
||||
typedef uint8_t bool;
|
||||
|
||||
#define true (1)
|
||||
#define false (0)
|
||||
|
||||
#endif /* MYSTDINT_H */
|
||||
8
ex01/include/utils.h
Normal file
8
ex01/include/utils.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include "mystd.h"
|
||||
|
||||
void delay_ms(uint16_t count);
|
||||
|
||||
#endif /* UTILS_H */
|
||||
44
ex01/main.c
Normal file
44
ex01/main.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <avr/io.h>
|
||||
#include "mystd.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define D1 PORTB0
|
||||
#define D2 PORTB1
|
||||
#define D3 PORTB2
|
||||
#define D4 PORTB4
|
||||
|
||||
#define PRESCALER 256
|
||||
#define TIMER_FREQ (F_CPU / PRESCALER)
|
||||
|
||||
|
||||
// at a high level:
|
||||
// Set the OC1B (PB2) pin as output
|
||||
// set the TIMER1 mode to COMPARE (CTC)
|
||||
// say to compare against OC1A
|
||||
// set the value to be compated at X count
|
||||
// say the presacler for the timer is 512
|
||||
//
|
||||
// all these information are on page ~140
|
||||
void timer1_init(void) {
|
||||
// Set PB2 (OC1B) as output
|
||||
DDRB |= _BV(PB2);
|
||||
|
||||
// CTC mode (WGM12 = 1)
|
||||
TCCR1B |= _BV(WGM12);
|
||||
|
||||
// Toggle OC1B on compare match (COM1B0 = 1)
|
||||
TCCR1A |= _BV(COM1B0);
|
||||
|
||||
// Set compare values
|
||||
OCR1A = TIMER_FREQ / 2;
|
||||
|
||||
// Start timer with prescaler 256 (CS12)
|
||||
TCCR1B |= _BV(CS12);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
timer1_init();
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
|
||||
19
ex01/utils.c
Normal file
19
ex01/utils.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include "utils.h"
|
||||
|
||||
// this just burns cycles.
|
||||
// the volatile is important, it means that the cpu can't optimize any
|
||||
// read/writes for the value
|
||||
static inline void spin_loop(volatile uint16_t counts) {
|
||||
while (counts)
|
||||
counts--;
|
||||
}
|
||||
|
||||
void delay_ms(uint16_t ms) {
|
||||
while (ms) {
|
||||
// this value was taken using a delay of 500ms, and just recording the led
|
||||
// blinking. it seems to be high enough such that each loop of delay_loop
|
||||
// takes 1ms :D
|
||||
spin_loop((F_CPU) / 5000);
|
||||
ms--;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue