找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6697|回復: 0
打印 上一主題 下一主題
收起左側(cè)

數(shù)碼管中斷顯示+高位滅零處理

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:80436 發(fā)表于 2015-5-21 23:26 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. //清理硬盤時發(fā)現(xiàn)自己以前寫的,貼上來吧,雖然沒什么水平,
  2. //5毫秒一個顯示周期,理論上顯示20位數(shù)碼管不會有閃爍感,
  3. 占用了一個定時器,簡約的算法實現(xiàn)高位滅零處理
  4. #include <avr/io.h>
  5. #include <avr/interrupt.h>
  6. unsigned int flag=0;
  7. unsigned char count=0;
  8. unsigned int num=0;
  9. //==================================================
  10. const unsigned char seg[]={        0xC0, // 0
  11. 0xF9, // 1
  12. 0xA4, // 2
  13. 0xB0, // 3
  14. 0x99, // 4
  15. 0x92, // 5
  16. 0x82, // 6
  17. 0xF8, // 7
  18. 0x80, // 8
  19. 0x90 // 9
  20. };


  21. //==================================================

  22. //IO端口初始化
  23. void PortInit(void)
  24. {
  25. DDRA=0XFF;
  26. PORTA=0X00;
  27. DDRB=0XFF;
  28. PORTB=0XFF;
  29. }

  30. //Timer0初始化
  31. void Timer0Init(void)
  32. {
  33. TCCR0 = 0x00; //stop
  34. TCNT0 = 0x06; //set count
  35. OCR0  = 0xFA;  //set compare
  36. TCCR0 = 0x03; //start timer
  37. }

  38. //==================================================

  39. //定時器0溢出中斷服務程序
  40. ISR(TIMER0_OVF_vect)
  41. {
  42. TCNT0=0X06;
  43. flag++;
  44. count++;
  45. switch(count)
  46. {
  47. case 1:if(num/1000){PORTA=0X01;PORTB=seg[num/1000];}else PORTB=0XFF;break;
  48. case 2:if(((num/1000)+(num%1000/100))){PORTA=0X02;PORTB=seg[num%1000/100];}else PORTB=0XFF;break;
  49. case 3:if(((num/1000)+(num%1000/100)+(num%100/10))){PORTA=0X04;PORTB=seg[num%100/10];}else PORTB=0XFF;break;
  50. case 4:PORTA=0X08;PORTB=seg[num%10];break;
  51. case 5:count=0;break;
  52. default:break;
  53. }
  54. if (flag==100)
  55. {
  56. flag=0;
  57. num++;
  58. if(num==9999)
  59. {
  60. num=0;
  61. }
  62. }
  63. }

  64. //==================================================

  65. //主函數(shù)
  66. int main(void)
  67. {
  68. cli();
  69.     TIMSK = 0x01; //timer interrupt sources
  70. PortInit();
  71. Timer0Init();
  72. sei();
  73. while(1)
  74.       {
  75.         ;
  76. }
  77. }
復制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表