找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1643|回复: 1
打印 上一主题 下一主题
收起左侧

ATtiny13-RGB-LED-Colormixer源程序

[复制链接]
跳转到指定楼层
楼主
ID:383566 发表于 2018-8-6 22:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
RGB LED MIXER- In this project we used the attiny 13 microcntroller for operating the RGB LED AND COLOR MIXING.


单片机源程序如下:

  1. /*
  2. Chapter - 2
  3. Project - 3 RGB Colormixer

  4. */

  5. #include<avr/io.h>
  6. #include<avr/interrupt.h>
  7. #define F_CPU 9600000UL
  8. #include<util/delay.h>

  9. //Prototypes

  10. //This function is called to switch on the LED at the
  11. //Required stage of PWM cycle
  12. void abc(unsigned char a,unsigned char b,unsigned char c,unsigned char status);

  13. //This function read the value of ADC from selected channel
  14. unsigned char read_adc(unsigned char channel);


  15. //Global variables
  16. volatile unsigned char e;
  17. volatile unsigned char pwm[3]={255,255,255};


  18. int main(void)
  19. {


  20.         DDRB  &= ~((1<<3)|(1<<4)|(1<<5));//ADC inputs
  21.        
  22.         //Initially LEDs should be off
  23.         DDRB |=  (1<<0|1<<1|1<<2);
  24.         PORTB |= (1<<0)|(1<<1)|(1<<2);
  25.        
  26.         //ADC init
  27.        
  28.         ADMUX =  0b00100000; //ADC0 (default),ADLAR,VCc
  29.         ADCSRA = 0b10000010; //prescaled by 4
  30.        
  31.         //Timer Initialisation
  32.         TCCR0A = 0x00;
  33.         TCCR0B = 0x01;//No Prescaling
  34.         TIMSK0 = 1<<TOIE0;//Overflow Interrupt Enabled
  35.         sei();//Global Interrupts Enabled

  36.         while(1)
  37.         {       
  38.        
  39.                 pwm[0]=read_adc(0);
  40.                 pwm[1]=read_adc(2);
  41.                 pwm[2]=read_adc(3);
  42.                
  43.                 _delay_ms(2);//Just a small delay       
  44.        
  45.         }

  46.        

  47. }


  48. //Overflow routine for Timer 0
  49. ISR(TIM0_OVF_vect)
  50. {
  51.        
  52.         //Value of e decides the no of levels of PWM.
  53.         //This has 256 levels for e - 0 to 255
  54.         //0 - complete on and 255 is complete off
  55.        
  56.         if(e==255)
  57.         {
  58.                 e=0;
  59.                 PORTB |= (1<<0)|(1<<1)|(1<<2);
  60.         }

  61.         abc(pwm[0],pwm[1],pwm[2],e);
  62.        
  63.         e++;
  64.        
  65. }


  66. //This function is called to switch on the LED at the
  67. //Required stage of PWM cycle
  68. void abc(unsigned char a,unsigned char b,unsigned char c,unsigned char status)
  69. {

  70.         if((status==a))
  71.         {
  72.                 PORTB&= ~(1<<0);
  73.         }
  74.         if((status==b))
  75.         {
  76.                 PORTB&= ~(1<<1);
  77.         }
  78.         if((status==c))
  79.         {
  80.                 PORTB&= ~(1<<2);
  81.         }
  82.        
  83. }

  84. //This function read the value of ADC from selected channel
  85. unsigned char read_adc(unsigned char channel)
  86. {
  87.        
  88.         unsigned char k;
  89.         unsigned int adcvalue=0;
  90.         ADMUX = ADMUX&(0b11111100); //clear channel select bits
  91.         ADMUX |= channel;        
  92.        
  93.         //neglect first reading after changing channel
  94.         ADCSRA |= 1<<ADSC;
  95.         while(ADCSRA&(1<<ADSC));//Wait
  96.         adcvalue=ADCH;
  97.         adcvalue=0;//neglectreading
  98.         for(k=0;k<=7;k++)
  99.         {
  100.                 ADCSRA |= 1<<ADSC;
  101.                 while(ADCSRA&(1<<ADSC));//Wait
  102.                 adcvalue += ADCH;
  103.         }
  104.         return (adcvalue>>3); //divide by 8
  105.        
  106. }

复制代码

所有资料51hei提供下载:
Project-3-RGB-LED-Colormixer.rar (116.49 KB, 下载次数: 21)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:240626 发表于 2019-10-30 16:40 | 只看该作者
你这个用arduino软件打开烧录不了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表