Dear Arduino Lovers,
I am facing very irritation error while making LED Flasher.
I just want to flash leds using 74hc575 and timer interrupt.
Here is my code.
Its works great if i turn serial print instruction.
When i comment Serial.print instructions in loop, this code does not works.
Is there anything wrong in timer library?
Serial.println(LED_DATA);
Here is my code
#include <TimerOne.h>
int data_pin = 4; //74hc595 data pin
int clock_pin = 3;//74hc595 clock pin
int latch_pin = 2;//74hc595 latch pin
int LED=5;
int LED_TIMER=13;
byte LED_DATA =0xff;
//=============================================
void TIMER_ISR()// Timer Interrupt this function is called after every 1 milli second
{
byte x=LED_DATA;
for(int j=0;j<8;j++)
{digitalWrite(data_pin,bitRead(x,j)); digitalWrite(clock_pin,HIGH);digitalWrite(clock_pin,LOW);}
digitalWrite(latch_pin,HIGH); // make latch pin high
digitalWrite(latch_pin,LOW); // make latch pin low
digitalWrite(LED_TIMER,LOW);
}
//=============================================
void setup()
{
Timer1.start();
Timer1.initialize(1000);// 1ms time
Timer1.attachInterrupt(TIMER_ISR); //enable timer interrupt
pinMode(data_pin, OUTPUT); //make pins output
pinMode(clock_pin,OUTPUT); //
pinMode(latch_pin, OUTPUT); //
pinMode(LED, OUTPUT);
pinMode(LED_TIMER, OUTPUT);
Serial.begin(9600);
}
//=================================================
void loop(){
digitalWrite(LED,HIGH); LED_DATA=0xff; delay(100);// make led pin (5) HIGH and led data= all one
Serial.println(LED_DATA);
digitalWrite(LED,LOW); LED_DATA=0; delay(100);// make led pin (5) LOW and led data= all low
Serial.println(LED_DATA);
}
//============================================
Get in the habit of only putting one statement/instruction on a line. It makes reviewing/editing/debugging a lot easier. Use the Auto Format capabilities of the IDE as well.
#include <TimerOne.h>
int data_pin = 4; //74hc595 data pin
int clock_pin = 3;//74hc595 clock pin
int latch_pin = 2;//74hc595 latch pin
int LED=5;
int LED_TIMER=13;
byte LED_DATA =0xff;
//=============================================
void TIMER_ISR()// Timer Interrupt this function is called after every 1 milli second
{
byte x=LED_DATA;
for(int j=0;j<8;j++)
{
digitalWrite(data_pin,bitRead(x,j));
digitalWrite(clock_pin,HIGH);
digitalWrite(clock_pin,LOW);
}
digitalWrite(latch_pin,HIGH); // make latch pin high
digitalWrite(latch_pin,LOW); // make latch pin low
digitalWrite(LED_TIMER,LOW);
}
//=============================================
void setup()
{
Timer1.start();
Timer1.initialize(1000);// 1ms time
Timer1.attachInterrupt(TIMER_ISR); //enable timer interrupt
pinMode(data_pin, OUTPUT); //make pins output
pinMode(clock_pin,OUTPUT); //
pinMode(latch_pin, OUTPUT); //
pinMode(LED, OUTPUT);
pinMode(LED_TIMER, OUTPUT);
Serial.begin(9600);
}
//=================================================
void loop()
{
digitalWrite(LED,HIGH);
LED_DATA=0xff;
delay(100);// make led pin (5) HIGH and led data= all one
Serial.println(LED_DATA);
digitalWrite(LED,LOW);
LED_DATA=0;
delay(100);// make led pin (5) LOW and led data= all low
Serial.println(LED_DATA);
}
//===========================================
Dear Arduino Lovers,
I am facing very irritation error while making LED Flasher.
I just want to flash leds using 74hc575 and timer interrupt.
Here is my code.
Its works great if i turn serial print instruction.
When i comment
instructions in loop, this code does not works.
Is there anything wrong in timer library?
Here is my code
#include <TimerOne.h>
int data_pin = 4; //74hc595 data pin
int clock_pin = 3;//74hc595 clock pin
int latch_pin = 2;//74hc595 latch pin
int LED=5;
int LED_TIMER=13;
byte LED_DATA =0xff;
//=============================================
void TIMER_ISR()// Timer Interrupt this function is called after every 1 milli second
{
byte x=LED_DATA;
for(int j=0;j<8;j++)
{digitalWrite(data_pin,bitRead(x,j)); digitalWrite(clock_pin,HIGH);digitalWrite(clock_pin,LOW);}
digitalWrite(latch_pin,HIGH); // make latch pin high
digitalWrite(latch_pin,LOW); // make latch pin low
digitalWrite(LED_TIMER,LOW);
}
//=============================================
void setup()
{
Timer1.start();
Timer1.initialize(1000);// 1ms time
Timer1.attachInterrupt(TIMER_ISR); //enable timer interrupt
pinMode(data_pin, OUTPUT); //make pins output
pinMode(clock_pin,OUTPUT); //
pinMode(latch_pin, OUTPUT); //
pinMode(LED, OUTPUT);
pinMode(LED_TIMER, OUTPUT);
Serial.begin(9600);
}
//=================================================
void loop(){
digitalWrite(LED,HIGH); LED_DATA=0xff; delay(100);// make led pin (5) HIGH and led data= all one
Serial.println(LED_DATA);
digitalWrite(LED,LOW); LED_DATA=0; delay(100);// make led pin (5) LOW and led data= all low
Serial.println(LED_DATA);
}
//============================================
If you are outputting to the LED every millisecond can you really discriminate between on and off in that short period ? Does the use of Serial.print() slow things down enough for you to see the LED output I wonder. Try increasing the baud rate. Does it make any difference ?
Its works great if i turn serial print instruction.
When i comment Serial.print instructions in loop, this code does not works.
Please try to explain this again using words like ON or OFF.
Works good when there is no print instruction?
Does not work when there is a print instruction?
While you're thinking on that, in the setup() you should be careful about the order that you do things. Mostly it is good to set all the hardware up before you start initialize software and cause code to start in the case of interrupt driven libraries.
As you can see that LEDs are connected using a shift register.
In loop function, I just want to change LED_DATA byte status and I want from interrupt function to put LED_DATA byte to shift register after every 1milli second.
For simplification
I want to flash led's on or off after every 500ms using timer interrupt.
I have tried to use my own timer function as shown here.
Its working and turning on / off led's after every 500ms.
I just want to delete these 2 instructions from loop.
1=Serial.println(LED_DATA);
2=Serial.println(LED_DATA);
Both are doing nothing in this code. but deleting these code does not flash led's.
#define ledPin 13
int data_pin = 4; //74hc595 data pin
int clock_pin = 3;//74hc595 clock pin
int latch_pin = 2;//74hc595 latch pin
int LED=5;
int LED_TIMER=13;
byte LED_DATA =0xff;
void setup()
{
pinMode(ledPin, OUTPUT);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 500; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
pinMode(data_pin, OUTPUT); //make pins output
pinMode(clock_pin,OUTPUT); //
pinMode(latch_pin, OUTPUT); //
pinMode(LED, OUTPUT);
pinMode(LED_TIMER, OUTPUT);
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
digitalWrite(LED_TIMER,HIGH);
byte x=LED_DATA;
for(int j=0;j<8;j++)
{
digitalWrite(data_pin,bitRead(x,j));digitalWrite(data_pin,bitRead(x,j));// get a bit from variable and put on data pin.
digitalWrite(clock_pin,HIGH);// clock high
digitalWrite(clock_pin,LOW); // clock low
}
digitalWrite(latch_pin,HIGH);// latch enable
digitalWrite(latch_pin,LOW);// lathc disable
digitalWrite(LED_TIMER,LOW); // flash led
}
void loop()
{
digitalWrite(LED,HIGH); LED_DATA=0xff; delay(500);// make led pin (5) HIGH and led data= all one
Serial.println(LED_DATA);
digitalWrite(LED,LOW); LED_DATA=0; delay(500);// make led pin (5) LOW and led data= all low
Serial.println(LED_DATA);
}
digitalWrite(data_pin,bitRead(x,j));digitalWrite(data_pin,bitRead(x,j));// get a bit from variable and put on data pin.Twice??
void loop()
{
digitalWrite(LED,HIGH);
LED_DATA=0xff;
delay(500);// make led pin (5) HIGH and led data= all one
Serial.println(LED_DATA);
digitalWrite(LED,LOW);
LED_DATA=0;
delay(500);// make led pin (5) LOW and led data= all low
Serial.println(LED_DATA);
}
You are flashing LED which has be declared as pin 5 yet your diagram shows nothing connected to pin 5. Did you mean pin 5 on PORTB which is the built-in LED a.k.a. pin 13?
I have not connected LED with pin5.
I am testing this in the simulation. Basically, I was working on hardware and it was working nicely with serial print instruction, I came to simulation to debug the code and I toggled led on pin 5 to verify loop is running, Toggling pin13 to test interrupt is working.
Have you got my point?
Tom George,
I was working on hardware before simulation.
I came to Proteus to debug and get help from this forum.
Proteus files are attached and anybody can simulate and check my code.
FWIW, I have a 595 connected running the interrupt version of you code and it works just fine. Two issues, though, you need Serial.begin() on your setup() and with OCR1A set at 500 there is an interrupt being fired every 8ms. You expect to see the on-board LED flashing at that rate?
I know why it is not blinking when you take out the Serial.print(): you forgot to declare the LED_DATA as 'volatile'. The compiler thinks you aren't using the values in loop() if you don't print them so it optimizes that data away.
ANY VARIABLE SHARED BETWEEN INTERRUPT CODE AND NON_INTERRUPT CODE SHOULD BE 'volatile'.
johnwasser:
I know why it is not blinking when you take out the Serial.print(): you forgot to declare the LED_DATA as 'volatile'. The compiler thinks you aren't using the values in loop() if you don't print them so it optimizes that data away.
ANY VARIABLE SHARED BETWEEN INTERRUPT CODE AND NON_INTERRUPT CODE SHOULD BE 'volatile'.