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

📄 main._c

📁 这是本人毕业设计的源码部分,主要完成了步进电机的智能控制:采用AVR系列单片机做主空单元,可红外遥控,其中脉冲分配由CPLD实现.
💻 _C
字号:
//ICC-AVR application builder : 2006-5-29 11:08:58
// Target : M128
// Crystal: 8.0000Mhz

#include <iom128v.h>
#include <macros.h>
#include 'lcd.c'

//global variable
extern  unsigned char direction ; //接受标志
extern  unsigned char direction_last ;
extern unsigned char speed; //store speed
extern unsigned char speed_last;
extern unsigned char on_off;


//declare memory mapped variables
extern unsigned char command_lcd;
extern unsigned char data_lcd;

//define mappings
void mapping_init(void)
{
 asm(
  ".area memory(abs)\n"
  ".org 0x8000\n"
  " _command_lcd:: .blkb 1\n"
  ".org 0xC000\n"
  " _data_lcd:: .blkb 1\n"
  ".text\n"
 );
}

void port_init(void)
{
 //输出口设置
 DDRA  = 0xff;
 DDRC  = 0xc0;
 DDRE  = 0xf8;
 DDRG  = 0x02;
 PORTD = (1 << PD2);  //上拉电阻输入
 DDRD  = 0x00;
}

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

//TIMER0 initialize - prescale:256
// WGM: CTC
// desired value: 5mSec
// actual value:  5.024mSec (-0.5%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 ASSR  = 0x00;if  //set async mode
 TCNT0 = 0x64; //set count
 OCR0  = 0x9C;
 TCCR0 = 0x0E; //start timer
}

void delay(char i)
{
  while(i--);
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
 TCNT0 = 0x64; //reload counter value
 if (on_off ==0)
     {
	 motor_off()
	 }
 else  (on_off ==1}
 
}
void motor_on()
{
PORTE = (1<<PE7);
}

void motor_off()
{
PORTE = (0<<PE7);
}



void motor_direction()
{
   if (direction == 1)
      PORTE = (1<<PE7)|(1<<PE6);
   else PORTE = (1<<PE7)|(0<<PE6);
}


//TIMER3 initialize - prescale:Stop
// WGM: 8) PWM phz freq correct, TOP=ICRn
// desired value: 1KHz
// actual value: Out of range
void timer3_init(void)
{
 TCCR3B = 0x00; //stop
 TCNT3H = 0xFF; //setup
 TCNT3L = 0x38;
 OCR3AH = 0x00;
 OCR3AL = 0xC8;
 ICR3H  = 0x00;
 ICR3L  = 0xC8;
 TCCR3A = 0x81;
 TCCR3B = 0x11; //start Timer
}

#pragma interrupt_handler timer3_compa_isr:27
void timer3_compa_isr(void)
{
 OCR3AH = 0x00;
 OCR3AL = 0xC8;
 //compare occured TCNT3=OCR3A
}


#pragma interrupt_handler timer3_ovf_isr:30
void timer3_ovf_isr(void)
{
 //TIMER3 has overflowed
 ICR3H  = (speed*400)/256;
 ICR3L  = (speed*400)%256;
 
 TCNT3H = 0xFF; //reload counter high value
 TCNT3L = 0x38; //reload counter low value
}


//UART1 initialize
// desired baud rate:2400
// actual baud rate:2404 (0.2%)
// char size: 8 bit
// parity: Odd
void uart1_init(void)
{
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x36;
 UBRR1L = 0xCF; //set baud rate lo
 UBRR1H = 0x00; //set baud rate hi
 UCSR1B = 0x90;
}

#pragma interrupt_handler uart1_rx_isr:31
void uart1_rx_isr(void)
{
  unsigned char temp;
  trmp = UDR1;
  switch (temp)
  {
     case A :
	   if(speed < 200) speed = speed + 10;
	   break;
	 case B :
	   if(speed > 0 ) speed = speed - 10;
	   break;
	 case C :
	   direction = 1;
	   break;
	 case D :
	   direction = 0;
	   break;
	 case E :
	   on_off = 1;
	   break;
	 case F :
	  on_off = 0;
	  break;
  }
 //uart has received a character in UDR
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 mapping_init();
 port_init();
 
 
 watchdog_init();
 timer0_init();
 timer3_init();
 uart1_init();

 MCUCR = 0x80;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x01; //timer interrupt sources
 ETIMSK = 0x04; //extended timer interrupt sources
 
 lcd_init();
 
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}




void main()
{
init_device(void);

while(1)
{  
   display_lcd();
}
}

⌨️ 快捷键说明

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