I was a making a project in which the intensity of LED glow increase with increase the intensity of light falling on a LDR.
I am using an ATmega32 , configured at 8MHz internal clock ,i have connected the LDR to pin 4 of port A of ATmega it is giving correct ADC values .Now for PWM to increase/decrease the LED light intensity i am using 16 bit timer 1 and pin OC1A is connected to with LED via a resistor of 330 ohm . The PWM on the output pin is 4KHz ,fast PWM mode with ICR1 as top and OCR1A to set the high time,also it is configured to clear OC1A pin on compare match. To check the values of ADC result and OCR1A which is calculated form ADC value using the equation
OCR1A = (1999*ADC_result)/1023;
i am using UART and checking it on my laptop.
but the value of OCR is coming out to be a very large number , or sometimes some random number (i have added screenshots for reference).
My USART, PWM and ADC all libraries/functions are working correctly independently.
i have copied the main function of the code below for reference.
USART_Init(51);//9600 baud rate
USART_Transmitchar(0x0D); // newline
adc_result = ReadADC(4); //reading ADC value from pin 4
USART_TransmitString("ADC result :- "); //sending string using UART
USART_TransmitNumber(adc_result); // sending 10 bit ADC number in
intermediate = adc_result*1999; //ERROR OCCURS HERE
USART_TransmitString("intermediate :- ");
OCR_value = intermediate/1023; //ERROR HERE AS WELL
USART_TransmitString("OCR result :- ");