ad space

One MHz sine wave with ATmega32A

 Here is an illustration of how one can generate 1MHz sine wave signal with ATmega32A. The ATmega32A Timer0 can be used in the CTC mode with toggle mode to generate 1MHz square wave signal. The square wave output appear in the OC0 pin which is Port B pin 3. This square wave can be filtered using low pass filter to generate 1MHz sinusoidal signal. We may also use Fast PWM mode for this but for square wave, CTC is more suitable.

Following picture shows ATmega32A on breadboard with Low Pass filter made up of 22uH inductor and 1nF capacitor for filtering the square wave. Also picture shows frequency counter measuring the frequency of signal from the filter output. Here Arduino Nano is used as frequency counter.

ATmega32A with LC LPF and Frequency counter

The following shows the schematic diagram of generate 1MHz sine wave.

In the schematic diagram a LC low pass filter made of up 22uH and 1nF is connected to the OC0 pin(Port B pin 3) of the ATmega32 microcontroller. The cutoff frequency of this LC LPF is 1.1MHz.

In order to generate the 1MHz square wave, we have to calculate the count value to be loaded into the OCR0 register. This depends upon the frequency of the microcontroller CPU frequency which is 4MHz in this case, and the desired output wave frequency which is 1MHz. We can use the Atmega32/ATmega32A online Timer0 calculator for calculating the OCR0 value. The value to be loaded for 1MHz square wave output with 4MHz crystal oscillator is just 1.

ATmega32 with LPF circuit diagram

The C program code for generating this 1MHz using CTC(Clear Timer on Compare) mode with toggle is below.

//Program to generate 1MHz sq.wave using toggle mode in CTC mode
//Source: http://ee-diary.bogspot.com

#ifndef F_CPU
#define F_CPU 4000000UL
#endif

#include <avr/io.h>

int main()
 { 
    DDRB |= (1<<PB3);	//set PORT B pin 3 as output
	
	OCR0 = 1;
	TCCR0 |= (1<<WGM01) | (1<<COM00) | (1<<CS00);	//CTC mode, Toggle, No Prescalar
	while(1);

   return 0;
 }

 The C program is very easy, we just load the OCR0 register with 1 and configure the Timer0 to use CTC mode and toggle mode. 

 The following video is demonstration.

You might also want to look Atmega32A Timer Counter Tutorial Examples.

Post a Comment

Previous Post Next Post