单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:4639|回复:3
打印上一主题下一主题

AVR 单片机初值计算

[复制链接]
来自 4#
ID:690448发表于 2020-11-19 18:52|只看该作者

ICC-AVR application builder  点几下鼠标就可完成


推荐大家试一试




  1. //ICC-AVR application builder : 2020/11/19 18:50:34
  2. // Target : M16
  3. // Crystal: 4.0000Mhz

  4. #include< iom16v.h>
  5. #include< macros.h>

  6. void port_init(void)
  7. {
  8. PORTA = 0x00;
  9. DDRA  = 0x00;
  10. PORTB = 0x00;
  11. DDRB  = 0x00;
  12. PORTC = 0x00; //m103 output only
  13. DDRC  = 0x00;
  14. PORTD = 0x00;
  15. DDRD  = 0x00;
  16. }

  17. //TIMER1 initialize - prescale:256
  18. // WGM: 0) Normal, TOP=0xFFFF
  19. // desired value: 1Sec
  20. // actual value:  1.000Sec (0.0%)
  21. void timer1_init(void)
  22. {
  23. TCCR1B = 0x00; //stop
  24. TCNT1H = 0xC2; //setup
  25. TCNT1L = 0xF7;
  26. OCR1AH = 0x3D;
  27. OCR1AL = 0x09;
  28. OCR1BH = 0x3D;
  29. OCR1BL = 0x09;
  30. ICR1H  = 0x3D;
  31. ICR1L  = 0x09;
  32. TCCR1A = 0x00;
  33. TCCR1B = 0x04; //start Timer
  34. }

  35. #pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
  36. void timer1_ovf_isr(void)
  37. {
  38. //TIMER1 has overflowed
  39. TCNT1H = 0xC2; //reload counter high value
  40. TCNT1L = 0xF7; //reload counter low value
  41. }

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

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

  55. //
  56. void main(void)
  57. {
  58. init_devices();
  59. //insert your functional code here...
  60. }

复制代码



评分

黑币 +50
收起理由
+ 50
回帖助人的奖励!

查看全部评分

沙发
ID:739227发表于 2020-4-27 19:30|只看该作者
非常好!不错!非常感谢楼主
板凳
ID:808608发表于 2020-10-10 14:23|只看该作者
非常好!不错!非常感谢师傅

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网