aide Pic 18F2520 et LCD 2*16
Répondre à la discussion
Affichage des résultats 1 à 2 sur 2

aide Pic 18F2520 et LCD 2*16



  1. #1
    invite1fc93821

    aide Pic 18F2520 et LCD 2*16


    ------

    Bonjour,

    Comme l'indique le titre du sujet, j'ai un souci avec le programe d'un Pic18F2520 pour indiquer une mesure sur un afficheur lcd 2 * 16.
    Je suis loin d'être un Pro et je me debrouille avec des bouts de code chiner ici et là.

    Le montage est un générateur PWM don la frequence et le rapport cylique sont reglable via le Can du PIC
    et je veux affiché le % du rapport cyclique ainsi que la fréquence sur le LCD.

    Le montage fonctionne à l'exeption que la broche 13 du PIC reliée a la broche RS du lcd qui ne repond pas.
    Cela a pour consequence de faire défiler les valeur de droite a gauche et de haut en bas sur le LCD ...
    J'ai donc fait une erreur biensur, mais je n'arrive pas a la localiser :

    Si vous pouvez m'aider se serais sympa

    Voici le code en MikroPascal :

    Code:
    program 
    
    const
    
      cFreq_00 = 500;
      cFreq_01 = 750;
      cFreq_02 = 1000;
      cFreq_03 = 2000;
      cFreq_04 = 3000;
      cFreq_05 = 4000;
      cFreq_06 = 5000;
      cFreq_07 = 7500;
      cFreq_08 = 10000;
      cFreq_09 = 15000;
      cFreq_10 = 20000;
      cFreq_11 = 25000;
      cFreq_12 = 30000;
      cFreq_13 = 35000;
      cFreq_14 = 40000;
      cFreq_15 = 45000;
      cFreq_16 = 50000;
      cFreq_17 = 55000;
      cFreq_18 = 60000;
      cFreq_19 = 65000;
      cFreq_20 = 70000;
      bShowUnit = 0;
    
    var
      iAdcPWMFreq, iAdcPWMDuty: word;
      PwmFreqNew, PwmFreqOld: word;
      PwmDutyNew, PwmDutyOld: byte;
      ArrValues: array[0..2] of dword;
      frecv: dword;
      fVal: real;
      txt: string[10];
      txtflt: string[23];
    
      TMR0_pin_dir : sbit at TRISB.5;
      RST_pin : sbit at LATB.4;
      RST_pin_dir : sbit at TRISB.4;
    
      // LCD display
      LCD_RS: sbit at RC2_bit;
      LCD_EN: sbit at RC3_bit;
      LCD_D4: sbit at RC4_bit;
      LCD_D5: sbit at RC5_bit;
      LCD_D6: sbit at RC6_bit;
      LCD_D7: sbit at RC7_bit;
      LCD_RS_Direction: sbit at TRISC2_bit;
      LCD_EN_Direction: sbit at TRISC3_bit;
      LCD_D7_Direction: sbit at TRISC7_bit;
      LCD_D6_Direction: sbit at TRISC6_bit;
      LCD_D5_Direction: sbit at TRISC5_bit;
      LCD_D4_Direction: sbit at TRISC4_bit;
      //s_LCD_Line1, s_LCD_Line2: string[16]
    
    procedure Main_Init;
    begin
      CMCON := %111;
      TRISA := %11111111;
      TRISB := %0;
      TRISC := %0;
    
      RCON.IPEN := 0;        // Disable Priority Levels on interrupts
      ADCON0.ADON := 1;
      LATA := 0;
      LATB := 0;
      LATC := 0;
    
      // int 8 MHz osc without 4 x PLL
      OSCCON.IRCF2 := 1;
      OSCCON.IRCF1 := 1;
      OSCCON.IRCF0 := 1;
      OSCTUNE.PLLEN := 0;  // PLL off
      
      // PWM init
      CCP2CON := CCP2CON or %00001111;
      PWM2_Init(cFreq_00);  // Freq = 10 kHz
      PWM2_Set_Duty(255);
      PWM2_Start;
      PwmFreqNew := cFreq_00;
      PwmFreqOld := cFreq_00;
      PwmDutyNew := 0;
      PwmDutyOld := 0;
    
    end;
    
    procedure PWM_GetFreq;
    begin
      // freq is initially set to 10 kHz
      iAdcPWMFreq := ADC_Read(1);
      PwmFreqNew := iAdcPWMFreq;
      if PwmFreqNew <> PwmFreqOld then
      begin
        PWM1_Stop;
        PWM2_Stop;
        if  ((PwmFreqNew > 0) and (PwmFreqNew < 50)) then
        begin
          PWM1_Init(cFreq_00);
          PWM2_Init(cFreq_00);
        end
        else if ((PwmFreqNew > 50) and (PwmFreqNew < 100)) then
        begin
          PWM1_Init(cFreq_01);
          PWM2_Init(cFreq_01);
        end
        else if ((PwmFreqNew >= 100) and (PwmFreqNew < 150)) then
        begin
          PWM1_Init(cFreq_02);
          PWM2_Init(cFreq_02);
        end
        else if ((PwmFreqNew >= 150) and (PwmFreqNew < 200)) then
        begin
          PWM1_Init(cFreq_03);
          PWM2_Init(cFreq_03);
        end
        else if ((PwmFreqNew >= 200) and (PwmFreqNew < 250)) then
        begin
          PWM1_Init(cFreq_04);
          PWM2_Init(cFreq_04);
        end
        else if ((PwmFreqNew >= 250) and (PwmFreqNew < 300)) then
        begin
          PWM1_Init(cFreq_05);
          PWM2_Init(cFreq_05);
        end
        else if ((PwmFreqNew >= 300) and (PwmFreqNew < 350)) then
        begin
          PWM1_Init(cFreq_06);
          PWM2_Init(cFreq_06);
        end
        else if ((PwmFreqNew >= 350) and (PwmFreqNew < 400)) then
        begin
          PWM1_Init(cFreq_07);
          PWM2_Init(cFreq_07);
        end
        else if ((PwmFreqNew >= 400) and (PwmFreqNew < 450)) then
        begin
          PWM1_Init(cFreq_08);
          PWM2_Init(cFreq_08);
        end
        else if ((PwmFreqNew > 450) and (PwmFreqNew < 500)) then
        begin
          PWM1_Init(cFreq_09);
          PWM2_Init(cFreq_09);
        end
        else if ((PwmFreqNew >= 500) and (PwmFreqNew < 550)) then
        begin
          PWM1_Init(cFreq_10);
          PWM2_Init(cFreq_10);
        end
        else if ((PwmFreqNew >= 550) and (PwmFreqNew < 600)) then
        begin
          PWM1_Init(cFreq_11);
          PWM2_Init(cFreq_11);
        end
        else if ((PwmFreqNew >= 600) and (PwmFreqNew < 650)) then
        begin
          PWM1_Init(cFreq_12);
          PWM2_Init(cFreq_12);
        end
        else if ((PwmFreqNew >= 650) and (PwmFreqNew < 700)) then
        begin
          PWM1_Init(cFreq_13);
          PWM2_Init(cFreq_13);
        end
        else if ((PwmFreqNew >= 700) and (PwmFreqNew < 750)) then
        begin
          PWM1_Init(cFreq_14);
          PWM2_Init(cFreq_14);
        end
        else if ((PwmFreqNew >= 750) and (PwmFreqNew < 800)) then
        begin
          PWM1_Init(cFreq_15);
          PWM2_Init(cFreq_15);
        end
        else if ((PwmFreqNew >= 800) and (PwmFreqNew < 850)) then
        begin
          PWM1_Init(cFreq_16);
          PWM2_Init(cFreq_16);
        end
        else if ((PwmFreqNew >= 850) and (PwmFreqNew < 900)) then
        begin
          PWM1_Init(cFreq_17);
          PWM2_Init(cFreq_17);
        end
        else if ((PwmFreqNew >= 900) and (PwmFreqNew < 950)) then
        begin
          PWM1_Init(cFreq_18);
          PWM2_Init(cFreq_18);
        end
        else if ((PwmFreqNew >= 950) and (PwmFreqNew < 1000)) then
        begin
          PWM1_Init(cFreq_19);
          PWM2_Init(cFreq_19);
        end
        else if ((PwmFreqNew >= 1000) and (PwmFreqNew < 1024)) then
        begin
          PWM1_Init(cFreq_20);
          PWM2_Init(cFreq_20);
        end;
    
        PWM2_Set_Duty(255 - PwmDutyNew);
    
        PWM2_Start;
        PwmFreqOld := PwmFreqNew;
      end;
    end;
    
    procedure PWM_GetDuty;
    begin
      iAdcPWMDuty := ADC_Read(0);
      if iAdcPWMDuty > 0 then
        //PwmDutyNew := iAdcPWMDuty shr 2
        PwmDutyNew := iAdcPWMDuty / 4
      else
        PwmDutyNew := 0;
      if PwmDutyNew <> PwmDutyOld then
      begin
    
        PWM2_Set_Duty(255 - PwmDutyNew);
        PwmDutyOld := PwmDutyNew;
      end;
    end;
    
    begin
      Main_Init;
          {
      Pin must be read as input with PORTx.
      Pin can be written as output with PORTx or with LATx.
      }
      Lcd_Init ;
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1, 1, 'Init');
      Delay_ms(2000);
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1, 1, ' Frequence (Hz) ');
      Lcd_Out(2, 1, ' ---');
      delay_ms(4000);
      
      while true do
      begin
        if bShowUnit = 0 then
        begin
          LongWordToStr(PwmDutyNew, txt);
          Lcd_Out(1, 1, txt);
          LongWordToStr(PwmFreqNew, txt);
          Lcd_Out(2, 1, txt);
          Delay_ms(200);
        end;
        // change PWM freq value ?
        PWM_GetFreq;
        // change PWM duty value ?
        PWM_GetDuty;
        delay_ms(100);
      end;
    end.

    -----
    Images attachées Images attachées  
    Dernière modification par gienas ; 21/12/2012 à 22h34. Motif: Ajouté la balise code, obligatoire pour les programmes

  2. #2
    invite1fc93821

    Re : aide Pic 18F2520 et LCD 2*16

    J'ai fini par trouver le souci, Rc2/ccp1 est déjà utilisé par la routine pwm, donc j'ai simplement changer de broche (RB7) pour RS et c'est parfait !

    encore une petite chose :

    Code:
    const
    
      cFreq_02 = 1000;
      cFreq_03 = 2000;
      cFreq_04 = 3000;
      cFreq_05 = 4000;
      cFreq_06 = 5000;
      cFreq_07 = 7500;
      cFreq_08 = 10000;
      cFreq_09 = 15000;
      cFreq_10 = 20000;
      cFreq_11 = 25000;
      cFreq_12 = 30000;
      cFreq_13 = 35000;
      cFreq_14 = 40000;
      cFreq_15 = 45000;
      cFreq_16 = 50000;
      cFreq_17 = 55000;
      cFreq_18 = 60000;
      cFreq_19 = 65000;
      cFreq_20 = 70000;
    var
    
      iAdcPWMFreq, iAdcPWMDuty: word;
      PwmFreqNew, PwmFreqOld: word;
      PwmDutyNew, PwmDutyOld: byte;
      ArrValues: array[0..2] of dword;
      fVal: real;
      txt: string[10];
      txtflt: string[23];
    
      TMR0_pin_dir : sbit at TRISB.5;
      RST_pin : sbit at LATB.4;
      RST_pin_dir : sbit at TRISB.4;
    
      // LCD display
      LCD_RS: sbit at RB7_bit;
      LCD_EN: sbit at RC3_bit;
      LCD_D4: sbit at RC4_bit;
      LCD_D5: sbit at RC5_bit;
      LCD_D6: sbit at RC6_bit;
      LCD_D7: sbit at RC7_bit;
      LCD_RS_Direction: sbit at TRISB7_bit;
      LCD_EN_Direction: sbit at TRISC3_bit;
      LCD_D7_Direction: sbit at TRISC7_bit;
      LCD_D6_Direction: sbit at TRISC6_bit;
      LCD_D5_Direction: sbit at TRISC5_bit;
      LCD_D4_Direction: sbit at TRISC4_bit;
      //s_LCD_Line1, s_LCD_Line2: string[16]
    
    procedure Main_Init;
    begin
      CMCON := %111;
      TRISA := %11111111;
      TRISB := %0;
      TRISC := %0;
    
      RCON.IPEN := 0;        // Disable Priority Levels on interrupts
      ADCON0.ADON := 1;
      LATA := 0;
      LATB := 0;
      LATC := 0;
    
      // int 8 MHz osc without 4 x PLL
      OSCCON.IRCF2 := 1;
      OSCCON.IRCF1 := 1;
      OSCCON.IRCF0 := 1;
      OSCTUNE.PLLEN := 0;  // PLL off
      
      // PWM init
      CCP2CON := CCP2CON or %00001111;
      PWM2_Init(cFreq_00);  // Freq = 10 kHz
      PWM2_Set_Duty(255);
      PWM2_Start;
      PwmFreqNew := cFreq_00;
      PwmFreqOld := cFreq_00;
      PwmDutyNew := 0;
      PwmDutyOld := 0;
    
    end;
    
    procedure PWM_GetFreq;
    begin
      // freq is initially set to 10 kHz
      iAdcPWMFreq := ADC_Read(1);
      PwmFreqNew := iAdcPWMFreq;
      if PwmFreqNew <> PwmFreqOld then
      begin
        PWM1_Stop;
        PWM2_Stop;
        if  ((PwmFreqNew > 0) and (PwmFreqNew < 50)) then
        begin
          PWM1_Init(cFreq_00);
          PWM2_Init(cFreq_00);
        end
        else if ((PwmFreqNew > 50) and (PwmFreqNew < 100)) then
        begin
          PWM1_Init(cFreq_01);
          PWM2_Init(cFreq_01);
        end
        else if ((PwmFreqNew >= 100) and (PwmFreqNew < 150)) then
        begin
          PWM1_Init(cFreq_02);
          PWM2_Init(cFreq_02);
        end
        else if ((PwmFreqNew >= 150) and (PwmFreqNew < 200)) then
        begin
          PWM1_Init(cFreq_03);
          PWM2_Init(cFreq_03);
        end
        else if ((PwmFreqNew >= 200) and (PwmFreqNew < 250)) then
        begin
          PWM1_Init(cFreq_04);
          PWM2_Init(cFreq_04);
        end
        else if ((PwmFreqNew >= 250) and (PwmFreqNew < 300)) then
        begin
          PWM1_Init(cFreq_05);
          PWM2_Init(cFreq_05);
        end
        else if ((PwmFreqNew >= 300) and (PwmFreqNew < 350)) then
        begin
          PWM1_Init(cFreq_06);
          PWM2_Init(cFreq_06);
        end
        else if ((PwmFreqNew >= 350) and (PwmFreqNew < 400)) then
        begin
          PWM1_Init(cFreq_07);
          PWM2_Init(cFreq_07);
        end
        else if ((PwmFreqNew >= 400) and (PwmFreqNew < 450)) then
        begin
          PWM1_Init(cFreq_08);
          PWM2_Init(cFreq_08);
        end
        else if ((PwmFreqNew > 450) and (PwmFreqNew < 500)) then
        begin
          PWM1_Init(cFreq_09);
          PWM2_Init(cFreq_09);
        end
        else if ((PwmFreqNew >= 500) and (PwmFreqNew < 550)) then
        begin
          PWM1_Init(cFreq_10);
          PWM2_Init(cFreq_10);
        end
        else if ((PwmFreqNew >= 550) and (PwmFreqNew < 600)) then
        begin
          PWM1_Init(cFreq_11);
          PWM2_Init(cFreq_11);
        end
        else if ((PwmFreqNew >= 600) and (PwmFreqNew < 650)) then
        begin
          PWM1_Init(cFreq_12);
          PWM2_Init(cFreq_12);
        end
        else if ((PwmFreqNew >= 650) and (PwmFreqNew < 700)) then
        begin
          PWM1_Init(cFreq_13);
          PWM2_Init(cFreq_13);
        end
        else if ((PwmFreqNew >= 700) and (PwmFreqNew < 750)) then
        begin
          PWM1_Init(cFreq_14);
          PWM2_Init(cFreq_14);
        end
        else if ((PwmFreqNew >= 750) and (PwmFreqNew < 800)) then
        begin
          PWM1_Init(cFreq_15);
          PWM2_Init(cFreq_15);
        end
        else if ((PwmFreqNew >= 800) and (PwmFreqNew < 850)) then
        begin
          PWM1_Init(cFreq_16);
          PWM2_Init(cFreq_16);
        end
        else if ((PwmFreqNew >= 850) and (PwmFreqNew < 900)) then
        begin
          PWM1_Init(cFreq_17);
          PWM2_Init(cFreq_17);
        end
        else if ((PwmFreqNew >= 900) and (PwmFreqNew < 950)) then
        begin
          PWM1_Init(cFreq_18);
          PWM2_Init(cFreq_18);
        end
        else if ((PwmFreqNew >= 950) and (PwmFreqNew < 1000)) then
        begin
          PWM1_Init(cFreq_19);
          PWM2_Init(cFreq_19);
        end
        else if ((PwmFreqNew >= 1000) and (PwmFreqNew < 1024)) then
        begin
          PWM1_Init(cFreq_20);
          PWM2_Init(cFreq_20);
        end;
    
        PWM2_Set_Duty(PwmDutyNew);
    
        PWM2_Start;
        PwmFreqOld := PwmFreqNew;
      end;
    end;
    
    procedure PWM_GetDuty;
    begin
      iAdcPWMDuty := ADC_Read(0);
      if iAdcPWMDuty > 0 then
        //PwmDutyNew := iAdcPWMDuty shr 2
        PwmDutyNew := iAdcPWMDuty / 4
      else
        PwmDutyNew := 0;
      if PwmDutyNew <> PwmDutyOld then
      begin
    
        PWM2_Set_Duty(PwmDutyNew);
        PwmDutyOld := PwmDutyNew;
      end;
    end;
    
    begin
      Main_Init;
          {
      Pin must be read as input with PORTx.
      Pin can be written as output with PORTx or with LATx.
      }
      Lcd_Init ;
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1, 1, 'Init');
      Delay_ms(2000);
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1, 1, ' Frequence (Hz) ');
      Lcd_Out(2, 1, ' DutyCycle  (%) ');
      delay_ms(4000);
      
      while true do
      begin
        if bShowUnit = 0 then
        begin
          LongWordToStr(PwmFreqNew , txt);
          Lcd_Out(1, 1, txt);
          LongWordToStr(PwmdutyNew / (256 / 100), txt);
          Lcd_Out(2, 1, txt);
          Delay_ms(200);
        end;
        // change PWM freq value ?
        PWM_GetFreq;
        // change PWM duty value ?
        PWM_GetDuty;
        delay_ms(100);
      end;
    end.

    Le rapport cylique s'affiche en pourcentage la tout va bien
    Mais la frequence ( en rouge ) affiche une valeur de 0 à 1023, normal se sont les valeur numérisé par le can
    J'aurai voulu afficher a la place la frequence decrit dans les constantes ( en bleu ).
    Mais je n'arrive pas trouvé la methode pour cela!

    un ptit coup de pouce svp ?

    Merci

Discussions similaires

  1. Aide Code en C Pic 16F88 - LCD HD44780
    Par invitec1a2f24b dans le forum Électronique
    Réponses: 6
    Dernier message: 26/10/2010, 22h08
  2. PIC 18F2520 ne démarre QUE avec le debugger
    Par invited67d736f dans le forum Électronique
    Réponses: 9
    Dernier message: 30/04/2010, 19h53
  3. Aide pour LCD graphique + PIC 16F886
    Par invite1817c4c0 dans le forum Électronique
    Réponses: 7
    Dernier message: 27/01/2010, 21h52
  4. PIC 16F8xx Aide....Menus LCD
    Par invite0090e9b8 dans le forum Électronique
    Réponses: 3
    Dernier message: 06/07/2008, 11h58
  5. Demande aide schéma électronique (Pic 16F84A et LCD) ???
    Par invitee0c1f250 dans le forum Électronique
    Réponses: 2
    Dernier message: 03/01/2006, 21h18
Dans la rubrique Tech de Futura, découvrez nos comparatifs produits sur l'informatique et les technologies : imprimantes laser couleur, casques audio, chaises gamer...