打印
[AVR单片机]

AVR ATmega1280定时器PWM输出

[复制链接]
1189|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yanmingyuan|  楼主 | 2015-6-10 16:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "kernel.h"

//ht
tp://
item.taobao.com/item.htm?spm=a1z09.5.0.0.juUpj0&id=37491969103&_u=a4gbj5f8f0e[/url]

unsigned int PWM_Buf[6];//6路PWM频率

unsigned long timer0_ticks;
unsigned long timer0_tickssec;
unsigned long timer1_ticks;
unsigned long timer1_ticksmin;
unsigned long timer2_ticks;
unsigned long timer2_tickssec;
unsigned long timer3_ticks;
unsigned long timer3_ticksmin;
unsigned long timer4_ticks;
unsigned long timer4_ticksmin;
unsigned long timer5_ticks;
unsigned long timer5_ticksmin;

void Timer0_Init(void)
{
    timer0_ticks = 0;
    timer0_tickssec = 0;
   
    TCCR0A = 0x00;
    TCCR0B = 0x00;
    TCNT0 = 0x00;
    OCR0A = F_CPU/(1024-1)/20;//频率F_PWM
    OCR0B = OCR0A/2;//占空比50%
    //TCCR0A = 0xA3;  //COM0A1 COM0A0 COM0B1 COM0B0 – – WGM01 WGM00
    TCCR0B = 0xCD;  //FOC0A FOC0B – – WGM02 CS02 CS01 CS00
    /*
    TCCR0A = 0x00;
    TCCR0B = 0x00;//stop
    TCNT0  = 0xE7;//25us
    OCR0A  = 0x18;
    OCR0B  = 0x18;
    TCCR0A = 0x03;
    TCCR0B = 0x02;//start,clkT0S/8
    TIMSK0|= 0x01;//enable 0verflow interrupt*/
}

void TC0_Set_PWM(unsigned int curF)
{//20~80为clkT0S/1024,80~300为clkT0S/256,300~2000为clkT0S/64
    if(curF==0)
    {
        OCR0A = 0;
        OCR0B = 0;
        TCCR0A = 0;
    }
    else if((curF>=20)&&(curF<80))
    {
        OCR0A = F_CPU/(1024-1)/curF;
        OCR0B = OCR0A/2;
        TCCR0A = 0xA3;
        TCCR0B = 0xCD;
    }
    else if((curF>=80)&&(curF<300))
    {
        OCR0A = F_CPU/(256-1)/curF;
        OCR0B = OCR0A/2;
        TCCR0A = 0xA3;
        TCCR0B = 0xCC;
    }
    else if((curF>=300)&&(curF<=2000))
    {
        OCR0A = F_CPU/(64-1)/curF;
        OCR0B = OCR0A/2;
        TCCR0A = 0xA3;
        TCCR0B = 0xCB;
    }
}

