Connect with us

What's wrong with this code? (PIC 16F528A)

Discussion in 'Microcontrollers, Programming and IoT' started by grkmr, May 21, 2011.

Scroll to continue with content
  1. grkmr

    grkmr

    13
    0
    Jan 31, 2011
    I want to compare two voltages and sound the buzzer if referance voltage is higher than the other. However, when I change the voltage values on corresponding pins, nothing changes at the buzzer pin (it always sounds or nothing happens). As I investigate some example codes on the internet I saw that I should use some commands like ADRESH or ADRESL to compare the converted values by the internal ADC of the PIC I'm using. However I dunno how to modify my code. I think there's a problem with "IF PORTA.3 > PORTA.0 THEN..." part of my code. I think I can not compare the pins directly but I should compare the converted binary values. I dunno how to do it exactly. Another problem might be activating the corresponding pins AN0 and AN1. Could you please help me? Here is my code:



    ****************************************************************
    '* Name : UNTITLED.BAS *
    '* Author : [select VIEW...EDITOR OPTIONS] *
    '* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 18.05.2011 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '****************************************************************
    @ DEVICE pic16F628a
    @ DEVICE pic16F628a, WDT_on
    @ DEVICE pic16F628a, PWRT_ON
    @ DEVICE pic16F628a, PROTECT_OFF
    @ DEVICE pic16F628a, MCLR_OFF
    @ DEVICE pic16F628a, INTRC_OSC_NOCLKOUT
    CMCON=7 ; Turn OFF the comparators, and use these pins as normal
    ; digital I/O-pins
    VRCON.7=0 ; Disable voltage reference module
    TRISA = %01111111
    CMCON = %00000010
    T1CON = %00000001 ; Enable Timer1 with a prescaler of 1:1(1microsec.)
    PIE1.0=1 ; Enable Timer1 as peripheral interrupt source
    PIR1.0=0 ; Clear Timer1 interrupt flag
    INTCON =0 ; All interrupt are disable
    BUZZER VAR PORTA.7
    MAIN:
    while 1=1
    IF PORTA.3 > PORTA.0 THEN
    HIGH BUZZER
    else
    low buzzer
    endIF
    wend
     
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,291
    2,727
    Jan 21, 2010
    You will need to use the ADC on those pins to determine a numerical value associates with the voltage on them. These values can be compared.
     
  3. grkmr

    grkmr

    13
    0
    Jan 31, 2011
    I know I should use ADC but in our PIC, we have an internal ADC. However, I am wondering how to activate this corresponding pins and how to compare by using code?
     
  4. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,291
    2,727
    Jan 21, 2010
    I am sure the documentation for your compiler will tell you how to use the ADCs.

    What is the compiler? Maybe I can google it for you :(
     
  5. Digital_Angel_316

    Digital_Angel_316

    41
    0
    Oct 1, 2011
    The code segment below is from a Microchip AppNote. The key is to set up your I/O first, then to enable and initiate the A/D function. Take a look at the code and see if it makes sense. The AppNote might be a useful read over lunch. BTW I do not see the particular part you are mentioning, is there a typo in the OP?

    APP NOTE:
    Four-Channel Digital Voltmeter with Display and Keyboard
    http://ww1.microchip.com/downloads/en/AppNotes/00557d.pdf

    AdDone
    btfsc ADCON0, GO ;ad done?
    goto AdDone ;no then loop
    bsf ADFlag, ADOver ;set a/d over flag
    call RestorePorts ;restore ports
    return

    DoAd
    clrf PORTB ;turn off leds
    bsf STATUS, RP0 ;select Bank1
    movlw 0x0f ;make port a hi-Z
    movwf TRISA ; /
    bcf STATUS, RP0 ;select Bank0
    bsf ADCON0, ADON ;start a/d
    movlw .125
    call Wait
    bsf ADCON0, GO ;start conversion
    return

    Wait
    movwf TempC ;store in temp
    Next
    decfsz TempC, F
    goto Next
    return
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-