Mittwoch, 15. Januar 2014

Ultrasonic Sensor HC-SR04 Microcontroller Test

I'm trying out the Sensor HC-SR04 Modul.
First of all I had to figure out, how it works.




As you can see it has 4 pins.

VCC 5V
Trigger
Echo
GND

Here is how it should be controlled:



Send a 10us impulse to trigger pin.
The module will send 8 ultrasonic impulses.
The echo pin goes high for 100us to 25ms, depending on the distance to the object.
If there is no reflecting surface, the echo pin goes down after max. 30ms


Here my test setup:


A little video:



And the Bascom code:



Config Pinc.0 = Output
Config Pinc.1 = Input
Dim A As Long
Cls

Do
 Pinc.0 = 0     'Trigger 0'
 Waitus 2
 Pinc.0 = 1     'Trigger 1'
 Waitus 10      'wait 10us'
 Pinc.0 = 0      'Trigger 0'

 If Pinc.1 = 1 Then Gosub Outlcd                            'when echo goes high'


Loop



Outlcd:
 A = 0
 Do
 A = A + 1
 Loop Until Pinc.1 = 0                                      'count until echo 0'

 Cls
 A = A / 30                                                 'divide value by 30 to get it nearly in cm'
 Lcd A
 Waitms 500

 Return

End

Dienstag, 7. Januar 2014

Led Matrix 8x5 with Shift register and Shiftout




The Bascom code



Dim S As String * 8
Dim U As String * 8
Dim L(6) As Byte
Dim A As Long
Dim B As Byte
Dim C As Byte
Config Portb = Output
Config Portc = Output

Clock Alias Portb.1
Outdata Alias Portb.2
Latch Alias Portb.3
Srreset Alias Portb.0

Srreset = 0
Srreset = 1

L(1) = &B0010000
L(2) = &B0111110
L(3) = &B1111110
L(4) = &B0111110
L(5) = &B0010000


Do

For A = 1 To 7
Portc = 1
For B = 1 To 5

Shiftout Outdata , Clock , L(b) , 2 , 8

Latch = 1
Latch = 0
Srreset = 0
Srreset = 1
Waitms 20
Latch = 1
Latch = 0
Rotate Portc , Left
Next B

Next A
For C = 1 To 5
Rotate L(c) , Left
Next C
Loop

End