(原文件名:120611_7.png) 图8
我们用也可以模拟相似的图形,代码如下:
程序清单6:点阵模拟音乐频谱分析
1 #include
2 #include "hw_config.h"
3 #include "my_type.h"
4
5 //频谱波形表
6 code u8 Wave[16][8]=
7 {
8 {0xFF,0xFF,0xFF,0xFF,0xFE,0xBB,0xFE,0xAA},
9 {0xFF,0xFF,0xFF,0xFE,0xFB,0xAE,0xFA,0xAA},
10 {0xFF,0xFF,0xFF,0xFE,0xEB,0xBE,0xEA,0xAA},
11 {0xFF,0xFF,0xFE,0xFB,0xAF,0xFE,0xAA,0xAA},
12 {0xFF,0xFE,0xFB,0xBE,0xEA,0xBA,0xAA,0xAA},
13 {0xFF,0xFE,0xBB,0xEE,0xBA,0xBA,0xAA,0xAA},
14 {0xFE,0xBB,0xEE,0xBA,0xAA,0xAA,0xAA,0xAA},
15 {0xBA,0xEF,0xBE,0xAA,0xAA,0xAA,0xAA,0xAA},
16 {0xEE,0xBB,0xFE,0xAA,0xAA,0xAA,0xAA,0xAA},
17 {0xEE,0xBB,0xFE,0xEA,0xAA,0xAA,0xAA,0xAA},
18 {0xFE,0xEB,0xBE,0xFE,0xAA,0xAA,0xAA,0xAA},
19 {0xFF,0xEE,0xBB,0xFF,0xAE,0xAA,0xAA,0xAA},
20 {0xFF,0xFE,0xAF,0xFB,0xEE,0xAA,0xAA,0xAA},
21 {0xFF,0xFF,0xFE,0xBB,0xEF,0xBA,0xAA,0xAA},
22 {0xFF,0xFF,0xFF,0xFE,0xAB,0xFF,0xEE,0xAA},
23 {0xFF,0xFF,0xFF,0xFF,0xFE,0xEB,0xBE,0xAA}
24 };
25
26 //亮度级别表
27 code u8 LightLevel[8]={1,2,4,8,16,32,50,64};
28
29 //函数声明
30 void timer0_init(void);
31
32 void main(void)
33 {
34 //使能控制点阵的译码器
35 LEDEN = 0;
36 ADDR3 = 0;
37
38 timer0_init();
39
40 while(1)
41 {
42 }
43 }
44
45 /*****************************************************************
46 函数名称:timer0_init
47 功 能:初始化定时器0
48 *****************************************************************/
49 void timer0_init(void)
50 {
51 TMOD = 0x01; //运行模式1
52 TH0 = 0xFF; //10us中断
53 TL0 = 0xFA;
54 EA = 1; //开启中断
55 ET0 = 1;
56 TR0 = 1; //启动定时器
57
58 }
59
60 /*****************************************************************
61 函数名称:timer0_overflow
62 功 能:定时器0溢出中断
63 *****************************************************************/
64 void timer0_overflow(void) interrupt TIMER0_OVERFLOW
65 {
66 u8 i;
67 u8 p1_value = 0;
68 static u8 state = 0; //点阵状态(扫描行数)
69 static u8 count = 0;
70
71 static u8 wave_state = 0; //波形状态
72 static u16 wave_count = 0;
73
74 TR0 = 0;
75
76 //每中断1000次,改变波形状态
77 wave_count++;
78 if(wave_count == 1000)
79 {
80 wave_count = 0;
81 wave_state++;
82 wave_state %= 16;
83 }
84
85 //每中断64次,改变扫描的行
86 count++;
87 if(count == 64)
88 {
89 state++;
90 state %= 8;
91 count = 0;
92 }
93
94 if(count < LightLevel[state])
95 {
1 P0 = Wave[wave_state][state];
2 }
3 else
4 {
5 P0 = 0xFF;
6 }
7
8 //输出位码
9 p1_value = P1 & 0xf8;
10 p1_value |= state;
11 P1 = p1_value;
12
13 //定时器重新赋值
14 TH0 = 0xFF;
15 TL0 = 0xF7;
16 TR0 = 1;
17 }
L6(6).波形表,共16个状态,每个状态下有8个数据,即一个点阵界面。扫描点 阵时循环发送,实现动态效果。
L6(77-83).每中断1000次,更换一个状态。
L6(86-101).和L5的功能一致,只是点阵段码输出的数据变成了波形表。
下载验证:下载程序到开发板,可以看到,点阵显示的频谱分析效果。
`