I am using a ATmega168 microcontroller, I wanted to control several I/O pins simultaneously. Can this be done?
Thnaks
Thnaks
Here is a link to an ATMEL application note that illustrates the FSM programming technique.I bought a Aruduino Diecimila, and I believe it's in C.
The pseudo code you wrote does not appear to do what you think.
(it will not compile as I tried so it has many problems.)
Your code is trying to vary the length of time the LED is ON and OFF at the high state and not vary brightness.
Try this example to vary brightness [the key is analogWrite(ledpin, i)]
Connect as follows:
9 ----- 1K R ------ +LED- -----Gnd
Copy, paste, compile then download after connecting.
int ledPin = 9;
void setup(){}
void loop()
{
for (int i=0; i<=255; i++)
{
analogWrite(ledPin, i);
delay(100);
}
for (int i=255; i>=0; i--)
{
analogWrite(ledPin, i);
delay(100);
}
}
Yeah i understand It will make things SOO much easier if i use analogWrite. but I need to use all of the of the I/O pins.If you want to run 3 LEDs and have them each vary their intensity randomly then you can download the following program. (Note: Each LED gets assigned a different random number-intensity level. Had I put the randomNumber generator only at the beginning then all the LED's would be at the same intensity of the random generator. Also note that the delay is at the end so they will appear seemless in switching. If the delay is after each then they will appear as if each is being changed independently instead of simultaneously.)
int ledPin9 = 9;
int ledPin10 = 10;
int ledPin11 = 11;
long randNumber;
void setup(){}
void loop()
{
randNumber = random(255);
analogWrite(ledPin9, randNumber);
randNumber = random(255);
analogWrite(ledPin10, randNumber);
randNumber = random(255);
analogWrite(ledPin11, randNumber);
delay(100);
}
/**************************
FINITE STATE MACHINE DEMO
**************************/
#include <avr/io.h>
#include <avr/interrupt.h>
unsigned char tick1;
unsigned char tick2;
unsigned char LED1_state;
unsigned char LED2_state;
void INIT_DIO(void)
{
// In this function, set up the IO pins to be used as
// your LED enable lines...
}
void INIT_TIMER0(void)
{
// In this function, set up TIMER0 prescale to interrupt
// once every 100 milliseconds and enable it for
// interrupt operation...
}
ISR(TIMER0_OVF_vect) // This interrupt happens 10 times per second...
{
tick1++;
tick2++;
}
void Service_LED1(void)
{
switch(LED1_state)
{
case 0: // IDLE or DO-NOTHING state
tick1 = 0;
break;
case 1:
if (tick1 >= 10)
{
// Turn on LED1...
tick1 = 0;
LED1_state = 2;
}
break;
case 2:
if (tick1 >= 10)
{
// Turn off LED1
tick1 = 0;
LED1_state = 1;
}
break;
default:
break;
}
}
void Service_LED2(void)
{
// Repeat the instruction from LED2 using tick2 and LED2_state...
}
int main(void)
{
INIT_DIO();
INIT_TIMER0();
LED1_state = 1; // Set state to first active state...
LED2_state = 1; // Set state to first active state...
sei(); // Enable Global Interrupts...
while(1) // Effectively a forever loop...
{
Service_LED1();
Service_LED2();
}
return(0); // This line should never be executed....
}
Thanks! I will post my program once i am done with it.On review I picked up on a glaring error in the initialized values of LED1_state and LED2_state.
Once you have your first stab at your code, post your code and those members with AVR programming experience will be happy to comment and suggest ways to improve your program.
You know that you can start coding without waiting for your hardware. AVRSTUDIO4 and WINAVR can be downloaded free right now and you can begin familiarizing yourself with the tools. Also the program simulation capability of AVRSTUDIO4 and WINAVR are quite good.
hgmjr
by Duane Benson
by Aaron Carman
by Aaron Carman