18M2 WJEC Assembler HC-SR04

I'm trying to use a HC-SR04 range sensor with a picaxe 18m2 chip, however it uses the WJEC Assembler language and I can't seem to find any tutorials or sample code that could help me set it up to start range detection. I know the pulses from the HC-SR04 need to sent out every 10 micro seconds but the Assembler language only allows for 1ms wait. I also need help setting up a variable that would continuously check if the distance is larger than my set amount.

Any help would be greatly appreciated.
 
I will give this a try tomorrow, but I don't think I will be able to use the ultra command as it is Basic code and not Assembler.

Thank you very much.
 

AllyCat

Senior Member
Hi,
... I can't seem to find any tutorials or sample code that could help me set it up to start range detection.
I know the pulses from the HC-SR04 need to sent out every 10 micro seconds but the Assembler language only allows for 1ms wait.
Welcome to the forum. Have you found THIS PAGE which gives two further links to "Getting Started" Tutorials?

Sound travels only about 30 cms in one millisecond, so I think you need a 10 milli-second wait and that is available by using one of the pre-defined subroutines. However, I don't know how you are to measure the much shorter time delay (of the echo) because it's not a "real" assembler but "emulated", as the introduction says:

"Note that the PICAXE-18M2 chip is emulating a 16F88/assembler and so the chip does not operate at quite the same speed as a PIC programmed in raw assembler code."

I've never used that assembler, but I think that "not .. quite the speed" might be rather an under-statement. None of the examples that I found seemed to be in any way time-critical (as is required for a range measurement).

Cheers, Alan.
 

hippy

Technical Support
Staff member
I will give this a try tomorrow, but I don't think I will be able to use the ultra command as it is Basic code and not Assembler.
When using the assembler you can include Basic code so you can use the ULTRA command, but I am not sure off-hand how you would use the data which it gets.
 

Flenser

Senior Member
Hippy,

Out of curiosity I've just checked out the WJEC Assembler language for the first time and it seems that all it involves is enabling the PICAXE-18M2 (WJEC Assembler) compiler in PE, coding your app in the WJEC Assembler language and downloading the program into a standard 18M2.

Do I understand correctly?
 

Flenser

Senior Member
Hippy,

And checking out compiling the "Tutorial 1 - Flash LED on and off" there appears to be a bug in the PICAXE-18M2 (WJEC Assembler) compiler.
UPDATE: This is not a bug. AlleyCat has identified what I was doing wrong in post #8

This tutorial code from the page WJEC Assembler Code Tutorials
Code:
init:
    clrf   PORTB        ; clear PORTB output latches
    bsf    STATUS,RP0   ; memory page 1
    movlw  b'11111111'  ; set portA pins to input
    movwf  TRISA        ; write to TRIS register
    movlw  b'00000000'  ; set portB pins to output
    movwf  TRISB        ; write to TRIS register
    bcf    STATUS,RP0   ; memory page 0
main:
    bsf    PORTB,1      ; output B.1 high
    call   wait1000ms   ; delay 1000 milliseconds
    bcf    PORTB,1      ; output B.1 low
    call   wait1000ms   ; delay 1000 milliseconds
    goto   main
Generates this error message in PE v6.1.0.0
movlw b;11111111' ; set portA pins to input
^
Syntax error on line 4 at/before position 12
Error: syntax error

and the precomplier output also has the first single-quote in the binary constant replaced with a semicolon:
Code:
init:
    clrf   PORTB        ; clear PORTB output latches
    bsf    STATUS,RP0   ; memory page 1
    movlw  b;11111111'  ; set portA pins to input
    movwf  TRISA        ; write to TRIS register
    movlw  b;00000000'  ; set portB pins to output
    movwf  TRISB        ; write to TRIS register
    bcf    STATUS,RP0   ; memory page 0
main:
    bsf    PORTB,1      ; output B.1 high
    call   wait1000ms   ; delay 1000 milliseconds
    bcf    PORTB,1      ; output B.1 low
    call   wait1000ms   ; delay 1000 milliseconds
    goto   main
The program compiles successfully if I switch to the alternate format for binary constants: 0b11111111
 
Last edited:

AllyCat

Senior Member
Hi,

Did you omit the "One Time Initial Installation" Instruction number 5: ?

"From the Compiler tab disable (uncheck) the BASIC language pre-processor (at top of list). "

I still wonder if "not .. quite the speed" is approximately the 400 times slower of typical simple PICaxe Instructions, compared with "Real" Assembler?

Cheers, Alan.
 
Last edited:

Flenser

Senior Member
Alan,

Good pickup. It was not following the instructions properly and disabling the pre-processor in the compiler tab that caused my error. So no bug.
 

hippy

Technical Support
Staff member
I still wonder if "not .. quite the speed" is approximately the 400 times slower of typical simple PICaxe Instructions, compared with "Real" Assembler?
Most likely. The WJEC assembly language is converted to PICAXE Basic by invisible macros which is then compiled and run in the usual way.

For example "movwf TRISA" translates to something like "dirsC = W ^ 0xFF" in PICAXE Basic.

I am not sure it's 400 times slower but it's been a while since I have looked at speed comparisons. Students are usually using WJEC To get an understanding of how typical microcontrollers work at the most fundamental level so speed of execution is not usually relevant.
 

Technical

Technical Support
Staff member
I'm trying to use a HC-SR04 range sensor with a picaxe 18m2 chip, however it uses the WJEC Assembler language and I can't seem to find any tutorials or sample code that could help me set it up to start range detection. I know the pulses from the HC-SR04 need to sent out every 10 micro seconds but the Assembler language only allows for 1ms wait. I also need help setting up a variable that would continuously check if the distance is larger than my set amount.

Any help would be greatly appreciated.
The far east HC-SR04 does NOT behave the same as a SRF005 and do get it to work with a PICAXE you need to use a single pin.

Connect echo to trigger with a 1K8 resistor or similar, then connect trigger to a single PICAXE pin e.g. B.1

High B.1
PulsIn B.1, 1, w0

The high you could easily reproduce in assembler (bsf PORTB,1), the pulsin is probably best left off as the BASIC pulsin command. It is acceptable for the WJEC course (projectwork element) to use extra BASIC commands as if they were predefined subroutines. However in the written exam it will be pure assembler.

Once you have the value in the variable you need to work around the word w0 to byte b0 issues (trying to keep distance in one byte only by using b0 (maximum 255) instead of w0 would help a lot). To check if the value is within a target threshold movf the b0 value into W (movf b0,W) and then use sublw to subtract W from a fixed literal value (which is your distance threshold) e.g. sublw d'30'. If the STATUS,C carry bit is set after this subtraction then W was greater than the literal value - and you can use BTFSC STATUS,C to conditionally do something.

This is a pretty advanced coding project for the WJEC course, good luck!
 
The far east HC-SR04 does NOT behave the same as a SRF005 and do get it to work with a PICAXE you need to use a single pin.

Connect echo to trigger with a 1K8 resistor or similar, then connect trigger to a single PICAXE pin e.g. B.1

High B.1
PulsIn B.1, 1, w0

The high you could easily reproduce in assembler (bsf PORTB,1), the pulsin is probably best left off as the BASIC pulsin command. It is acceptable for the WJEC course (projectwork element) to use extra BASIC commands as if they were predefined subroutines. However in the written exam it will be pure assembler.

Once you have the value in the variable you need to work around the word w0 to byte b0 issues (trying to keep distance in one byte only by using b0 (maximum 255) instead of w0 would help a lot). To check if the value is within a target threshold movf the b0 value into W (movf b0,W) and then use sublw to subtract W from a fixed literal value (which is your distance threshold) e.g. sublw d'30'. If the STATUS,C carry bit is set after this subtraction then W was greater than the literal value - and you can use BTFSC STATUS,C to conditionally do something.

This is a pretty advanced coding project for the WJEC course, good luck!
Yeah I gave that a try and I think I got it to work now, just need to use a buzzer now to make it beep when I get it to a certain distance.
 
symbol SIG = C.1 ; Define pin for Trigger & Echo (All M2, X2 parts)
symbol range = w1 ; 16 bit word variable for range
main:
clrf PORTB
ultra SIG,range ;dedicated ultra command for ultrasonic sensor
debug range ; display range via debug command
call wait100ms ; short delay
movfw B2
addlw 225
btfsc STATUS,c
goto slowbeep
movfw B2
addlw 255
btfsc STATUS,c
goto fastbeep
goto main ; loop around forever

slowbeep:
bsf PORTB,0
call wait1000ms
goto main

fastbeep:
bsf PORTB,0
call wait100ms
call wait100ms
call wait100ms
call wait100ms
goto main

I have this so far, and when I simulate it, it seems to work properly. I think I need to integrate an interrupt routine as well and I think that's all the specification asks for, but am I allowed to use the "ultra" command for the course work ?
 

hippy

Technical Support
Staff member
but am I allowed to use the "ultra" command for the course work ?
Only your tutor or teacher can definitively answer that. I would have thought so but best to ask.

Once you have it working with ULTRA it might be worth showing how it would have been done with pure assembler which would demonstrate understanding of what it is you are being asked to show. I think it could work in pure assembler though the resolution in readings may be somewhat lower.
 
Only your tutor or teacher can definitively answer that. I would have thought so but best to ask.

Once you have it working with ULTRA it might be worth showing how it would have been done with pure assembler which would demonstrate understanding of what it is you are being asked to show. I think it could work in pure assembler though the resolution in readings may be somewhat lower.
Yeah I'll send him an email, and I'll make a copy of the code and replace the ultra with pure assembler as well.

Thank you so much for the help.
 
Top