• AVR Freaks

Hot!PIC16F690 (SSP module) as I2C Master sender

Author
Graham_G
Starting Member
  • Total Posts : 31
  • Reward points : 0
  • Joined: 2007/09/09 15:47:17
  • Status: offline
2013/04/06 14:08:45 (permalink)
0

PIC16F690 (SSP module) as I2C Master sender

Hi all,
 
I have an old Philips I2C DTMF / single tone transmitter IC which, for reasons best kept to myself, I want to drive with a PIC16F690.
 
The Philips IC is theoretically very simple to use as it is an I2C receive-only device, and the commands consist of one address (+write bit) byte and one data (+ write bit) byte. That's it.
 
My expectation was that I could use the SSP module in I2C mode and, with appropriate polling of the correct flags, just hand the hardware two bytes to transmit to I2C for me one after the other by placing them in the SSPBUF register.
 
However, the more I read about it the more it seems that this just isn't going to happen, as it seems that the SSP interface on the 16F690 (as opposed to the MSSP peripheral on many other devices) doesn't really do hardware I2C, at least, not as I2C Master. The section of the 16F690 data sheet dealing with this is remarkably vague, stating only that the I2C pins will have to be 'set to inputs and outputs as appropriate'. This sounds awfully like 'Sorry, but if you want to be an I2C master on this chip you're going to have to bit-bang it'.
 
I've seen a text which describes bidirectional control of the I2C pins by first writing a '0' to the two output port latch bits concerned, and then using the associated TRIS bits to switch the pins between input and output mode. When combined with pullup resistors to +V, this method means that the SDA / SCL lines are only ever pulled LOW by the output when in output mode, and are pulled high by the pullup resistor when you release the line by switching the pin back to input mode. This gives a nice simulation of an open-collector or open drain line driver, and is exactly what I suppose I would have to do if I really was going to bit-bang the I2C to the PCD3312.
 
Would someone be kind enough to walk me through the process of transmitting two consecutive I2C bytes, using as much hardware assistance as the SSP module actually does provide?
 
Pseudocode or plain English would be fine, but it would help me if the names of any flags or registers mentioned were those used by Hitech 'C' - if not, use the Microchip flag / register names and I will cross-convert them by looking through the appropriate Hitech 'C' device header file.
post edited by Graham_G - 2013/04/07 13:25:51
#1

15 Replies Related Threads

    markp
    Super Member
    • Total Posts : 397
    • Reward points : 0
    • Joined: 2006/01/26 13:54:56
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2013/04/07 03:53:09 (permalink)
    +2 (1)
    I agree, this looks like the original intention was to have master but it didn't work and the functionality was removed!
    It looks like you're going to have to bit-bang. I've attached some simple routines you might find useful, these have been used many times. You'll need to modify the #defines at the top for the PIC TRIS and PORT registers.
    Mark.
    #2
    Graham_G
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2007/09/09 15:47:17
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2013/04/07 13:13:53 (permalink)
    0
    markp

     I've attached some simple routines you might find useful, these have been used many times. You'll need to modify the #defines at the top for the PIC TRIS and PORT registers.
     
    Mark.
     

     
    Mark,
     
    Thanks very much indeed for sharing this code. I've had a quick look at it and although it is obviously for AVR (which I have never used) I understand it well enough to convert it to Hitech C for PIC, create my own delay routine for the half clock delay and so on, so very much appreciated.
     
    As far as I know, this deficiency in the SSP peripheral seems to have been corrected in the MSSP peripheral used in devices like the PIC16F88x (ie, PIC16F887) series, but as luck would have it, my device is one of those with the SSP peripheral.
     
    #3
    markp
    Super Member
    • Total Posts : 397
    • Reward points : 0
    • Joined: 2006/01/26 13:54:56
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2013/04/08 11:49:30 (permalink)
    0
    You're welcome, let us know how you get on with them!
    Mark.
    #4
    Graham_G
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2007/09/09 15:47:17
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2013/04/08 13:06:28 (permalink)
    0
    markp

    You're welcome, let us know how you get on with them!
    Mark.

     
    I had a little bit of spare time at work and managed to make the necessary modifications to make it work on PIC16F690 / Hitech C, and was able to take control of the aforementioned PCD3312 chip, thank you.
     
    I tried cutting the modified code from MPLAB's editor and pasting it here in a code box for the benefit of others with the same chip / compiler but it looked disastrous in the preview so I cancelled that - unfortunately I have never set up online file storage anywhere so I can't park it somewhere and link to it either.
     
    Ah- just spotted an 'attach' button in the message editor - I'll try that.

    If it worked, it is the .c file as saved by MPLAB, and is probably best viewed in that editor.  
    post edited by Graham_G - 2013/04/08 13:30:32
    #5
    markp
    Super Member
    • Total Posts : 397
    • Reward points : 0
    • Joined: 2006/01/26 13:54:56
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2013/04/08 13:29:08 (permalink)
    0
    Glad you got it working!
    Mark.
    #6
    WaltR
    Super Member
    • Total Posts : 3812
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2013/04/08 17:23:28 (permalink)
    0
    Thanks for posting the code. I saved it in my example folder for later reference.
     
    The PIC16F87 has a similar limitation, Slave mode but not master I2C.
    So far I've only need that PIC to be an I2C slave. If I need it to be a master your code will come in handy.
    #7
    ceteras
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2009/03/29 01:29:15
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2014/01/25 06:13:04 (permalink)
    0
    Thank you markp and Graham_G for the code, it allowed me to make faster progress with a project!
    I have one issue with it, though: if I need to toggle PORTBbits.RB5, it stops working.
    I use PIC16F690.
    I have a while loop that sends continuously I2C commands to a device. It works as long as I keep PORTB=0.
    If I touch RB5 (which should be independently controllable), it all hangs.
    My TRISB value is 0b01010000, so RB5 should be an output.
    If I run PORTBbits.RB5=1;, the write_i2c_device() function hangs.
    I don't have any debug tools unfortunately, only a pickit2 clone programmer.
    Any hints would be much appreciated, thanks!
    #8
    WaltR
    Super Member
    • Total Posts : 3812
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2014/01/25 06:42:55 (permalink)
    +2 (1)
    Sounds like the R-M-W issue from using PORTBbits.RB5=1
    Search and read about R-M-W in the data sheet, forum and web.
    I always use a shadow register in PICs without LATx register to avoid the R-M-W issue.
    #9
    NKurzman
    A Guy on the Net
    • Total Posts : 18858
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re:PIC16F690 (SSP module) as I2C Master sender 2014/01/25 13:07:05 (permalink)
    +2 (1)
    It is R-M-W  in fact this thy of use is a great way to show it.
    Use a Schadow register, or set the pin back each time the port is used.
    #10
    ceteras
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2009/03/29 01:29:15
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2014/01/26 06:13:33 (permalink)
    0
    It makes sense, thank you!
    When RB5 gets set, RB4 and RB6 are inputs, with pull-up resistors.
    So writing bit RB5, at first the whole port is read, RB4 and 6 read as 1's, then they are written back as 1 when I need them to stay zeros.
    (so that other noobs like me reading this can understand).
    That is why PORTBbits.RB5=1 also sets RB4 and RB6 as 1. If I do PORTB=0b00100000, this doesn't happen.
    The strange thing is that when I want to clear RB5, PORTBbits.RB5=0 doesn't seem to cause the same problem (and it happens in the same place in code, it's a branch). I'll need to double check on that.
    I've totally forgot about the shadow register method (that is, having a variable to work with, representing the port B, and write that variable to the port when bits need to be changed on it).
    Thank you!
     
    #11
    faruk97
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2020/06/11 15:23:49
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2020/07/10 15:18:03 (permalink)
    0
    Hello to everyone, i have a ask. i would like make a master sender. But there are some problem.  
     
    i can did not ssp module as master sender. May you help? Thanks you.
     
    signed char Write(unsigned char veri){
        //Bekle();
        
        //char data,j;
        //while(PIR1bits.SSPIF);
       // data=veri;
        char i,temp;
        
     veri1=veri;
        TRISBbits.TRISB4=1;
        TRISBbits.TRISB6=1;
        SSPBUF=veri;
        //while(PIR1bits.SSPIF);
        TRISBbits.TRISB4=0;
        TRISBbits.TRISB6=0;
        //while(SSPSTATbits.BF);
        SSPCONbits.CKP=1;
        PIR1bits.SSPIF=0;
        return(CKP);
        }

     
     
    post edited by faruk97 - 2020/07/10 15:27:05
    #12
    faruk97
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2020/06/11 15:23:49
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2020/07/10 15:25:24 (permalink)
    0
    How can i achieve this?:
    "In Master mode, the SCL and SDA lines are manipulated by clearing the corresponding TRISB<6,4> bit(s). The output level is always low, irrespective of the value(s) in PORTB<6,4>. So when transmitting data, a ‘1’ data bit must have the TRISB<4> bit set (input) and a ‘0’ data bit must have the TRISB<4> bit cleared (output). The same scenario is true for the SCL line with the TRISB<6> bit. Pull-up resistors must be provided externally to the SCL and SDA pins for proper operation of the I2C module."
    #13
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re:PIC16F690 (SSP module) as I2C Master sender 2020/07/10 16:15:37 (permalink)
    +1 (1)
    faruk97
    Hello to everyone, i have a ask. i would like make a master sender. But there are some problem.  
     
    i can did not ssp module as master sender. May you help? Thanks you.
     

    Did you read the rest of the posts in this thread?
    The PIC16F690 SSP peripheral does NOT include Master functionality. The only way to do it is via "bit banging" which means you do it all yourself by manually twiddling the SDA and SCL pins.
    This is further complicated by RMW (read-modify-write) problems on GPIO ports in these old PICs.
    You would make your life MUCH easier if you use a newer PIC with an MSSP peripheral.
    (The "M" is the secret ingredient!)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #14
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2020/07/10 16:39:20 (permalink)
    -1 (3)
    15 Years is A Long Long Time Ago in Silicon History.
    (Hidden Stars Wars ref.)
     
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #15
    faruk17
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2020/05/31 06:12:27
    • Location: 0
    • Status: offline
    Re:PIC16F690 (SSP module) as I2C Master sender 2020/07/10 17:24:01 (permalink)
    0
    Thanks Ric. I understood the subject.
    #16
    Jump to:
    © 2020 APG vNext Commercial Version 4.5