Menu

I2C - PICAXE20X2 (Master), 16F628A (Slave)

Help
Morty
2020-05-01
2020-05-02
1 2 > >> (Page 1 of 2)
  • Morty

    Morty - 2020-05-01

    Hello ALL,

    Newbie here. I came across GCB trying to find a way I can program PIC's with BASIC. Boy... Did I strike gold here or what!!! Excited to come across this platform.

    Anywayz, I have come from the land of PICAXE and am reasonably fluent nowdays, but a total noob around these parts.

    I have viewed many videos in just a few days and I am now here looking to communicate to a 16F628A as a slave from a PICAXE 20X2 master.

    I viewed this video attempting to understand...
    https://www.youtube.com/watch?v=Qu8xWAQaJm4

    However from what I can gather, it is rather incomplete for noobs to fully grasp.

    It would have been greatly beneficial if the gentleman had shown the entire code and/or provided a link to the complete code example. I am uncertain with some of the variables.

    Essentially I am currently looking to send 4 bytes (32bits) across I2C to the PIC16F628A from a PICAXE20X2, do some number crunching, and return a 16bit word result from the calculations.

    From what I read around here, it should be quite easy,. But I be buggered if I can get it to compile, let alone. function as I want.

    ANY help would be greatly appreciated.

    P.S. I have accidently posted this same query under 'Great Cow Graphical BASIC Problems' if an Admin could possibly remove that post.

    Regards,
    mortify-u

     
  • Anobium

    Anobium - 2020-05-01

    Morning Morty (Is this a second post?)

    Firstly, welcome. I came from PICAXE a while ago. This is good fun but forget the constraints you had - you have the freedom to use the full capabilities of the PIC.

    Questions.

    Those chips. What are the real part names? 16F?????? . With this info I can tell you why it may not compile.
    Why I2C? Why not a robust serial ring buffer?
    Got the latest version of the Great Cow BASIC tools?

     
  • Morty

    Morty - 2020-05-01

    Hello, thanks for the quick response.

    This post is the second/correctly placed post. Please remove the other post if you can.

    PICAXE20X2 = PIC18F14K22-I/P

    I need to use I2C because I need to add the PIC16F628A to an existing circuit/system.

    I believe I have the latest versions of everything GCB.

    If I am not mistaken, you are the man to whom posted the I2C video in question. Great to meet you mate. Your video appeared to do roughly what I want to do, but I was unable to view the entire code of that exercise. Could you please possibly post the complete code here?

    Regards,
    Morty.

     
  • Anobium

    Anobium - 2020-05-01

    The root cause is pretty simple.

    The 16F628A does not support hardware I2C with the required interrupts. The library relies on specific I2C capabilities which that chip does not have.

    1. The 18F you have would work - got another one? or
    2. Get a nedw chip PIC16F18444, or,
    3. Use the software I2C (dumping the library).

    What route do you want to go?

     
  • Morty

    Morty - 2020-05-01

    Using software I2C was the route I was looking at.

    I don't have anymore 18F's on hand and the 16F628A was one I just happened to have here.

    For the purpose of getting this to work today as I am hoping, I'd like to stick with the chips I am currently using, but can indeed get another one at a later date.

    I suspect the 16F628A is miles 'over the top' for what I am doing, but will do for now. I am realistically using it as an I2C interfaced math co-processor. Doing this via software will get me on the ground running with GCB.

    The 4 bytes (32bits) being sent from the PICAXE will need bits 6 to 29 (24bit value) extracted and used in a calculation with the end result being a 1 word (16bit) value being returned to the PICAXE. This 24bit value has originally come from a LTC2485 24bit ADC.

    Regards,
    Morty.

     
  • Anobium

    Anobium - 2020-05-01

    OK. Then, software I2C is the route to go. [Context... PICAXE was software I2C using Bit-Banging]

    Look at I2CReceive in the Help. You are creating a Slave - so, look at the second code example. You will need wait (as on the code) then hold the clock line low (to stretch the I2C communications) do your maths and then send a value back.

     
  • Morty

    Morty - 2020-05-01

    Thanks for the tips. I thought I went down that road, but will go over it again carefully.

    Also thank you very much for the huge tip of holding the clock line low to stretch the I2C comms.

    For the sake of the forum, I'll come back here and let you know how I went and if there was any other issues.

    Regards,
    Morty.

     
  • Morty

    Morty - 2020-05-01

    Compiles OK, but still no comms...
    I am sure this is so simple...
    Where am I going wrong?

    PICAXE CODE:
    test:
    HI2cSetup I2CMASTER, $60, I2CFAST, I2CBYTE 'I2C comms to PIC16F628A
    let b20=10
    hi2cout 0,(b20)
    hi2cin (b21)
    sertxd(cr,cr,"Returned from 16F628A: ",#b21,cr,cr,lf)
    goto test

    GCB CODE:
    `#chip 16F628A

    #define I2C_MODE    Slave     ;this is a slave device now
    #define I2C_CLOCK   portb.2   ;SCL on pin 8
    #define I2C_DATA    portb.1   ;SDA on pin 7
    #define I2C_ADDRESS 0x60      ;address of the slave device
    
    ;----- Variables
    dim addr, reg, value as byte
    
    ;----- Program
    do
    
      I2CStart                 ;wait for Start signal
      I2CReceive( addr )         ;then wait for an address
    
      if I2CMatch  = true then    ;if it matches, proceed
    
        I2CReceive(regval, ACK)   ;get the register number
        I2CReceive(value, ACK)    ;and its value
    
    'My math routine will go here
    PulseOut porta.2, 1000 ms
    value=value*10
    I2CSend value
    
    
        I2CStop                   ;release the bus from this end
    
      else
         I2CStop           ;release bus in any event
      end if
    
    loop`
    
     
  • Anobium

    Anobium - 2020-05-01

    This code is waiting for two values from the master.

    I2CReceive(regval, ACK)   ;get the register number
    I2CReceive(value, ACK)    ;and its value
    

    You are sending one value. So, remove the following.

    I2CReceive(regval, ACK)   ;get the register number
    
     
  • Morty

    Morty - 2020-05-01

    Thanks for that. I suspected that was what the 2x I2CReceive lines were suggesting.

    I removed I2CReceive(regval, ACK) ;get the register number

    But there is no change.

    The PICAXE is displaying 255 as a returned value. Obviously not getting anything back.

    Regards,
    Morty.

     
  • Anobium

    Anobium - 2020-05-01

    Got an analyser?

     
    • Anobium

      Anobium - 2020-05-01

      Got pullsup on the SDA/SCL ?

       
  • Morty

    Morty - 2020-05-01

    No I don't. Hence why I put the PulseOut line in there which has an LED attached to it.

    I have confirmed it is processing by placing the PulseOut line immediately after do

    By this, the LED does flash.

    Regards,
    Morty.

     
  • Morty

    Morty - 2020-05-01

    Certainly do. 4K7 in both cases. There is also an LCD on the same I2C bus, and it is working fine.

    To me it seems the 16F628A is just not taking notice of the command sent from the PICAXE.

    I am sure I have the addressing correct.

    I have now just tried I2CSLOW in the PICAXE program to see if that made any difference and now 97 is being returned. So I would imagine the communications are right now. However, why is it returning 97 instead of 100?

    Essentially I am sending a value of 10, multiplying it by 10 in the 16F628A and then sending the result back which obviously should be 100. Not sure where 97 is coming from? Strange.

    Regards,
    Morty.

     
  • Morty

    Morty - 2020-05-01

    Sorry, no I just repowered the circuit and it still returns 255. 97 Must have been some noise from the programming phase.

    Regards,
    Morty.

     
  • Morty

    Morty - 2020-05-01

    And the LED is NOT pulsing as I would hope.

    Regards,
    Morty.

     
  • Anobium

    Anobium - 2020-05-01

    This is what the code is generating from the Master. Which from a protocol state looks ok. But, the slave is not responding.

    I am using Software I2C on the Master.

    This is the code I2C protocol. Trust me. Picaxe may not have followed the protocol.

    I2CStart
    I2CSend ( 0x60 )   'write address
    I2CSend ( 10 )
    I2CReStart
    I2CSend ( 0x61 )    'read address
    I2CReceive ( invar )
    I2CStop
    

    I am not getting a result back because the slave is not responding. Not sure why. I did not write these libraries as I only use the hardware I2C. Need to resolve why the slave is NOT responding with an ACK.

     
  • Morty

    Morty - 2020-05-01

    Ok. So where does this leave me? The need to get another PIC with hardware I2C?

    As this is a pretty simple and small task I am trying to achieve, is there perhaps a suitable 8pin PIC?

    The calculations I require run into the millions. With a 16F628A I did a little experimental test that worked fine which just left me with the dilemma of I2C comms to deal with.

    #chip 16f628A
    
      Do Forever
    
      let b0=(31358024406/123456789)
    
        if b0 = 254 then
          PulseOut porta.2, 100 ms
        End If
    
      wait 1000 ms
    
      loop
    
     

    Last edit: Morty 2020-05-01
  • Morty

    Morty - 2020-05-01

    I should also note that on this I2C bus there is:

    Master - PICAXE20X2/PIC18F14K22-I/P
    LCD - 20x4
    LTC2485 - 24bit ADC
    +
    16F628A - To be a math co-processor

    The first 3 components are working fine together, however with such large numbers it has proven very difficult to do the math with the PICAXE hence the addition of the 16F628A.

    Regards,
    Morty.

     
  • Anobium

    Anobium - 2020-05-01

    Some working code. Not use the SW I2C for receive.

    Master sends values 1 to 10, slave factors and the terminal shows the factored numbers.

    Works ok... May update the Help!

    Master

    dim invar as Byte
    dim outvar as byte
    outvar = 1
    
    do
    
      I2CStart
      I2CSend ( 0x61 )
      if I2CAckPollState then
        I2CSend ( outvar )
        I2CReStart
        I2CReceive ( invar )
    
        HSerPrintByteCRLF invar
    
        outvar++
        if outvar > 10 then outvar = 1
      end if
      I2CStop
      wait 500 ms
    
    loop
    

    Slave

    ;----- Variables
    dim addr, reg, value as byte
    
    ;----- Program
    do
    
      if I2CStartOccurred then                ;wait for Start signal
          I2CReceive( addr,ack )             ;then wait for an address
          if I2CMatch  = true then           ;if it matches, proceed
            I2CReceive(value)                ;and its value
                                              'My math routine will go here
            value=value*10
            I2CSend value
          end if
       end if
    loop
    
     
  • Morty

    Morty - 2020-05-01

    Hmmm.. Interesting.

    This is what I have now done, but still no joy...

    PICAXE 20X2

    main:
    HI2cSetup I2CMASTER, $60, I2CFAST, I2CBYTE  'I2C comms to PIC16F628A
    let b20=10
    hi2cout 0,(b20)
    hi2cin (b21)
    sertxd(cr,cr,"Returned from 16F628A: ",#b21,cr,cr,lf)
    
    pause 1000
    goto main
    

    16F628A

        #chip 16F628A
    
        #define I2C_MODE    Slave     ;this is a slave device now
        #define I2C_CLOCK   portb.2   ;SCL on pin 8
        #define I2C_DATA    portb.1   ;SDA on pin 7
        #define I2C_ADDRESS 0x60      ;address of the slave device
    
        ;----- Variables
        dim addr, value as byte
    
      ;----- Program
      do
        if I2CStartOccurred then                ;wait for Start signal
          I2CReceive( addr,ack )             ;then wait for an address
            if I2CMatch  = true then           ;if it matches, proceed
            I2CReceive(value)                ;and its value
                                              'My math routine will go here
            value=value*10
            I2CSend value
            end if
        end if
      loop
    
     
  • Anobium

    Anobium - 2020-05-01

    The read address is 0x61, that is the standard not a Great Cow BASIC rule . Change and test.

    HI2cSetup I2CMASTER, $61, I2CFAST, I2CBYTE 'I2C comms to PIC16F628A

     
  • Morty

    Morty - 2020-05-01

    This error occurred.

    So I tried:
    PICAXE 20X2

    HI2cSetup I2CMASTER, $58, I2CFAST, I2CBYTE  'I2C comms to PIC16F628A
    

    16F628A

    #define I2C_ADDRESS 0x59
    

    This didn't work. But have I done the correct change? I thought it was to be a 7-bit address?

    Regards,
    Morty.

     
  • Anobium

    Anobium - 2020-05-01

    I am guessing. I guess the hi2cin (b21) is a read on 0x61.

    Can you write me a PICAXE program for an 18M2? I have an AXE091. I need a program that I test. I need to know what pins the 18M2 uses for SDA and SCL (I am not hunting for this info). I need to see what PICAXE actaully does on the I2C bus. I dont like flying in the blind.

     
  • Morty

    Morty - 2020-05-01

    The CODE for 18M2 is exactly the same for 08M2. Within PE6 they both PASS Syntax Check.

    Attached is the PINOUTS of a 18M2 if that helps.

    Regards,
    Morty.

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.