I have created 2 different PWMs using timer 1 and 2 on ATmega8, that is used to control a linear motor. I'm using a hall effect sensor to detect changes in voltage as the linear motor moves left and right. This change in voltage I feed into a ADC pin in the ATmega8 in order to convert it to digital. Then I basically use the USART functions of the ATmega8 to display the voltage the hall effect sensor basically detects.
I'm running the ADC in free running mode with interrupts to continuously detect a hall effect value.
The problem im having at the moment is that as soon as the interrupt is called to read the adc value i have a function in the interrupt that involves transmitting the adc value to the screen.......
Currently what happens is that as soon as I do that the PWM stops working, its as if the program is stuck in the interrupt in an infinite loop.
However, if I remove the serial transmission function, the PWM starts working again which confuses me?
//TRANSMISSION FUNCTION
void USARTWriteChar(uint8_t data) { //Wait untill the transmitter is ready
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
UDR=data;
}
//Main function containing PWM
while (1)
{
OCR1A = Push1; // Output compare register A Time 1
OCR1B = 0;
_delay_ms(400); // set on time A
OCR1A = 0;
OCR1B = 0;
_delay_ms(400); // set dead time B
}
//Interrupt function
ISR(ADC_vect) {
read = ADCH;
USARTWriteChar(read);
}
I am running the ATmega8 at 8Mhz clock frequency with BAUD of 9600.
However when I take off USARTWriteChar(read); (still keeping rad = ADCH ) from the ISR the PWM works meaning the ADC interrupt is not causing problems but USART is for some reasons.