📄 ni.c
字号:
//ICC-AVR application builder : 2008-5-18 11:09:55
// Target : M16
// Crystal: 1.0000Mhz
#include <iom16v.h>
#include <macros.h>
# define Read_PD0 ((PIND<<7)>>7) //平衡传感器
# define Read_PD1 ((PIND<<6)>>7) //平衡传感器
# define Read_PD2 ((PIND<<5)>>7) //左右传感器
# define Read_PD3 ((PIND<<4)>>7) //左右传感器
unsigned char sec,second,mini,minute=0,minisecond,miao,fen;
void qianjin2(void);
void run2(void);
void run3(void);
void runback(void);
void qianjin1(unsigned i) ;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0xff;
PORTD = 0x00;
DDRD = 0xc0;
}
///////////////计时中断函数/////////////////////////////////////////
void time_inite(void)
{
TCCR2|= 0x05;//分频比64
mini=0;
second=0;
minisecond=0;
}
#pragma interrupt_handler time2_isr:5
void time2_isr(void)//minute\second为两个最大为99的整数
{
TCNT2=0xb2;
minisecond++;
if (minisecond<100)//计算总时间
;
else
{
minisecond=0;
if (second<60)second++;
else
{
second=0;
minute++;
if(minute==100)
minute=0;
}
}
miao=second;
fen=minute;
}
///////////////计时中断函数/////////////////////////////////////////
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
TIMSK|=0x40;//开始计时
//all peripherals are now initialized
}
void delay_us(unsigned int time)
{//微秒级延时程序
do
{
time--;
}
while (time>1);
}
void delay_ms(unsigned int time)
{//毫秒级延时程序
while(time!=0)
{
delay_us(1000);
time--;
}
}
void delay_s(unsigned int time)
{//毫秒级延时程序
while(time!=0)
{
delay_ms(1000);
time--;
}
}
void run3()
{
while(1)
{
if((Read_PD2==1)&&(Read_PD3==1))
{
PORTD&=~0x80;
PORTC&=0x00;
runback();
}
else if((Read_PD2!=1)&&(Read_PD3==1))
{
PORTD&=~0x80;
PORTC|=0x80;
runback();
// PORTD&=0x00;
PORTC&=0x00;
}
else if((Read_PD2==1)&&(Read_PD3!=1))
{
// PORTD|=0x80;
PORTC&=0x00;
runback();
// PORTD&=0x00;
PORTC&=0x00;
}
else
{
PORTD&=0x00;
PORTC&=0x00;
}
}
}
void run2()
{
while(1)
{
/* if((Read_PD2==1)&&(Read_PD3==1))
{
//PORTD&=0x00;
// PORTC&=0x00;
PORTD&=~0x80;
PORTC&=~0x80;
TIMSK&=~0x40;
second=0;
TIMSK|=0x40;//开始计时
PORTD|=0x40; //亮灯
while (second<2)
{
PORTD&=~0x80;
PORTC&=~0x80;
}
PORTD&=~0x40; //关灯
//run3();
}
else */if((Read_PD2!=1)&&(Read_PD3==1))
{
PORTD&=~0x80;
PORTC|=0x80;
qianjin2();
PORTD&=~0x80;
PORTC&=~0x80;
}
else if((Read_PD2==1)&&(Read_PD3!=1))
{
PORTD|=0x80;
PORTC&=~0x80;
qianjin2();
PORTD&=~0x80;
PORTC&=~0x80;
}
else if((Read_PD2!=1)&&(Read_PD3!=1))
{
qianjin2();
}
else
{
//PORTD|=0x40;
} ;
}
}
void runback()
{
PORTC|=0x01; //调速
delay_ms(1);
PORTC&=0x00;
delay_ms(2);
}
void banalance()
{
TIMSK&=~0x40;
while(1)
{
if((Read_PD0==1)&&(Read_PD1==1))
{
PORTC&=0x00;
second=0;
TIMSK|=0x40;//开始计时
PORTD|= 0x40; //亮灯
while (second<2)
{
//PORTD&=0x00;
PORTC&=0x00;
}
PORTD&= ~0x40; //关灯
run2();
}
else if((Read_PD0!=1)&&(Read_PD1==1))//后退 慢
{
PORTC|=0x01; //调速
delay_ms(4);
PORTC&=0x00;
delay_ms(1);
}
else if((Read_PD0==1)&&(Read_PD1!=1))//前进
{
PORTC|=0x02; //调速
delay_ms(2);
PORTC&=0x00;
delay_ms(1);
}
else
{
PORTC&=0x00;
}
}
}
void banalance1()
{
TIMSK&=~0x40;
while(1)
{
if((Read_PD0==1)&&(Read_PD1==1))
{ PORTD|= 0x40; //亮灯
PORTC&=0x00;
/* second=0;
TIMSK|=0x40;//开始计时
while (second<2)
{
//PORTD&=0x00;
PORTC&=0x00;
}
PORTD&= ~0x40; //关灯
//run2();*/
}
else if((Read_PD0!=1)&&(Read_PD1==1))//后退 慢 亮灯
{ PORTD&= ~0x40;
PORTC|=0x01; //调速
delay_us(15);
PORTC&=0x00;
delay_us(3);
}
else if((Read_PD0==1)&&(Read_PD1!=1))//前进
{ PORTD&= ~0x40;
PORTC|=0x02; //调速
delay_ms(2);
PORTC&=0x00;
delay_ms(1);
}
else
{ PORTD&= ~0x40;
PORTC&=0x00;
}
}
}
void qianjin1(unsigned i) //上坡
{
unsigned int j;
for(j=0;j<i;j++)
{
PORTC|=0x02; //走
delay_ms(2); //可调
PORTC&=0x00; //停
delay_ms(1);//可调
}
}
void qianjin2()//下坡
{
PORTC|=0x02;
delay_ms(2);
PORTC&=0x00;
delay_ms(1);
}
void main()
{
time_inite();
init_devices();
while(1)
{//run2();
qianjin1(80);
banalance1();
}
}
/*
if((Read_PD0==1)&&(Read_PD1==1))
{
PORTC&=0x00;
}
else if((Read_PD0!=1)&&(Read_PD1==1))//后退
{
PORTC|=0x01;
delay_ms(7);
PORTC&=0x00;
delay_ms(1);
}
else if((Read_PD0==1)&&(Read_PD1!=1))//前进
{
//PORTD|=0x80; //前行右拐
PORTC|=0x02;
delay_ms(2);
PORTC&=0x00;
delay_ms(2);
//PORTD&=0x00;
}
else
{
PORTC&=0x00;
}
if((Read_PD2==1)&&(Read_PD3==1))
{
PORTD&=0x00;
PORTC&=0x00;
}
else if((Read_PD2!=1)&&(Read_PD3==1))
{
PORTD&=0x00;
PORTC|=0x80;
}
else if((Read_PD2==1)&&(Read_PD3!=1))
{
PORTD|=0x80;
PORTC&=0x00;
}
else
{
PORTD&=0x00;
PORTC&=0x00;
}
} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -