Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : I2C help for Micromite?
Author | Message | ||||
Oldbitcollector Senior Member Joined: 16/05/2014 Location: United StatesPosts: 172 |
Can someone provide some examples of how to use the I2C commands with the Micromite? I've got some code for reading the Wii controller in another language and would like to convert it over. I tend to learn by example. Any help would be appreciated. Thanks Jeff My Propeller/Micromite mini-computer project. |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5923 |
If you can provide the code in the other language, we should be able to help with the conversion. As an example, this code controls the alarm functions on the PCF8563 RTC. dim RTCbuffer(16) PCF8563address = &h51 function setRTCint(count,scale) ' scale: 2 = seconds, 3 = minutes local t_ctrl t_ctrl = 128 + scale I2C write PCF8563address,0,1, &H00 ' set the starting address for a read i2c read PCF8563address,0,16,RTCbuffer(0) I2C write PCF8563address,0,2, &H01, &H11 I2C write PCF8563address,0,3, &H0E, t_ctrl, count' timer control register setRTCint=RTCbuffer(1) end function function setRTCalarm(hour, minute,day,WkDay) local alarmH,alarmM,alarmD,alarmWD alarmH=int(hour/10)*16+(hour mod 10) 'alarmH=128 ' not used (once an hour) alarmM=int(minute/10)*16+(minute mod 10) 'alarmD=int(day/10)*16+(day mod 10) alarmD=128 ' not used (every day) 'alarmWD=WkDay alarmWD=128 ' not used (every day) I2C write PCF8563address,0,1, &H00 i2c read PCF8563address,0,16,RTCbuffer(0) I2C write PCF8563address,0,2, &H01, &H12 I2C write PCF8563address,0,5, &H09, alarmM, alarmH,alarmD, alarmWD setRTCint=RTCbuffer(1) end function FUNCTION setRTC() ' set the RTC to MicroMite time local newtime$, year, month, day, hour, min, sec newtime$= time$ ' make a copy to stop time changing while we set the clock year = val(mid$(date$,9)) month = val(mid$(date$,4,2)) day = val(mid$(date$,1,2)) hour = val(mid$(newtime$,1,2)) min = val(mid$(newtime$,4,2)) sec = val(mid$(newtime$,7,2)) rtc settime year,month,day,hour,min,sec END FUNCTION function clearRTCalarm() ' two bits indicate if alarm or timer triggered the interupt ' we clear them both local x I2C write PCF8563address,0,1, &H00 i2c read PCF8563address,0,2,RTCbuffer(0) x = (RTCbuffer(1) and &H13) I2C write PCF8563address,0,2, &H01, x clearRTCalarm=RTCbuffer(1) and &H0C ' 8 = alarm and 4 = timer end function You really need to start with the data sheets for the module you are talking to. You should find some clues if you do a search of the forum for "numchuck". The code will need to be changed to suit the new I2C commands. Jim VK7JH MMedit MMBasic Help |
||||
bigmik Guru Joined: 20/06/2011 Location: AustraliaPosts: 2870 |
Hi Jeff, I posted this Thread a couple of days ago. I modified Jman's modifications of `letsmakerobots.com' code to use the uMite commands then modified it to talk to two different I2C LCD modules. Works really well and not too difficult to understand. Mick Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<< |
||||
Bill.b Senior Member Joined: 25/06/2011 Location: AustraliaPosts: 225 |
This is picaxe code for the wii classic controller Unfortunately I have dismantled my wii controller to use the parts in other project, so I will not be trying to migrate it across. [code] ' Program to read data form a Wii Classic Controller into a picaxe 28X2. ' Bill Brown Bill.b 29th July 2013 ' ' I have tested the program with a 40X2 and it worked OK. The program does not work correctlly with M2 chips ' Some more work is required for M2s ' ' ' The Wii Receiver module required a 3.3volt supply. ' In my test unit i derived the 3.3v by connection two IN4001 diodes in series to the 5v supply giving approximatly 3.4v. ' 4k7 pullup reistors are unsed on the SCL and SDA - Connected to the +3.4v supply ' ' Data format for the 6 bytes of data from the Wii Classic Controller (WiiByte0 - 5) '----------------------------------------------------------- ---- '|Byte | Bit | '|------|--------------------------------------------------- ----| '| | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | '|---------------------------------------------------------- ----| '| 0 | RX<4:3> | LX<5:0> | '|---------------------------------------------------------- ----| '| 1 | RX<2:1> | LY<5:0> | '|---------------------------------------------------------- ----| '| 2 |RX<0> | LT<4:3> | RY<4:0> | '|---------------------------------------------------------- ----| '| 3 | LT<2:0> | RT<4:0> | '|---------------------------------------------------------- ----| '| 4 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 | '|---------------------------------------------------------- ----| '| 5 | BZL | BB | BY | BA | BX | BZR | BDL | BDU | '!---------------------------------------------------------- ----! ' 'LX,LY are the left analogue stick, X and Y (0 - 63). RX and RY are the right analogue stick, X and Y (0 - 31). 'LT and RT are the left and right triggers (0 - 31). The left analogue stick has twice the precision of the other stick ' ' BD(L,R,U,D) are the D-Pad direction buttons. B(ZR,ZL,A,B,X,Y,+,H,-) are the discrete buttons. 'B(LT,RT) are the digital button click of the LT and RT. all Buttons are 0 when pressed. #picaxe 28x2 setfreq m8 symbol WiiByte0 = b7 'Wii byte 0 symbol WiiByte1 = b8 'Wii byte 1 symbol WiiByte2 = b9 'Wii byte 2 symbol WiiByte3 = b10 'Wii byte 3 symbol WiiByte4 = b11 'Wii byte 4 symbol WiiByte5 = b12 'Wii byte 5 symbol WiiTemp0 = b13 'Wii Temperary storage byte symbol WiiLeftX = b14 'Left stick X axis 0 - 63 symbol WiiLeftY = b15 'Left stick Y axis 0 - 63 symbol WiiRightX = b16 'Right stick X axis 0 - 31 symbol WiiRightY = b17 'Right stick Y axis 0 - 31 symbol WiiLeftT = b18 'Left trigger 0 - 31 symbol WiiRightT = b19 'Right trigger 0 - 31 i2cslave %010100100, i2cfast_16, i2cbyte 'I2C setup for Wii controller hi2cout 0,($F0,$55) 'Wii Configuration bytes pause 10 hi2cout 0,($FB,00) 'Wii Configuration bytes pause 10 hi2cout 0, ($00) 'Wii Request data 'Main program start here main: hi2cout 0, ($00) 'Wii Request data pause 70 hi2cin 0,(WiiByte0,WiiByte1,WiiByte2,WiiByte3,WiiByte4,WiiByte5) 'Data from Wii Controller 'The following section arranges the data from the Wii bytes 0 - 4 into seperate bytes for used in the program pause 50 WiiLeftX = WiiByte0 & %00111111 'Left stick X = Bits 0 - 5 of wiibyte0 WiiLeftY = WiiByte1 & %00111111 'Left stick Y = Bits 0 - 5 of wiibyte1 WiiRightX = WiiByte2 & %00011111 'Right stick X = Bits 0 - 4 of wiibyte2 WiiRightY = WiiByte2 & %10000000 'Move bit 7 of wiibyte2 to Right stick Y WiiRightY = WiiRightY >> 2 'shift right 2 places WiiTemp0 = WiiByte1 & %11000000 'move bits 6 & 7 of wiibyte1 to temp WiiRightY = WiiRightY OR WiiTemp0 'Combine temp and right stick Y WiiRightY = WiiRightY >> 2 'Shift right 2 places WiiTemp0 = WiiByte0 & %11000000 'move bits 6 & 7 of wiibyte0 to temp WiiRightY = WiiRightY OR WiiTemp0 'Combine temp and right stick Y WiiRightY = WiiRightY >> 3 'Shift right 3 places. Right stick Y byte completed WiiRightT = WiiByte3 & %00011111 'move Right Tigger bits 0 - 5 form wiibyte 3 to wiiRightT WiiLeftT = WiiByte3 & %11100000 'move left trigger bits 5 - 7 form wiibyte 3 to wiiLeftT WiiLeftT = WiiLeftT >> 3 'shift right 3 positions. WiiTemp0 = WiiByte2 & %01100000 'Move Left trigger bits 3 & 4 to temp WiiLeftT = WiiLeftT OR WiiTemp0 ' combine wiiLeftT and Temp WiiLeftT = WiiLeftT >> 2 'Shift right 2 positions to complete wiiLeftT byte. 'debug goto main [/code] Bill In the interests of the environment, this post has been constructed entirely from recycled electrons. |
||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
Hi OBC, I started playing with the I2C also between 2 uMites. The example in the uMite manual is a very good starting place for this. I had to study it a few times and experiment for a while until it sunk in. It is a great ability for the uMite and once you get the basics, interfacing just about anything can be done. There seems to be a small bug in the slave receive, however there is a work around that does work. It is shown here I2C slave receive fix |
||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Take a look at the thread below Wii numchuck with the original Maximite http://www.thebackshed.com/forum/forum_posts.asp?TID=4312&KW =wii Jman |
||||
Oldbitcollector Senior Member Joined: 16/05/2014 Location: United StatesPosts: 172 |
Wow. Thank you all for the responses.. Talk about a welcome.. @Jman, That looks like the ticket! I can see the similarities between that code and my existing code and {expletive edited}! the I2C interface looks as sweet as French Silk pie! This little Micromite chip is packs a lot of horsepower. I'm used to doing jobs like this with a page of code, not two or three commands. I'll post up some working code as soon as I noodle it out later today. Jeff My Propeller/Micromite mini-computer project. |
||||
Oldbitcollector Senior Member Joined: 16/05/2014 Location: United StatesPosts: 172 |
As promised.. As someone else may need this thread at some point. [code] i2caddr = &h52 DIM RDBuff(255) I2C open 400,100 ' Enable I2C I2C write i2caddr,0,2,&hf0,&h55 I2C write i2caddr,0,2,&hfb,&h0 :readwii I2C write i2caddr,0,1,&h0 I2C write i2caddr,0,6,RDBuff(0) PRINT " Joy X:";RDBuff(0); PRINT " Joy Y:";RDBuff(1); PRINT " AccelX:";RDBuff(2); PRINT " AccelY:";RDBuff(3); PRINT " AccelZ:";RDBuff(4); PRINT " ButtonZ:";(RDBuff(5) AND &h01); PRINT " ButtonC:";(RDBuff(5) AND &h02) pause 50 GOTO readwii [/code] My Propeller/Micromite mini-computer project. |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3167 |
I don't have any hardware to test this code but should not line 8 read: I2C READ i2caddr,0,6,RDBuff(0)
And there is no need to declare such a large array to receive the data. This would be more economical with memory: DIM RDBuff(5)
Geoff Geoff Graham - http://geoffg.net |
||||
Print this page |