Programming PWM on ADuC812

Discussion in 'Embedded Systems and Microcontrollers' started by samik999, Dec 3, 2011.

  1. samik999

    Thread Starter New Member

    Dec 3, 2011
    1
    0
    Hi
    I work with a microcontroller ADuC812. wrote a program that produces the formation of a linearly increasing output voltage (DAС0), converting an input voltage of 0-channel ADC, and transfer the data via the RS-232. I do it in the Keil uVision. This is a code(C++):

    Code ( (Unknown Language)):
    1.  
    2.  # Include <ADuC812.h>
    3.  # Include <stdio.h>
    4.  # Include "ADC.h"
    5.  # Include "DAC.h"
    6.  # Include "max.h"
    7.  # Include "vect.h"
    8.  
    9.  float in, out;
    10.  unsigned int i;
    11.  void INT_T0 (void) interrupt 1
    12.  {
    13.  out = out + 0.1;
    14.  
    15.  if (out> = 5) out = 0;
    16.  SetVoltage (out, 0);
    17.  TR1 = 1;
    18.  
    19.  }
    20.  void INT_T1 (void) interrupt 3
    21.  {
    22.  in = GetVoltage (0);
    23.  
    24.  printf ("DAC0 =% f", out); /
    25.  printf (": ADC0 =% f \ n", in);
    26.  TR1 = 0;
    27.  TF1 = 0;
    28.  TH1 = 0;
    29.  TL1 = 0;
    30.  }
    31.  
    32.  void main (void)
    33.  
    34.  {
    35.  
    36.  
    37.  //----------- Set UART ---------------
    38.  SCON = 0x40;
    39.  REN = 1;
    40.  / / Set timer 2 ------
    41.  RCLK = 1;
    42.  TCLK = 1,
    43.  RCAP2L = 0xDC;
    44.  RCAP2H = 0xFF;
    45.  TR2 = 1
    46.  ES = 0,
    47.  TI = 1; RI = 1;
    48.  EA = 0;
    49.  //------------------------------------------
    50.  EA = 1;
    51.  ET0 = 1;
    52.  ET1 = 1;
    53.  TMOD = 0x00010101;
    54.  TR0 = 1;
    55.  SetVect (1, INT_T0);
    56.  SetVect (3, INT_T1);
    57.  SwitchDAC (0,1);
    58.  InitDAC (0);
    59.  SetVoltage (5,0);
    60.  InitADC ();
    61.  while (1)
    62.  {}
    63.  }
    64.  
    65.  
    I need to make a PWM (Pulse-width modulation) generator depending on the voltage at the ADC input. Frequency is 1 kHz. I work with microcontrollers only for 3 months, so do not have enough experience to do without help. Please help me!
    P. S. Sorry for my English. :)
     
    Last edited by a moderator: Dec 3, 2011
  2. tommydyhr

    Active Member

    Feb 3, 2009
    39
    4
    Is the duty cycle to be directly proportional to the ADC-input? If that's the case, then a simple solution would be to perform some basic arithmetic to the ADC result, so that it can be appended to the PWM SFR.

    You could also just capture the ADC-value, and match it to some constants you've defined in your ROM.

    In any case, it's a question of priorities. Do you need to conserve space in the ROM, or do you need the program to perform the task as quickly as possible?

    Edit: After looking up the specific MCU, it would seem as if it doesn't have a PWM-module at all, and thus has no specific SFR. Now some new questions arise; How great accuracy do you demand? Are you OK with additional external circuitry?
     
    Last edited: Dec 9, 2011
Loading...