Maker Pro
Maker Pro

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

grkmr

Jan 31, 2011
13
Joined
Jan 31, 2011
Messages
13
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
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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.
 

grkmr

Jan 31, 2011
13
Joined
Jan 31, 2011
Messages
13
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?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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 :(
 

Digital_Angel_316

Oct 1, 2011
41
Joined
Oct 1, 2011
Messages
41
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?

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
 
Top