16F1847 Usart and I2C


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    20

    Default 16F1847 Usart and I2C

    Wonder if some one has used the 16F1847. I am trying to get the USART and I2C working at the same time. The code comes from 16F877a that I adapted which worked. But I can not get it to work on the 16F1847.....I have changed the header to reflect the new chip. Any ideas?

    define OSC 20
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_BAUD 9600 'Set Baud rate to 9600bps
    DEFINE HSER_BITS 8 'Set to 8 bit mode

    DEFINE HSER_CLROERR 1 'Clear overflow error automaticallyAFPCON1 = %00000001

    APFCON0 = %11110001 'Set RB pins to SCL1 and SDA 1
    APFCON1 = 1 ' TX on RB5
    B0 var Word ' Temp variable
    Heading var Word ' Compass heading
    B2 var byte ' Temp variable
    DPIN var portb.7 ' I2C data pin RB2 and RB4
    CPIN var Portb.6 ' I2C clock pin
    APFCON0 = %11110001 'Set RB pins to SCL1 and SDA 1
    APFCON1 = 1

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924

    Default Re: 16F1847 Usart and I2C

    Can you get either one of the functions working independently?
    Posting your code may also help.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Sep 2011
    Posts
    20

    Default Re: 16F1847 Usart and I2C

    Yep the TX works fine....tried putting the I2C connections on different pins, but no dice.

  4. #4
    Join Date
    Sep 2011
    Posts
    20

    Default Re: 16F1847 Usart and I2C

    Code as requested....basically it is generating GPS sentence, and using a I2C compass to populate the heading information in the sentence.....it works fine on the 16f877a. Got a problem with space on the gizmo I am building hence the move to a smaller chip.

    Include "modedefs.bas"
    define OSC 20
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_BAUD 9600 'Set Baud rate to 9600bps
    DEFINE HSER_BITS 8 'Set to 8 bit mode

    DEFINE HSER_CLROERR 1 'Clear overflow error automaticallyAFPCON1 = %00000001

    APFCON0 = %11110001 'Set RB pins to SCL1 and SDA 1
    APFCON1 = 1 ' TX on RB5
    B0 var Word ' Temp variable
    Heading var Word ' Compass heading
    B2 var byte ' Temp variable
    DPIN var portb.7 ' I2C data pin RB2 and RB4
    CPIN var Portb.6 ' I2C clock pin
    APFCON0 = %11110001 'Set RB pins to SCL1 and SDA 1
    APFCON1 = 1 ' TX on RB5
    p var byte

    Dec_lat1 var word
    Dec_lat2 var word
    S_byte var byte
    Dec_long1 var word
    Dec_long2 var word
    E_byte var byte
    variation var byte
    UTC var byte
    UTC_2 var byte
    Dec_off var byte
    Course var byte
    date_stamp var byte
    UTC_A var byte
    UTC_2_A var byte
    Off_set var byte
    Dec_long1_A var byte
    Dec_long2_A var byte
    Dec_lat1_A var byte
    Dec_lat2_A var byte
    TracID var word
    WPT_array var byte (63)
    WPT_array_A var byte (60)

    cksum var Byte
    cksum1 var byte
    ' "["
    WPT_array[0]="$"
    WPT_array[1]="G"
    WPT_array[2]="P"
    WPT_array[3]="R"
    WPT_array[4]="M"
    WPT_array[5]="C"
    WPT_array[6]=","
    WPT_array[7]=UTC DIG 5+"0"
    WPT_array[8]=UTC DIG 4+"0"
    WPT_array[9]=UTC DIG 3+"0"
    WPT_array[10]=UTC DIG 2+"0"
    WPT_array[11]=UTC DIG 1+"0"
    WPT_array[12]=UTC DIG 0+"0"
    WPT_array[13]="."
    WPT_array[14]=UTC_2 DIG 2+"0"
    WPT_array[15]=UTC_2 DIG 1+"0"
    WPT_array[16]=UTC_2 DIG 0+"0"
    WPT_array[17]=","
    WPT_array[18]="A"
    WPT_array[18]=","
    WPT_array[20]=Dec_long1 Dig 3+"0"
    WPT_array[21]=Dec_long1 Dig 2+"0"
    WPT_array[22]=Dec_long1 Dig 1+"0"
    WPT_array[23]=Dec_long1 Dig 0+"0"
    WPT_array[24]="."
    WPT_array[25]=Dec_long2 Dig 3+"0"
    WPT_array[26]=Dec_long2 Dig 2+"0"
    WPT_array[27]=Dec_long2 Dig 1+"0"
    WPT_array[28]=Dec_long2 Dig 0+"0"
    WPT_array[29]=","
    WPT_array[30]="N"
    WPT_array[31]=","
    WPT_array[32]=Dec_lat1 Dig 3+"0"
    WPT_array[33]=Dec_lat1 Dig 2+"0"
    WPT_array[34]=Dec_lat1 Dig 1+"0"
    WPT_array[35]=Dec_lat1 Dig 0+"0"
    WPT_array[36]="."
    WPT_array[37]=Dec_lat2 Dig 3+"0"
    WPT_array[38]=Dec_lat2 Dig 2+"0"
    WPT_array[39]=Dec_lat2 Dig 1+"0"
    WPT_array[40]=Dec_lat2 Dig 0+"0"
    WPT_array[41]=","
    WPT_array[42]="W"
    WPT_array[43]=","
    WPT_array[44]=Dec_off Dig 1+"0"
    WPT_array[45]=Dec_off Dig 0+"0"
    WPT_array[46]="."
    WPT_array[47]=Dec_off Dig 0+"0"
    WPT_array[48]=","
    WPT_array[49]= "0" + heading dig 2
    WPT_array[50]= "0" + heading dig 1
    WPT_array[51]= "0" + heading dig 0
    WPT_array[52]="."
    WPT_array[53]=Course Dig 0+"0"
    WPT_array[54]=","
    WPT_array[55]=date_stamp Dig 1+"0"
    WPT_array[56]=date_stamp Dig 0+"0"
    WPT_array[57]=","
    WPT_array[58]=variation Dig 1+"0"
    WPT_array[59]=variation Dig 0+"0"
    WPT_array[60]=","
    WPT_array[62]="E"
    WPT_array[63]="*"


    WPT_array_A[0]="$"
    WPT_array_A[1]="G"
    WPT_array_A[2]="P"
    WPT_array_A[3]="G"
    WPT_array_A[4]="G"
    WPT_array_A[5]="A"
    WPT_array_A[6]=","
    WPT_array_A[7]=UTC_A DIG 5+"0"
    WPT_array_A[8]=UTC_A DIG 4+"0"
    WPT_array_A[9]=UTC_A DIG 3+"0"
    WPT_array_A[10]=UTC_A DIG 2+"0"
    WPT_array_A[11]=UTC_A DIG 1+"0"
    WPT_array_A[12]=UTC_A DIG 0+"0"
    WPT_array_A[13]="."
    WPT_array_A[14]=UTC_2_A DIG 2+"0"
    WPT_array_A[15]=UTC_2_A DIG 1+"0"
    WPT_array_A[16]=UTC_2_A DIG 0+"0"
    WPT_array_A[17]=","
    WPT_array_A[18]=Dec_long1_A Dig 3+"0"
    WPT_array_A[19]=Dec_long1_A Dig 2+"0"
    WPT_array_A[20]=Dec_long1_A Dig 1+"0"
    WPT_array_A[21]=Dec_long1_A Dig 0+"0"
    WPT_array_A[22]="."
    WPT_array_A[23]=Dec_long2_A Dig 3+"0"
    WPT_array_A[24]=Dec_long2_A Dig 2+"0"
    WPT_array_A[25]=Dec_long2_A Dig 1+"0"
    WPT_array_A[26]=Dec_long2_A Dig 0+"0"
    WPT_array_A[27]=","
    WPT_array_A[28]="N"
    WPT_array_A[29]=","
    WPT_array_A[30]=Dec_lat1_A Dig 3+"0"
    WPT_array_A[31]=Dec_lat1_A Dig 2+"0"
    WPT_array_A[32]=Dec_lat1_A Dig 1+"0"
    WPT_array_A[33]=Dec_lat1_A Dig 0+"0"
    WPT_array_A[34]="."
    WPT_array_A[35]=Dec_lat2_A Dig 3+"0"
    WPT_array_A[36]=Dec_lat2_A Dig 2+"0"
    WPT_array_A[37]=Dec_lat2_A Dig 1+"0"
    WPT_array_A[38]=Dec_lat2_A Dig 0+"0"
    WPT_array_A[39]=","
    WPT_array_A[40]="W"
    WPT_array_A[41]=","
    WPT_array_A[42]="1"
    WPT_array_A[43]=","
    WPT_array_A[44]="0"
    WPT_array_A[45]="9"
    WPT_array_A[46]=","
    WPT_array_A[47]="1"
    WPT_array_A[48]=","
    WPT_array_A[49]=Off_set Dig 0+"0"
    WPT_array_A[50]=Off_set Dig 0+"0"
    WPT_array_A[51]=Off_set Dig 0+"0"
    WPT_array_A[52]=","
    WPT_array_A[53]="M"
    WPT_array_A[54]=","
    WPT_array_A[55]="0"
    WPT_array_A[56]=","
    WPT_array_A[57]="M"
    WPT_array_A[58]=","
    WPT_array_A[59]=","
    WPT_array_A[60]="*"

    pause 1000
    cksum=0
    p=0
    for p = 1 to 63
    cksum = cksum^(WPT_array[p])
    Next P
    p=0
    cksum1=0
    for p = 1 to 60
    cksum1 = cksum1^(WPT_array_A[p])
    Next P
    Main
    I2CREAD DPIN,CPIN,$C0,$C1,[B2] ' Read 2 locations in a row
    B0=B2*14
    Heading=B0/10
    WPT_array[49]= "0" + heading dig 2
    WPT_array[50]= "0" + heading dig 1
    WPT_array[51]= "0" + heading dig 0

    pause 100

    Hserout [Str WPT_array\63, HEX Cksum,13,1]
    pause 100

    hserout [Str WPT_array_A\60, HEX Cksum1,13,10]
    pause 100

    Goto Main
    End

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343

    Default Re: 16F1847 Usart and I2C

    Seahound,
    Take a look at the table (Table 1) on page 4 of the datasheet.
    From what I can see, you can move the RX pin for the USART from RB1 to RB2. The SDA1 for I2C can not be moved, and is on RB1. SCL1 for I2C is on RB4. And TX fro the USART can be moved between RB2 and RB5. So here is how it should fall out:
    I2C:
    RB1 SDA1
    RB4 SCL1
    USART
    RB2 RX
    RB5 TX

    So the APFCONx registers should be
    APFCON0 = %10000000
    APFCON1 = %00000001
    and the pin assignments:
    DPIN var portb.1 ' I2C data pin
    CPIN var Portb.4 ' I2C clock pin

    HTH
    Last edited by SteveB; - 17th December 2012 at 22:11.

  6. #6
    Join Date
    Sep 2011
    Posts
    20

    Default Re: 16F1847 Usart and I2C

    Thanks for the reply. Just tried it and it does not work. May have to try a short bit of code to see if I can display the heading on a LCD using the I2C routine with out all the other stuff. You helped the last time when I could not get the 16F877a working...

  7. #7
    Join Date
    Sep 2011
    Posts
    20

    Default Re: 16F1847 Usart and I2C

    Had to change to SW TX then it works....well sort of. The I2C reads the compass and the data is stored in the array and tehn transmitted....however the OSD compass heading displays garbage (643, 888, 127 random stuff).
    Changed the chip set up to

    Include "modedefs.bas"
    define OSC 20
    Define I2C_SLOW 1
    APFCON0 = %11100000 'Set RB pins to SCL1 and SDA 1
    APFCON1 = 1 ' TX on RB5
    CM1CON0 = 0 ' COMPARATORS OFF
    CM1CON1 =0
    CM2CON0 = 0 ' COMPARATORS OFF
    CM2CON1 =0
    CPSCON0 = 0 'CAPACTIVE SENSE MODULE OFF
    ANSELA = 0 'turn off analog port A
    ANSELB = 0 'turn off analog port B

    TRISA= %11111111
    TRISB= %11111111

Similar Threads

  1. Interfacing with Arduino I2C LCD
    By norohs in forum Documentation
    Replies: 47
    Last Post: - 30th May 2017, 19:53
  2. Problem with PICto PIC I2C MASTER-SLAVE
    By juanen19 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th June 2013, 03:58
  3. Strange I2C and serial behaviour.......
    By jimbostlawrence in forum General
    Replies: 4
    Last Post: - 30th October 2012, 02:02
  4. USART to PC
    By tonyfelloni in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 15th October 2008, 01:09
  5. Usart
    By lew247 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 3rd June 2008, 00:41

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts