夏日炎炎搞个会呼吸的小灯做个深呼吸
使用正弦PWM控制,实现呼吸灯。#include <mega16.h>
// Declare your global variables here
unsigned char i=0;
flash unsigned char sin_num={
130 ,
133 ,
136 ,
139 ,
143 ,
146 ,
149 ,
152 ,
155 ,
158 ,
161 ,
164 ,
167 ,
170 ,
173 ,
176 ,
178 ,
181 ,
184 ,
187 ,
190 ,
192 ,
195 ,
198 ,
200 ,
203 ,
205 ,
208 ,
210 ,
212 ,
215 ,
217 ,
219 ,
221 ,
223 ,
225 ,
227 ,
229 ,
231 ,
233 ,
234 ,
236 ,
238 ,
239 ,
240 ,
242 ,
243 ,
244 ,
245 ,
247 ,
248 ,
249 ,
249 ,
250 ,
251 ,
252 ,
252 ,
253 ,
253 ,
253 ,
254 ,
254 ,
254 ,
254 ,
254 ,
254 ,
254 ,
253 ,
253 ,
253 ,
252 ,
252 ,
251 ,
250 ,
249 ,
249 ,
248 ,
247 ,
245 ,
244 ,
243 ,
242 ,
240 ,
239 ,
238 ,
236 ,
234 ,
233 ,
231 ,
229 ,
227 ,
225 ,
223 ,
221 ,
219 ,
217 ,
215 ,
212 ,
210 ,
208 ,
205 ,
203 ,
200 ,
198 ,
195 ,
192 ,
190 ,
187 ,
184 ,
181 ,
178 ,
176 ,
173 ,
170 ,
167 ,
164 ,
161 ,
158 ,
155 ,
152 ,
149 ,
146 ,
143 ,
139 ,
136 ,
133 ,
130 ,
127 ,
124 ,
121 ,
118 ,
115 ,
111 ,
108 ,
105 ,
102 ,
99 ,
96 ,
93 ,
90 ,
87 ,
84 ,
81 ,
78 ,
76 ,
73 ,
70 ,
67 ,
64 ,
62 ,
59 ,
56 ,
54 ,
51 ,
49 ,
46 ,
44 ,
42 ,
39 ,
37 ,
35 ,
33 ,
31 ,
29 ,
27 ,
25 ,
23 ,
21 ,
20 ,
18 ,
16 ,
15 ,
14 ,
12 ,
11 ,
10 ,
9 ,
7 ,
6 ,
5 ,
5 ,
4 ,
3 ,
2 ,
2 ,
1 ,
1 ,
1 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
1 ,
1 ,
1 ,
2 ,
2 ,
3 ,
4 ,
5 ,
5 ,
6 ,
7 ,
9 ,
10 ,
11 ,
12 ,
14 ,
15 ,
16 ,
18 ,
20 ,
21 ,
23 ,
25 ,
27 ,
29 ,
31 ,
33 ,
35 ,
37 ,
39 ,
42 ,
44 ,
46 ,
49 ,
51 ,
54 ,
56 ,
59 ,
62 ,
64 ,
67 ,
70 ,
73 ,
76 ,
78 ,
81 ,
84 ,
87 ,
90 ,
93 ,
96 ,
99 ,
102 ,
105 ,
108 ,
111 ,
115 ,
118 ,
121 ,
124 ,
127
};
// Timer2 output compare interrupt service routine
interrupt void timer2_comp_isr(void)
{
// Place your code here
OCR0=sin_num;
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x08;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value:7327.280 kHz
// Mode: Fast PWM top=0xFF
// OC0 output: Inverted PWM
TCCR0=0x79;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 7.156 kHz
// Mode: CTC top=OCR2
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x0F;
TCNT2=0x00;
OCR2=0xF0;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x80;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
}
}
PB3输出驱动LED 呼吸灯看资料说指数上升、下降的。到底那种波形效果好啊? 用正弦能柔和点 指数的上升下降效果更明显 呵呵, 用CVAVR写的... 晕,最开始的定义搞成那么多行,除了增加阅读难度,占用篇幅,还有什么别的意义吗? 我也做了个,打算弄在自行车上,平方曲线的,亮度比较线性 afeionline 发表于 2014-6-26 23:20
晕,最开始的定义搞成那么多行,除了增加阅读难度,占用篇幅,还有什么别的意义吗? ...
cvavr自动生成的 整个程序就是定时查表 更新OCR0 有没有很大的那种灯可以用来做呼吸灯的那种买啊,,求连接。 可不可以使用大功率led来做呼吸灯?
shejian001 发表于 2014-6-27 08:07
可不可以使用大功率led来做呼吸灯?
对对对 就是要用 3W 做 车 左边一个 右边一个 很棒
如果再来点色彩
没看到 有这种 所以要DIY
页:
[1]