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

📄 step._c

📁 这是关于AVR单片机学习的初步开发
💻 _C
字号:
//Designed by Buaa Alf
//通过定时器实现了步进电机的运转,仅仅是一演示程序;
//烧写程序后,断开电源开关,闭合SW9的1、2、3、4和7、8管角,然后闭合电源开关即可看到电机的运转一周。
//按复位可以重起单片机,使电机再旋转一周;
//可参考设计自己的电机控制程序。

//ICC-AVR application builder : 2005-3-31 上午 11:35:43
// Target : M8
// Crystal: 11.059Mhz

#include <iom8v.h>
#include <macros.h>

#define aon PORTB =0x01
#define bon PORTB =0x02
#define con PORTB =0x04
#define don PORTB =0x08

#define abon PORTB =0x03
#define bcon PORTB =0x06
#define cdon PORTB =0x0C
#define daon PORTB =0x09

unsigned int Pnum=4160;//总脉冲个数,包含减速比
char Freq=50;//脉冲频率

unsigned int pcont=0;
char step =0;
char dirc=0;

void port_init(void)
{
 PORTB = 0xFF;
 DDRB  = 0x0F;
 PORTC = 0x7F; //m103 output only
 DDRC  = 0x00;
 PORTD = 0xFF;
 DDRD  = 0x00;
}

//TIMER1 initialisation - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 50Hz
// actual value: 50.001Hz (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0x90; //setup
 TCNT1L = 0xF0;
 OCR1AH = 0x6B;
 OCR1AL = 0xFF;
 OCR1BH = 0x6B;
 OCR1BL = 0xFF;
 ICR1H  = 0x6B;
 ICR1L  = 0xFF;
 TCCR1A = 0x00;
 TCCR1B = 0x02; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed
 if(pcont <=Pnum)
 { 
   if(dirc == 0)
   { if(step == 0)
   	 	aon;
	 else if(step == 1)
	 	abon;
	 else if(step == 2)
	 	bon;
     else if(step == 3)
	 	bcon;
     else if(step == 4)
	 	con;
     else if(step == 5)
	 	cdon;
	 else if(step == 6)
	 	don;
     else 
	 	daon;
   }
   else
   {
   	 if(step == 0)
   	 	aon;
	 else if(step == 1)
	 	daon;
	 else if(step == 2)
	 	don;
     else if(step == 3)
	 	cdon;
     else if(step == 4)
	 	con;
     else if(step == 5)
	 	bcon;
	 else if(step == 6)
	 	bon;
     else
	 	abon;
   }
   step ++;
   if(step>=8) step =0;
   
   TCNT1H = 0x90; //reload counter high value
   TCNT1L = 0x01; //reload counter low value   
   TCCR1A = 0x00;
   TCCR1B = 0x02; //start Timer
   
   pcont++;
 }
 else
 	TCCR1B = 0x00;
}

//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = 0x86;
 UBRRL = 0x47; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x98;
}

#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
 //uart has received a character in UDR
}

//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer1_init();
 uart0_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x04; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

//
void main(void)
{
 init_devices();
 //insert your functional code here...
 while(1);
}

⌨️ 快捷键说明

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