Autor Tema: PWM desde Cero!!!!!  (Leído 2174 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado jose_adrian_aguilar

  • PIC10
  • *
  • Mensajes: 12
PWM desde Cero!!!!!
« en: 26 de Septiembre de 2010, 21:23:35 »
Hola a todos espero me puedan ayudar!!!!!! :-/ :-/ :-/ :-/
deceo configurar PWM desde cero en mikrobasic, la libreria no me sirve
ya que deseo poder cambiar la frecuencia en tiempo de ejecucuin y la
funcion PWM_init(xxx) no me acepta variables, epero alguin me pueda dar un ejemplo de lo que tengo que hacer
la frecuencia que necesito es de 1Hz hasta 15khz y poder varia el ciclo de trabajo!!! del 1% al 10%
gracias!!!!!! de antemano :-/ :-/ :-/ :-/

los datos seran cargados desde un key pad 4*4 se mostraran en lcd
se validaran los datos correctos y se cargara el pwm nadamas que no se deja configurar el pwm


este es el codigo


program KeyPadLcdPic18f4550DelayPwm
dim kp, ks, dig, dig1, retardo as byte
dim kr, frequ as longint
dim txt as char[5]
dim txt1 as char[2]
' Keypad modulo de coneccion
dim keypadPort as byte at PORTB
' End Keypad modulo de coneccion
' Lcd modulo de coneccion
dim LCD_RS as sbit  at RD4_bit
    LCD_EN as sbit  at RD5_bit
    LCD_D4 as sbit  at RD0_bit
    LCD_D5 as sbit  at RD1_bit
    LCD_D6 as sbit  at RD2_bit
    LCD_D7 as sbit  at RD3_bit

    LCD_RS_Direction as sbit at TRISD4_bit
    LCD_EN_Direction as sbit at TRISD5_bit
    LCD_D4_Direction as sbit at TRISD0_bit
    LCD_D5_Direction as sbit at TRISD1_bit
    LCD_D6_Direction as sbit at TRISD2_bit
    LCD_D7_Direction as sbit at TRISD3_bit
' End Lcd modulo de coneccion
main:
  PORTC = $ff
  CMCON = 7              'configurar pic como todo digital
  kr = 0
  dig = 0
  frequ = 0
  dig1 = 0
  ks = 0
  retardo = 0
  Keypad_Init()                       ' Inicializar Keypad
  Lcd_Init()                          ' Inicializar LCD
  Lcd_Cmd(_LCD_CLEAR)                 ' Borrar display
  Lcd_Cmd(_LCD_CURSOR_OFF)            ' curcor apagado
  Lcd_Out(1, 1, "Frecuencia en Hz:")  ' Escribir mensage de texto en lcd
  while TRUE
    kp = 0                            ' Reiniciar la variable del codigo de tecla
    while ( kp = 0 )
      kp = Keypad_Key_Click()         '
    wend

    select case kp
    case 1
      kp = 68  '  d
    case 2
      kp = 35  '  #
    case 3
      kp = 48  '  0
      kr = 0
      ks = 0
    case 4
      kp = 42  '  *
    case 5
      kp = 67  '  c
    case 6
      kp = 57  '  9
      kr = 9
      ks = 9
    case 7
      kp = 56  '  8
      kr = 8
      ks = 8
    case 8
      kp = 55  '  7
      kr = 7
      ks = 7
    case 9
      kp = 66  '  b
    case 10
      kp = 54  '  6
      kr = 6
      ks = 6
    case 11
      kp = 53  '  5
      kr = 5
      ks = 5
    case 12
      kp = 52  '  4
      kr = 4
      ks = 4
    case 13
      kp = 65  '  a
    case 14
      kp = 51  '  3
      kr = 3
      ks = 3
    case 15
      kp = 50  '  2
      kr = 2
      ks = 2
    case 16
      kp = 49  '  1
      kr = 1
      ks = 1
    end select

    if (dig <6) then
    Lcd_Chr(2,1+dig, kp)
      if (dig=0)then
        if (kr <> 0 )then
          frequ=kr*10000
          kr=0
          Inc (dig)
        end if
      end if
     
      if (dig=1)then
        if (kr<>0)then
          frequ=frequ+(kr*1000)
          kr=0
          Inc (dig)
        end if
      end if

      if (dig=2)then
        if (kr<>0)then
          frequ=frequ+(kr*100)
          kr=0
          Inc (dig)
        end if
      end if

      if (dig=3)then
        if (kr<>0)then
          frequ=frequ+(kr*10)
          kr=0
          Inc (dig)
        end if
      end if

      if (dig=4)then
        if (kr<>0)then
          frequ=frequ+kr
          kr=0
          Inc (dig)
        end if
      end if
      if (dig=5) then
        if (kp=65)then 'tecla A
          LongIntToStr(frequ,txt)
          Lcd_Cmd(_LCD_CLEAR)
          Lcd_Out(1, 1, "Frecuency salida")
          Lcd_Out(2,1,txt)
          Lcd_Out(2, 7,"Hz")
          Inc (dig)
        end if
      end if
    end if
   
    Delay_ms(3000)
    Lcd_Cmd(_LCD_CLEAR)
    Lcd_Out(1, 1, "Delay 1-10 ms")
   
    if (dig1<3)then
    Lcd_Chr(2,1+dig1,kp)
      if (dig1=0)then
        if (ks<>0 )then
          retardo=(ks*10)
          ks=0
          Inc(dig1)
        end if
      end if

      if (dig1=1)then
        if (ks<>0)then
          retardo=retardo+ks
          ks=0
          Inc(dig1)
        end if
      end if
     
      if (dig1=2)then
        if (ks<>0)then
          ByteToStr(retardo,txt1)
          Lcd_Cmd(_LCD_CLEAR)
          Lcd_Out(1, 1, "Delay en ms")
          Lcd_Out(2,1,txt1)
          Lcd_Out(2, 4,"ms")
          Inc(dig1)
        end if
      end if
     
    end if
    if (frequ>15000) then
    Lcd_Cmd(_LCD_CLEAR)                 ' Borrar display
    Lcd_Out(1,1, "Error!!Frecuency")
    Lcd_Out(2,1, "Debe ser menor")
    goto main
    end if

    if (retardo<1) then
    Lcd_Cmd(_LCD_CLEAR)                 ' Borrar display
    Lcd_Out(1,1, "Error!!Delay")
    Lcd_Out(2,1, "Debe ser mayor")
    goto main
    end if

    if (retardo>10) then
    Lcd_Cmd(_LCD_CLEAR)                 ' Borrar display
    Lcd_Out(1,1, "Error!!Delay")
    Lcd_Out(2,1, "Debe ser menor")
    goto main
    end if

    PWM1_Init(frequ)
    PWM1_Set_Duty(retardo)
    PWM1_Start()

  wend
end.
« Última modificación: 26 de Septiembre de 2010, 21:35:55 por jose_adrian_aguilar »

Desconectado dogflu66

  • Moderador Local
  • DsPIC30
  • *****
  • Mensajes: 3510
Re: PWM desde Cero!!!!!
« Respuesta #1 en: 27 de Septiembre de 2010, 06:43:00 »
En el indice de este hilo puedes encontrar como configurar el modulo CCP a mano.

"TRABAJANDO CON EL MODULO CCP1 DIRECTAMENTE"
http://www.todopic.com.ar/foros/index.php?PHPSESSID=ccfe536794006c3ee590bdb13d6c188f&topic=14917.0
« Última modificación: 27 de Septiembre de 2010, 06:46:52 por dogflu66 »
Saludos desde Granada, España.


 

anything