找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2555|回復(fù): 0
收起左側(cè)

單片機(jī)+8255芯片實(shí)現(xiàn)簡(jiǎn)易電子琴程序與Proteus仿真

[復(fù)制鏈接]
ID:971484 發(fā)表于 2021-10-13 22:54 | 顯示全部樓層 |閱讀模式
1到7為7個(gè)音節(jié),8為送別,在播放音樂時(shí)操作按鍵無效,只有復(fù)位或等歌曲放完

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
11.png
51hei.png

單片機(jī)源程序如下:
  1. #include "reg52.h"
  2. #include "absacc.h"
  3. #include<stdio.h>
  4. #include"intrins.h"
  5. sbit CS =P2^7;        //8255片選端口
  6. sbit SPK=P1^0;  //定義音樂輸出端口
  7. #define PA XBYTE[0x0000]
  8. #define PB XBYTE[0x0401]
  9. #define PC XBYTE[0x0802]
  10. #define CN XBYTE[0x0C03]
  11. #define RS_CLR PB &= ~(1<<0)                                                
  12. #define RS_SET PB |= (1<<0)
  13. #define RW_CLR PB &= ~(1<<1)
  14. #define RW_SET PB |= (1<<1)
  15. #define EN_CLR PB &= ~(1<<2)
  16. #define EN_SET PB |= (1<<2)
  17. #define DataPort PA
  18. unsigned char state = 0 ;
  19. unsigned char temp,num,updat ;
  20. unsigned table[ ]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  21. unsigned char number=0;
  22. unsigned char W=0,T;        //用于選擇音樂
  23. unsigned char Timer0_H,Timer0_L,Time;
  24. void DelayMs(unsigned char t);

  25.                                                 //送別 數(shù)據(jù)表        音譜,高中低音,音長(zhǎng)
  26. code unsigned char MUSIC1[174]={        5,2,2,                3,2,1,                5,2,1,                1,3,4,        
  27.                                                                         6,2,2,                1,3,2,                5,2,4,
  28.                                                                         5,2,2,                1,2,1,                2,2,1,                3,2,2,                2,2,1,                1,2,1,
  29.                                                                         2,2,8,
  30.                                                                         5,2,2,                3,2,1,                5,2,1,                1,3,3,                7,2,1,
  31.                                                                         6,2,2,                1,3,2,                5,2,4,
  32.                                                                         5,2,2,                2,2,1,                3,2,1,                4,2,3,                7,1,1,
  33.                                                                         1,2,8,
  34.                                                                         6,2,2,                1,3,2,                1,3,4,
  35.                                                                         7,2,2,                6,2,1,                7,2,1,                1,3,4,
  36.                                                                         6,2,1,                7,2,1,                1,3,1,                6,2,1,                6,2,1,                5,2,1,                3,2,1,                1,2,1,
  37.                                                                         2,2,8,
  38.                                                                         5,2,2,                3,2,1,                5,2,1,                1,3,3,                7,2,1,
  39.                                                                         6,2,2,                1,3,2,                5,2,4,
  40.                                                                         5,2,2,                2,2,1,                3,2,1,                4,2,3,                7,1,1,
  41.                                                                         1,2,8
  42.                                                                 };
  43.                          // 音階頻率表 高八位
  44. code unsigned char FREQH[]={
  45.                                 0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
  46.                                 0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
  47.                                 0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
  48.                                 0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
  49.                                } ;
  50.                          // 音階頻率表 低八位
  51. code unsigned char FREQL[]={
  52.                                  0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
  53.                                  0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
  54.                                  0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
  55.                                  0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
  56.                                 };
  57. /*------------------------------------------------
  58.                 節(jié)拍延時(shí)函數(shù)
  59. 各調(diào)1/4節(jié)拍時(shí)間:
  60. 調(diào)4/4  125ms
  61. 調(diào)2/4  250ms
  62. 調(diào)3/4  187ms
  63. ------------------------------------------------*/
  64. void delay(unsigned char t)
  65. {
  66.     unsigned char i;
  67.         for(i=0;i<t;i++)
  68.             DelayMs(250);
  69.     TR0=0;
  70. }
  71. /*------------------------------------------------
  72.                定時(shí)器0中斷
  73. ------------------------------------------------*/
  74. void TIM0_ISR() interrupt 1
  75. {
  76.         TR0=0;      
  77.         SPK=!SPK;
  78.         TH0=Timer0_H;
  79.         TL0=Timer0_L;
  80.         TR0=1;
  81. }
  82. /*------------------------------------------------
  83.                 歌曲處理函數(shù)
  84. ------------------------------------------------*/
  85. void Song()
  86. {
  87.         TH0=Timer0_H;//賦值定時(shí)器時(shí)間,決定頻率
  88.         TL0=Timer0_L;
  89.         TR0=1;       //打開定時(shí)器
  90.         delay(Time); //延時(shí)所需要的節(jié)拍                     
  91. }
  92. //微妙延時(shí)函數(shù)
  93. void DelayUs2x(unsigned char t)
  94. {   
  95. while(--t);
  96. }
  97. //毫秒延時(shí)函數(shù)
  98. void DelayMs(unsigned char t)
  99. {

  100. while(t--)
  101. {
  102.      DelayUs2x(245);
  103.          DelayUs2x(245);
  104. }
  105. }
  106. //液晶寫指令函數(shù)
  107. void LCD_Write_Com(unsigned char com)
  108. {  
  109. DelayMs(5);                //延時(shí)5毫秒
  110. RS_CLR;                         //指令數(shù)據(jù)端口設(shè)置為低電平,指令模式
  111. RW_CLR;                         //讀寫端口設(shè)置為寫模式
  112. EN_SET;
  113. DataPort= com;                //寫數(shù)據(jù)到液晶數(shù)據(jù)端口
  114. _nop_();
  115. EN_CLR;                        //EN產(chǎn)生下降沿講數(shù)據(jù)寫入到液晶中
  116. }
復(fù)制代碼
51hei.png

全部資料51hei下載地址,注意仿真工程只能用Proteus7.5打開:
基于8255的LCD顯示簡(jiǎn)易電子琴.7z (121.57 KB, 下載次數(shù): 46)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

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

快速回復(fù) 返回頂部 返回列表