[Atmel] 用AtmelStudio6.2跑mega16例程(4)

[复制链接]
1253|0
 楼主| ddllxxrr 发表于 2014-8-28 22:15 | 显示全部楼层 |阅读模式

由定时器产生的PWM 通过PD4,PD5输出


  1. /*
  2. * GccApplication6.c
  3. *
  4. * Created: 2014-8-28 21:50:18
  5. *  Author: Administrator
  6. */


  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. //PWM输出程序
  10. //在端口PD4,PD5(led5,led6)输出PWM脉冲,可用示波器测量,或者外加一LED灯,可以看到LED由于脉宽的变化引起的灯的亮度不同

  11. //#include <macros.h>

  12. void port_init(void)
  13. {
  14.         PORTA = 0x00;
  15.         DDRA  = 0x00;
  16.         PORTB = 0x00;
  17.         DDRB  = 0x08;
  18.         PORTC = 0x00; //m103 output only
  19.         DDRC  = 0x00;
  20.         PORTD = 0xff;
  21.         DDRD  = 0xff;
  22. }

  23. //TIMER1 initialize - prescale:64
  24. // WGM: 10) PWM phz correct, TOP= ICRn
  25. // desired value: 1000Hz
  26. // actual value: 1000.000Hz (0.0%)
  27. void timer1_init(void)
  28. {
  29.         TCCR1B = 0x00; //stop
  30.         TCNT1H = 0xFF; //setup
  31.         TCNT1L = 0x83;
  32.         OCR1AH = 0x00;
  33.         OCR1AL = 0x65; //占空比
  34.         OCR1BH = 0x00;
  35.         OCR1BL = 0x1f; //占空比
  36.         ICR1H  = 0x00;
  37.         ICR1L  = 0x7D;
  38.         TCCR1A = 0xA2;
  39.         TCCR1B = 0x13; //start Timer
  40. }

  41. //call this routine to initialize all peripherals
  42. void init_devices(void)
  43. {
  44.         //stop errant interrupts until set up
  45.         cli();//CLI(); //disable all interrupts
  46.         port_init();
  47.         timer1_init();

  48.         MCUCR = 0x00;
  49.         GICR  = 0x00;
  50.         TIMSK = 0x00; //timer interrupt sources
  51.         sei();//SEI(); //re-enable interrupts
  52.         //all peripherals are now initialized
  53. }

  54. int main(void)
  55. {
  56.         init_devices();
  57. }


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7002

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部