source code for voltage & freq detection

Discussion in 'General Electronics Chat' started by keverin, Sep 13, 2012.

  1. keverin

    Thread Starter New Member

    Sep 13, 2012
    3
    0
    hi guys

    I am new to the pic programming . I need source code example for measuring frequency and voltage . Currently , the complier i using is Mikroc .. and the pic i be using is PIC 16F887. Any one can help ??? i need the code urgently ....
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    I'll get right on that after my coffee break.

    The code for reading voltage is to have the A2D converter do a conversion and read the result.

    Measuring frequency requires reading the value of a free running timer, then reading it again on the next edge of the same type. the difference is the period and the reciprocal of the period is the frequency.

    Why don't you show us what you come up with and we'll help you refine it. Would that work for you?
     
  3. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,969
    744
    this is an A/D routine to measure temperature in binary, that i built,which is the same principle , getting a voltage on the Analogue i/p using a Pic-16F690, it uses external Vref for the A/D

    you may need to modify your registers if they are not the same>>

    Code ( (Unknown Language)):
    1.  
    2. ;  USING A LM35CZ sensor on AN2, Vref (AN1)set to 2,56Volts gives 1mVolt =0.4bits,
    3. ;   Sensor gives out 10mVolt per degree C= 4 bits per degree
    4. ; because we are using upper 8 bits, there is an automatic divide by 4, so 1 degree =1 bit
    5. ;  0 Deg to 100Deg'C= 0mV to 1V output from sensor =0 to 1024 bits.
    6.  
    7.     list        p=16f690        ; list directive to define processor
    8.     #include    <P16F690.inc>    ; processor specific variable definitions
    9.    
    10.     __CONFIG    _CP_OFF & _CPD_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF
    11.                             ; 4Mhz clock
    12.  
    13.                             ; '__CONFIG' directive is used to embed configuration data within .asm file.
    14.                             ; The labels following the directive are located in the respective .inc file.
    15.                             ; See respective data sheet for additional information on configuration word.
    16.  
    17.         errorlevel -302     ; suppress banksel warning messages during assembly
    18.         errorlevel -311     ;  suppress HIGH operator warning messages during assembly
    19.  
    20.  
    21.   org 0000
    22.     goto  Main
    23.  
    24.  
    25.  
    26. ;***** VARIABLE DEFINITIONS
    27.     cblock 0x20
    28.         RESULTLO     ; temp regs for routine
    29.         RESULTHI
    30.         Set_temp
    31.         d1
    32.         d2
    33.         d3
    34.        
    35.     endc
    36.  
    37.  
    38. Main
    39.  
    40.     ; set up registers ***************************************************
    41.    
    42.      BSF STATUS,RP0   ;Bank 1
    43.     BCF STATUS,RP1    ; set up status bits
    44.     MOVLW b'01010000' ; A/D 1/8 osc =2usec a/d time at 4mhz clock
    45.     MOVWF ADCON1      
    46.     BSF TRISA,0       ;Set RA0 to input
    47.     clrf TRISC
    48.     clrf TRISB        ; set port c output
    49.     BCF STATUS,RP0    ;Bank 2
    50.     BSF STATUS,RP1    ; setup status bits
    51.     clrf ANSEL        ;****************** portc to digital i/o
    52.     BSF ANSEL,0       ;Set RA0 to analog
    53.     clrf ANSELH       ; set port b/c to digital I/O
    54.     BCF STATUS,RP1    ;Bank 0
    55.     clrf PORTB        ; clear output ports
    56.     clrf PORTC
    57.     goto Begin
    58. ;*************************************************************************
    59.  
    60. ;**********************************************************************
    61. Temperature
    62.     MOVLW b'01001001' ;Left justified(bit 7),  Vref(bit 6 ,1=external source), AN2 source input(bit 3) temp i/p
    63.     MOVWF ADCON0        
    64.     call Delay                              
    65.     BSF ADCON0,GO   ;Start conversion
    66.     BTFSC ADCON0,GO ;Is conversion done?
    67.     GOTO $-1        ;No, test again
    68.     MOVF ADRESH,W   ;Read upper 8 bits
    69.     MOVWF RESULTHI  ; store upper 8 bits
    70.     BSF STATUS,RP0  ;Bank 1
    71.     MOVF ADRESL,W   ;Read lower 2 bits
    72.     BCF STATUS,RP0  ;Bank 0
    73.     MOVWF RESULTLO  ; save lower 2 bits
    74.     return
    75. ;*************************************************************************************    
    76. Begin
    77.     call Temperature
    78.     movfw RESULTHI
    79.     movfw PORTC   ;  display temp on leds
    80.     goto Begin
    81.  
    82. end
    83.  
     
    Last edited: Sep 13, 2012
  4. keverin

    Thread Starter New Member

    Sep 13, 2012
    3
    0
    The code as follow:

    seem not right ...

    Code ( (Unknown Language)):
    1.  
    2. // Lcd module connections
    3.  
    4. sbit LCD_RS at RB2_bit;
    5. sbit LCD_EN at RB3_bit;
    6. sbit LCD_D4 at RB4_bit;
    7. sbit LCD_D5 at RB5_bit;
    8. sbit LCD_D6 at RB6_bit;
    9. sbit LCD_D7 at RB7_bit;
    10.  
    11. sbit LCD_RS_Direction at TRISB2_bit;
    12. sbit LCD_EN_Direction at TRISB3_bit;
    13. sbit LCD_D4_Direction at TRISB4_bit;
    14. sbit LCD_D5_Direction at TRISB5_bit;
    15. sbit LCD_D6_Direction at TRISB6_bit;
    16. sbit LCD_D7_Direction at TRISB7_bit;
    17.  
    18. // End of Lcd module connections
    19.  
    20. void main() {
    21. unsigned long frequency;               // complete frequency count
    22. unsigned long freq_low;                // low word of frequency count
    23. char freq_txt[11];                     // buffer for ASCII conversion
    24. unsigned char ch;                    //
    25. unsigned int adc_rd;                 // Declare variable
    26. char *text;                          //
    27. long tlong;                          //
    28.  
    29.     ANSEL = 0x04;                    // Pin RA2 is configured as an analog input
    30.     ANSELH = 0;                      // Rest of pins are configured as digital
    31.     TRISA = 0xFF;
    32.    
    33.      /*ANSELA = 0;                       // Configure PORTA pins as digital
    34.      ANSELB = 0;                       // Configure PORTB pins as digital
    35.      ANSELC = 0;                       // Configure PORTC pins as digital
    36.      ANSELD = 0;*/                       // Configure PORTD pins as digital
    37.  
    38.      ADCON0 = 0;                       // A/D off
    39.      ADCON1 = 0x0f;                    // all digital
    40.    
    41.      CM1CON0 = 0;                    // disable 18F45K22 comparator
    42.      CM2CON0 = 0;                      // disable 18F45K22 comparator
    43.    
    44.      TRISB = 0;                        // PORTB is output
    45.      PORTB = 0x00;                     // Initialize PORTB
    46.  
    47.      //LATA = 0;                         // all off
    48.      //LATB = 0xff;                      // disable GLCD if fitted
    49.      //LATC = 0;                         // all off
    50.  
    51.      //TRISA = 0xC0;                     // all output except OSC pins
    52.      //TRISB = 0x00;                     // all output
    53.      //TRISC = 0x01;                     // all output except RC0
    54.  
    55.      Delay_ms(100);                       // Wait for LCD to stabilise
    56.      Lcd_Init();                          // Initialize Lcd
    57.      Lcd_Cmd(_LCD_CLEAR);                 // Clear display
    58.      Lcd_Cmd(_LCD_CURSOR_OFF);            // Turn cursor off
    59.      Lcd_Out(1,1, "Frequency on RA2");        // Print text to Lcd, 1st row, 1st column
    60.      Lcd_Out(2,1, "00 Hz   ");                 // Print text to Lcd, 2nd row, 1st column
    61.  
    62.  
    63.      adc_rd = ADC_Read(2);        // A/D conversion. Pin RA2 is an input.
    64.      tlong = (long)adc_rd * 5000; // Convert the result in millivolts
    65.      tlong = tlong / 1023;        // 0..1023 -> 0-5000mV
    66.      ch = tlong / 1000;             // A/D conversion. Pin RA2 is an input.
    67.      
    68.      //PWM1_Init(2000);                  // Test frequency PWM1 module
    69.      //PWM1_Start();                     // start PWM1
    70.      //PWM1_Set_Duty(127);              // Set current duty for PWM1 50%
    71.      
    72.      T2CON = 0b00001111;                     // T0 on, 16 bit, prescaler 256
    73.      T1CON = 0b11000101;
    74.      //T1CON = 0x85;                     // T1 on, ext input, no prescale
    75.     //1GCON = 0x00;                    // no gating
    76.  
    77.  
    78.    
    79.    
    80.         while(1){
    81.  
    82.          TMR0 = 0x85; //0b10000101     // set up timer 0 for 1 second time out
    83.                                        // Timer register
    84.          TMR0 = 0xee;                  // writing TMR0L also writes TMR0H
    85.          INTCON.TMR0IF = 0;            // reset time-out indicator
    86.          frequency = 0;                // initialise count
    87.          TMR1L = 0;                    // reset to 0
    88.          TMR1H = 0;                    // reset to 0
    89.          PIR1.TMR1IF = 0;              // initialise
    90.  
    91.          while(!INTCON.TMR0IF){        // wait for 1 second time-out
    92.              if(PIR1.TMR1IF){          // monitor frequency count overflow
    93.                  PIR1.TMR1IF=0;        // reset overflow
    94.                  frequency += 65536;   // increment count high bytes
    95.              }
    96.          }
    97.          
    98.          freq_low = (TMR1H << 8) + TMR1L;  // collect frequency bytes
    99.          frequency += freq_low;            // collect frequency bytes
    100.          LongWordToStrWithZeros(frequency, freq_txt);          // covert result to ASCII
    101.          Lcd_Out(2,1, freq_txt);       // Print text to Lcd, 2nd row, 1st column
    102.         // LATA=LATA^1;                  // show some life by toggling LED
    103.      }
    104. }
    105.  
     
    Last edited by a moderator: Sep 23, 2012
  5. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,969
    744
    Mine is in Assembly Language, yours must be in C?
     
  6. keverin

    Thread Starter New Member

    Sep 13, 2012
    3
    0
    hi

    yes... but the code seem wrong
    any one can help????
     
Loading...