?? dtm8-td-b1.c
字號:
/******************************************/
/* 燕山大學無線電愛好者協會 */
/* 電子調速器 */
/* 作者:張翃鵬 2005-2-27 9:04:40 */
/* 目標MCU:MEGA8 晶振:INT RC 8MHZ */
/******************************************/
#include <iom8v.h>
#include <macros.h>
#include <eeprom.h>
#define uchar unsigned char
#define unit unsigned int
#define ulong unsigned long
#define xtal 8
#define j11 PORTC|=0x3C
#define j10 PORTC&=~0x3c
#define LED1 PORTB|=0x08;
#define LED0 PORTB&=~0x08;
int mark=1500;
unit ai,aa;
uchar i,j,az,flag,flagh,zb,mid,pwm;
void port_init(void)
{
PORTB = 0x21;
DDRB = 0x18;
PORTC = 0x00; //m103 output only
DDRC = 0x3C;
PORTD = 0x00;
DDRD = 0x00;
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00 /*INVALID SETTING*/; //setup
TCNT1L = 0x00 /*INVALID SETTING*/;
OCR1AH = 0x00 /*INVALID SETTING*/;
OCR1AL = 0x00 /*INVALID SETTING*/;
OCR1BH = 0x00 /*INVALID SETTING*/;
OCR1BL = 0x00 /*INVALID SETTING*/;
ICR1H = 0x00 /*INVALID SETTING*/;
ICR1L = 0x00 /*INVALID SETTING*/;
TCCR1A = 0x00;
TCCR1B = 0x02; //start Timer
}
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x00 /*INVALID SETTING*/; //setup
OCR2 = 0x00 /*INVALID SETTING*/;
TCCR2 = 0x01; //start
}
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
WDTCR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}
void init_devices(void)
{
CLI(); //disable all interrupts
port_init();
// timer1_init();
//timer2_init();
//watchdog_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x40; //timer interrupt sources
SEI(); //re-enable interrupts
}
/* 微秒級延時程序 */
void del(int time)
{
do
{
time--;
}
while (time>1);
}
/* 毫秒級延時程序 */
void delay_1ms(void)
{ unsigned int i1;
for(i1=1;i1<(unsigned int)(xtal*143-2);i1++)
;
}
void delay(unsigned int n)
{
unsigned int i2=0;
while(i2<n)
{delay_1ms();
i2++; WDR();
}
}
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
WDR();
pwm++;
if(pwm==250)
{
pwm=0;
if(zb>0)
{
if(flag==1)
j11;
}
}
if(pwm==zb)
j10;
TCNT2 = 0xFb; //reload counter value
}
unit fenc(int bb)
{
int bc;
if((bb<mark)||(bb>(mark+700))) return(0);
flag=1;
bc=bb-mark;
if(bc<=25) { LED1; } else { LED0; }
if(bc<=50) return(0);
if(bc>=300) return(250);
return(bc-50);
}
void cy(void)
{
if(!(PINB&0x01))
{
TCNT1=0;
while(!(PINB&0x01)) WDR();
aa=TCNT1;
zb=fenc(aa);
}
}
void main()
{
EEPROM_READ(0x10,ai);//校正振蕩頻率
OSCCAL=ai;
init_devices();
EEPROM_READ(0x112,ai);
if(ai==0xFFFF)
{
mark=1500; //0xD805
EEPROM_WRITE(0x110,mark);
aa=0;
EEPROM_WRITE(0x112,aa);
}
EEPROM_READ(0x110,mark);
while(1)
{
CLI();
WDR();
}
while(1)
{
cy();
WDR();
if(!(PINB&0x20))
{
delay(10);
if(!(PINB&0x20))
{
cy();
WDR();
LED1;
mark=aa; EEPROM_WRITE(0x110,mark);
CLI();
delay(300);
SEI();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -