⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.c

📁 我写的atmega16驱动的nrf24l01程序
💻 C
字号:
//ICC-AVR application builder : 2006-11-14 8:40:20
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>

void port_init(void)
{
	PORTC = 0xFF; 
	DDRC  = 0xfe;
	PORTD = 0xFe;
	DDRD  = 0x02;
}

//Watchdog initialisation
// prescale: 16K cycles
void watchdog_init(void)
{
	WDR(); //this prevents a timout on enabling
	WDTCR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}


void SPI_MasterInit(void)
{
	SPI_PORT |= (1<<SpiSs);
	SPI_DDR |= (1<<SpiMosi)|(1<<SpiSck)|(1<<SpiSs);		//设置MOSI、SS 和SCK 为输出 
	SPI_DDR &= ~(1<<SpiMiso);					//设置MISO 为输入 
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);		//使能SPI 主机模式,设置时钟速率为fck/16=500kHz,MSB first
}
void SPI_MasterTransmit(char cData)
{
	SPI_PORT &= ~(1<<SpiSs);
	_NOP();_NOP();
	SPDR = cData;							//启动数据传输
	while(!(SPSR & (1<<SPIF)));				//等待传输结束
}

void SPI_SlaveInit(void)
{
	SPI_DDR = (1<<SpiMiso);					//设置MISO 为输出,其他为输入
	SPCR = (1<<SPE);						//使能 SPI 
}
char SPI_SlaveReceive(void)
{
	while(!(SPSR & (1<<SPIF)));				//等待接收结束
	return SPDR;							//返回数据
}
//call this routine to initialise all peripherals

void InitInterrupt(void)
{	
	UBRRH = 0x80;			
	UCSRC=0x86;
	UBRRL=0xcf;
	UCSRC=0x86;										//定义串口 8位数据位置 异步通讯
	UBRRL=0xcf;										//定义波特率 在8M时钟情况下 0xcf 2400波特
													//4800波特 0x67
	UCSRA=0x20;										//UCSRA:USART Control and Status A 清除接收寄存器
	UCSRB=0xd8;										//UCSRB:USART Control and Status B
													//允许接收及发送中断,接收使能 发送使能
	UBRRH = 0x80;			
	UCSRC=0x86;
	UBRRL=0xcf;
	
	TCCR0 = 0x00;									//定时器0溢出值初始化 0.5豪秒中断一次
 	TCNT0 = 0x82;
 	TCCR0 = 0x03;									//定义时钟32分频
 	
 	TCCR1B = 0x00;									//定时器1溢出值初始化 2豪秒中断一次
 	TCNT1H = 0xf8;
 	TCNT1L = 0x2f;
 	TCCR1A = 0x00;
 	TCCR1B = 0x02;									//定义时钟8分频
 	
	MCUCR = 0x03;
	GICR=0x00;										//不允许INT0 INT1 INT2 
 	TIMSK=0x05;										//TIMER0 及 TIMER1溢出中断使能
}

void init_devices(void)
{
//stop errant interrupts until set up
	CLI(); //disable all interrupts
	port_init();
	watchdog_init();
	InitInterrupt();
	
	MCUCR = 0x00;
	GICR  = 0x00;
	TIMSK = 0x05; //timer interrupt sources
	SEI(); //re-enable interrupts
//all peripherals are now initialised
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -