用mega8生成spwm程序(单极性倍频控制)

发布者:CuriousMind123最新更新时间:2016-10-29 来源: eefocus关键字:mega8  spwm程序 手机看文章 扫描二维码
随时随地手机看文章

//ICC-AVR application builder : 2006-7-7 11:30:51
// Target : M8
// Crystal: 8.0000Mhz     20k for G4PC50U

#include
#include
#define INT0 6
//=====================周期为20ms,spwm频率为20k===========================//
short list[]={1,4,7,10,13,16,19,22,25,28,31,34,37,39,42,45,48,51,54,57,60,62,
65,68,71,74,76,79,82,84,87,90,92,95,97,100,103,105,108,110,112,115,117,119,
122,124,126,128,131,133,135,137,139,141,143,145,147,149,151,152,154,156,157,
159,161,162,164,165,167,168,169,171,172,173,174,176,177,178,179,180,181,182,
182,183,184,185,185,186,186,187,187,188,188,189,189,189,189,189,189,189,189,
189,189,189,189,189,189,188,188,187,187,186,186,185,185,184,183,182,182,181,
180,179,178,177,176,174,173,172,171,169,168,167,165,164,162,161,159,157,156,
154,152,151,149,147,145,143,141,139,137,135,133,131,128,126,124,122,119,117,
115,112,110,108,105,103,100,97,95,92,90,87,84,82,79,76,74,71,68,65,62,60,57,
54,51,48,45,42,39,37,34,31,28,25,22,19,16,13,10,7,4,1};
//=====================周期为20ms,spwm频率为20k===========================//
int count,zf=0;
void port_init(void) 
{
 PORTB = 0x00;
 DDRB  = 0xFF;//06;b口输出
 PORTC = 0x00; 
 DDRC  = 0xFF;//c口输出
 PORTD = 0x00;
 DDRD  = 0x00;//d口输入
 
}

//TIMER1 initialize - prescale:1
// WGM13:0=10) PWM phz correct, TOP= ICR1
// desired value: 10KHz
// actual value: 10.000KHz (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop                                                                    
 TCNT1H = 0xFF; //setup
 TCNT1L = 0x38;
 OCR1AH = 0x00;
 OCR1AL = 0xC8;
 OCR1BH = 0x00;
 OCR1BL = 0xC8; 
 ICR1H  = 0x00;
 ICR1L  = 200;//    载波周期为50us            
 TCCR1A = 0xA2; //10100010 top时更新OCR1A OCR1B,向上计数比较匹配清零,反之置位
 TCCR1B = 0x11; //start Timer 00010001  clk无分频,相位修正PWM
 TIMSK |= 0x20; //timer interrupt sources使能捕捉中断
}

#pragma interrupt_handler timer1_capt_isr:6
void timer1_capt_isr(void)
{
 OCR1A=500;
 OCR1B=500;
 //PORTB ^=0x10;
//    2          1      .spwm
//
//   4    3  180
//
//

if(zf)                                 
 { 
   if(count<200)
     {
    if(count==1)       //此时2#管子才关断
  {
    //PORTB &= ~BIT(3);    //3#管子已经关断
   PORTB |= BIT(4);     //开通4#管子
  }
  OCR1A=list[count++];     //开通1#管子
   }
   else if((count>=200)&&(count<400))
     {
     if(count==200)     
  {
    PORTB &= ~BIT(4);    //关断4#管子
  }
  if(count==201)     //此时1#管子才关断
  {
   PORTB |= BIT(3);    //开通3#管子
  }
     OCR1B=list[count++-200];  //开通2#管子
  }     
   else 
     {
     count=0;      //2#管子已经关断
  PORTB &= ~BIT(3);    //关断3#管子
  //PORTB |= BIT(4);    //开通4#管子
  OCR1A=list[count++];   //开通1#管子
  }
 }
 else;

}

//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();

 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
//
void main(void)
{
 init_devices();
 //insert your functional code here...
 zf = 1;
 OCR1A=500;
 OCR1B=500;
 PORTB = 0;
 while(1);
}

生成50hz正弦波

关键字:mega8  spwm程序 引用地址:用mega8生成spwm程序(单极性倍频控制)

上一篇:关于用mega128输出任意频率脉冲的问题
下一篇:TC1的双路PWM输出实验

推荐阅读最新更新时间:2024-03-16 15:18

STC12C5A60S2单片机产生SPWM程序C代码
本程序通过模拟仿真,实际示波器测试,均表现良好,愿拿出来分享,若有更好的实现方法,希望高手多多指教。。。。 /*************spwm产生程序******/ /*单片机STC12C5A60S2,晶振11.0592M,正弦波50Hz,spwm波形21.6KHz*/ #include STC12x5A60S2.H #define uchar unsigned char #define uint unsigned int uchar code pwm ={255,240,226,211,196,182,168,154,141,128,115,103,91 ,80,69,59,50,42,34,27,20,15,10,6,3,1
[单片机]
小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved