PDA

توجه ! این یک نسخه آرشیو شده میباشد و در این حالت شما عکسی را مشاهده نمیکنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : برنامه SRF08



Erni
13-03-2009, 11:46
سلام به همگی دوستان
من میخواستم یه ماژول SRF08 رو با ATMega 128 راه بندازم، اما از اونجایی که اطلاعاتی درباره USART و I2C ندارم، درخواست کمکککککککک دارم.
اگه ممکنه بگید که پایه های SRF08 چطوری باید به 128 ATMega وصل بشه و برنامش با بسکام چجوریه؟
ممنون میشم اگه راهمایی کنید.

kavir
13-03-2009, 12:06
سلام
با این کد پین های مورد استفاده رو در i2c مشخص می کنید
'config I2C lines for mavric
Config Sda = Portd.1
Config Scl = Portd.0
بعد هم مطابق دیتا شیت به میکرو وصل کنید و تغذیه هم وصل کنید
برای آشنایی با I2c و USART از اموزشی که در صفحه اول سایت هست استفاده کنید

[align=left:ccbaa137c5]'-------------------------------------------------------------------------------
' starter.bas
' this is an example for readining a SFR08 sonar
' all output will be on com2
' There is also an LED on portB.0 that will light if the
' distance reading is lower then 6 inches
'-------------------------------------------------------------------------------


$crystal = 14745600
$regfile = "m128def.dat"
$baud1 = 19200
'$sim

'config serial buffer
Config Serialout1 = Buffered , Size = 40
Config Serialin1 = Buffered , Size = 40

'com 2 standard conection
Config Com2 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

'config I2C lines for mavric
Config Sda = Portd.1
Config Scl = Portd.0

'this function is used to read the sonar value
'declare Functons
Declare Function I2cread8_w(byval I2cnode As Byte , Byval Loc As Byte) As Word

'this sub starts the PING of all conected sonars
'Declare Subs
Declare Sub I2cwrite8_b(byval I2cnode As Byte , Byval Loc As Byte , Byval Value As Byte)


' Startup int code
I2cinit 'start the I2C buss
Open "com2:" For Random As #2 'open come port
Enable Interrupts 'enable interupts
Config Portb = Output
Portb = 0 'set all low

'varables
Dim Distance As Word 'varable to hold sonar value
Const Sonar1 = 226 'make address of sonar

'start main program
Do
'small delay of 500mS
Waitms 500
Call I2cwrite8_b(0 , 0 , 80) 'ping all sonars and return value in inches
Waitms 70 'delay 70mS for all pings to hapen
Distance = I2cread8_w(sonar1 , 2) 'read the distance from node 226 and loactions 2 & 3
Print #2 , "Distance = " + Str(distance) + Chr(10) + Chr(13) 'send distance out com port
If Distance <= 6 Then 'check distance on sonar reading
Portb.0 = 1 ' less then 6 inch then led ON
Print #2 , "LED on" + Chr(10) + Chr(13)
Else
Portb.0 = 0 ' greater then 6 inch then led OFF
Print #2 , "LED off" + Chr(10) + Chr(13)
End If
Print #2 , "" + Chr(10) + Chr(13) 'blank print to seperate readings
Waitms 500 'wait 500mS and start again
Loop
'end main program

'subs
'------------------------------ I2C I2C I2C I2C I2C I2C I2C I2C I2C I2C I2C
Sub I2cwrite8_b(byval I2cnode As Byte , Loc As Byte , Value As Byte)
I2cstart
I2cwbyte I2cnode
I2cwbyte Loc
I2cwbyte Value
I2cstop
Waitms 10
End Sub
'-----------------------------------------------------------------------------
'8x16
Function I2cread8_w(byval I2cnode As Byte , Loc As Byte) As Word
Local Lsb As Byte
Local Msb As Byte
I2cstart
I2cwbyte I2cnode
I2cwbyte Loc
I2cstart
Incr I2cnode
I2cwbyte I2cnode
I2crbyte Msb , Ack
I2crbyte Lsb , Nack
I2cstop
I2cread8_w = Makeint(lsb , Msb)
End Function[/align:ccbaa137c5]

Erni
13-03-2009, 17:59
خیلی ممنون از راهنماییتون
فقط اگه ممکنه یه توضیح مختصر و کلی درباره دستورات بدبد.
چون من تازه کارم!!! البته مقاله صفحه اول سایت رو درباره I2C خوندم و یه چیزایی دستم اومد اما اگه محبت کنید یه کم بیشتر توضیح بدید خیلی خوبه.
مثلا چرا کریستال رو 14745600 انتخاب کردید؟