@Levi, I was afraid of this answer --- to use a ... Timer. So, let's get back to using a Timer.
I searched the office drawer for an Atmega16 and I want to generate on the port C pins, let's say two frequencies used in the old analog communications, namely: 1644 Hz and 2544 Hz. I opened a very good Mikroelectronika tool, namely: "
Timer Calculator" and here, I would like your opinion or other users, I chose as "
Intrrrerupt time" the value: 0.4us ??? Is that enough? What do you think ?
Here's how the code looks like:
Code: Select all
unsigned int cnt=0;
unsigned long adc;
//Timer0 Prescaler = 0; Preload = 2; Actual Interrupt Time = 375 ns
void InitTimer0(){
SREG_I_bit = 1; // enable intrrerupts
OCR0 = 2;
TCCR0 = 0x28;
TCCR0 |= 0x01;
OCIE0_bit = 1;
}
void Timer0Overflow_ISR() org IVT_ADDR_TIMER0_COMP {
if(++cnt==46) // "cnt" can adjust the value of the PORTC output frequency
// cnt=46 for 1646Hz
// if I choose a cnt = 29 it results in 2600Hz and at a cnt = 30 it results in 2519Hz. Ups!
{ cnt = 0;
if(PORTC) PORTC = 0x00;
else PORTC = 0xFF;
}
}
void main() {
DDRC = 0xFF; // Setup PORT C output
ADC_Init(); // ADC init
do
{
adc = 0;
adc = ADC_Read(0);
if(adc<900)
{
InitTimer0();
}
} while(1);
}
In the code above the timer starts and the PORTC activates with a frequency of approximately 1646Hz, not 1644Hz, more precisely, it also depends on the accuracy of the measuring device, in my case 1646Hz.
The problem is that I can't get 2544Hz fixed ??? Should I use another timer ??? Or could I use just one timer?
However, how can I stop the generation of these frequencies on port C ??