Fragrance
Advanced Member level 4
- Joined
- Jul 26, 2002
- Messages
- 1,190
- Helped
- 248
- Reputation
- 496
- Reaction score
- 202
- Trophy points
- 1,343
- Location
- East Of Earth
- Activity points
- 8,933
thanks rajat bhai
i have written the code for RGB led using pwm. each leg is connected with individual pin of timer 0 & 1 (using ocr0, ocr1a, ocr1b). & it is working ok. but i just want to change the color sequence using a button switch. i have written code for each color using diffrent functions. now i just want to press the button & change the color. i just want to know that how a switch will react after each press. please send me a code for working of switch
thanks
while(1)
{
if (SWITCH_ON) // is the button pressed/
{
__delay_cycles(1000); // wait a bit for contact bounce allowance
if (SWITCH_ON) // // repeat the check
{
index++; // cycle the color
if (index==10) // let's assume we only have 10 colors to cycle around
index=0;
update_registers(index); // this programs the PWM channels
// now wait for the switch to be released
for (i=0; i<2000; i++) // we're going to wait about a couple of seconds
{
__delay_cycles(100); // tiny (1 msec) delay approx
if (SWITCH_OFF) // if the switch has been released, we're done!
break;
}
__delay_cycles(1000); // wait a bit for contact bounce allowance
}
}
#include <ioxxx.h>
#include <intrinsics.h>
#include <avr_macros.h>
#include <stdio.h>
// Port B
#define LED 0x01
#define BUTTON 0x02
// inputs
#define SWITCH_ON (PINB & BUTTON) == 0
#define SWITCH_OFF (PINB & BUTTON) != 0
// outputs
#define LED_ON PORTB |= LED
#define LED_OFF PORTB &= ~LED
// This is the PWM register, it is called OCR0A
#define LED_BRIGHTNESS OCR0A
int
main( void )
{
// initialise
PORTB=0;
DDRB=0x01; // LED pin is output the rest of port B is inputs
PORTB=0x3e; // enable pull-ups on all input pins
// turn off the LED:
LED_OFF;
// Set up the PWM:
OCR0A = 255; // set the dim level to 255 (lamp off)
TCCR0A = 0xc3; // set COM0A1, COM0A0, WGM00, WGM01
TCCR0B=0x01; // clear WGM02. Set the prescaler to divide by 1
// end of initialisation
// done! now we can control the brightness by setting OCR0A
// (i.e. LED_BRIGHTNESS) to 0..255 (0 being the brightest,
// 255 being the dimmest)
while(1)
{
if (SWITCH_ON) // is the button pressed/
{
__delay_cycles(1000); // wait a bit for contact bounce allowance
[B] if (SWITCH_ON) // // repeat the check
{
index++; // cycle the color
if (index==10) // let's assume we only have 10 colors to cycle around
index=0;[/B]
update_registers(index); // this programs the PWM channels
// now wait for the switch to be released
for (i=0; i<2000; i++) // we're going to wait about a couple of seconds
{
__delay_cycles(100); // tiny (1 msec) delay approx
if (SWITCH_OFF) // if the switch has been released, we're done!
break;
}
__delay_cycles(1000); // wait a bit for contact bounce allowance
}
}
Holding the switch on will not make necessarily make it keep increasing.now switch is on so untill switch is on index will increase in each cycle then how we get the exact index value
// now wait for the switch to be released
for (i=0; i<2000; i++) // we're going to wait about a couple of seconds
{
__delay_cycles(100); // tiny (1 msec) delay approx
if (SWITCH_OFF) // if the switch has been released, we're done!
break;
}
__delay_cycles(1000); // wait a bit for contact bounce allowance
__delay_cycles(1000); // wait a bit for contact bounce allowance
if (i>=2000)
{
index=0;
update_registers(index);
}
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
int main()
{
unsigned char i,j,k;
DDRB = 0x08;
DDRD = 0x30;
OCR0 = 255; //blue pb3
OCR1AH = 00;
OCR1AL = 255; //red pd5
OCR1BH = 00;
OCR1BL = 255; //green pd4
TCCR0 = 0x63;
TCCR1B = 0x03;
TCCR1A = 0xA1;
while(1)
{
red();
_delay_ms(2000);
white();
_delay_ms(2000);
yellow();
_delay_ms(2000);
lightyellow();
_delay_ms(2000);
green();
_delay_ms(2000);
bluishgreen();
_delay_ms(2000);
lightblue();
_delay_ms(2000);
blue();
_delay_ms(2000);
lvoilet();
_delay_ms(2000);
bluishvoilet();
_delay_ms(2000);
voilet();
_delay_ms(2000);
darkvoilet();
_delay_ms(2000);
return 0;
}
white(void)
{
char i=0;
OCR0 = i;
OCR1AL = i;
OCR1BL = i;
}
lightyellow(void)
{
char i=0;
OCR0 = 180;
OCR1AL = 0;
OCR1BL = 0;
}
yellow(void)
{
char i=0;
OCR0 = 255;
OCR1AL = i;
OCR1BL = i;
}
red(void)
{
char i=0;
OCR0 = 255;
OCR1AL = i;
OCR1BL = 255;
}
voilet(void)
{
char i=0;
OCR0 = i;
OCR1AL = i;
OCR1BL = 255;
}
lvoilet(void)
{
char i=0;
OCR0 = i;
OCR1AL = i;
OCR1BL = 128;
}
darkvoilet(void)
{
char i=0;
OCR0 = 128;
OCR1AL = i;
OCR1BL = 255;
}
tdarkvoilet(void)
{
char i=128;
OCR0 = 192;
OCR1AL = i;
OCR1BL = 255;
}
blue(void)
{
char i=20;
OCR0 = i;
OCR1AL = 255;
OCR1BL = 255;
}
bluishgreen(void)
{
char i=0;
OCR0 = 0;
OCR1AL = 128;
OCR1BL = 0;
}
green(void)
{
char i=20;
OCR0 = 255;
OCR1AL = 255;
OCR1BL = i;
}
Sorry, the entire code to do all you've requested is in this thread. I don't think any tiny bit is missing to be honest.please arrange the code in a sequence for me
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?