搜索
bottom↓
回复: 35

avr-51板的pwm實驗, 利用pwm控制led光暗及峰鳴器音量大小.

[复制链接]

出0入0汤圆

发表于 2005-4-18 13:17:46 | 显示全部楼层 |阅读模式
//ICC-AVR application builder : 2005-4-18 12:46:03

// Target : M16

// Crystal: 4.0000Mhz



#include <iom16v.h>

#include <macros.h>



#define uchar unsigned char

#define uint unsigned int



void port_init(void);

void timer0_init(void);

void init_devices(void);

void delay_short(uint t);

uchar scan_key(void);





void port_init(void)

{

PORTA = 0x00;

DDRA  = 0x00;

PORTB = BIT(PB3);

DDRB  = BIT(PB3);

PORTC = 0x00; //m103 output only

DDRC  = 0x00;

PORTD = 0x00;

DDRD  = 0x00;

}



// WGM: PWM Phase correct

// desired value: 1KHz

// actual value:  0.980KHz (-2.0%)

void timer0_init(void)

{

TCCR0 = 0x00; //stop

TCNT0 = 0x01; //set count

OCR0  = 0xFF;  //set compare

TCCR0 = 0x62; //start timer ; 相位修正, 8分頻

}



//call this routine to initialize all peripherals

void init_devices(void)

{

//stop errant interrupts until set up

CLI(); //disable all interrupts

port_init();

timer0_init();



MCUCR = 0x00;

GICR  = 0x00;

TIMSK = 0x00; //timer interrupt sources

SEI(); //re-enable interrupts

//all peripherals are now initialized

}



void delay_short(uint t) // 短延時

{

  uint i;

  for (i=0;i<t;i++);

}



uchar scan_key(void)  // 按鍵掃瞄

{

  uchar v;

  

  v = 0;          

  

  if ((PIND & 0x07) != 0x07)

  {



  if ((PIND & 0x01) == 0)

  {

        v = 1;

    delay_short(1000);  

  }

  

  if ((PIND & 0x2) == 0)

  {

    v = 2;

    delay_short(1000);  

  }

   

  if ((PIND & 0x4) == 0)

  {

    v = 3;

    delay_short(1000);  

  }

  };

  while((PIND & 0x07) != 0x07);        // 判斷按鍵是不是放開   

  return v;  

}



//

void main(void)

{  

uchar key, OCR0_V;



init_devices();

OCR0_V = 0xff;



while(1)

{

   key = scan_key();

   

   if (key > 0)

   {

     if (key==1) // 減少佔空比

         {

           OCR0_V -= 10;

           OCR0 = OCR0_V;

         };

         

     if (key==2) // 增加佔空比

         {

           OCR0_V += 10;

           OCR0 = OCR0_V;

         };         

         

     if (key==3) // 全黑,佔空比為100%

         {

           OCR0_V = 0xff;

           OCR0 = OCR0_V;

         };          

   }

};

}





實驗板接線:

PB3 -----> JA.1 及 JM

PD0 -----> K1

PD1 -----> K2

PD2 -----> K3

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

 楼主| 发表于 2005-4-18 19:48:57 | 显示全部楼层
發覺led在佔空比大於20%, led的光暗就不明顯了...

用pwm做led白光燈可以省70%以上的電啊!!

出0入27汤圆

发表于 2005-4-18 21:35:15 | 显示全部楼层
好好好好,正郁闷怎么利用avr51实验板呢。

谢谢。

出0入0汤圆

发表于 2005-4-19 23:27:38 | 显示全部楼层
你的有意思

出0入0汤圆

发表于 2005-5-29 23:57:52 | 显示全部楼层
请问一个新手问题: 就是TCNT0的初始值的计算和设置



---------根据myhk007 提供的算法--------

最原始的方法:以T0记到255溢出为例,如果AVR的主频是8M,T0为1024分频的话,那么T0每加1,需要的时间就是1024/8000000,加255次的总共延时就是1024/8000000*255,依次类推。

--------------------------------------



如果"TCNT0 = 0x01; " ,则计数次数应为255次, 总时间应为 255*8/4000000=0.00051s, 频率应是约2kHz, 而楼主上面的代码中是1kHz, 困惑中~~!



是否我什么地方还没理解正确

出0入0汤圆

 楼主| 发表于 2005-5-30 09:27:11 | 显示全部楼层
這可能是icc的bug吧,pwm的代碼我是用icc的自生成做的(說明也是icc加上去的), 我做的時候就沒有認真的計算過,因為只是做實驗!

出0入0汤圆

发表于 2005-5-31 08:56:05 | 显示全部楼层
用示波器看了一下波形, 跟想像的形状差不多, 这个实验很有意思

出0入0汤圆

发表于 2005-8-21 21:35:37 | 显示全部楼层
果然有意思,在proteus防真,效果不错哟

出0入0汤圆

发表于 2006-2-13 11:22:44 | 显示全部楼层
avrboy:

   有跳线图吗?我也想试试。

出0入0汤圆

发表于 2006-2-13 11:31:26 | 显示全部楼层
哎呀,对不起!我再次看时见到了跳线了。我试试。有情况再请教

出0入0汤圆

发表于 2006-2-13 11:40:01 | 显示全部楼层
哎呀,对不起!我再次看时见到了跳线了。我试试。有情况再请教

出0入0汤圆

发表于 2006-2-13 11:42:40 | 显示全部楼层
avrboy:iccavr在那里下载?双龙的只是45天的演示版

出0入0汤圆

发表于 2006-2-13 15:22:27 | 显示全部楼层
我终于又能在实验板上运行。在示波器上也能清楚地看到波形。谢谢!

出0入0汤圆

发表于 2006-11-12 10:59:48 | 显示全部楼层
非GCC的,不要。



