Advertisement
Electgpl

PIC - Medición TrueRMS

Jun 23rd, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <16F883.h>
  2. #device adc=10
  3. #use delay(int=4000000)
  4. #define LCD_ENABLE_PIN  PIN_B2
  5. #define LCD_RS_PIN      PIN_B0
  6. #define LCD_RW_PIN      PIN_B1
  7. #define LCD_DATA4       PIN_B4
  8. #define LCD_DATA5       PIN_B5
  9. #define LCD_DATA6       PIN_B6
  10. #define LCD_DATA7       PIN_B7
  11. #include <LCD.C>
  12. #include <math.h>
  13. void main(){
  14.    setup_adc_ports(sAN0|sAN1|VSS_VDD);
  15.    setup_adc(ADC_CLOCK_DIV_2);
  16.    int16 i, adcZ=0;
  17.    float adc1=0, valorMax1=0, adc2=0, valorMax2=0;
  18.    float tension, corriente, potencia;
  19.    lcd_init();
  20.    while(true){
  21.       do{
  22.          set_adc_channel(0);
  23.          adcZ=read_adc()-512;
  24.          delay_us(20);
  25.       }while(adcZ<10);
  26.       for(i=0;i<300;i++){
  27.          set_adc_channel(0);
  28.          adc1=(read_adc()*5.0/1023.0)-2.5;
  29.          delay_us(33);
  30.          valorMax1=adc1*adc1+valorMax1;
  31.          set_adc_channel(1);
  32.          adc2=(read_adc()*5.0/1023.0)-2.5;
  33.          delay_us(33);
  34.          valorMax2=adc2*adc2+valorMax2;
  35.       }  
  36.       tension=sqrt(valorMax1/300);
  37.       corriente=sqrt(valorMax2/300);
  38.       potencia=tension*corriente;
  39.       lcd_gotoxy(1,1);
  40.       printf(lcd_putc,"RMS     W:%1.3f ",potencia);
  41.       lcd_gotoxy(1,2);
  42.       printf(lcd_putc,"V:%1.3f A:%1.3f ",tension, corriente);
  43.       valorMax1=0;
  44.       valorMax2=0;
  45.    }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement