'Call interrupt on the PORTB.0
On Interrupt ExtInt0 Call Interrupt_RB0
DIM Flag_apagado,Address AS INTEGER
'interrupt on PORTB.0 which manages the start button
SUB Interrupt_RB0()
IntOFF
DO
LOOP UNTIL PORTB.0=1
WAIT 200 ms
IF Flag_apagado=0 THEN
Flag_apagado=1
ELSE
Flag_apagado=0
END IF
IntON
END SUB
'Sub interface that the pic with the HC-SR04
SUB Sonar()
DIM Centimetros AS WORD
Centimetros=0
'I put down for 2us port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
WAIT 2 us
'I put up for 10us port that sends the pulse to trigger the sensor
SET PORTD.7 ON
WAIT 10 us
'I put down port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
'Wait until the door entrance echo is not high
WAIT UNTIL PORTD.6 ON
'Wait until the door of the echo is not low
DO WHILE PORTD.6=1
Centimetros+=1
IF Centimetros=0 THEN EXIT DO
LOOP
'The value of the variable accumulation sending internal EEPROM
EPWrite Address,Centimetros/256
Address+=1
EPWrite Address,Centimetros%256
Address+=1
END SUB
Main:
TRISA=b'00000'
TRISB=b'00000001'
TRISC=b'00000000'
TRISD=b'01000000'
TRISE=b'00000000'
PORTA=b'000000'
PORTB=b'00000000'
PORTC=b'00000000'
PORTD=b'00000000'
PORTE=b'00000000'
INTCON=b'10010000'
OPTION_REG=b'11000000'
CVRCON=b'00000000'
CCP1CON=b'00000000'
CCP2CON=b'00000000'
ADCON0=b'00000000'
ADCON1=b'10000110'
Address=0
Flag_apagado=0
WAIT 250 ms
SET PORTC.0 OFF
'If you do not press the start button does not go forward
Inicio:
SET PORTD.3 OFF
IF Flag_apagado=0 THEN GOTO Inicio
'Button pressed do a measurement cycle.
'each switch and indicator light to allow me to movefrom
'10 cm each time
FOR Ciclo=1 TO 7
SET PORTD.3 ON
Sonar()
WAIT 3 s
SET PORTD.3 OFF
WAIT 2 s
NEXT Ciclo
GOTO Inicio
As the title,if i run this code from these values in EEPROM.
I am no expert on the device but some changes to get you thinking.
a) I do not use the direct setting of the ports etc. I let GCB do this for me. Hence, I commented out all the assignments. As least with these commented out I could get the simulator to run.
b) I always set the port directions with DIR. I made this change. You should always do this.
c) and...Centimetros is counting clock cycles, and you have the interrupt still running. This may be your issue. See the help file for TIMER1 for a better way to count things but you need to certain that the event does not exceed the WORD.
d) and, see https://sourceforge.net/p/gcbasic/discussion/579126/thread/b5acc382#4e7d this may be faster to write your variables (using _H) to EEPROM.
e) The routine
WAITUNTILPORTD.6ON'Wait until the door of the echo is not low DO WHILE PORTD.6=1 Centimetros+=1 IF Centimetros=0 THEN EXIT DO LOOP
f) In the simulator I get values being posted to the EEPROM however, these values are meaningless as the issue at point d (see above) needs to be resolved.
g) I change the use of the ADDRESS variable. I have started using ug_ in front of all my variables to ensure I do not clash with reserved variables. ug = User Global. I also use ugl_ and ugw for LONG and WORD respectively.
'Call interrupt on the PORTB.0
On Interrupt ExtInt0 Call Interrupt_RB0
DIM ug_ug_Address AS word
Dim Flag_apagado as Bit
dir PORTD.7 Out
dir PORTD.0 Out
dir PORTC.0 OUT
DIR PORTD.3 OUT
DIR PORTD.6 IN
'interrupt on PORTB.0 which manages the start button
SUB Interrupt_RB0()
IntOFF
set portd.0 on
DO
LOOP UNTIL PORTB.0=1
WAIT 200 ms
IF Flag_apagado=0 THEN
Flag_apagado=1
ELSE
Flag_apagado=0
END IF
Set portd.0 off
IntON
END SUB
'Sub interface that the pic with the HC-SR04
SUB Sonar()
DIM Centimetros AS WORD
Centimetros=0
'I put down for 2us port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
WAIT 2 us
'I put up for 10us port that sends the pulse to trigger the sensor
SET PORTD.7 ON
WAIT 10 us
'I put down port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
'Wait until the door entrance echo is not high
WAIT UNTIL PORTD.6 ON
'Wait until the door of the echo is not low
DO WHILE PORTD.6=1
Centimetros+=1
IF Centimetros=0 THEN EXIT DO
LOOP
'The value of the variable accumulation sending internal EEPROM
EPWrite ug_Address,Centimetros/256
ug_Address+=1
EPWrite ug_Address,Centimetros%256
ug_Address+=1
END SUB
'If you do not press the start button does not go forward
Inicio:
SET PORTD.3 OFF
IF Flag_apagado=0 THEN GOTO Inicio
'Button pressed do a measurement cycle.
'each switch and indicator light to allow me to movefrom
'10 cm each time
FOR Ciclo=1 TO 7
SET PORTD.3 ON
Sonar()
WAIT 3 s
SET PORTD.3 OFF
WAIT 2 s
NEXT Ciclo
GOTO Inicio
~~~~~~~~~~~
Last edit: Anobium 2013-08-20
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Enough.
better to use the TMR1.
In fact, I can now detect the echo of the detector as datasheet.
These sensors Chinese work very well (even if they have an accuracy of + - 2 cm).
Code:
On Interrupt ExtInt0 Call Interrupt_RB0
On Interrupt Timer1Overflow Call Interrupt_TMR1
DIM Flag_apagado,Address AS INTEGER
SUB Interrupt_RB0()
IntOFF
DO
LOOP UNTIL PORTB.0=1
WAIT 200 ms
IF Flag_apagado=0 THEN
Flag_apagado=1
ELSE
Flag_apagado=0
END IF
IntON
END SUB
SUB Interrupt_TMR1()
IF PIR1.TMR1IF=1 THEN Out_Range=1:PIR1.TMR1IF=0
END SUB
FUNCTION Sonar() AS WORD
DIM Distancia AS WORD
T1CON.TMR1ON=0
PIE1.TMR1IE=0
PIR1.TMR1IF=0
Out_Range=0
Distancia=0
TMR1H=0xBC
TMR1L=0x07
SET PORTD.7 OFF
WAIT 2 us
PULSEOUT PORTD.7,1 10us
WAIT UNTIL PORTD.6 ON
T1CON.TMR1ON=1
PIE1.TMR1IE=1
DO
'''''''''''''''''''''
LOOP UNTIL PORTD.6=0|Out_Range=1
T1CON.TMR1ON=0
PIE1.TMR1IE=0
PIR1.TMR1IF=0
Distancia_H=TMR1H
Distancia+=TMR1L
Distancia-=0xBC07
Sonar=Distancia/58
END FUNCTION
Main:
TRISA=b'00000'
TRISB=b'00000001'
TRISC=b'00000000'
TRISD=b'01000000'
TRISE=b'00000000'
PORTA=b'000000'
PORTB=b'00000000'
PORTC=b'00000000'
PORTD=b'00000000'
PORTE=b'00000000'
INTCON=b'11010000'
OPTION_REG=b'11000000'
T1CON=b'00000000'
PIE1.TMR1IE=0
PIR1.TMR1IF=0
CVRCON=b'00000000'
CCP1CON=b'00000000'
CCP2CON=b'00000000'
ADCON0=b'00000000'
ADCON1=b'10000110'
Address=0
Flag_apagado=0
WAIT 250 ms
SET PORTC.0 OFF
Inicio:
SET PORTD.3 OFF
SET PORTD.1 OFF
IF Flag_apagado=0 THEN GOTO Inicio
DO
IF Sonar()<=25 THEN
SET PORTD.1 ON
SET PORTD.3 OFF
ELSE
SET PORTD.1 OFF
SET PORTD.3 ON
END IF
WAIT 200 ms
LOOP UNTIL Flag_apagado=0
GOTO Inicio
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If this has resolved the issue, may I ask that you edit the first posted and edit the title and add RESOLVED: and in the body of the first posting put the URL (link) to the entry that shows the resolution. Most grateful if you can do this. This will ensure in the future those who look for an answer can see the answer very quickly.
:-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You are correct! Edit the first posting to edit add RESOLVED and the link to you closure record. Use the 'link' of the posting you want to point people at when you add the link.
Hello again Evan and once again I'm on the scrounge (again!) for some help with one of these HC-SR04 Ultrasonic Modules but there is a twist in the tail so to speak.
Firstly, @alejandro195, did you publish a schematic? That would be a fantastic help with my project. Did the code you have published above work?
Then what I am looking for is that the device gives an output on detection of a preset range. i.e. the range is set at 4 metres, and a port goes high or low when that distance is achieved.
It sounds straightforward when it is written like this, but I don’t really know where to start.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
RESOLVED: See https://sourceforge.net/p/gcbasic/discussion/579126/thread/d1e0c5c7/#0c0b
CHIP 16F877A,4
CONFIG OSC=XT,WDT_OFF,PWRTE_ON,CP_OFF,DEBUG_OFF,WRT_OFF,CPD_OFF,LVP_OFF,BODEN_ON
'Call interrupt on the PORTB.0
On Interrupt ExtInt0 Call Interrupt_RB0
DIM Flag_apagado,Address AS INTEGER
'interrupt on PORTB.0 which manages the start button
SUB Interrupt_RB0()
IntOFF
DO
LOOP UNTIL PORTB.0=1
WAIT 200 ms
IF Flag_apagado=0 THEN
Flag_apagado=1
ELSE
Flag_apagado=0
END IF
IntON
END SUB
'Sub interface that the pic with the HC-SR04
SUB Sonar()
DIM Centimetros AS WORD
Centimetros=0
'I put down for 2us port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
WAIT 2 us
'I put up for 10us port that sends the pulse to trigger the sensor
SET PORTD.7 ON
WAIT 10 us
'I put down port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
'Wait until the door entrance echo is not high
WAIT UNTIL PORTD.6 ON
'Wait until the door of the echo is not low
DO WHILE PORTD.6=1
Centimetros+=1
IF Centimetros=0 THEN EXIT DO
LOOP
'The value of the variable accumulation sending internal EEPROM
EPWrite Address,Centimetros/256
Address+=1
EPWrite Address,Centimetros%256
Address+=1
END SUB
Main:
TRISA=b'00000'
TRISB=b'00000001'
TRISC=b'00000000'
TRISD=b'01000000'
TRISE=b'00000000'
PORTA=b'000000'
PORTB=b'00000000'
PORTC=b'00000000'
PORTD=b'00000000'
PORTE=b'00000000'
INTCON=b'10010000'
OPTION_REG=b'11000000'
CVRCON=b'00000000'
CCP1CON=b'00000000'
CCP2CON=b'00000000'
ADCON0=b'00000000'
ADCON1=b'10000110'
Address=0
Flag_apagado=0
WAIT 250 ms
SET PORTC.0 OFF
'If you do not press the start button does not go forward
Inicio:
SET PORTD.3 OFF
IF Flag_apagado=0 THEN GOTO Inicio
'Button pressed do a measurement cycle.
'each switch and indicator light to allow me to movefrom
'10 cm each time
FOR Ciclo=1 TO 7
SET PORTD.3 ON
Sonar()
WAIT 3 s
SET PORTD.3 OFF
WAIT 2 s
NEXT Ciclo
GOTO Inicio
As the title,if i run this code from these values in EEPROM.
mm.........EEPROM......Ciclos de loop
100..........0x16............22
200..........0x2B............43
300..........0x41............65
400..........0x57............87
500..........0x6F............111
600..........0x83............131
700..........0x99............153
800..........0xAF............175
operate works,but not expected values.any device for me??
the pic is 16F877A to 4mhz.
Last edit: alejandro1957 2013-08-22
So I/we can help. Please document your code. It is tough to understand your code cold in the posting.
Do you have time to update to include documentation?
And, are you porting some code from elsewhere? If so, post this code also.
Last edit: Anobium 2013-08-19
I commented the code with edit
I am no expert on the device but some changes to get you thinking.
a) I do not use the direct setting of the ports etc. I let GCB do this for me. Hence, I commented out all the assignments. As least with these commented out I could get the simulator to run.
b) I always set the port directions with DIR. I made this change. You should always do this.
c) and...Centimetros is counting clock cycles, and you have the interrupt still running. This may be your issue. See the help file for TIMER1 for a better way to count things but you need to certain that the event does not exceed the WORD.
d) and, see https://sourceforge.net/p/gcbasic/discussion/579126/thread/b5acc382#4e7d this may be faster to write your variables (using _H) to EEPROM.
e) The routine
f) In the simulator I get values being posted to the EEPROM however, these values are meaningless as the issue at point d (see above) needs to be resolved.
g) I change the use of the ADDRESS variable. I have started using ug_ in front of all my variables to ensure I do not clash with reserved variables. ug = User Global. I also use ugl_ and ugw for LONG and WORD respectively.
~~~~~~~~~~~~~~
CHIP 16F877A,4
CONFIG OSC=XT,WDT_OFF,PWRTE_ON,CP_OFF,DEBUG_OFF,WRT_OFF,CPD_OFF,LVP_OFF,BODEN_ON
'Call interrupt on the PORTB.0
On Interrupt ExtInt0 Call Interrupt_RB0
DIM ug_ug_Address AS word
Dim Flag_apagado as Bit
dir PORTD.7 Out
dir PORTD.0 Out
dir PORTC.0 OUT
DIR PORTD.3 OUT
DIR PORTD.6 IN
'interrupt on PORTB.0 which manages the start button
SUB Interrupt_RB0()
IntOFF
set portd.0 on
DO
LOOP UNTIL PORTB.0=1
WAIT 200 ms
IF Flag_apagado=0 THEN
Flag_apagado=1
ELSE
Flag_apagado=0
END IF
Set portd.0 off
IntON
END SUB
'Sub interface that the pic with the HC-SR04
SUB Sonar()
DIM Centimetros AS WORD
Centimetros=0
'I put down for 2us port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
WAIT 2 us
'I put up for 10us port that sends the pulse to trigger the sensor
SET PORTD.7 ON
WAIT 10 us
'I put down port that sends the pulse to trigger the sensor
SET PORTD.7 OFF
'Wait until the door entrance echo is not high
WAIT UNTIL PORTD.6 ON
'Wait until the door of the echo is not low
DO WHILE PORTD.6=1
Centimetros+=1
IF Centimetros=0 THEN EXIT DO
LOOP
'The value of the variable accumulation sending internal EEPROM
EPWrite ug_Address,Centimetros/256
ug_Address+=1
EPWrite ug_Address,Centimetros%256
ug_Address+=1
END SUB
Main:
' TRISA=b'00000'
' TRISB=b'00000001'
' TRISC=b'00000000'
' TRISD=b'01000000'
' TRISE=b'00000000'
' PORTA=b'000000'
' PORTB=b'00000000'
' PORTC=b'00000000'
' PORTD=b'00000000'
' PORTE=b'00000000'
' INTCON=b'10010000'
' OPTION_REG=b'11000000'
' CVRCON=b'00000000'
' CCP1CON=b'00000000'
' CCP2CON=b'00000000'
' ADCON0=b'00000000'
' ADCON1=b'10000110'
ug_Address=0
Flag_apagado=0
WAIT 250 ms
SET PORTC.0 OFF
'If you do not press the start button does not go forward
Inicio:
SET PORTD.3 OFF
IF Flag_apagado=0 THEN GOTO Inicio
'Button pressed do a measurement cycle.
'each switch and indicator light to allow me to movefrom
'10 cm each time
FOR Ciclo=1 TO 7
SET PORTD.3 ON
Sonar()
WAIT 3 s
SET PORTD.3 OFF
WAIT 2 s
NEXT Ciclo
GOTO Inicio
~~~~~~~~~~~
Last edit: Anobium 2013-08-20
Enough.
better to use the TMR1.
In fact, I can now detect the echo of the detector as datasheet.
These sensors Chinese work very well (even if they have an accuracy of + - 2 cm).
Code:
CHIP 16F877A,4
CONFIG OSC=XT,WDT_OFF,PWRTE_ON,CP_OFF,DEBUG_OFF,WRT_OFF,CPD_OFF,LVP_OFF,BODEN_ON
On Interrupt ExtInt0 Call Interrupt_RB0
On Interrupt Timer1Overflow Call Interrupt_TMR1
DIM Flag_apagado,Address AS INTEGER
SUB Interrupt_RB0()
IntOFF
DO
LOOP UNTIL PORTB.0=1
WAIT 200 ms
IF Flag_apagado=0 THEN
Flag_apagado=1
ELSE
Flag_apagado=0
END IF
IntON
END SUB
SUB Interrupt_TMR1()
IF PIR1.TMR1IF=1 THEN Out_Range=1:PIR1.TMR1IF=0
END SUB
FUNCTION Sonar() AS WORD
DIM Distancia AS WORD
T1CON.TMR1ON=0
PIE1.TMR1IE=0
PIR1.TMR1IF=0
Out_Range=0
Distancia=0
TMR1H=0xBC
TMR1L=0x07
SET PORTD.7 OFF
WAIT 2 us
PULSEOUT PORTD.7,1 10us
WAIT UNTIL PORTD.6 ON
T1CON.TMR1ON=1
PIE1.TMR1IE=1
DO
'''''''''''''''''''''
LOOP UNTIL PORTD.6=0|Out_Range=1
T1CON.TMR1ON=0
PIE1.TMR1IE=0
PIR1.TMR1IF=0
Distancia_H=TMR1H
Distancia+=TMR1L
Distancia-=0xBC07
Sonar=Distancia/58
END FUNCTION
Main:
TRISA=b'00000'
TRISB=b'00000001'
TRISC=b'00000000'
TRISD=b'01000000'
TRISE=b'00000000'
PORTA=b'000000'
PORTB=b'00000000'
PORTC=b'00000000'
PORTD=b'00000000'
PORTE=b'00000000'
INTCON=b'11010000'
OPTION_REG=b'11000000'
T1CON=b'00000000'
PIE1.TMR1IE=0
PIR1.TMR1IF=0
CVRCON=b'00000000'
CCP1CON=b'00000000'
CCP2CON=b'00000000'
ADCON0=b'00000000'
ADCON1=b'10000110'
Address=0
Flag_apagado=0
WAIT 250 ms
SET PORTC.0 OFF
Inicio:
SET PORTD.3 OFF
SET PORTD.1 OFF
IF Flag_apagado=0 THEN GOTO Inicio
DO
IF Sonar()<=25 THEN
SET PORTD.1 ON
SET PORTD.3 OFF
ELSE
SET PORTD.1 OFF
SET PORTD.3 ON
END IF
WAIT 200 ms
LOOP UNTIL Flag_apagado=0
GOTO Inicio
If this has resolved the issue, may I ask that you edit the first posted and edit the title and add RESOLVED: and in the body of the first posting put the URL (link) to the entry that shows the resolution. Most grateful if you can do this. This will ensure in the future those who look for an answer can see the answer very quickly.
:-)
Thanks Anobium,I did not understand out how to edit the title of the thread and link it as
Last edit: alejandro1957 2013-08-21
You are correct! Edit the first posting to edit add RESOLVED and the link to you closure record. Use the 'link' of the posting you want to point people at when you add the link.
See my example here https://sourceforge.net/p/gcbasic/discussion/579125/thread/41bb58f4/#077d
thanks to all, especially to Anobium
Hello again Evan and once again I'm on the scrounge (again!) for some help with one of these HC-SR04 Ultrasonic Modules but there is a twist in the tail so to speak.
Firstly, @alejandro195, did you publish a schematic? That would be a fantastic help with my project. Did the code you have published above work?
Then what I am looking for is that the device gives an output on detection of a preset range. i.e. the range is set at 4 metres, and a port goes high or low when that distance is achieved.
It sounds straightforward when it is written like this, but I don’t really know where to start.
@Keith. Others may be better to help as i do not have one of those module. Sorry.
@All. Wade in!