呵呵

出0入0汤圆

发表于 2006-11-15 11:02:19 | 显示全部楼层
请问这个试验板的资源有哪些啊?如果有图片就好了。我想买一块?是否是AVR和51都有?

出0入0汤圆

发表于 2006-12-13 15:09:53 | 显示全部楼层
在相位修正模式中,上,下记数怎么理解

出0入0汤圆

发表于 2006-12-14 15:48:19 | 显示全部楼层
刚做了一块实验板,正好在上面试试。

出0入0汤圆

发表于 2007-5-9 19:48:37 | 显示全部楼层
赞楼主,不错

出0入0汤圆

发表于 2007-12-4 23:37:50 | 显示全部楼层
不错,我已经按楼主的方法做了一个DEMO板,效果还不错,

小弟按自己的意见稍加改动了,各位大哥给点建议。
//***********************PWM 程序*********************
//***********************PWM 程序*********************
//***********************PWM 程序*********************


//ICC-AVR application builder : 2005-4-18 12:46:03  
// Target : M16  
// Crystal: 4.0000Mhz  

#include <iom8515v.h>  
#include <macros.h>  

#define uchar unsigned char
#define uint unsigned int
//****************定义过程中要的程序***********************//
void port_init(void);
void timer0_init(void);
void init_devices(void);
void delay_short(uint t);
uchar scan_key(void);


void port_init(void)  
{  
PORTA = 0x00;  
DDRA  = 0x00;  
PORTB = BIT(PB0);  
DDRB  = BIT(PB0);  
PORTC = 0x00; //m103 output only  
DDRC  = 0x00;  
PORTD = 0x38;  ///config my testing board
DDRD  = 0x38;  
}  

// WGM: PWM Phase correct
// desired value: 1KHz
// actual value:  0.980KHz (-2.0%)
void timer0_init(void)  
{  
TCCR0 = 0x00; //stop  //控制寄存器,此时为无时钟,不工作//
TCNT0 = 0x01; //set count  
OCR0  = 0xFF;  //set compare  
TCCR0 = 0x62; //start timer ; 相位修正, 8分頻
}  

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

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

void delay_short(uint t) // 短延時
{
  uint i;
  for (i=0;i<t;i++);
}

uchar scan_key(void)  // 按鍵掃瞄
{  
  uchar v;
   
  v = 0;           
   
  if ((PIND & 0x38) != 0x38)
  {
  
  if ((PIND & 0x10) == 0)  //config my testing board
  {
        v = 1;
               
  delay_short(1000);   //delay 去抖处理//
  }
   
  if ((PIND & 0x20) == 0)  
  {
    v = 2;
       
    delay_short(1000);   
  }
   
  if ((PIND & 0x08) == 0)  
  {
    v = 3;
    delay_short(1000);   
  }
  };
  while((PIND & 0x38) != 0x38);        // 判斷按鍵是不是放開   
  return v;
  }

//  
void main(void)  
{   
uchar key, OCR0_V;
  
init_devices();  
OCR0_V = 0xe6;
  
while(1)
{
   key = scan_key();
   
   if (key > 0)
   {
     if (key==1) // 減少佔空比
         {  
           OCR0_V -= 10;
                   if(OCR0_V<11)
                   {
                    OCR0_V = 0x0b;
                        OCR0 = OCR0_V;
                   }
                   else
                   {
                   OCR0 = OCR0_V;
                   }
           };
         
     if (key==2) // 增加佔空比
         {
                   OCR0_V += 10;
                   if(OCR0_V>240)
                   {
                    OCR0_V = 0xe7;
                        OCR0 = OCR0_V;
                   }
                   else
                   {
                   OCR0 = OCR0_V;
                   }
         };         
         
     if (key==3) // 全黑,佔空比為100%  
         {  
           OCR0_V = 0x00;
           OCR0 = OCR0_V;
                   OCR0_V = 0x0a;
         };           
   }
};  
}

出0入0汤圆

发表于 2009-3-16 22:30:43 | 显示全部楼层
消化一下

出0入0汤圆

发表于 2009-3-25 14:36:15 | 显示全部楼层
avrboy =單騙皇帝=
你的联系是多少呀?我的QQ是641677143,想和你交流一下,感觉你做的不错!

出0入0汤圆

发表于 2009-9-16 10:58:50 | 显示全部楼层
好奇怪哦,我的程序就是运行不出这样的效果,看不到灯的亮度变化。。

出0入0汤圆

发表于 2009-12-7 11:29:21 | 显示全部楼层
mark

出0入0汤圆

发表于 2009-12-28 12:14:12 | 显示全部楼层
很好用

出0入0汤圆

发表于 2009-12-28 18:32:49 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-1-2 01:22:50 | 显示全部楼层
这个要记号……

出0入0汤圆

发表于 2010-1-22 10:26:22 | 显示全部楼层
留个爪子

出0入0汤圆

发表于 2010-4-15 23:44:36 | 显示全部楼层
印记

出0入0汤圆

发表于 2010-5-4 21:50:24 | 显示全部楼层
mark

出0入4汤圆

发表于 2010-5-24 20:14:51 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-6-7 16:42:05 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-7-30 14:17:22 | 显示全部楼层
pwm,mark

出0入0汤圆

发表于 2010-9-1 19:03:09 | 显示全部楼层
我学的是ATTINY13!希望高手指点!这个程序能移植到ATTINY13里能用吗?谢谢!

出0入0汤圆

发表于 2010-9-1 20:17:49 | 显示全部楼层
mark
头像被屏蔽

出0入0汤圆

发表于 2011-12-18 18:29:58 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2013-3-11 20:29:07 | 显示全部楼层
谢谢分享
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-25 12:38

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表