1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| #include <p18f452.h>
#include <delays.h>
#include<stdio.h>
#include<i2c.h>
#include"lcdpd2p.h"
#pragma config WDT = OFF
int octetrecu1;
int octetrecu2;
int octetrecu3;
int octetrecu4;
int octetrecu5;
int octetrecu6;
int octetrecu7;
int octetrecu8;
/////////////////////////////////////////////////////////////
// PROTOTYPE //
/////////////////////////////////////////////////////////////
void ack(void);
void init_I2C(void);
void read_trameI2C(int *octet1, int *octet2, int *octet3, int *octet4,int *octet5, int *octet6, int *octet7, int *octet8);
void tempo(unsigned int count);
unsigned int SWReadI2C( void );
/////////////////////////////////////////////////////////////
// read_tram_i2c(); //
/////////////////////////////////////////////////////////////
void read_trameI2C(int *octet1, int *octet2, int *octet3, int *octet4,int *octet5, int *octet6, int *octet7, int *octet8)
{
*octet1 = ReadI2C();
SSPCON2bits.ACKEN = 1;
ack();
*octet2 = ReadI2C();
SSPCON2bits.ACKEN = 1;
ack();
*octet3 = ReadI2C();
SSPCON2bits.ACKEN = 1;
ack();
*octet4 = ReadI2C();
SSPCON2bits.ACKEN = 1;
ack();
*octet5 = ReadI2C();
SSPCON2bits.ACKEN = 1;
ack();
*octet6 = ReadI2C();
SSPCON2bits.ACKEN = 1;
ack();
*octet7 = ReadI2C();
SSPCON2bits.ACKEN = 1;
ack();
*octet8 = ReadI2C();
}
////////////////////////////////////////////////////////////////////////
// TEST ACK //
////////////////////////////////////////////////////////////////////////
void ack(void)
{
IdleI2C();
while (SSPCON2bits.ACKSTAT);
}
////////////////////////////////////////////////////////////////////////
// TEMPO //
////////////////////////////////////////////////////////////////////////
void tempo(unsigned int compte)
{
while (compte--);
}
/////////////////////////////////////////////////////////////
// INITIALISATION I2C //
/////////////////////////////////////////////////////////////
void init_I2C(void)
{
DDRCbits.RC3=1;
DDRCbits.RC4=1;
SSPCON1=0x28;
SSPSTATbits.SMP=1;
SSPADD=9;
}
/////////////////////////////////////////////////////////////
// Fonction Principale //
/////////////////////////////////////////////////////////////
void main(void)
{
int Valeur = 1;
InitLCD();
stdout = _H_USER;
init_I2C();
IdleI2C();
StartI2C();
while (SSPCON2bits.RSEN);
WriteI2C(0xD0);
ack();
WriteI2C(0x00);
ack();
WriteI2C(0x00);
ack();
WriteI2C(0x01);
ack();
WriteI2C(0x00);
ack();
WriteI2C(0x02);
ack();
WriteI2C(0x51);
ack();
WriteI2C(0x03);
ack();
WriteI2C(0x03);
ack();
WriteI2C(0x04);
ack();
WriteI2C(0x11);
ack();
WriteI2C(0x05);
ack();
WriteI2C(0x03);
ack();
WriteI2C(0x06);
ack();
WriteI2C(0x09);
ack();
WriteI2C(0x07);
ack();
WriteI2C(0x03);
ack();
StopI2C();
while (SSPCON2bits.PEN);
tempo(60000);
IdleI2C();
StartI2C();
while (SSPCON2bits.RSEN);
WriteI2C(0xD1);
ack();
////////////////////////////////////////////////////////////////////////
// lecture de la trame I2C //
////////////////////////////////////////////////////////////////////////
read_trameI2C(&octetrecu1, &octetrecu2, &octetrecu3, &octetrecu4, &octetrecu5, &octetrecu6, &octetrecu7, &octetrecu8);
////////////////////////////////////////////////////////////////////////
// CLOTURATION I2C //
////////////////////////////////////////////////////////////////////////
NotAckI2C(); // transmet l'acquittement du maître
while (SSPCON2bits.ACKEN); // attend fin de l'acquittement
StopI2C(); // initialise une stop-condition
while (SSPCON2bits.PEN); // attend fin de stop-condition
printf("=%X", octetrecu1);
printf("=%X", octetrecu2);
printf("=%X", octetrecu3);
printf("=%X", octetrecu4);
printf("=%X", octetrecu5);
Gotoxy(0,1);
printf("=%X", octetrecu6);
printf("=%X", octetrecu7);
printf("=%X", octetrecu8);
while(1); // boucle infinie
} |
Partager