I wanna produce a square wave with mode CTC of timer 1. In addition, I activate usart1 of atmega128 and I could send and receive data by Rs232 Interface. But, Atmega128 doesn't generate square wave, I midified the registers of timer 1 but the output is zero. Why?
Can everybody help me?
My code is here.
Can everybody help me?
My code is here.
Code:
*****************************************************/
#include <mega128a.h>
#include <stdio.h>
#include <delay.h>
#ifndef RXB8
#define RXB8 1
#endif
#ifndef TXB8
#define TXB8 0
#endif
#ifndef UPE
#define UPE 2
#endif
#ifndef DOR
#define DOR 3
#endif
#ifndef FE
#define FE 4
#endif
#ifndef UDRE
#define UDRE 5
#endif
#ifndef RXC
#define RXC 7
#endif
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// Get a character from the USART1 Receiver
#pragma used+
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
}
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
}
char getchar1(void)
{
char status,data;
while (1)
{
while (((status=UCSR1A) & RX_COMPLETE)==0);
data=UDR1;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
return data;
}
}
#pragma used-
// Write a character to the USART1 Transmitter
#pragma used+
void putchar1(char c)
{
while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
UDR1=c;
}
#pragma used-
// Declare your global variables here
void main(void)
{
char a=0;
// Declare your local variables here
//-----------------------------------------------------------------------------------------Ports
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x10;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xff;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x00;
// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;
// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;
//-------------------------------------------------------------------------------------------Usart1
// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 9600
UCSR1A=0x00;
UCSR1B=0x18;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x19;
//-----------------------------------------------------------------------------------------Timer0
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 4000.000 kHz
// Mode: Normal top=0xFF
// OC0 output: Set on compare match
ASSR=0x00;
TCCR0=0x31;
TCNT0=0x00;
OCR0=0x00;
//------------------------------------------------------------------------------------------Timer1
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 4000.000 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Set
// OC1B output: Set
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
//-----------------------------------------------------------------------------------------------Interrup
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x05;
// Global enable interrupts
#asm("sei")
//-----------------------------------------------------------------------------------------------------
while (1)
{
PORTB.4=0;
delay_ms(50);
PORTB.4=1;
delay_ms(1000);
a=getchar1();
if (a=='p')
{
putchar1('T');
}
if (a=='y')
{
putchar1('A');
}
if (a=='a')
{
PORTC.1=1;
}
if (a=='b')
{
PORTC.2=1;
}
if (a=='q')
{
PORTC.1=0;
}
if (a=='r')
{
PORTC.2=0;
}
if (a=='j')
{
TCCR1A=0xFC;
TCCR1B=0x10;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0xFF;
OCR1AH=0x00;
OCR1AL=0x7F;
OCR1BH=0x00;
OCR1BL=0x7F;
OCR1CH=0x00;
OCR1CL=0x7F;
//TCCR1A=0xF0;
//TCCR1B=0x01;
// TCNT1H=0x00;
//TCNT1L=0x00;
//ICR1H=0x00;
//ICR1L=0x50;
// OCR1AL=0x3D;
// OCR1BL=0x3D;
}
}
}