电调程序
//ICC-AVR application builder : 2005-3-23 10:23:44
// Target : M128
// Crystal: 16.000Mhz
#include
#include
unsigned int up,down,a; unsigned int T;
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0xFF; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0xFF;
DDRE = 0x00;
PORTF = 0xFF;
DDRF = 0x00;
PORTG = 0x1F;
DDRG = 0x00;
}
//TIMER0 initialisation - prescale:64
// WGM: PWM Fast
// desired value: 1.5KHz // actual value: 0.977KHz (-53.6%)
void timer0_init(void) {
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode TCNT0 = 0x00; //set count OCR0 = 0xFF;
TCCR0 = 0x7C; //start timer }
//TIMER1 initialisation - prescale:1 // WGM: 15) PWM fast, TOP=OCRnA // desired value: 1uSec
// actual value: 1.000uSec (0.0%) void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0xF1;
OCR1AH = 0xFF; //OCR1AH = 0x29; OCR1AL = 0xFF; //OCR1AL = 0xA9; OCR1BH = 0x11;
OCR1BL = 0x11;
OCR1CH = 0x00;
OCR1CL = 0x00;
ICR1H = 0x00;
ICR1L = 0x00;
TCCR1A = 0x33;
TCCR1B = 0xD9; //start Timer }
#pragma interrupt_handler timer1_capt_isr:12
void timer1_capt_isr(void) {
if((TCCR1B&0x40)==0)
{
down=ICR1L; //Read low byte first (important)
down|=(int)ICR1H << 8; //Read high byte and shift into top byte
T=down/16;
//OCR1B=T;
//if(T>=1000)
//{T=1000;}
//OCR0 = 254;
a=T;
//T=T-994;
if(T<=994)
OCR0=0x00;
else
if(T>=1994)
OCR0=0xFF;
else
OCR0 = (T/3.921);
// a=T/4;
printf("T=%u,down=%u,a=%d\n",T,down,a);
TCCR1B^=0x40;
TIFR=(1<