<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->
2011 年 1 月 10 日 17 时 24 分 58 秒
分频方法解析
分频是最基本的操作之一,根据系数不同,有偶数分频,奇数分频,小数分频之分,根据使用的方法不同有:纯粹计数器法,单一计数器法,双重计数器法,奇数分频根据信号的生成方式,有与法,或法。
一:纯粹计数法(利用最高数据位的自然溢出)
如果是 25MHz 的时钟,那么这里利用数据的自然溢出,也就是取其最高位,而得到其输出频率为: 25000000/65536=381Hz ,周期为 1/381s ,则高电平时间为 1/(2*381)s ,其对应的计数值为: 32768
module music(clk, speaker);
input clk;
output speaker;
// Binary counter, 16-bits wide
reg [15:0] counter;
always @(posedge clk) counter <= counter+1;
// Use the highest bit of the counter (MSB) to drive the speaker
assign speaker = counter[15];// 只要 counter>=32767 ,那么 counter[15] 就等于 1 了,因为这里的计数是自然溢出的,所以低于 32767 和高于 32767 数据刚好各占用一半,所以占空比就是 50% 了。
endmodule
其中 counter[0] 的频率是 12.5MHz , counter[1] 的频率是 6.125MHz ,以此类推。它是按照 2 n+1 次方来分频的,且是方波。
分频率为 440Hz ,这时就要除另外一个数了 440=25M/56818 ,代码如下:
module music(clk, speaker);
input clk;
output speaker;
reg [15:0] counter;
always @(posedge clk) if (counter==56817) counter <= 0; else counter <= counter+1;
assign speaker = counter[15];// 因为这里 56817