📄 owarray.pbas
字号:
'This demo is to show how to read 8 bytes of data via 1-wire protocol.
'The core of the example is the function [b]Ow_Read_Serial[/b].
'The example can be further opimized.
'
'
'******************************************************************************
' microcontroller : P16F877A
'
' Project: Dallas iButton
'
' The project was written for P16F877A, but with minor adjustments can be used
' with any other PIC MCU.
'
' The example demonstrates 1-wire communication with electronic key
' DS1990A connected to RD2 pin. Upon reset, PIC reads all 64 bits (8 bytes)
' of iButton key and prints them on LCD in hex. LCD is 4-wire on PORTB
'******************************************************************************
program OwArray
dim text1 as char[17] ' message1 on LCD
dim text2 as char[17] ' message2 on LCD
dim Js as byte[8] ' accepts code from iButton
dim presence_check as byte
const pin_ow = 2 ' pin used for 1Wire
' -------------------------------------------------------------
' Procedure for converting byte variable to HEX and displaying
' it on LCD display.
'
' procedure int2hex(row, start_x : byte number : byte)
' row - LCD line,
' start_x - position of first hex digit (left to right)
' -------------------------------------------------------------
sub procedure int2hex(dim row as byte, dim start_x as byte, dim number as byte)
dim high as byte
dim low as byte
' high nibble
high = ((number and $F0) >> 4)+ 48 ' + "0"
if (high > 57) then high = high + 7 ' > "9"
end if
' low nibble
low = (number and $0F) + 48 ' + "0" low nibble
if (low > 57) then low = low + 7 ' > "9"
end if
Lcd_Chr(row, start_x, high)
Lcd_Chr(row, start_x + 1, low)
end sub
'Ow_Read_Serial reads 8 bytes via 1-wire protocol and
'places them into the array "out". The destination array
'needs to be at least 8 bytes in size
sub procedure Ow_Read_Serial(dim byref PORT as byte, dim pin as byte, dim byref out as byte[8])
dim i as byte
dim tmpRead as byte
i = 0
while i <= 7 ' we will move 8 bytes
tmpRead = ow_read(PORT,PIN) ' get one byte from Dallas iButton
out[i] = tmpRead
inc(i) ' next i
wend
end sub
main:
PORTB = 0 ' initialize portb on 0
trisb = 0 ' portb is output
text1 = "mikro iButton:"
text2 = "not present "
Lcd_Init(PORTB)
Lcd_Cmd(LCD_CURSOR_OFF)
LCD_Out(1, 1, text1) ' print initial message
_loop:
' first, we read iButton if present on pin RD2
' if not present, print appropriate message on LCD
trisd = 255
ow_reset(PORTD,pin_ow) ' onewire reset signal
ow_write(PORTD,pin_ow,$33) ' read iButton
delay_us(120) ' pause
Ow_Read_Serial(PORTD, pin_ow, Js) ' read 8 bytes of Dallas iButton
' if iButton is not present, j1 ~ j8 will be at 255, so we can easily
' test them:
presence_check = js[0] and js[1] and js[2] and js[3] and js[4] and js[5] and js[6] and js[7]
if presence_check = $FF then ' iButton is not present
LCD_Out(2, 1, text2) ' print message
else ' iButton is present, convert codes to hex and print them on LCD
int2hex(2, 1, js[7])
int2hex(2, 3, js[6])
int2hex(2, 5, js[5])
int2hex(2, 7, js[4])
int2hex(2, 9, js[3])
int2hex(2, 11, js[2])
int2hex(2, 13, js[1])
int2hex(2, 15, js[0])
end if
Delay_ms(500)
goto _loop
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -