电路图
(三)输出方波
(四)输出锯齿波
(五)输出三角波
(六)输出正玄波
(七)程序
1 #include
2 #include
3
4 #define DA0832 P2
5 #define DATA 57 //定时器延时
6
7 #define uchar unsigned char
8 #define uint unsigned int
9
10 sbit S1 = P1^0;
11 sbit S2 = P1^1;
12 //00锯齿波;01方波;10三角波;11正弦波
13
14 uchar code sin_tab[] = //正弦波输出表
15 {
16 0x80,0x83,0x86,0x89,0x8D,0x90,0x93,0x96,0x99,0x9C,0x9F,0xA2,0xA5,0xA8,0xAB,0xAE,
17 0xB1,0xB4,0xB7,0xBA,0xBC,0xBF,0xC2,0xC5,0xC7,0xCA,0xCC,0xCF,0xD1,0xD4,0xD6,0xD8,
18 0xDA,0xDD,0xDF,0xE1,0xE3,0xE5,0xE7,0xE9,0xEA,0xEC,0xEE,0xEF,0xF1,0xF2,0xF4,0xF5,
19 0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFD,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
20 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFD,0xFC,0xFB,0xFA,0xF9,0xF8,0xF7,0xF6,
21 0xF5,0xF4,0xF2,0xF1,0xEF,0xEE,0xEC,0xEA,0xE9,0xE7,0xE5,0xE3,0xE1,0xDF,0xDD,0xDA,
22 0xD8,0xD6,0xD4,0xD1,0xCF,0xCC,0xCA,0xC7,0xC5,0xC2,0xBF,0xBC,0xBA,0xB7,0xB4,0xB1,
23 0xAE,0xAB,0xA8,0xA5,0xA2,0x9F,0x9C,0x99,0x96,0x93,0x90,0x8D,0x89,0x86,0x83,0x80,
24 0x80,0x7C,0x79,0x76,0x72,0x6F,0x6C,0x69,0x66,0x63,0x60,0x5D,0x5A,0x57,0x55,0x51,
25 0x4E,0x4C,0x48,0x45,0x43,0x40,0x3D,0x3A,0x38,0x35,0x33,0x30,0x2E,0x2B,0x29,0x27,
26 0x25,0x22,0x20,0x1E,0x1C,0x1A,0x18,0x16,0x15,0x13,0x11,0x10,0x0E,0x0D,0x0B,0x0A,
27 0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
28 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
29 0x0A,0x0B,0x0D,0x0E,0x10,0x11,0x13,0x15,0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x25,
30 0x27,0x29,0x2B,0x2E,0x30,0x33,0x35,0x38,0x3A,0x3D,0x40,0x43,0x45,0x48,0x4C,0x4E,
31 0x51,0x55,0x57,0x5A,0x5D,0x60,0x63,0x66,0x69,0x6C,0x6F,0x72,0x76,0x79,0x7C,0x7E
32 };
33
34 void stair(void);//锯齿波
35 void square(void);//方波
36 void trian();//三角波
37 void sin();//正弦波
38 void scan();//扫描函数
39 void Timer0Init(void); //75微秒@11.0592MHz
40 uchar flag = 0;
41
42
43 void main()
44 {
45 P2 = 0xff;
46 Timer0Init();
47 while(1)
48 {
49 //逐个测试
50 //stair(); /*锯齿波*/
51 // square(); /*方波*/
52 // trian(); /*三角波*/
53 // sin(); /*正弦波*/
54 scan();
55 }
56 }
57
58 void Timer0Init(void) //75微秒@11.0592MHz
59 {
60
61 TMOD = 0x01; //设置定时器模式
62 TH0 = (65536 - DATA) / 256; //设置定时初值
63 TL0 = (65536 - DATA) % 256; //设置定时初值
64 ET0 = 1; //TF0标志
65 TR0 = 1; //定时器0开始计时
66 EA = 1;
67 }
68 void timer0_ISR(void) interrupt 1
69 {
70 TH0 = (65536 - DATA) / 256; //设置定时初值
71 TL0 = (65536 - DATA) % 256; //设置定时初值
72 TR0 = 0;
73 flag = 1;
74 }
75
76
77 void scan()
78 {
79 if((0 == S1) &&(0 == S2))
80 stair();
81 else if((1 == S1) && (0 == S2))
82 square();
83 else if((0 == S1) && (1 == S2))
84 trian();
85 else
86 sin();
87
88 }
89
90
91 void stair(void)
92 {
93 uchar i;
94 for(i = 0 ;i < 255; i++)
95 {
96 DA0832 = i;
97 TR0 = 1;
98 while(flag == 0);
99 flag = 0;
100 }
101
102 }
103
104 void square(void)
105 {
106 int i;
107 DA0832 = 0xff;
108 for(i = 0;i < 125;i++)
109 {
110 TR0 = 1;
111 while(flag == 0);
112 flag = 0;
113 }
114 DA0832 = 0x00;
115 for(i = 0;i < 125;i++)
116 {
117 TR0 = 1;
118 while(flag == 0);
119 flag = 0;
120 }
121 }
122
123 void trian(void)
124 {
125 uchar i;
126 for(i = 0 ;i < 125; i++)
127 {
128 DA0832 = i;
129 TR0 = 1;
130 while(flag == 0);
131 flag = 0;
132 }
133
134 for(i=125 ;i > 0; i--)
135 {
136 DA0832 = i;
137 TR0 = 1;
138 while(flag == 0);
139 flag = 0;
140 }
141 }
142
143 void sin()
144 {
145 uchar i;
146 for(i = 0; i < 255; i++)
147 {
148 DA0832 = sin_tab[i];
149 TR0 = 1;
150 while(flag == 0);
151 flag = 0;
152 }
153 }