#include <avr/io.h>
#define F_CPU 8000000UL
#define SCL_freq 200000 //TWBR = 16, TWPS = 0
// SCL_freq = F_CPU / (16+2*TWBR*4^TWPS)
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#define I2C_SCL PD0
#define I2C_SDA PD1
unsigned int TimerCount =0,sec = 0,min =0;
char buff[100];
void init_Buzzer();
void init_Serial();
void init_I2C();
void init_Timer();
void test_buzzer();
void test_Serial(int mode);
unsigned char USART0_TX();
void USART0_RX(unsigned char data);
void USART0_RX_str(unsigned char *str);
int main(void)
{
init_Buzzer();
init_I2C();
init_Serial(115200);
init_timer();
sprintf(buff,"Hello\r\n");
USART0_RX_str(buff);
//test_buzzer();
sei();
while (1)
{
}
}
void init_Buzzer(){
DDRB |= 0x10; // PB4
PORTB &= ~(0x10);
}
void init_Serial(long bautrate){
UBRR0H = 0;
switch(bautrate){
case 115200:
UBRR0L = 3;
break;
case 57600:
UBRR0L = 8;
break;
}
UCSR0A = 0x00;
UCSR0B = 0x18;
UCSR0C = 0x06;
}
void test_serial(int mode){
switch(mode){
case 0:
USART0_RX('A');
break;
case 1:
if(USART0_TX() == 'h'){
USART0_RX('B');
}
break;
}
}
void init_I2C(){
DDRD |= (1 << I2C_SCL);
DDRD |= (1 << I2C_SDA);
TWBR = 16;
}
void test_buzzer(){
PORTB = 0x10;
_delay_ms(10);
PORTB = ~0x10;
_delay_ms(10);
}
unsigned char USART0_TX(){
while(!(UCSR0A & (1<<RXC0)));
return UDR0;
}
void USART0_RX(unsigned char data){
while(!(UCSR0A & 0x20));
UDR0 = data;
}
void USART0_RX_str(unsigned char *str){
while(*str != '\0'){
USART0_RX(*str);
str++;
}
}
void init_timer(){
TCCR0 = 0x04;
TIMSK = 0x01;
TCNT0 = 256-125;
/*
1/8000000 = 0.000000125
64분주 0.000008
0.000008 *125 = 0.001
TCNT0 = 256-125;
*/
}
ISR(TIMER0_OVF_vect){
TCNT0=256-(256-125);
TimerCount++;
if(TimerCount>=1000){
TimerCount=0;
sec++;
/* test_timer
sprintf(buff, "min : %u, sec : %u \r\n", min,sec);
USART0_RX_str(buff);
*/
if(sec>=60){
min++; sec=0;
if(min>=60) min=0;
}
}
}