📄 init._c
字号:
//ICC-AVR application builder : 14/3/2007 23:29:10
// Target : M16
// Crystal: 7.3728Mhz
#include "config.h"
#include "predef.h"
#define BAUD_RATE 9600
#define BAUD_PRESCALE (((F_CPU / (BAUD_RATE * 16UL))) - 1)
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0xFD;
DDRB = 0xB0;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//UART0 initialize
// desired baud rate:
// actual: baud rate: (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06; //Use 8-bit character sizes - URSEL bit set to select the UCRSC register
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
UCSRB = 0xD8;
}
extern int sendready=1;
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char status,data;
status=UCSRA; //get the usart status
data = UDR; //put the received character to data variable
if(data == 'R') {
sendready = 1;
}
if((status & (FRAMING_ERROR | DATA_OVERRUN))==0)
{
uart_putc(data);
}
}
#pragma interrupt_handler uart0_tx_isr:14
void uart0_tx_isr(void)
{
//character has been transmitted
}
//SPI initialize
//Master mode
// clock rate: 460799hz
void spi_init(void)
{
SPCR = 0xD1; //setup SPI
SPSR = 0x00; //setup SPI
}
#pragma interrupt_handler spi_stc_isr:11
void spi_stc_isr(void)
{
//byte in SPDR has been sent/received
}
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value: 0.998mSec (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x8D; //set count
OCR0 = 0x73; //set compare
TCCR0 = 0x03; //start timer
}
unsigned int count=0; //to count timer0
extern char buffer [33];
char uabuffer [33];
extern int rcount;
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x8D; //reload counter value
if (++count == 5000) //1000 ms = 1 Sec
{
count = 0;
if (sendready == 1) {
uart_puts(buffer); //put the buffer to the serial
sendready = 0;
}
else {
//put to the queue buffer
}
//reset the counter 1
TCNT1H=0x00;
TCNT1L=0x00;
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
spi_init();
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -