Atmega8L need help PWM

Discussion in 'Embedded Systems and Microcontrollers' started by y3llowfl4sh, Oct 28, 2009.

  1. y3llowfl4sh

    Thread Starter New Member

    Oct 28, 2009
    4
    0
    hi there,

    im using codevisionAVR to program Atmega8L,
    PWW on timer 2 (8-bit)
    Clock source: System Clock
    Clock value: 1000.000 kHz
    Mode: Phase correct PWM top=FFh
    OC2 output: Non-Inverted PWM

    im changing OCR2 from 0-255 to alter the duty cycle but
    when putting OCR2=0 the PWM output PIN is high,still can't figure this out it should be zero but when using fast PWM with top=ffh Mode this problem is solved.

    also when i tried to use PWM on timer1(16 bit)
    for both Modes still got the same problem

    here's my code
    any help will be highly appreciated
    thanx in advance

    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <mega8.h>
    3. #include <delay.h>
    4.  
    5. // Alphanumeric LCD Module functions
    6. #asm
    7.    .equ __lcd_port=0x12 ;PORTD
    8. #endasm
    9. #include <lcd.h>
    10.  
    11. // Declare your global variables here
    12.  
    13. unsigned char w[2];
    14.  
    15. int show_variable_on_lcd(int x)
    16.  
    17. {
    18. sprintf(w,"\r %04d",x);
    19. lcd_clear();
    20. lcd_puts(w);
    21. delay_ms(200);
    22. return;
    23. }
    24.  
    25.  
    26. void main(void)
    27. {
    28. // Declare your local variables here
    29.  
    30. // Input/Output Ports initialization
    31. // Port B initialization
    32. // Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In
    33. // State7=T State6=T State5=T State4=T State3=0 State2=T State1=T State0=T
    34. PORTB=0x00;
    35. DDRB=0x08;
    36.  
    37. // Port C initialization
    38. // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    39. // State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    40. PORTC=0x00;
    41. DDRC=0x00;
    42.  
    43. // Port D initialization
    44. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    45. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    46. PORTD=0x00;
    47. DDRD=0x00;
    48.  
    49. // Timer/Counter 0 initialization
    50. // Clock source: System Clock
    51. // Clock value: Timer 0 Stopped
    52. TCCR0=0x00;
    53. TCNT0=0x00;
    54.  
    55. // Timer/Counter 1 initialization
    56. // Clock source: System Clock
    57. // Clock value: Timer 1 Stopped
    58. // Mode: Normal top=FFFFh
    59. // OC1A output: Discon.
    60. // OC1B output: Discon.
    61. // Noise Canceler: Off
    62. // Input Capture on Falling Edge
    63. // Timer 1 Overflow Interrupt: Off
    64. // Input Capture Interrupt: Off
    65. // Compare A Match Interrupt: Off
    66. // Compare B Match Interrupt: Off
    67. TCCR1A=0x00;
    68. TCCR1B=0x00;
    69. TCNT1H=0x00;
    70. TCNT1L=0x00;
    71. ICR1H=0x00;
    72. ICR1L=0x00;
    73. OCR1AH=0x00;
    74. OCR1AL=0x00;
    75. OCR1BH=0x00;
    76. OCR1BL=0x00;
    77.  
    78. // Timer/Counter 2 initialization
    79. // Clock source: System Clock
    80. // Clock value: 1000.000 kHz
    81. // Mode: Phase correct PWM top=FFh
    82. // OC2 output: Non-Inverted PWM
    83. ASSR=0x00;
    84. TCCR2=0x61;
    85. TCNT2=0x00;
    86. OCR2=0x00;
    87.  
    88. // External Interrupt(s) initialization
    89. // INT0: Off
    90. // INT1: Off
    91. MCUCR=0x00;
    92.  
    93. // Timer(s)/Counter(s) Interrupt(s) initialization
    94. TIMSK=0x00;
    95.  
    96. // Analog Comparator initialization
    97. // Analog Comparator: Off
    98. // Analog Comparator Input Capture by Timer/Counter 1: Off
    99. ACSR=0x80;
    100. SFIOR=0x00;
    101.  
    102. // LCD module initialization
    103. lcd_init(16);
    104.  
    105. while (1)
    106.       {
    107.       // Place your code here
    108.       OCR2=0;
    109.       show_variable_on_lcd(OCR2);
    110.  
    111.       };
    112. }
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    I have checked the registers of the microcontroller from its datasheet and the code CodeVision generated is correct (at least for timer 2). Maybe a fuse is not set properly.
     
  3. Frakk

    Active Member

    Jul 6, 2009
    40
    0
    When using fast pwm, the output will be high for 1 clock cycle (assuming non-inverting mode), even if the compare register is 0 so you can't achieve 0% duty cycle. If I remember correctly this is written in the datasheet and I had this experience with them as well. Try doing phase correct pwm and see if that helps.
     
  4. y3llowfl4sh

    Thread Starter New Member

    Oct 28, 2009
    4
    0
    i tried phase correct mode but it's the same,also when i set the compare register to 0 the duty cycle is 100%.

    i think i'll go for inverting mode to handle this problem,but if you have any suitable solution just through it .

    im really grateful for your help
    thnx for your time,,

    Amr
     
  5. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Does inverting mode setting solves the problem?
     
  6. y3llowfl4sh

    Thread Starter New Member

    Oct 28, 2009
    4
    0
    it doesn't solve the problem, but at least when the compare register=0 the output=0
     
  7. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Are you sure you have the ATMega8 chip and not another similar to it?
     
  8. y3llowfl4sh

    Thread Starter New Member

    Oct 28, 2009
    4
    0
  9. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    If you put the correct settings in codevision and let generate the code it will work fine. Remove the LCD code from the program and try again with only the PWM code. Maybe your programming the problem is with the programmer.
     
Loading...