📄 avr单片机的嵌入式开发-照片显影机控制及显示程序.c
字号:
/*void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0xFF; //m103 output only
DDRC = 0x00;
PORTD = 0xF0;
DDRD = 0x0F;
PORTE = 0x00;
DDRE = 0x01;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x1C;
DDRG = 0x03;
}*/
void timer1_init(void)
{
CLI();
TCNT1H = 0x00; //setup
TCNT1L = 0x00;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
OCR1BH = 0x00;
OCR1BL = 0x00;
TCCR1A = 0x54;//ctc
//start Timer64分
TCCR1B = 0x00;
TIMSK = 0x12; //timer interrupt sources
SEI();
}
#pragma interrupt_handler timer1_compa_isr:13
void timer1_compa_isr(void)
{
//1启动过渡
if(step_count3<step3_ready)
{
mt3_adsb++;
if (mt3_adsb==2)
{
mt3_count-=11;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
mt3_adsb=0;
}
}
if(step_count3==step3_ready)
{
mt3_count=250;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
}
//1停机过渡
if((step_count3>(steps3_1-step3_ready))&&(step_count3<steps3_1))
{
mt3_adsb++;
if (mt3_adsb==2)
{
mt3_count+=11;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
mt3_adsb=0;
}
}
//2启动过渡
if((step_count3>steps3_1)&&(step_count3< (steps3_1+step3_ready) ))
{
mt3_adsb++;
if (mt3_adsb==2)
{
mt3_count-=11;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
mt3_adsb=0;
}
}
if(step_count3==(steps3_1+step3_ready))
{
mt3_count=250;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
}
//2停机过渡
if((step_count3<steps3_2)&&(step_count3>(steps3_2-step3_ready) ))
{
mt3_adsb++;
if (mt3_adsb==2)
{
mt3_count+=11;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
mt3_adsb=0;
}
}
//3启动过渡
if((step_count3<(steps3_2+step3_ready))&&(step_count3>steps3_2))
{
mt3_adsb++;
if (mt3_adsb==2)
{
mt3_count-=11;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
mt3_adsb=0;
}
}
if(step_count3==(steps3_2+step3_ready))
{
mt3_count=250;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
}
//3停机过渡
if(step_count3>(steps3-step3_ready))
{
mt3_adsb++;
if (mt3_adsb==2)
{
mt3_count+=11;
OCR1AH = mt3_count/256;
OCR1AL = mt3_count%256;
mt3_adsb=0;
}
}
//累加
if(step_count3<=steps3)
{
step_count3++;
}
else
{
step_count3=0;
TCCR1B = 0x00;
}//compare occured TCNT1=OCR1A
}
//TIMER0 initialize - prescale:32
// WGM: CTC
// desired value: 2KHz
// actual value: 1.984KHz (-0.8%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x00; //set count
OCR0 =mt2_count;
TCCR0 = 0x00; //暂时关闭
}
/*
OCR0 = 0x9C;//200hz,156
OCR0 = 0x0f;//2000hz,15
TCCR0 = 0x06;//256分频
// #define step2_ready 140
//unsigned int mt2_count=156 ;
*/
#pragma interrupt_handler timer0_comp_isr:16
void timer0_comp_isr(void)
{
//启动过渡
/* if(step_count2<steps2_ready)
{
mt2_count-=1;
OCR0 = mt2_count;
}
if(step_count2==steps2_ready)
{
OCR0 = 15;
}
//停机过渡
if(step_count2>(steps2-steps2_ready))
{
mt2_count+=1;
OCR0 = mt2_count;
}*/
if(step_count2<=steps2)
step_count2++;
else
{
step_count2=0;
TCCR0= 0x00;
}
}
/////////////////////////////////////////
//pwm波按键控制程序//
///////////////////////////////////////////
void motor1_key(void)//第一电机的步数按键扫描
{
delay_ms(15);
if(mt1_up ==0)
{
step_inc1+=20;
while(mt1_up ==0) {; }
}
else if (mt1_down==0)
{
if(step_inc1>0)
step_inc1-=20;
while(mt1_down==0){;}
}
send_lcd(1);
}
////////////////////////////////////////////////////////////////////
void motor3_key(void)//第三电机的步数按键扫描
{
while(1)
{
if(mt3_up ==0)
break;
else if(mt3_down==0)
break;
}
delay_ms(15);
if(mt3_up ==0)
{
step_inc3+=20;
while(mt3_up ==0)
{; }
}
else if (mt3_down==0)
{
if(step_inc3>0)
step_inc3-=20;
while(mt3_down==0)
{;}
}
send_lcd(3);
}
void motor2_key(void)//第二电机的步数按键扫描
{
while(1)
{
if(mt2_up ==0)
break;
else if(mt2_down==0)
break;
}
delay_ms(15);
if(mt2_up ==0)
{
step_inc2+=5;
while(mt2_up ==0)
{; }
}
else if (mt2_down==0)
{
if(step_inc2>0)
step_inc2-=5;
while(mt2_down==0)
{;}
}
send_lcd(2);
}
///////////////////////////////////////////////////////////////////
/*纸张尺寸选择
按键引脚:
5寸——PE2
6寸---PE3
7寸---PE4
8寸---PE5*/
/////////////////////////////////////////////////////////////////
void selec_key(void)//纸张型号设定键查询扫描,使用时根据情况循环。并且处理
{
delay_ms(15);
if(s5==0)
{
keynum=5;
while(s5==0){;}
}
if(s6==0)
{
keynum=6;
while(s6==0) {;}
}
if(s7==0)
{
keynum=7;
while(s7==0){;}
}
else if(s8==0)
{
keynum=8;
while(s8==0){;}
}
switch (keynum)
{
case 5: step_num1=8000;step_num2=3200;step_num3=24000;PORTG=0x00;break;
case 6: step_num1=9600;step_num2=3200;step_num3=28800;PORTG=0x02;break;
case 7: step_num1=11200;step_num2=3200;step_num3=33600;PORTG=0x03;break;
case 8: step_num1=12800;step_num2=3200;step_num3=34800;PORTG=0x01;break;
default: break;
}
selec_lcd(keynum);
}
void send_to_port(void)
{
if(MD<16)
{
PORTD=~MD;//能否保证高四位不受影响?
}
else
{
PORTD=0xff;
}
}
//////////////////////////////////////////////////////////////
//密度设定按键查询
//////////////////////////////////////////////////////////////
void MD_key(void)
{
while(1)
{
if(key_add==0)
break;
else if(key_sub==0)
break;
}
delay_ms(15);
if(key_add==0)
{
if(MD<16)
MD++;
while(key_add==0)
{;}
}
else if (key_sub==0)
{
if(MD>0)
MD--;
while(key_sub==0)
{;}
}
send_to_port();
}
//////////////////////////////////////////////////////////////////
//待机按键查询函数
////////////////////////////////////////////////////////////
void waiting(void)
{
send_to_port();
wcount_lcd();
while(1)
{
if((mt1_up==0)||(mt1_down==0)) motor1_key();
if((mt2_up==0)||(mt2_down==0)) motor2_key();
if((mt3_up==0)||(mt3_down==0)) motor3_key();
if((s5==0)||(s6==0)) selec_key();
if ((s7==0)||(s8==0)) selec_key();
if((key_add==0)||(key_sub==0)) MD_key();
else if(cls_key==0) {count=0; wcount_lcd();}//清零张数
else if(start_key==0) break;
// wcount_lcd();
}
}
//-------启动曝光函数----------------------//
void start(void)
{
timer0_init();//此时电机为关,应调整该函数中的控制寄存器
timer1_init();
start_lcd();
}
/////////////////////////////////////////////////////////////////
//主函数
///////////////////////////////////////////////////////////////
void main(void)
{
//port_init();//端口初始化
PORTA = 0x00;
DDRA = 0xFF;//输出液晶口
PORTB = 0x00;
DDRB = 0xFF;//电机驱动口
PORTC = 0xFC; //m103 output only
DDRC = 0x00;//电机调整键输入
PORTD = 0xF0;
DDRD = 0x0F;//高位输入按键,低位输出密度值
PORTE = 0x00;
DDRE = 0x82;//尺寸选择,液晶信号,7脚输出
PORTF = 0x01;
DDRF = 0xff;
PORTG = 0x1C;
DDRG = 0x03;
////////////////////////////////////////////
ocmj_init();//液晶初始化
while(1)
{
waiting(); //等待按键
start(); //开始画面
TCCR1B = 0x0a;//开电机1和3
while(1)
{
if(step_count3==steps3_1)
{
TCCR1B = 0x00;//关电机1/3;
break;
}
}
// delay_ms(1000); //延时稳定
clr_dir2; //正向转动
delay_ms(10);//电平稳定
TCCR0=0x1b; //电机2启动,切刀
while(!(step_count2==0))
{;}
delay_ms(10);
set_dir2; //反向转动
delay_ms(10);//电平稳定
TCCR0=0x1b; //电机2启动,收刀
//delay_ms(1000); //延时稳定
DDRB=0xDF; //关电机1
TCCR1B = 0x0a;//开电机3
while (1)
{if (step_count3==steps3_2) break; }
TCCR1B = 0x00;//到位。关电机3
///////////////////////////
delay_ms(1000); //延时稳定
/////////////////
PORTF&=0x00;//PG2高电平
delay_ms(30);//给555脉冲,此处须精确延时,不可太长.
PORTF|=0x01;//PG2低电平
delay_ms(1000);//电平建立
exposal_lcd(); //曝光等待画面
while(!(end_ack==0))//等待显影结束信号。无设备调试时,可将此句注释掉,
{ ; } //屏蔽反馈信号,以完成整个运转
count++; //曝光完成,张数增加一
TCCR1B = 0x0a;//开电机3,送入落槽
while(1)
{
if (step_count3==0)
{
DDRB=0xff;//全开开pwm输出口
break;
}
}//电机停转,回到待机状态
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -