1 /*******************************************************/
2 /* pwm音阶程序 */
3 /*******************************************************/
4
5
6 #include <hidef.h> /* common defines and macros */
7 #include "derivative.h" /* derivative-specific definitions */
8
9 #define LEDCPU PORTK_PK4
10 #define LEDCPU_dir DDRK_DDRK4 //
11 #define BUS_CLOCK 32000000
12 #define OSC_CLOCK 16000000
13
14
15 unsigned int sounghigh[10]={
16 4048,3816,3402,3030,2866,2551,2273,2024,1912,1704
17 };
18
19 unsigned int soundtone[8]={
20 1,2,3,4,5,6,7,8
21 };
22
23 unsigned int soundlong[8]={
24 1,1,1,1,1,1,1,1
25 };
26
27 unsigned int t;
28
29
30 /*******************************************************/
31 /* 初始化锁相环 */
32 /*******************************************************/
33 void INIT_PLL(void) {
34 CLKSEL &= 0x7f;
35 PLLCTL &= 0x8f;
36 CRGINT &= 0xdf;
37
38 #if(BUS_CLOCK == 40000000)
39 SYNR = 0x44;
40 #elif(BUS_CLOCK == 32000000)
41 SYNR = 0x43;
42 #elif(BUS_CLOCK == 24000000)
43 SYNR = 0x42;
44 #endif
45
46 REFDV = 0X81;
47 PLLCTL |= 0X70;
48 asm NOP;
49 asm NOP;
50 while(!(CRGFLG&0x08));
51 CLKSEL |= 0x80;
52 }
53
54
55 /*******************************************************/
56 /* 初始化PWM */
57 /*******************************************************/
58 void init_pwm(void) {
59 PWMCTL_CON01 = 1;
60 PWMPOL_PPOL1 = 1;
61 PWMPRCLK = 0X55;
62 PWMCLK = 0X00;
63 PWMPER01 = 3816;
64 PWME_PWME1 = 1;
65 }
66
67 /*******************************************************/
68 /* 延时函数 */
69 /*******************************************************/
70 void delay(void) {
71 unsigned int i,j;
72 for(i=0;i<10;i++)
73 for(j=0;j<50000;j++);
74 }
75
76 /*******************************************************/
77 /* 延时函数 */
78 /*******************************************************/
79 void delay2(unsigned int n) {
80 unsigned int i,j,k;
81 for(k=0;k<n;k++)
82 for(i=0;i<50;i++)
83 for(j=0;j<50000;j++);
84 }
85
86 /*******************************************************/
87 /* 主函数 */
88 /*******************************************************/
89
90 void main(void) {
91 DisableInterrupts;
92 INIT_PLL();
93 init_pwm();
94 LEDCPU_dir=1;
95 LEDCPU=0;
96 EnableInterrupts;
97
98 for(;;) {
99 for(t=0;t<8;t++) {
100 PWMPER01=sounghigh[soundtone[t]];
101 PWMDTY01=sounghigh[soundtone[t]]/2;
102 delay2(soundlong[t]);
103 PWMDTY01=0;
104 delay();
105 }
106 delay2(5);
107
108 for(t=8;t>0;t--) {
109 PWMPER01=sounghigh[soundtone[t-1]];
110 PWMDTY01=sounghigh[soundtone[t]]/2;
111 delay2(soundlong[t]);
112 PWMDTY01=0;
113 delay();
114 }
115 delay2(5);
116 }
117
118
119 EnableInterrupts;
120
121
122 for(;;) {
123 _FEED_COP(); /* feeds the dog */
124 } /* loop forever */
125
126 }