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

📄 autocontrol.c

📁 AVR单片机控制码盘程序
💻 C
字号:
//ICC-AVR application builder : 2005-11-21 21:35:32
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>

#include "Main.h"
#include "HD7279.C"
#include "MotorControl.c"

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0xFF;
 DDRB  = 0xFF;	//output IO
 
 PORTC = 0x0F; //PC口作为舵机信号输出口和外部信号输入口
 DDRC  = 0xF0;	//PC0-3输入,PC4-7输出	
 
 PORTD = 0xFF;
 DDRD  = 0xFF;
}



//TIMER0 initialize - prescale:8
// WGM: Normal
// desired value: 100uSec
// actual value: 100.000uSec (0.0%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0x9C; //set count
 OCR0  = 0x64;  //set compare
 TCCR0 = 0x02; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
	
 	TCNT0 = 0x9C; //reload counter value
	
}

//TIMER1 initialize - prescale:8
// WGM: 8) PWM phz freq correct, TOP=ICRn
// desired value: 1KHz
// actual value:  1.000KHz (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0x00; //setup
 TCNT1L = 0x00;
 OCR1AH = 0x01;
 OCR1AL = 0xF4;
 OCR1BH = 0x01;
 OCR1BL = 0xF4;
 ICR1H  = 0x01;
 ICR1L  = 0xF4;
 TCCR1A = 0xF0;
 //TCCR1B = 0x12; //start Timer
}

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

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x01;
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//
void main(void)
{
	 unsigned int flower;
     init_devices();
	 init_7279();
	 L_M_Go(Forth, 0);	//左电机向前
	 R_M_Go(Forth, 0);//右电机向前
	 L_M_Go(Back, 0);	//左电机向前
	 R_M_Go(Back, 0);//右电机向前
	 TCCR1B = 0x12;
	 while(1)
	 {
	  flower = get_data(0,0);	//输入左右路径编号	左:A 	右:B
	 
	    switch ( flower )
		{
		 case 0X00:L_M_Go(Forth, 70);break;
		 case 0X01:L_M_Go(Back, 70);break;
		 case 0X02:L_M_Go(Forth, 0);break;
		 case 0X03:R_M_Go(Forth, 99);break;
		 case 0X04:R_M_Go(Back, 99);break;
		 case 0X05:R_M_Go(Forth, 0);break;
		 default:break;
		}
	 }
	
}

void Delay_ms(uint n)
{
	uint i,j;
	for (i = 0; i <n;i++)
		for (j=1;j<(uint)(xtal*143-2);j++);
}

⌨️ 快捷键说明

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