I2C Slave PIC18 & XC8 Problem mit &-Operator?

Gast #3957559
Lesenswert?

Hallo

ich habe ein kleines Problem bei folgendem Code und ich verstehe nicht, 
warum. Der Code soll einen einfachen I2C-Slave darstellen, der einfach 
nur das zuletzt geschriebene Byte bei einem Lesezugriff widergibt. 
Funktioniert soweit, nur wenn ich
1
//                  SSPBUF = i2c_count;             // returns last written byte
2
                    SSPBUF = i2c_count & 0x0f;      // always returns 0?

schreibe, wird immer 0 zurückgegeben? Eigentlich müssten dann doch nur 
die unteren 4 Bits zurückgegeben werden? An welcher Stelle stehe ich auf 
dem Schlauch und sehe es nicht?

PIC18F46K80 & XC8 V1.34
1
#include "config.h"
2
#include <stdint.h>
3

4
#define I2C_ADDR    0x78                        // 8 bit address (0x3c)
5

6
volatile unsigned char    i2c_index;            // register address
7
volatile unsigned char    i2c_register[256];    // register memory
8
volatile unsigned char    i2c_count;            // byte counter for multi byte writes
9
volatile unsigned char    i2c_buf;              // temporary buffer for SSPBUF data
10

11
void main (void)
12
{
13
    ANCON0 = 0;                                 // disable analog inputs on port a
14
    ANCON1 = 0;                                 // disable analog inputs on port b
15
    TRISCbits.TRISC3 = 1;                       // configure rc3 for scl as input
16
    TRISCbits.TRISC4 = 1;                       // configure rc4 for sda as input
17
    TRISBbits.TRISB0 = 0;                       // configure b0 as led output
18
    SSPADD = I2C_ADDR;
19
    SSPCON1 = 0x36;                             // 7bit slave, SSPEN, CKP release
20
    SSPCON2 = 0x01;                             // SEN
21

22
//  SSPSTAT = 0x00;                             // clear ssp status
23

24
    LATBbits.LATB0 = 0;                         // led off
25

26
    PIR1bits.SSPIF = 0;                         // Clear MSSP interrupt request flag
27
    PIE1bits.SSPIE = 1;                         // Enable MSSP interrupt enable bit
28
    INTCONbits.GIE_GIEH  = 1;                   // GIE/GIEH: Global Interrupt Enable bit
29
    INTCONbits.PEIE_GIEL = 1;                   // PEIE/GIEL: Peripheral Interrupt Enable bit
30

31
    while(1)
32
    {
33
    }
34
}
35

36
void interrupt isr(void)
37
{
38
    if (PIR1bits.SSPIF) 
39
    {                                           // MSSP interrupt
40

41
        if (SSPSTATbits.BF)                     // Read the buffer use it later
42
            i2c_buf = SSPBUF;
43

44
        if (1)
45
        {
46
            if (!SSPSTATbits.R_NOT_W)               // Master write
47
            {
48
                if (!SSPSTATbits.D_NOT_A)           // Address
49
                {
50
                    
51
                } else                              // Data
52
                {
53
                    i2c_count = i2c_buf;
54
                }
55
            } else                                  // Master read
56
            {
57
                SSPCON1bits.WCOL = 0;
58
                do
59
                {
60
//                  SSPBUF = i2c_count;             // returns last written byte
61
                    SSPBUF = i2c_count & 0x0f;      // always returns 0?
62
                } while (SSPCON1bits.WCOL);         // Until collision bit cleared
63
            }
64
        }
65
        PIR1bits.SSPIF = 0;                     // Clear interrupt flag
66
        SSPCON1bits.CKP = 1;                    // Release SCL
67

68
    }
69
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren