📄 control._c
字号:
//ICC-AVR application builder : 2006-12-10 17:22:46
// Target : M8
// Crystal: 8.0000Mhz
//ICC-AVR application builder : 2006-12-10 21:46:41
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
long Ddummy,Counter_MotorSpeedPulse,Counter_IRPulse,Settings_IRTopValue;
char Settings_PrintQuality;
const FTC2 =64;
void port_init(void)
{
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x7F; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
}
//TIMER1 initialisation - prescale:1
// WGM: 4) CTC, TOP=OCRnA
// desired value: 1KHz
// actual value: 1.000KHz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1 = 0x00; //setup
ICR1H = 0x1F;
ICR1L = 0x3F;
OCR1A = FTC2*Settings_PrintQuality; //每组Settings_PrintQuality个墨滴选一个充电
OCR1B = FTC2/2; //充电脉宽是墨滴周期时间的一半,充电相位为0,充电好坏由断点调整
TCCR1A = 0x50;
TCCR1B = 0x09; //start Timer
}
//
#pragma interrupt_handler timer1_compa_isr:7
void timer1_compa_isr(void) //开始一个新的充电墨滴时PB1开始产生一个下降沿或上升沿
{ //compare occured TCNT1=OCR1A
//if(PINB1==0){ //如果PB1是低电平,就开始一个新的DA转换
// ;
//}
}
#pragma interrupt_handler timer1_compb_isr:8
void timer1_compb_isr(void) //停止充电时刻,PB2开始产生上升沿或下降沿
{
//compare occured TCNT1=OCR1B
}
//
//TIMER2 initialisation - prescale:1
// WGM: Normal
// desired value: 62.5KHz
// actual value: 62.5KHz (0.0%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x00; //setup
OCR2 = FTC2; //0x40;振荡频率的分频值,8M-64,4M-32
TCCR2 = 0x19; //CTC 模式
}
#pragma interrupt_handler int0_isr:2
void int0_isr(void) //external interupt on INT0 记录电机速度
{
Counter_MotorSpeedPulse++;
if(Counter_MotorSpeedPulse>1000){
if(PINB5>0){PORTB &=223;} //PB5脚置低电平
else{PORTB |=32;} //PB5脚置高电平
Counter_MotorSpeedPulse =0;
//计算并显示电机速度
}
}
#pragma interrupt_handler int1_isr:3
void int1_isr(void) //external interupt on INT1 记录光电开关的触发脉冲
{
Counter_IRPulse++;
if(Counter_IRPulse >= Settings_IRTopValue){
if(PINB6>0){PORTB &=191;} //PB6脚置低电平
else{PORTB |=64;} //PB6脚置高电平
Counter_IRPulse=0;
//计算并触发打印按钮
}
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer2_init();
timer1_init();
MCUCR = 0x0F;
GICR = 0xC0;
TIMSK = 0x18; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
void main()
{
Settings_IRTopValue = 1000; //光电开关触发Settings_IRTopValue次后就开始打印一条信息
Settings_PrintQuality = 10; //10个墨滴选一个充电
Counter_MotorSpeedPulse =0;
Counter_IRPulse =0;
init_devices();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -