📄 init.c
字号:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <compat/twi.h>
#include "main.h"
#include "init.h"
#include "inter.h"
volatile uint16_t cnt_t0; //记录t0发生的次数
void port_init(void)
{
PORTB = 0xFF; //输出为高
DDRB = 0xfC; //输出 pb0.1为输入
PORTC = 0xfF; //m103 output only
DDRC = 0x07;
PORTD = 0xFF;
DDRD = 0x00;
}
/*TIMER0 initialisation - prescale:1
WGM: Normal
desired value: 38KHz
actual value: 38.095KHz (0.2%)*/
void timer0_init(void) //0.1毫秒定时
{
// TCCR0 = 0x00; //stop
TCCR0 = 0x02; //start timer 8分频
TCNT0 = 0x9B; //set count
cnt_t0 = 0;
}
/*TIMER1 initialisation - prescale:1024
WGM: 0) Normal, TOP=0xFFFF
desired value: 1Sec
actual value: 1.000Sec (0.0%)*/
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1 = 0x0008; //setup
// OCR1A = 0x0F42;
// OCR1B = 0x0F42;
// ICR1 = 0x0F42;
TCCR1A = 0x00;
TCCR1B = 0x01; //start Timer 1分频,内部晶振为1M
}
/*TIMER2 initialisation - prescale:128
WGM: Normal
desired value: 2mSec
actual value: 1.979mSec (1.0%)*/
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0xC7; //setup
OCR2 = 0x39;
TCCR2 = 0x05; //start
}
/*TWI初始化*/
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x32; //set bit rate
TWSR= 0x00; //set prescale
TWAR= 0x00; //set slave address
// TWCR= 0x04; //enable twi
}
void uart0_init(void)
{
UCSRA=0x00;
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
UBRRL=(FOSC/16/(BAUD+1))%256;
UBRRH=(FOSC/16/(BAUD+1))/256;
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP位
}
void init_devices(void)
{
port_init();
timer0_init();
// timer1_init();
// timer2_init();
// spi_init();
twi_init();
uart0_init();
putchar('O');
putchar('K');
// comparator_init();
// adc_init();
// lcd_init();
MCUCR = 0x08;
OSCCAL = 0xAF;
GICR = 0x80;
TIMSK |= _BV(TOIE0); // enable T0 interrupt
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -