Hi there,
I want to generate a square wave with certain frequency but the output frequency is smaller by a factor of 10 than what I expected.
To test it out I make this code. I use the internal 4Mhz clock (I also checked it with my 4 Mhz crystal). I expect to get a 1 Mhz wave out (since I counted to 3), but I only get 100 kHz.
#include <
avr/io.h>
#define F_CPU 4000000UL
// initialize timer, interrupt and variable
void timer1_init()
{
TCCR1B |= (1 << WGM12)|(1 << CS10);
// initialize counter
TCNT1 = 0;
// initialize compare value
OCR1A = 3;
}
int main(void)
{
// connect led to pin PC0
DDRC |= (1 << 0);
timer1_init(); // initialize timer
while(1) // loop forever
{
if (TIFR & (1 << OCF1A))
{
PORTC ^= (1 << 0); // toggles the led
}
// clear flag bit by writing '1' to it (as per the datasheet)
TIFR |= (1 << OCF1A);
}
}[size=-10]پروژه هاي الکترونيک کنترل مانيتورينگ اتوماسيون صنعتي ارایه انواع پروژه هاي صنعتي آزمايشگاهي دانشگاهي تحقيقاتي automasys.wordpress.com انواع ميکروکنترلر هاي ميکروچيپ اتمل اينتل فيليپس AVR ARM LPC STM32 PIC dsPIC 8051 8086 z80 انواع اينترفيس و پروتکل هاARM AVR PIC MicroController Electronic Project GPRS GSM USB BlueTooth Ethernet Network CAN I2C SPI RS485 Automation PLC پروژه الکترونيک ميکروکنترلر آرم 8051 اتمل فيليپس ميکروچيپ Interface اينترفيس ويژوال استاديو Visual Studio 2010 C# VB.Net Protel DXP Altium Eagle PCB SPICE PSPICE HSPICE ADS OrCAD Silvacoج[/size]
-----------------------------------------------------------------------------------------------------
I also made this code, hoping to get the 4 Mhz out, but only get 400 kHz.
#include <
avr/io.h>
#include <util/delay.h>
#define F_CPU 4000000UL
int main(void)
{
// connect led to pin PC0
DDRC |= (1 << 0);
while(1) // loop forever
{
PORTC ^= (1 << 0); // toggles the led
}
}
I use AVR studio 4 btw. I use a oscilloscope to measure the output wave. I also checked the code in Proteus.
I think I got the fuse bits right. When I use the second code with a 8 Mhz internal clock, I got 800 kHz out.
Please help me point out what I did wrong.