RGB LED MIXER- In this project we used the attiny 13 microcntroller for operating the RGB LED AND COLOR MIXING.
單片機源程序如下:
- /*
- Chapter - 2
- Project - 3 RGB Colormixer
- */
- #include<avr/io.h>
- #include<avr/interrupt.h>
- #define F_CPU 9600000UL
- #include<util/delay.h>
- //Prototypes
- //This function is called to switch on the LED at the
- //Required stage of PWM cycle
- void abc(unsigned char a,unsigned char b,unsigned char c,unsigned char status);
- //This function read the value of ADC from selected channel
- unsigned char read_adc(unsigned char channel);
- //Global variables
- volatile unsigned char e;
- volatile unsigned char pwm[3]={255,255,255};
- int main(void)
- {
- DDRB &= ~((1<<3)|(1<<4)|(1<<5));//ADC inputs
-
- //Initially LEDs should be off
- DDRB |= (1<<0|1<<1|1<<2);
- PORTB |= (1<<0)|(1<<1)|(1<<2);
-
- //ADC init
-
- ADMUX = 0b00100000; //ADC0 (default),ADLAR,VCc
- ADCSRA = 0b10000010; //prescaled by 4
-
- //Timer Initialisation
- TCCR0A = 0x00;
- TCCR0B = 0x01;//No Prescaling
- TIMSK0 = 1<<TOIE0;//Overflow Interrupt Enabled
- sei();//Global Interrupts Enabled
- while(1)
- {
-
- pwm[0]=read_adc(0);
- pwm[1]=read_adc(2);
- pwm[2]=read_adc(3);
-
- _delay_ms(2);//Just a small delay
-
- }
-
- }
- //Overflow routine for Timer 0
- ISR(TIM0_OVF_vect)
- {
-
- //Value of e decides the no of levels of PWM.
- //This has 256 levels for e - 0 to 255
- //0 - complete on and 255 is complete off
-
- if(e==255)
- {
- e=0;
- PORTB |= (1<<0)|(1<<1)|(1<<2);
- }
- abc(pwm[0],pwm[1],pwm[2],e);
-
- e++;
-
- }
- //This function is called to switch on the LED at the
- //Required stage of PWM cycle
- void abc(unsigned char a,unsigned char b,unsigned char c,unsigned char status)
- {
- if((status==a))
- {
- PORTB&= ~(1<<0);
- }
- if((status==b))
- {
- PORTB&= ~(1<<1);
- }
- if((status==c))
- {
- PORTB&= ~(1<<2);
- }
-
- }
- //This function read the value of ADC from selected channel
- unsigned char read_adc(unsigned char channel)
- {
-
- unsigned char k;
- unsigned int adcvalue=0;
- ADMUX = ADMUX&(0b11111100); //clear channel select bits
- ADMUX |= channel;
-
- //neglect first reading after changing channel
- ADCSRA |= 1<<ADSC;
- while(ADCSRA&(1<<ADSC));//Wait
- adcvalue=ADCH;
- adcvalue=0;//neglectreading
- for(k=0;k<=7;k++)
- {
- ADCSRA |= 1<<ADSC;
- while(ADCSRA&(1<<ADSC));//Wait
- adcvalue += ADCH;
- }
- return (adcvalue>>3); //divide by 8
-
- }
復制代碼
所有資料51hei提供下載:
Project-3-RGB-LED-Colormixer.rar
(116.49 KB, 下載次數: 21)
2018-8-6 22:29 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|