Hi guys,
You helped me to solved some problems of the code but now I got stuck somewhere else!
As you know I had decided to use our buddy, "Serial Monitor" to change the frequency of blinking.
Here is the final code as follows.
volatile boolean l;
//unsigned int integerValue=0; // Max value is 65535
//char incomingByte;
void TC0_Handler()
{
long dummy=REG_TC0_SR0; // vital - reading this clears some flag --- Status REG
// otherwise you get infinite interrupts
l= !l;
//REG_TC0_CCR0=0b101; //Start counter
}
//void setFrequen
void setup(){
Serial.begin(57600);
pinMode(13,OUTPUT);
}
void loop(){
digitalWrite(13,l);
if (Serial.available() > 0) { // something came across serial
char buffer[] = {' NUL',' NUL',' NUL',' NUL',' NUL',' NUL',' NUL',' NUL',' NUL'};
Serial.readBytesUntil('\n', buffer, 20);
int incomingValue = atoi(buffer);
Serial.println(incomingValue);
pinMode(2,OUTPUT); // port B pin 25
analogWrite(2,255);
REG_PIOB_PDR = 1<<25; // disable PIO, enable peripheral
REG_PIOB_ABSR= 1<<25;
REG_TC0_CMR0=0b00000000000010011100010000000000;
REG_TC0_IER0=0b00010000;
REG_TC0_RC0 = incomingValue*1000000;
NVIC_EnableIRQ(TC0_IRQn);
}
}
This works perfectly and each time when I put a new value for "incomingValue", it changes the frequency by changing the amount of "REG_TC0_RC0".
But I can only count up. What does this mean. I mean as follows:
1
8
20
40
80
100
Then I can see the frequency slows down and the LED beats slower and this change is instantly ..... But what if I go this way
1
8
1
Surprisingly it does change it instantly .... from 1 to 8 it is immediately but from 8 to 1 it takes 2 minutes!!!
I look for "COUNT DOWN/UP" in the datasheet and found the following register:
REG_TC0_SMMR0
which is responsible for count down and up
but I guess the problem is something else...
I am totally confused!
The problem is the "Serial monitor" and the way I used the buffer or NO! The problem is the Register configuration itself?
What do you think?
Any idea about this?
I really appreciate your kind and prompt reply in advance!