xuechenghao 发表于 2008-2-29 16:15:40

怎样用M128产生50hz占空比可调的pwm波?

我调了好久了,引脚一点反应都没有,以下是我的程序,在时钟1中断里改变OCR1A,OCR1B,OCR1C的值来调节占空比,请各位帮我看看吧:
unsigned charangle0_h=0x05,angle0_l=0xDC;
unsigned charangle1_h=0x05,angle1_l=0xDC;
unsigned charangle2_h=0x05,angle2_l=0xDC;
static void io_init(void)//
{
        PORTA = 0x08;
        DDRA= 0xFF;
        PORTB = 0xFF;
        DDRB= 0xFF;
        PORTC = 0x00;
        DDRC= 0x00;
        PORTD = 0xFF;
        DDRD= 0xFF;
        PORTE = 0xFF;
        DDRE= 0x0F;
        PORTF = 0xFF;
        DDRF= 0xFF;
        PORTG = 0x1F;
        DDRG= 0x1F;
}
///////////////////////////////////////////
//时钟初始化:
void timer1_init(void)
{       TCCR1B = 0x00;                                                                                                                                    //stop
       
       TCNT1H = 0x00;                                                                                                                                 //setup
       TCNT1L = 0x00;

       OCR1AL = 0xDC;
       OCR1AH = 0x05;//1500
       OCR1BH = 0x05;
       OCR1BL = 0xDC;//1500
       OCR1CH = 0x05;
       OCR1CL = 0xDC;//1500
       ICR1H= 0x4E;
       ICR1L= 0x20;//20000

       TCCR1A = 0xAA;//COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10
                        //1          0   1       0      1      0             1           0
       TCCR1B = 0x1A;//ICNC1 ICES1 – WGM13 WGM12 CS12 CS11 CS10                                                                               
}                     //0      0   0   1   1    0   1    0   8分频
////////////////////////////////////////////////////////////////////////////
#pragma interrupt_handler timer1_ovf_isr:15
void timer1_ovf_isr(void)
{
       OCR1AH = angle0_h;
       OCR1AL = angle0_l;
       OCR1BH = angle1_h;
       OCR1BL = angle1_l;
       OCR1CH = angle2_h;
       OCR1CL = angle2_l;
}
void DeviceInit(void)
{
        CLI();
        io_init;
        timer1_init();
      TIMSK = 0x04; //timer interrupt sources
        MCUCR = 0x00;
        SEI();
}

Gorgon_Meducer 发表于 2008-3-8 10:44:25

对不起,今天才看到帖子。

//ICC-AVR application builder : 2008-3-8 AM 10:36:14
// Target : M8
// Crystal: 4.0000Mhz

#include <iom8v.h>
#include <macros.h>

void port_init(void)
{
PORTB = 0x00;
DDRB= 0x00;
PORTC = 0x00; //m103 output only
DDRC= 0x00;
PORTD = 0x00;
DDRD= 0x00;
}

//TIMER1 initialize - prescale:1
// WGM: 8) PWM phz freq correct, TOP=ICRn
// desired value: 50Hz
// actual value: 50.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x63; //setup
TCNT1L = 0xC0;
OCR1AH = 0x9C;
OCR1AL = 0x40;
OCR1BH = 0x9C;
OCR1BL = 0x40;
ICR1H= 0x9C;
ICR1L= 0x40;
TCCR1A = 0x80;
TCCR1B = 0x11; //start Timer
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();

MCUCR = 0x00;
GICR= 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}


记得,一定要把OCR1A的端口方向设置为输出

xuechenghao 发表于 2008-4-6 04:36:55

谢谢
页: [1]
查看完整版本: 怎样用M128产生50hz占空比可调的pwm波?