Forum: Mikrocontroller und Digitale Elektronik PIC18 als I2C Slave


von Dernsteiner (Gast)


Lesenswert?

Hallo Zusammen,

Ich wollte zwischen zwei Pic18F46k42 über I2C 8Bit werte austauschen, 
jedoch bekomme ich keinen Interrupt am Slave. Die Adresse ist auf dem 
Bus, hab ich gemessen. Konfiguriert hab ichs mit dem MCC.

Mastercode:
1
#include "mcc_generated_files/mcc.h"
2
#include "mcc_generated_files/examples/i2c1_master_example.h"
3
4
/*
5
                         Main application
6
 */
7
void main(void)
8
{
9
    // Initialize the device
10
    SYSTEM_Initialize();
11
12
    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
13
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
14
    // Use the following macros to:
15
16
    // Enable the Global Interrupts
17
    //INTERRUPT_GlobalInterruptEnable();
18
19
    // Disable the Global Interrupts
20
    //INTERRUPT_GlobalInterruptDisable();
21
    
22
    while (1)
23
    {
24
        I2C1_Write1ByteRegister(8, 0, 1);
25
    }
26
}

Slavecode:
1
#include "mcc_generated_files/mcc.h"
2
3
/*
4
                         Main application
5
 */
6
void I2C_Int(void);
7
8
void main(void)
9
{
10
    // Initialize the device
11
    SYSTEM_Initialize();
12
    
13
    FET_LAT = 0;
14
    
15
    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
16
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
17
    // Use the following macros to:
18
19
    // Enable the Global Interrupts
20
    INTERRUPT_GlobalInterruptEnable();
21
    
22
    // Disable the Global Interrupts
23
    //INTERRUPT_GlobalInterruptDisable();
24
    
25
    I2C1_SlaveSetAddrIntHandler(I2C_Int);
26
    I2C1_SlaveSetIsrHandler(I2C_Int);
27
    
28
   
29
    I2C1_Enable();
30
    I2C1_Open();
31
    
32
    while (1)
33
    {
34
        I2C1_Read();
35
    }
36
}
37
38
void I2C_Int(void)
39
{
40
    FET_LAT = 1;
41
}

von Hans (Gast)


Lesenswert?

Peripherie Interrupt enabled?

von DerGast (Gast)


Lesenswert?

Hans schrieb:
> Peripherie Interrupt enabled?

Diese aktivieren sich doch mit den Global Interrupts? Oder wo müssten 
die sein?

von Franko P. (sgssn)


Lesenswert?

Hi
das ist der Nachteil von MCC. Hast du in MCC angegeben, für I2C 
Interrupts zu verwenden? In den MCC_generated_files müsste ja dein Setup 
stehen. Das Register das du brauchst ist I2C1PIE. Siehe TABLE 33-18 im 
datasheet.
Ausserdem PIE3 und INTCON0

Gerhard

: Bearbeitet durch User
von DerGast (Gast)


Lesenswert?

Franko P. schrieb:
> Das Register das du brauchst ist I2C1PIE. Siehe TABLE 33-18 im
> datasheet.
> Ausserdem PIE3 und INTCON0

Vielen Dank dafür, jetzt klappt wenigstens erstmal der Interrupt, wie 
kriege ich jedoch nun die Daten raus? Ich habe jetzt den Code so:

1
#include "mcc_generated_files/mcc.h"
2
3
/*
4
                         Main application
5
 */
6
#define SLAVEADDRESS        0x00
7
#define SLAVEREGISTER     0x00
8
#define TEMPADDRESSPELT     0b1001000
9
#define TEMPADDRESSINTERN   0b1001000
10
#define TEMPREG             0x00
11
12
void setFanSpeed(uint16_t temperature);
13
uint8_t getSetTemperature();
14
void I2C_Read_Int(void);
15
void I2C_Write_Int(void);
16
17
uint8_t masterData = 0;
18
19
void main(void)
20
{
21
    // Initialize the device
22
    SYSTEM_Initialize();
23
    
24
    //I2C Init
25
    I2C1PIEbits.ADRIE = 1;
26
    I2C1PIEbits.WRIE = 1;
27
    I2C1PIEbits.RSCIE = 1;
28
    PIE3 = 1;
29
    I2C1_Open();
30
    I2C1_SlaveSetWriteIntHandler(I2C_Write_Int);
31
    I2C1_SlaveSetReadIntHandler(I2C_Read_Int);
32
    
33
    PELTIER_LAT = 1; //Turn off Mosfet
34
    
35
    // Enable the Global Interrupts
36
    INTERRUPT_GlobalInterruptEnable();
37
    
38
    
39
    //Variables
40
    uint8_t temperaturePeltier = 0;
41
    uint8_t temperatureIntern = 0;
42
    uint8_t setTemp = 0;
43
44
    while (1)
45
    {
46
//        temperaturePeltier = I2C1_Read1ByteRegister(TEMPADDRESSPELT, TEMPREG);
47
//        setTemp = I2C1_Read1ByteRegister(TEMPADDRESSPELT, TEMPREG);
48
//        setFanSpeed(setTemp);
49
//        setTemp = getSetTemperature();
50
    }
51
}
52
53
void setFanSpeed(uint16_t temperature)
54
{
55
    uint16_t multiplicator = 10;//temperature/12;
56
    uint16_t dutyCycle = (temperature * multiplicator); //+ (temperature+5);
57
    PWM6_LoadDutyValue(dutyCycle);
58
}
59
60
uint8_t getSetTemperature()
61
{
62
//    uint8_t setTemp = I2C1_Read1ByteRegister(8,2);
63
//    return setTemp;
64
}
65
66
void displaySetTemperature()
67
{
68
    
69
}
70
71
void I2C_Read_Int(void)
72
{
73
    masterData = I2C1_Read();
74
    if(masterData >= 1)
75
    {
76
        PELTIER_LAT = 0;
77
    }
78
}
79
80
void I2C_Write_Int(void)
81
{
82
    masterData = I2C1_Read();
83
    if(masterData >= 1)
84
    {
85
        PELTIER_LAT = 0;
86
    }
87
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.