Hey guys,I have got some problems with my school project.This project is about a square-wave generator.
My currently my project is separated into 2 parts:
- The input
- The actual square-wave generator
Part 1 works smoothly:
volatile int frequenz = 4000; //set frequencyint
i = 0;
void setup(){
DDRD = DDRD | B00000100; //set port 2
noInterrupts(); //disable Interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = frequenz;
TCCR1B |=(1<<WGM12); //CTC mode p 133
TCCR1B |=(1<<CS10); //no prescale p 135
TCCR1B |=(0<<CS11);
TCCR1B |=(0<<CS12);
TIMSK2 = ( 1<<OCIE1A ) ; // Enable interrupt when Timer reaches OCRA
interrupts();
Serial.begin(9600);
}
void loop(){}
ISR(TIMER1_COMPA_vect) {
PORTD ^=0x04; //toggles the pin 2
}
}
[
and the 2nd part runs smoothly too:
volatile long frequenz = 0;
char Data[7];
int i = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
setFrequenz();
Serial.println(frequenz);
}
void setFrequenz(){
if( Serial.available())
{
char ch = Serial.read();
if(i < 6 && isDigit(ch) )
{
Data[i] = ch;
i++;
}
else
{
Data[i] = NULL;
frequenz = atoilData);
i = 0;
}
}
}
but when I now try to combine them I just doesn't work anymore.
instead of the number inputted I get 0jc all the time
I merged it together like this:
volatile long frequenz = 0;
char Data[7];
volatile boolean pin = LOW;
int i = 0;
void setup()
{
DDRD = DDRD | B00000100; //set port 3
noInterrupts(); //disable Interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = frequenz;
TCCR1B |=(1<<WGM12); //CTC mode p 133
TCCR1B |=(1<<CS10); //no prescale p 135
TCCR1B |=(0<<CS11);
TCCR1B |=(0<<CS12);
TIMSK2 = ( 1<<OCIE1A ) ; // Enable interrupt when Timer reaches OCRA
interrupts();
//DDRD &= ~_BV(PD2); pin 2 = 1
//DDRD |= _BV(PD2); pin 2 = 0
Serial.begin(9600);
}
void loop()
{
noInterrupts();
setFrequenz();
Serial.println(frequenz);
Interrupts();
}
ISR(TIMER1_COMPA_vect)
{
PORTD ^=0x04;
}
void setFrequenz(){
if( Serial.available())
{
char ch = Serial.read();
if(i < 6 && isDigit(ch) )
{
Data[i] = ch;
i++;
}
else
{
Data[i] = NULL;
frequenz = atol(Data);
i = 0;
}
}
}
So where is my mistake is there someone who knows what to do?