|
这个电调是我用我公司实验室里的电路板飞线做的(那板是仪器里拆出来的),接收机的输出用信号发生器来模拟的,用信号发生器试验已成功,当时IRF管用的是16A的IRF530,实验后我买了MEGA8和MEGA16做一个真实放到船上试的,但不知为什么我的这个网上买的二个MEGA8都下载不到,MEGA16能下,但我移植了一下,不知为什么不行,近来公司又有新的项目所以无时间了,拿出来和大家分享一下算了,本了用AVR只是玩玩,所以可能有错的地方.用ICC写的,原理简单,有条件的朋友可以玩玩
//ICC-AVR application builder : 2005-3-23 10:23:44
// Target : M128
// Crystal: 16.000Mhz
#include <iom128v.h>
#include <macros.h>
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<<ICF1);
/*
//TCNT1=0;
//timer 1 input capture event, read (int)value in ICR1 using;
down=ICR1L; //Read low byte first (important)
down|=(int)ICR1H << 8; //Read high byte and shift into top byte
ICR1=0;
TCNT1=0;
a=down-up;
T=a/16;
OCR1B = T;
TCCR1B^=0x40;
TIFR=(1<<ICF1);
printf("T=%d,down=%d,up=%d\n",T,down,up);
*/
}
else
{
TCNT1=0;
TCCR1B^=0x40;
TIFR=(1<<ICF1);
/*
up=ICR1L; //Read low byte first (important)
up|=(int)ICR1H << 8; //Read high byte and shift into top byte
ICR1=0;
TCCR1B^=0x40;
TIFR=(1<<ICF1);
*/
}
}
//UART0 initialisation
// desired baud rate: 11520
// actual: baud rate:11520
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x08; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
timer1_init();
uart0_init();
timer0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x20; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
void main(void)
{
int T,up,down,a;
a='d';
init_devices();
printf("ok!");
while(1)
{
//printf("%u",T);
}
} |
欢迎继续阅读楼主其他信息
|