Software I2C

Discussion in 'Programmer's Corner' started by harry99932, Mar 5, 2012.

  1. harry99932

    Thread Starter Member

    Dec 30, 2010
    38
    2
    Evening all, i wonder if anyone can help im having my first play with some I2C lcds for a new project and im struggling a bit. The I2C routines are in software (pic16f90) which ive never done before and ive not had the joy of using a serial lcd before either so im having lots of fun and games[​IMG]


    I found the original source of confusion after reading the data sheet for the 300th time lol! My code is below at its current state (please ignore the poor layout its been chopped and changed a lot trying to get this lcd working! Id appreciate anyone pointing out blindingly obvious errors as its my first I2c attempt im sure there will be plenty[​IMG]

    The programs run on a p16f690 using portc c pin 0 and 1 as clock and data respectivley. data to be transmitted is moved to file transmitme were subroutine transmit takes care of transmitting individual bits. As a side question is there anyway to slow down the program running in mplab so i can watch it execute step by step and see if it hangs up anywere?
    Thanks
    harry


    Code ( (Unknown Language)):
    1.  
    2. GENVAR UDATA
    3. count res 1
    4. transmitme res 1
    5. countd res 1
    6. count2 res 1
    7.  
    8. reset code 0x0000
    9. banksel MAIN
    10. goto MAIN
    11.  
    12. MAIN
    13. BANKSEL TRISC
    14. MOVLW B'00000011'
    15. MOVWF TRISC
    16. BANKSEL PORTC
    17. CLRF PORTC
    18. BANKSEL ANSEL
    19. movlw b'00000000'
    20. movwf ANSEL
    21.  
    22. CALL delay
    23. CALL start ;start bit ;
    24. CALL adres ;address + write bit;
    25. CALL delay1
    26. MOVLW B'00000000' ;control bit, only data to follow until stop bit;
    27. MOVWF transmitme
    28. MOVLW B'00001000'
    29. MOVWF count
    30. CALL transmit
    31.  
    32. MOVLW B'00111000' ;parameter
    33. MOVWF transmitme
    34. movlw B'00001000'
    35. movwf count
    36. CALL transmit
    37.  
    38. movlw b'00111001' ;parameter
    39. movwf transmitme
    40. movlw b'00001000'
    41. movwf count
    42. call transmit
    43.  
    44. movlw b'01010000' ;parameter
    45. movwf transmitme
    46. movlw b'00001000'
    47. movwf count
    48. call transmit
    49.  
    50. movlw b'00001100' ;parameter
    51. movwf transmitme
    52. movlw b'00001000'
    53. movwf count
    54. call transmit
    55.  
    56. movlw b'00001000' ;parameter
    57. movwf count
    58. movlw b'00010100'
    59. movwf transmitme
    60. call transmit
    61.  
    62. movlw b'00001000' ;parameter
    63. movwf count
    64. movlw b'01111001'
    65. movwf transmitme
    66. call transmit
    67.  
    68. movlw b'00001000' ; parameter
    69. movwf count
    70. movlw b'01101100'
    71. movwf transmitme
    72. call transmit
    73.  
    74. movlw b'00001000' ;parameter
    75. movwf count
    76. movlw b'00000001'
    77. movwf transmitme
    78. call transmit
    79.  
    80. call stop
    81. call delay1
    82. call start
    83. call adres
    84. movlw b'01000000'
    85. movwf transmitme
    86. movlw b'00001000'
    87. movwf count
    88. call transmit
    89.  
    90. capp movlw b'00000101' ;transmit screen data capital P
    91. movwf transmitme
    92. movlw b'00001000'
    93. movwf count
    94. call transmitme
    95. call stop
    96. nop
    97. goto $-1
    98.  
    99.  
    100. transmit
    101. RLF transmitme
    102. BTFSS STATUS,0
    103. GOTO zero
    104. GOTO one
    105. CALL delay1
    106. RETURN
    107.  
    108. zero
    109. CALL lowd
    110. CALL clkp
    111. DECFSZ count
    112. GOTO transmit
    113. return
    114.  
    115. one
    116. CALL highd
    117. CALL clkp
    118. DECFSZ count
    119. GOTO transmit
    120. return
    121. clkp
    122. BANKSEL PORTC
    123. BCF PORTC,0
    124. BANKSEL TRISC
    125. BCF TRISC,1
    126. BANKSEL delay
    127. CALL delay
    128. BANKSEL TRISC
    129. BSF TRISC,0
    130. BANKSEL delay
    131. CALL delay
    132. RETURN
    133.  
    134.  
    135.  
    136. lowd
    137. BANKSEL PORTC
    138. BCF PORTC,1
    139. BANKSEL TRISC
    140. BCF TRISC,1
    141. BANKSEL delay
    142. CALL delay
    143. RETURN
    144.  
    145. highd
    146. BANKSEL TRISC
    147. BSF TRISC,1
    148. BANKSEL delay
    149. CALL delay
    150. RETURN
    151.  
    152. lowc
    153. BANKSEL PORTC;
    154. BCF PORTC,0;
    155. BANKSEL TRISC;
    156. BCF TRISC,1;
    157. BANKSEL delay;
    158. CALL delay;
    159. RETURN;
    160.  
    161. highc
    162. BANKSEL TRISC
    163. BSF TRISC,0
    164. BANKSEL delay
    165. CALL delay
    166. RETURN
    167.  
    168. delay
    169. BANKSEL countd
    170. MOVLW B'00001100'
    171. MOVWF countd
    172. dly1 DECFSZ countd
    173. GOTO dly1
    174. RETURN
    175.  
    176. delay1
    177. banksel countd
    178. movlw b'11111111'
    179. movwf countd
    180. banksel count2
    181. movlw b'11111111'
    182. movwf count2
    183. dlylo decfsz countd
    184. goto dlylo
    185. decfsz count2
    186. goto dlylo
    187. return
    188.  
    189. adres
    190. CALL lowd
    191. CALL clkp
    192. CALL highd
    193. CALL clkp
    194. CALL highd
    195. CALL clkp
    196. CALL highd
    197. CALL clkp
    198. CALL highd
    199. CALL clkp
    200. CALL highd
    201. CALL clkp
    202. CALL lowd
    203. CALL clkp
    204. CALL lowd
    205. CALL clkp
    206. RETURN
    207.  
    208. start
    209. CALL highd
    210. CALL highc
    211. call delay
    212. CALL lowd
    213. call delay
    214. call lowc
    215. RETURN
    216.  
    217. stop
    218. call lowd
    219. call highc
    220. call delay
    221. call highd
    222. call delay
    223. call lowc
    224. RETURN
    225.  
     
    Last edited by a moderator: Mar 5, 2012
  2. Felo

    Member

    Feb 20, 2012
    91
    13
    Hello,

    Is there any reason to put yourself trough the i2c bit-banged misery when the 16f690 comes shipped with a ready to use, hardware SSP module?

    What frequency are you running your PIC?

    The Mplab debugger can be slowed down, but in my case it has never helped, try break-points instead and single stepping.
     
  3. Felo

    Member

    Feb 20, 2012
    91
    13
    Get a copy of PROTEUS for simulating your projects, it' a very good software and will help you a lot.
     
  4. harry99932

    Thread Starter Member

    Dec 30, 2010
    38
    2
    Hi felo, i was under the impression (just checked datasheet and it confirms) 16f690 on has I2C slave not master in hardware?!?! if theres and alternative to software id be gratefull haha im running the pic at 20MHZ. Ive had a play with mplab sim and found im getting stack overflow errors in 3 locations. There are no stack calls beyond 4 layers though so im just trying to find were the coding goes a bit awry now! il give proteus a go and see how i get on with that

    cheers mate
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    How about getting another chip that do implement master mode. The PIC
    PIC16F87X series do this. As long as your programmer support this you should be OK. It is a lot of stuff on Google. Just serch for MSSP and microchip
     
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    This is totally false. The PIC16F690 has a full SPI port, and I've used it in master mode. Can you please explain where you got this information from, giving a section number in the data sheet?
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The 16f690 do have something named "I2C Firmware Controlled Master mode" This not something I have not used my self. But I think it will be far more easy just using this. Instead of bit-banging
     
  8. harry99932

    Thread Starter Member

    Dec 30, 2010
    38
    2
    Towards the end of the spi section of the datasheet were it first mentions I2C states it has full hardware for slave I2C but not for master. Your correct in that it does have full hardware spi but not I2C. There is always the option of chaning mcu but if i can do it this way and learn why it wont work i wont be reliant on needing mcu's with hardware drivers in the future - Although i am running out of hair so it may be a neccessary option :D

    from data sheet;


    The SSP module in I​
    2C mode, fully implements all slave
    functions, except general call support, and provides
    interrupts on Start and Stop bits in hardware to facilitate
    firmware implementations of the master functions. The
    SSP module implements the Standard mode

    specifications, as well as 7-bit and 10-bit addressing.


     
  9. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    I'm sorry, somehow when you said I2C I read it as SPI. That was basically because I've used SPI a fair amount, and I'm just used to seeing I2C in the same chapter of the manual. But I wasn't totally wrong--there is I2C master mode available, but it says "Master mode of operation is supported in firmware using interrupt generation on the detection of the Start and Stop conditions." I'm not totally clear on what this means, and how much work you'd have to do to get a port running, but it's in section 13.13 of the manual. It seems as if you have to operate the SCL and SDA lines yourself (actually the TRIS bits) but interrupts will occur. I'm not eager to try this!
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Keep RTFM. Section 13.13 details I2C master operation.

    Note I2C is typically a PITA to implement no matter how you do it.
     
  11. harry99932

    Thread Starter Member

    Dec 30, 2010
    38
    2
    Hi all, thanks for the help so far always amazes me how much people will chip in to help strangers!

    After looooads of headbanging and rewriting ive cleared the original problems that were code related! the new clean code is below, it still doesnt work:D But it does debug properly so at least now i know the code is correct and its just the interfacing thats the problem! i think the first thing to play with is the delay lengths ive heard of people finding software I2C runs to fast for the slave to recognise it so il try slowing processor frequency and see what happens then! I suppose i should run through the lcd initialise parameters and check the cursor direction etc are correct aswell incase im erasing what im writing etc!


    Code ( (Unknown Language)):
    1.  
    2.  list p=16f690
    3.   include <p16f690.inc>
    4. GENVAR  UDATA
    5. count   res 1
    6. out  res 1
    7. rcount res 1
    8.  
    9. reset code 0x0000
    10.  banksel MAIN
    11.  goto MAIN
    12. MAIN
    13.   BANKSEL TRISC
    14.         MOVLW B'00000011'
    15.   MOVWF TRISC
    16.   BANKSEL PORTC
    17.    CLRF PORTC
    18.   BANKSEL ANSEL
    19.   movlw b'00000000'
    20.   movwf ANSEL
    21.  
    22.   CALL start
    23.   CALL address        ;address + write bit;
    24.   MOVLW B'00000000'       ;control bit, only data to follow until stop bit;
    25.   MOVWF out  
    26.   CALL transmit
    27.  
    28.   MOVLW B'00111000'       ;parameter
    29.   MOVWF out
    30.   CALL transmit
    31.    
    32.   movlw b'00111001'       ;parameter
    33.   movwf out
    34.   call transmit
    35.  
    36.   movlw b'01010000'       ;parameter
    37.   movwf out
    38.   call transmit
    39.  
    40.   movlw b'00001100'       ;parameter
    41.   movwf out
    42.   call transmit
    43.  
    44.   movlw b'00010100'
    45.   movwf out
    46.   call transmit
    47.  
    48.   movlw b'01111001'
    49.   movwf out
    50.   call transmit
    51.  
    52.   movlw b'01101100'
    53.   movwf out
    54.   call transmit
    55.  
    56.   movlw b'00000001'
    57.   movwf out
    58.   call transmit
    59.  
    60.   call stop
    61. cappp call start
    62.   call address
    63.   movlw b'01000000'
    64.   movwf out
    65.   call transmit
    66.  
    67. capp movlw b'00000101'       ;transmit screen data capital P
    68.   movwf out
    69.   Call transmit
    70.   movlw b'00000101'
    71.   movwf out
    72.   call transmit
    73.   call stop
    74.   goto cappp
    75.  
    76.  
    77. transmit    
    78.    movlw b'00001000'
    79.    movwf rcount
    80. sendout  rlf  out
    81.    btfss STATUS,0
    82.    goto zero
    83.    goto one
    84.    RETURN
    85.  
    86. one
    87.    banksel TRISC
    88.    bsf  TRISC,1  
    89.    MOVLW B'11111111'
    90.    MOVWF count
    91. DELAY3  DECFSZ count
    92.    GOTO DELAY3
    93.    DECFSZ rcount
    94.    GOTO sendout
    95.    RETURN
    96.  
    97. zero
    98.    banksel PORTC
    99.    BCF  PORTC,1
    100.    BANKSEL TRISC
    101.    BCF  TRISC,1
    102.    MOVLW B'11111111'
    103.    MOVWF count
    104. DELAY4  DECFSZ count
    105.    GOTO DELAY4
    106.    DECFSZ rcount
    107.    GOTO sendout
    108.    RETURN
    109. address
    110.    MOVLW B'01111100'
    111.    MOVWF out
    112.    CALL transmit
    113.    RETURN
    114. stop
    115.    BANKSEL PORTC
    116.    BCF  PORTC,1
    117.    BANKSEL TRISC
    118.    BCF  TRISC,1
    119.    BSF  TRISC,0
    120.    MOVLW B'11111111'
    121.    MOVWF count
    122. delay5  decfsz count
    123.    goto delay5
    124.    banksel TRISC
    125.    BSF  TRISC,1
    126.    MOVLW B'11111111'
    127.    MOVWF count
    128. delay6  decfsz count
    129.    goto delay6
    130.    banksel PORTC
    131.    BCF  PORTC,0
    132.    BANKSEL TRISC
    133.    BCF  TRISC,0
    134.    RETURN    
    135.      
    136. start
    137.    BANKSEL TRISC
    138.    BSF  TRISC,1
    139.    BSF  TRISC,0
    140.    MOVLW B'11111111'
    141.    MOVWF count
    142. delay7  decfsz count  
    143.    goto delay7
    144.    banksel PORTC
    145.    BCF  PORTC,1
    146.    BANKSEL TRISC
    147.    BCF  TRISC,1
    148.    MOVLW B'11111111'
    149.    MOVWF count
    150. delay8  decfsz count
    151.    goto delay8
    152.    banksel PORTC
    153.    BCF  PORTC,0
    154.    BANKSEL TRISC
    155.    BCF  TRISC,0
    156.    RETURN
    157.  
     
    Last edited by a moderator: Mar 8, 2012
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Here's an idea as it seems you don't have a 'scope handy to observe the signals.

    I2C has no lower speed limit, so you can clock these signals in as slow as you want. Slow enough to use a multimeter to check the signals.

    A very important piece of the signal is the ACK or NAK, the last bit of each piece. When you send the first group, the slave address, if the slave is there and you correctly send the address the slave will NAK, or pull the data line down for that bit.

    If your slave doesn't ACK the address then your have a problem to address.

    You could literally just stall the PIC for the last bit to check the data line.
     
  13. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,644
    759
  14. harry99932

    Thread Starter Member

    Dec 30, 2010
    38
    2
    Hi Ernie thats just what im doing at the moment, clocking the start and the address through slowly and checking them that way. Found huge problems in the data sheet, reset info was wrong, timing data was wrong and and sample coding was wrong:D hence a very 'friendly' call to the manufacturers:mad: now i have proper information im slowly getting there:D
     
  15. harry99932

    Thread Starter Member

    Dec 30, 2010
    38
    2
    P.s note to fello i got a trial of proteus and will be getting the full version awsome software! being able to test code in virtual replica of the real circuit is heaven!!!
     
Loading...