atmega128 doesn't generate a square wave

Discussion in 'General Electronics Chat' started by Bluestar88, Mar 9, 2015.

  1. Bluestar88

    Thread Starter New Member

    Nov 16, 2014
    23
    0
    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.
    Code (Text):
    1. *****************************************************/
    2.  
    3. #include <mega128a.h>
    4. #include <stdio.h>
    5. #include <delay.h>
    6.  
    7. #ifndef RXB8
    8. #define RXB8 1
    9. #endif
    10.  
    11. #ifndef TXB8
    12. #define TXB8 0
    13. #endif
    14.  
    15. #ifndef UPE
    16. #define UPE 2
    17. #endif
    18.  
    19. #ifndef DOR
    20. #define DOR 3
    21. #endif
    22.  
    23. #ifndef FE
    24. #define FE 4
    25. #endif
    26.  
    27. #ifndef UDRE
    28. #define UDRE 5
    29. #endif
    30.  
    31. #ifndef RXC
    32. #define RXC 7
    33. #endif
    34.  
    35. #define FRAMING_ERROR (1<<FE)
    36. #define PARITY_ERROR (1<<UPE)
    37. #define DATA_OVERRUN (1<<DOR)
    38. #define DATA_REGISTER_EMPTY (1<<UDRE)
    39. #define RX_COMPLETE (1<<RXC)
    40.  
    41. // Get a character from the USART1 Receiver
    42. #pragma used+
    43.  
    44. // Timer 0 overflow interrupt service routine
    45.  
    46.  
    47. interrupt [TIM0_OVF] void timer0_ovf_isr(void)
    48. {
    49. // Place your code here
    50.  
    51. }
    52.  
    53. // Timer1 overflow interrupt service routine
    54. interrupt [TIM1_OVF] void timer1_ovf_isr(void)
    55. {
    56. // Place your code here
    57.  
    58. }
    59.  
    60.  
    61. char getchar1(void)
    62. {
    63. char status,data;
    64. while (1)
    65.       {
    66.       while (((status=UCSR1A) & RX_COMPLETE)==0);
    67.       data=UDR1;
    68.       if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
    69.          return data;
    70.       }
    71. }
    72. #pragma used-
    73.  
    74. // Write a character to the USART1 Transmitter
    75. #pragma used+
    76. void putchar1(char c)
    77. {
    78. while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
    79. UDR1=c;
    80. }
    81. #pragma used-
    82.  
    83. // Declare your global variables here
    84.  
    85. void main(void)
    86. {
    87. char a=0;
    88. // Declare your local variables here
    89.  
    90. //-----------------------------------------------------------------------------------------Ports
    91. // Input/Output Ports initialization
    92. // Port A initialization
    93. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    94. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    95. PORTA=0x00;
    96. DDRA=0x00;
    97.  
    98. // Port B initialization
    99. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    100. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    101. PORTB=0x00;
    102. DDRB=0x10;
    103.  
    104. // Port C initialization
    105. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    106. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    107. PORTC=0x00;
    108. DDRC=0xff;
    109.  
    110. // Port D initialization
    111. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    112. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    113. PORTD=0x00;
    114. DDRD=0x00;
    115.  
    116. // Port E initialization
    117. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    118. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    119. PORTE=0x00;
    120. DDRE=0x00;
    121.  
    122. // Port F initialization
    123. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    124. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    125. PORTF=0x00;
    126. DDRF=0x00;
    127.  
    128. // Port G initialization
    129. // Func4=In Func3=In Func2=In Func1=In Func0=In
    130. // State4=T State3=T State2=T State1=T State0=T
    131. PORTG=0x00;
    132. DDRG=0x00;
    133. //-------------------------------------------------------------------------------------------Usart1
    134. // USART1 initialization
    135. // Communication Parameters: 8 Data, 1 Stop, No Parity
    136. // USART1 Receiver: On
    137. // USART1 Transmitter: On
    138. // USART1 Mode: Asynchronous
    139. // USART1 Baud Rate: 9600
    140. UCSR1A=0x00;
    141. UCSR1B=0x18;
    142. UCSR1C=0x06;
    143. UBRR1H=0x00;
    144. UBRR1L=0x19;
    145.  
    146.  
    147. //-----------------------------------------------------------------------------------------Timer0
    148. // Timer/Counter 0 initialization
    149. // Clock source: System Clock
    150. // Clock value: 4000.000 kHz
    151. // Mode: Normal top=0xFF
    152. // OC0 output: Set on compare match
    153. ASSR=0x00;
    154. TCCR0=0x31;
    155. TCNT0=0x00;
    156. OCR0=0x00;
    157.  
    158. //------------------------------------------------------------------------------------------Timer1
    159. // Timer/Counter 1 initialization
    160. // Clock source: System Clock
    161. // Clock value: 4000.000 kHz
    162. // Mode: Normal top=0xFFFF
    163. // OC1A output: Set
    164. // OC1B output: Set
    165. // OC1C output: Discon.
    166. // Noise Canceler: Off
    167. // Input Capture on Falling Edge
    168. // Timer1 Overflow Interrupt: On
    169. // Input Capture Interrupt: Off
    170. // Compare A Match Interrupt: Off
    171. // Compare B Match Interrupt: Off
    172. // Compare C Match Interrupt: Off
    173. TCCR1A=0x00;
    174. TCCR1B=0x00;
    175. TCNT1H=0x00;
    176. TCNT1L=0x00;
    177. ICR1H=0x00;
    178. ICR1L=0x00;
    179. OCR1AH=0x00;
    180. OCR1AL=0x00;
    181. OCR1BH=0x00;
    182. OCR1BL=0x00;
    183. OCR1CH=0x00;
    184. OCR1CL=0x00;
    185.  
    186. //-----------------------------------------------------------------------------------------------Interrup
    187. // Timer(s)/Counter(s) Interrupt(s) initialization
    188. TIMSK=0x05;
    189.  
    190. // Global enable interrupts
    191. #asm("sei")
    192. //-----------------------------------------------------------------------------------------------------
    193. while (1)
    194.       {
    195.      
    196.      
    197.       PORTB.4=0;
    198.       delay_ms(50);
    199.  
    200.       PORTB.4=1;
    201.       delay_ms(1000);
    202.      
    203.      
    204.        a=getchar1();
    205.      
    206.      
    207.         if (a=='p')
    208.       {
    209.        putchar1('T');
    210.            
    211.        }
    212.      
    213.          if (a=='y')
    214.       {
    215.        putchar1('A');
    216.            
    217.        }          
    218.  
    219.     if (a=='a')
    220.       {
    221.        PORTC.1=1;
    222.        }
    223.  
    224.  
    225.     if (a=='b')
    226.       {
    227.        PORTC.2=1;
    228.        }    
    229.  
    230.     if (a=='q')
    231.       {
    232.        PORTC.1=0;
    233.        }
    234.          
    235.     if (a=='r')
    236.       {
    237.        PORTC.2=0;
    238.        }  
    239.    
    240.     if (a=='j')
    241.     {
    242.     TCCR1A=0xFC;  
    243.     TCCR1B=0x10;  
    244.     TCNT1H=0x00;  
    245.     TCNT1L=0x00;  
    246.     ICR1H=0x00;  
    247.     ICR1L=0xFF;  
    248.     OCR1AH=0x00;  
    249.     OCR1AL=0x7F;  
    250.     OCR1BH=0x00;  
    251.     OCR1BL=0x7F;  
    252.     OCR1CH=0x00;  
    253.     OCR1CL=0x7F;
    254.      //TCCR1A=0xF0;
    255.      //TCCR1B=0x01;
    256.     // TCNT1H=0x00;
    257.      //TCNT1L=0x00;
    258.      //ICR1H=0x00;
    259.      //ICR1L=0x50;
    260.     // OCR1AL=0x3D;
    261.     // OCR1BL=0x3D;
    262.  
    263.     }  
    264.  
    265.   }  
    266. }
    267.  
     
  2. Hypatia's Protege

    Distinguished Member

    Mar 1, 2015
    2,795
    1,236
    While I apologize that I cannot be of assistance regarding Atmel products I suggest that your post may be better placed on the forum entitled "Programmer's corner"

    With helpful intent
    HP
     
Loading...