111 lines
1.8 KiB
C
111 lines
1.8 KiB
C
|
#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;
|
||
|
}
|