ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 8254

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
EnableInterrupts
'socket status
Const Sock_closed = $00 Const Sock_arp = $01 Const Sock_listen = $02 Const Sock_synsent = $03
Const Sock_synsent_ack = $04 Const Sock_synrecv = $05 Const Sock_established = $06 Const Sock_close_wait = $07 Const Sock_last_ack = $08 Const Sock_fin_wait1= $09 Const Sock_fin_wait2= $0a Const Sock_closing = $0b Const Sock_time_wait = $0c Const Sock_reset = $0d Const Sock_init = $0e
Const Sock_udp = $0f
Const Sock_raw = $10
'we do the usual Print"Init TCP"

© MCS Electronics, 1995-2007

$crystal= 8000000 $baud = 19200 $hwstack= 80 $swstack= 128 $framesize = 80

Const Debug = 1

Const Sock_stream = $01

Const Sock_dgram = $02

Const Sock_ipl_raw = $03

Const Sock_macl_raw = $04

Const Sel_control = 0

Const Sel_send = 1

Const Sel_recv = 2

'used crystal frequency

'use baud rate

'default use 32 for the hardware stack

'default use 10 for the SW stack

'default use 40 for the frame space

'Tcp

'Udp

'Ip Layer Raw Sock

'Mac Layer Raw Sock

'Confirm Socket Status

'Confirm Tx Free Buffer Size

'Confirm Rx Data Size

'Status Of Connection Closed

'Status Of Arp

'Status Of Waiting For Tcp Connection Setup

'Status Of Setting Up Tcp Connection ' Status Of Setting Up Tcp Connection

'Status Of Setting Up Tcp Connection

'Status Of Tcp Connection Established

'Status Of Closing Tcp Connection

'Status Of Closing Tcp Connection

'Status Of Closing Tcp Connection

'Status Of Closing Tcp Connection

'Status Of Closing Tcp Connection

'Status Of Closing Tcp Connection

'Status Of Closing Tcp Connection

'Status Of Socket Initialization

'Status Of Udp

'Status of IP RAW

' display a message

' before we use config tcpip , we need to enable the interrupts Config Tcpip = Int0, Mac = 12.128.12.34.56.78 ,Ip= 192.168.0.8 , Submask = 255.255.255.0 , Gateway = 192.168.0.1 ,Localport = 1000 ,Tx= $55 ,Rx = $55 ,Twi= &H80 ,Clock = 400000

Print"Init done"

Dim Peersize As Integer, Peeraddress As Long ,Peerport As Word

Dim IdxAs Byte,Result As Word ,JAs Byte, Res As Byte

Dim IpAs Long

Dim Dta(12)As Byte, Rec(12)As Byte

Dta(1)= 8

'type is echo

Dta(2)= 0

'code

Dta(3)= 0

' for checksum initialization

Dta(4)= 0

' checksum

Dta(5)= 0

' a signature can be any number

Dta(6)= 1

' signature

Dta(7)= 0

' sequence number - any number

Dta(8)= 1

Dta(9)= 65

Dim W As Word At Dta + 2Overlay

'same as dta(3) and dta(4)

page -645-


© MCS Electronics, 1995-2007

W = Tcpchecksum(dta(1),9)

' calculate checksum and store in dta(3) and dta(4)

#ifDebug

For J= 1To 9

Print Dta(j)

Next

#endif

Ip= Maketcp(192.168.0.16)

'try to check this server

Print"Socket ";Idx;"";Idx

'set protocol to 1

Setipprotocol Idx,1

'the protocol value must be set BEFORE the socket is openend

Idx= Getsocket(idx,3, 5000 ,0)

Do

'writepingdata

'

Result= Udpwrite(ip,7,Idx,Dta(1),9)

Print Result

Waitms 100

'check for data

Result= Socketstat(idx,Sel_recv)

Print Result

IfResult >= 11 Then

Print"Ok"

'get data with TCPREAD !!!

Res= Tcpread(idx, Rec(1),Result)

#ifDebug

'

Print"DATA RETURNED :";Res

For J= 1To Result

Print Rec(j);"";

Next

Print

#endif

'there might be a problem

Else

Print"Network not available"

End If

Waitms 1000

Loop

SGN

Action

Returns the sign of a float value.

Syntax

var = SGN( x )

Remarks

Var

A single or double variable that is assigned with the SGNS of variable

x.

X

The single or double to get the sign of.

page -646-


© MCS Electronics, 1995-2007

For values <0, -1 will be returned

For 0, 0 will be returned

For values >0, 1 will be returned

See Also

INT , FIX , ROUND

Example

Dim S As Single , X As Single , Y As Single

X = 2.3 : S = Sgn(x)

Print S

X = -2.3 : S = Sgn(x)

Print S

End

SHIFT

Action

Shift all bits one place to the left or right.

Syntax

SHIFT var , LEFT/RIGHT[ , shifts]

Remarks

Var

Byte, Integer/Word, Long or Single variable.

Shifts

The number of shifts to perform.

The SHIFT statement rotates all the bits in the variable to the left or right.

When shifting LEFT the most significant bit, will be shifted out of the variable. The LS bit becomes zero. Shifting a variable to the left, multiplies the variable with a value of two.

When shifting to the RIGHT, the least significant bit will be shifted out of the variable. The MS bit becomes zero. Shifting a variable to the right, divides the variable by two.

A Shift performs faster than a multiplication or division.

See also

ROTATE , SHIFTIN , SHIFTOUT

Example

'-----------------------------------------------------------------------------

------------

: shift.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: example for SHIFTIN and SHIFTOUT statement

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

page -647-


© MCS Electronics, 1995-2007

'-----------------------------------------------------------------------------

------------

$regfile = "m48def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 19200

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

' default use 40

$framesize = 40

for the frame space

Dim L As Long

Clock Alias Portb.0

Output Alias Portb.1

'watch the PIN

Sin Alias Pinb.2

instead of PORT

'shiftout pinout,pinclock, var,parameter [,bits , delay]

'value for parameter :

'0 - MSB first ,clock low

'1 - MSB first,clock high

'2 - LSB first,clock low

'3 - LSB first,clock high

'The bits is a new option to indicate the number of bits to shift out

'For a byte you should specify 1-8 , for an integer 1-16 and for a long 1-32 'The delay is an optional delay is uS and when used, the bits parameter must 'be specified too!

'Now shift out 9 most significant bits of the LONG variable L

Shiftout Output , Clock , L , 0 , 9

'shiftin pinin,pinclock,var,parameter [,bits ,delay]

'0 - MSB first ,clock low (4)

'1 - MSB first,clock high (5)

' 2 - LSB first,clock low (6)

'3 - LSB first,clock high (7)

'To use an external clock, add 4 to the parameter

'The shiftin also has a new optional parameter to specify the number of bits

'The bits is a new option to indicate the number of bits to shift out

'For a byte you should specify 1-8 , for an integer 1-16 and for a long 1-32 'The delay is an optional delay is uS and when used, the bits parameter must 'be specified too!

'Shift in 9 bits into a long Shiftin Sin , Clock , L , 0 , 9

'use shift to shift the bits to the right place in the long

Shift L , Right , 23

End

SHIFTCURSOR

page -648-