1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

pulse width generation help

Discussion in 'Microcontrollers' started by neil1221, Mar 4, 2009.

  1. neil1221

    neil1221 New Member

    Likes Received:
    Trophy Points:
    hello can anyone help me about my problem my aim is to output a PWM frequency of 5khz and i wanted the on state to be .16ms.

    my theory is this.....
    my crystal oscillator is equal to 12 MHz base from the tutorial that I have read cycles per second can be obtain by dividing the value of the osillator by 12clock pulses so for mine I have 1,000,000 cycles per second.So to execute 1 machine cycle it takes .001ms.

    I want a 5Khz frequency so the period is 1/5 khz =.2ms
    now I want the on state to be .16ms so the low state should be
    .2ms-.16ms = .04ms

    In terms of machine cycle for the on state
    I need .16ms/.001ms =160 cycles
    and for the off state
    I need .04/.001ms = 40 cycles

    I use a timer mode 1(16 bit) so it will overflow at 65356 now I must start the timer at 65536 - 160 = 65376 = FF60 ; hex for the on state
    and for off state I must start the timer at
    65536-40 = 65496 = FFD8 ;hex for the off state.

    nowI have finsih coding the program and butn it on th MC, but when I check the pulse output of the pin on oscilloscope the PWM width on state and off state is on 50%. What I expect it to see a square wave that is almost 80% on and 20% off because from the value that I've set (.16ms) it is 80% of the period. Also when I check the frequency output it plays around 25-26Khz not 5 Khz.

    could anyone help me with this are my assumptions and computations incorrect..Or there is no chance to see a square wave that is 80% on and 20% low on a microcontroller?? Im using at89c2051 mc. ive attach the code and sample picture that i able to generate on the mc the one that I expect to see.

    Code (Text):
    2. PWMPIN  EQU   P1.5  ;
    4. ORG 0H
    5. PWM_SETUP:
    6.  MOV TMOD,#01H    ;enable timer 0 to mode1 (16bit mode)
    7.  SETB EA                  ;enable interrupts
    8.  SETB ET0         ;enable timer0 interrupt
    9.  SETB TR0                 ; start timer
    11. acall TIMER_0_INTERRUPT ;  call the timer0interrup routine
    12. SJMP PWM_SETUP ;
    15.  JB F0, HIGH_DONE ;       If F0 flag is set then we just finished
    16.                                 ; the high section of the
    18. LOW_DONE:
    19.  SETB F0          ; Make F0=1 to indicate start of high section
    20.  SETB PWMPIN  ; Make output pin high
    21.  MOV TH0,0xFF ; setting initial value 65376
    22.  MOV TL0,0x60 ;
    23.  CLR TF0          ; clear flag
    24.  RETI                  
    26. HIGH_DONE:
    27.  CLR F0          ;Make F0=0 to indicate start of low section
    28.  CLR PWMPIN  ;  Make PWM output pin low
    29.  MOV TH0,0xFF ;  setting intial value 65496
    30.  MOV TL0,0xD8;
    31.  CLR TF0   ;clear flag
    32.  RETI
    33. END

    Image Insert:
  2. Binu

    Binu Administrator

    Likes Received:
    Trophy Points:
    Your program seems ok.
    Change the timer values in TH0 & TL0 and try.
    I think the problem is on the Timer values.

Share This Page