void Timer1_Init(void)
{
    unsigned int temp;
   
    timer1_ticks = 0;
    timer1_ticksmin = 0;
   
    TCCR1A = 0x00;
    TCCR1B = 0x00;
    TCCR1C = 0x00;
    TCNT1H = 0x00;
    TCNT1L = 0x00;
    temp = F_CPU/16/20;
    OCR1A=temp;
    //temp = temp/2;
    //OCR1B=temp;
    //OCR1C=temp;
    //TCCR1A=(0<<COM1A1)|(1<<COM1A0)|/*(1<<COM1B1)|(0<<COM1B0)|(1<<COM1C1)|(0<<COM1C0)|*/(1<<WGM11)|(1<<WGM10);
    TCCR1B=(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(1<<CS11)/*|(1<<CS10)*/;
    /*
    TCCR1A = 0x00;//COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10
    TCCR1B = 0x00;//stop,ICNC1 ICES1 – WGM13 WGM12 CS12 CS11 CS10
    TCCR1C = 0x00;//FOC1A FOC1B FOC1C – – – – –
    TCNT1H = 0xE3;
    TCNT1L = 0xE0;
    OCR1AH = 0x1C;
    OCR1AL = 0x1F;
    OCR1BH = 0x1C;
    OCR1BL = 0x1F;
    OCR1CH = 0x1C;
    OCR1CL = 0x1F;
    ICR1H  = 0x00;
    ICR1L  = 0x00;
    TCCR1B = 0x05;
//    TIMSK |= 0x04;//enable 0verflow interrupt*/
}

void TC1_Set_PWM(unsigned int curF)
{
    unsigned int temp;
   
    if(curF==0)
    {
        //OCR1A = 0;
        //OCR1B = 0;
        TCCR1A=0;
    }
    else if((curF>=20)&&(curF<=2000))
    {
        temp = F_CPU/16/curF;
        OCR1A=temp;
        TCCR1A=(0<<COM1A1)|(1<<COM1A0)|/*(1<<COM1B1)|(0<<COM1B0)|(1<<COM1C1)|(0<<COM1C0)|*/(1<<WGM11)|(1<<WGM10);
    }
}

void Timer2_Init(void)
{
    timer2_ticks = 0;
    timer2_tickssec = 0;
   
    TCCR2A = 0x00;
    TCCR2B = 0x00;
    TCNT2 = 0x00;
    OCR2A = F_CPU/(1024-1)/20;//频率F_PWM
    OCR2B = OCR2A/2;//占空比50%
    //TCCR2A = 0xA3;  //COM2A1 COM2A0 COM2B1 COM2B0 – – WGM21 WGM20
    TCCR2B = 0xCF;  //FOC2A FOC2B – – WGM22 CS22 CS21 CS20
   
//    TCCR2 = 0x00;//stop,FOC2 WGM20 COM21 COM20 WGM21 CS22 CS21 CS20
//    TCNT2 = 0xB8;//set count 10ms
//    OCR2  = 0x47;
//    TCCR2 = 0x05;//start,clkT0S/1024
//    TIMSK|= 0x40;//enable 0verflow interrupt
}

void TC2_Set_PWM(unsigned int curF)
{//20~80为clkT0S/1024,80~300为clkT0S/256,300~2000为clkT0S/64
    if(curF==0)
    {
        OCR2A = 0;
        OCR2B = 0;
        TCCR2A = 0;
    }
    else if((curF>=20)&&(curF<80))
    {
        OCR2A = F_CPU/(1024-1)/curF;
        OCR2B = OCR2A/2;
        TCCR2A = 0xA3;
        TCCR2B = 0xCF;
    }
    else if((curF>=80)&&(curF<300))
    {
        OCR2A = F_CPU/(256-1)/curF;
        OCR2B = OCR2A/2;
        TCCR2A = 0xA3;
        TCCR2B = 0xCE;
    }
    else if((curF>=300)&&(curF<=2000))
    {
        OCR2A = F_CPU/(64-1)/curF;
        OCR2B = OCR2A/2;
        TCCR2A = 0xA3;
        TCCR2B = 0xCC;
    }
}

void Timer3_Init(void)
{
    unsigned int temp;
   
    timer3_ticks = 0;
    timer3_ticksmin = 0;
   
    TCCR3A = 0x00;
    TCCR3B = 0x00;
    TCCR3C = 0x00;
    TCNT3H = 0x00;
    TCNT3L = 0x00;
    temp = F_CPU/16/20;
    OCR3A=temp;
    //temp = temp/2;
    //OCR3B=temp;
    //OCR3C=temp;
    //TCCR3A=(0<<COM3A1)|(1<<COM3A0)|/*(1<<COM3B1)|(0<<COM3B0)|(1<<COM3C1)|(0<<COM3C0)|*/(1<<WGM31)|(1<<WGM30);
    TCCR3B=(1<<WGM33)|(1<<WGM32)|(0<<CS32)|(1<<CS31)/*|(1<<CS30)*/;
    /*
    TCCR3A = 0x00;//COM3A1 COM3A0 COM3B1 COM3B0 COM3C1 COM3C0 WGM31 WGM30
    TCCR3B = 0x00;//stop,ICNC3 ICES3 – WGM33 WGM32 CS32 CS31 CS30
    TCCR3C = 0x00;//FOC3A FOC3B FOC3C – – – – –
    TCNT3H = 0xE3;
    TCNT3L = 0xE0;//set count 1s
    OCR3AH = 0x1C;
    OCR3AL = 0x1F;
    OCR3BH = 0x1C;
    OCR3BL = 0x1F;
    OCR3CH = 0x1C;
    OCR3CL = 0x1F;
    ICR3H  = 0x00;
    ICR3L  = 0x00;
    TCCR3B = 0x05;//start,clkT0S/1024
//    ETIMSK|= 0x04;//enable 0verflow interrupt*/
}

void TC3_Set_PWM(unsigned int curF)
{
    unsigned int temp;
   
    if(curF==0)
    {
        //OCR3A = 0;
        //OCR3B = 0;
        TCCR3A=0;
    }
    else if((curF>=20)&&(curF<=2000))
    {
        temp = F_CPU/16/curF;
        OCR3A=temp;
        TCCR3A=(0<<COM3A1)|(1<<COM3A0)|/*(1<<COM3B1)|(0<<COM3B0)|(1<<COM3C1)|(0<<COM3C0)|*/(1<<WGM31)|(1<<WGM30);
    }
}

void Timer4_Init(void)
{
    unsigned int temp;
   
    timer4_ticks = 0;
    timer4_ticksmin = 0;
   
    TCCR4A = 0x00;
    TCCR4B = 0x00;
    TCCR4C = 0x00;
    TCNT4H = 0x00;
    TCNT4L = 0x00;
    temp = F_CPU/16/20;
    OCR4A=temp;
    //temp = temp/2;
    //OCR4B=temp;
    //OCR4C=temp;
    //TCCR4A=(0<<COM4A1)|(1<<COM4A0)|/*(1<<COM4B1)|(0<<COM4B0)|(1<<COM4C1)|(0<<COM4C0)|*/(1<<WGM41)|(1<<WGM40);
    TCCR4B=(1<<WGM43)|(1<<WGM42)|(0<<CS42)|(1<<CS41)/*|(1<<CS40)*/;
}

void TC4_Set_PWM(unsigned int curF)
{
    unsigned int temp;
   
    if(curF==0)
    {
        //OCR4A = 0;
        //OCR4B = 0;
        TCCR4A=0;
    }
    else if((curF>=20)&&(curF<=2000))
    {
        temp = F_CPU/16/curF;
        OCR4A=temp;
        TCCR4A=(0<<COM4A1)|(1<<COM4A0)|/*(1<<COM4B1)|(0<<COM4B0)|(1<<COM4C1)|(0<<COM4C0)|*/(1<<WGM41)|(1<<WGM40);
    }
}

void Timer5_Init(void)
{
    unsigned int temp;
   
    timer5_ticks = 0;
    timer5_ticksmin = 0;
   
    TCCR5A = 0x00;
    TCCR5B = 0x00;
    TCCR5C = 0x00;
    TCNT5H = 0x00;
    TCNT5L = 0x00;
    temp = F_CPU/16/20;
    OCR5A=temp;
    //temp = temp/2;
    //OCR5B=temp;
    //OCR5C=temp;
    //TCCR5A=(0<<COM5A1)|(1<<COM5A0)|/*(1<<COM5B1)|(0<<COM5B0)|(1<<COM5C1)|(0<<COM5C0)|*/(1<<WGM51)|(1<<WGM50);
    TCCR5B=(1<<WGM53)|(1<<WGM52)|(0<<CS52)|(1<<CS51)/*|(1<<CS50)*/;
}

void TC5_Set_PWM(unsigned int curF)
{
    unsigned int temp;
   
    if(curF==0)
    {
        //OCR5A = 0;
        //OCR5B = 0;
        TCCR5A=0;
    }
    else if((curF>=20)&&(curF<=2000))
    {
        temp = F_CPU/16/curF;
        OCR5A=temp;
        TCCR5A=(0<<COM5A1)|(1<<COM5A0)|/*(1<<COM5B1)|(0<<COM5B0)|(1<<COM5C1)|(0<<COM5C0)|*/(1<<WGM51)|(1<<WGM50);
    }
}

/*******************************************************************************
* Function Name  : TIMER0_OVF_vectFunc
* Description    : 定时器0溢出中断程序
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
#pragma vector = TIMER0_OVF_vect
__interrupt void TIMER0_OVF_vectFunc(void)
{
   
}

/*******************************************************************************
* Function Name  : TIMER1_OVF_vectFunc
* Description    : 定时器1溢出中断程序
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
#pragma vector = TIMER1_OVF_vect
__interrupt void TIMER1_OVF_vectFunc(void)
{
   
}

/*******************************************************************************
* Function Name  : TIMER2_OVF_vectFunc
* Description    : 定时器2溢出中断程序
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
#pragma vector = TIMER2_OVF_vect
__interrupt void TIMER2_OVF_vectFunc(void)
{
   
}

/*******************************************************************************
* Function Name  : TIMER3_OVF_vectFunc
* Description    : 定时器3溢出中断程序
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
#pragma vector = TIMER3_OVF_vect
__interrupt void TIMER3_OVF_vectFunc(void)
{
   
}

相关帖子

沙发
JY-DX-JY| | 2015-6-15 20:21 | 只看该作者
这是要干嘛?分享吗?

使用特权

评论回复
板凳
694844985| | 2015-6-16 13:08 | 只看该作者
鼓励大家,晒一晒自己的成果,互相交流共同进步

使用特权

评论回复
地板
li1376263028| | 2015-6-23 15:46 | 只看该作者
要是人人都能分享,,,知识交换的速度一定会上升很多的

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

1

粉丝