Jens Altenburg
/
Fachbuch_Initialisierungen
Initialisation Nucleo-F446 by Jens Altenburg
i2c.cpp@0:9b6fbe273511, 2021-03-17 (annotated)
- Committer:
- prof_al
- Date:
- Wed Mar 17 13:55:03 2021 +0000
- Revision:
- 0:9b6fbe273511
Hardware Abstraction Layer (HAL) Nucleo-F446
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
prof_al | 0:9b6fbe273511 | 1 | #if 0 |
prof_al | 0:9b6fbe273511 | 2 | |
prof_al | 0:9b6fbe273511 | 3 | /*************************************************************************** |
prof_al | 0:9b6fbe273511 | 4 | * Titel : I2C.C |
prof_al | 0:9b6fbe273511 | 5 | * |
prof_al | 0:9b6fbe273511 | 6 | * Beschreibung : IIC - Interface |
prof_al | 0:9b6fbe273511 | 7 | * |
prof_al | 0:9b6fbe273511 | 8 | * Kategorie : |
prof_al | 0:9b6fbe273511 | 9 | * |
prof_al | 0:9b6fbe273511 | 10 | * Verantwortlich : Jens Altenburg |
prof_al | 0:9b6fbe273511 | 11 | * |
prof_al | 0:9b6fbe273511 | 12 | * Modulhistory : |
prof_al | 0:9b6fbe273511 | 13 | * |
prof_al | 0:9b6fbe273511 | 14 | ---------------------------------------------------------------------------- |
prof_al | 0:9b6fbe273511 | 15 | Date | Author | Change |
prof_al | 0:9b6fbe273511 | 16 | ---------------------------------------------------------------------------- |
prof_al | 0:9b6fbe273511 | 17 | 28.09.03 | J. Altenburg | Ersterstellung |
prof_al | 0:9b6fbe273511 | 18 | ---------------------------------------------------------------------------- |
prof_al | 0:9b6fbe273511 | 19 | |
prof_al | 0:9b6fbe273511 | 20 | ****************************************************************************/ |
prof_al | 0:9b6fbe273511 | 21 | |
prof_al | 0:9b6fbe273511 | 22 | /*************************************************************************** |
prof_al | 0:9b6fbe273511 | 23 | * HEADER-FILES (Only those that are needed in this file) |
prof_al | 0:9b6fbe273511 | 24 | ****************************************************************************/ |
prof_al | 0:9b6fbe273511 | 25 | /* Own headerfiles */ |
prof_al | 0:9b6fbe273511 | 26 | #include "i2c.h" /* */ |
prof_al | 0:9b6fbe273511 | 27 | |
prof_al | 0:9b6fbe273511 | 28 | |
prof_al | 0:9b6fbe273511 | 29 | |
prof_al | 0:9b6fbe273511 | 30 | /*************************************************************************** |
prof_al | 0:9b6fbe273511 | 31 | * FILE LOCAL DEFINITIONS |
prof_al | 0:9b6fbe273511 | 32 | * |
prof_al | 0:9b6fbe273511 | 33 | * In this section define |
prof_al | 0:9b6fbe273511 | 34 | * - all file local macros |
prof_al | 0:9b6fbe273511 | 35 | * - all file local define-constants |
prof_al | 0:9b6fbe273511 | 36 | * - all file local ROM-constants (static) |
prof_al | 0:9b6fbe273511 | 37 | * - all file local type definitions |
prof_al | 0:9b6fbe273511 | 38 | * - all file local variables (static) |
prof_al | 0:9b6fbe273511 | 39 | ****************************************************************************/ |
prof_al | 0:9b6fbe273511 | 40 | #define nShortDelay 20 /* Verzögerungszeit für I2C */ |
prof_al | 0:9b6fbe273511 | 41 | |
prof_al | 0:9b6fbe273511 | 42 | |
prof_al | 0:9b6fbe273511 | 43 | /*************************************************************************** |
prof_al | 0:9b6fbe273511 | 44 | * Variablendefinitionen |
prof_al | 0:9b6fbe273511 | 45 | ****************************************************************************/ |
prof_al | 0:9b6fbe273511 | 46 | |
prof_al | 0:9b6fbe273511 | 47 | |
prof_al | 0:9b6fbe273511 | 48 | /*************************************************************************** |
prof_al | 0:9b6fbe273511 | 49 | * Definition der einzelnen I2C-Befehle |
prof_al | 0:9b6fbe273511 | 50 | ****************************************************************************/ |
prof_al | 0:9b6fbe273511 | 51 | #define _0 0xb0 |
prof_al | 0:9b6fbe273511 | 52 | #define _1 0xb1 |
prof_al | 0:9b6fbe273511 | 53 | #define _2 0xb2 |
prof_al | 0:9b6fbe273511 | 54 | #define _3 0xb3 |
prof_al | 0:9b6fbe273511 | 55 | #define _4 0xb4 |
prof_al | 0:9b6fbe273511 | 56 | #define _5 0xb5 |
prof_al | 0:9b6fbe273511 | 57 | #define _6 0xb6 |
prof_al | 0:9b6fbe273511 | 58 | #define _7 0xb7 |
prof_al | 0:9b6fbe273511 | 59 | #define _8 0xb8 |
prof_al | 0:9b6fbe273511 | 60 | #define _9 0xb9 |
prof_al | 0:9b6fbe273511 | 61 | |
prof_al | 0:9b6fbe273511 | 62 | const byte abInit[] = {0x74, 0x00, 0x25, 0x06, 0x24, 0x0f, 0x84}; |
prof_al | 0:9b6fbe273511 | 63 | const byte abCls[] = {0x74, 0x00, 0x84}; |
prof_al | 0:9b6fbe273511 | 64 | const byte abText1[] = {0x74, 0x40, 0xe3, 0xa7, 0xf4, 0xad, 0xd2, 0xef, 0xe2}; |
prof_al | 0:9b6fbe273511 | 65 | const byte abZeile2[] = {0x74, 0x00, 0xc4}; |
prof_al | 0:9b6fbe273511 | 66 | const byte abNr0[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _0}; |
prof_al | 0:9b6fbe273511 | 67 | const byte abNr1[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _1}; |
prof_al | 0:9b6fbe273511 | 68 | const byte abNr2[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _2}; |
prof_al | 0:9b6fbe273511 | 69 | const byte abNr3[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _3}; |
prof_al | 0:9b6fbe273511 | 70 | const byte abNr4[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _4}; |
prof_al | 0:9b6fbe273511 | 71 | const byte abNr5[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _5}; |
prof_al | 0:9b6fbe273511 | 72 | const byte abNr6[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _6}; |
prof_al | 0:9b6fbe273511 | 73 | const byte abNr7[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _7}; |
prof_al | 0:9b6fbe273511 | 74 | const byte abNr8[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _8}; |
prof_al | 0:9b6fbe273511 | 75 | const byte abNr9[] = {0x74, 0x40, 0xd3, 0xf4, 0xe1, 0xf4, 0xe5, 0xa0, _9}; |
prof_al | 0:9b6fbe273511 | 76 | |
prof_al | 0:9b6fbe273511 | 77 | |
prof_al | 0:9b6fbe273511 | 78 | /***************************************************************************** |
prof_al | 0:9b6fbe273511 | 79 | Name: vStarti2c() |
prof_al | 0:9b6fbe273511 | 80 | Parameters: none |
prof_al | 0:9b6fbe273511 | 81 | Returns: nothing |
prof_al | 0:9b6fbe273511 | 82 | Description: Generiert das Startsignal für I2C-Bus |
prof_al | 0:9b6fbe273511 | 83 | |
prof_al | 0:9b6fbe273511 | 84 | ******************************************************************************/ |
prof_al | 0:9b6fbe273511 | 85 | void vStarti2c(void){ |
prof_al | 0:9b6fbe273511 | 86 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 87 | SDA(); /* data high */ |
prof_al | 0:9b6fbe273511 | 88 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 89 | SCL(); /* clk high */ |
prof_al | 0:9b6fbe273511 | 90 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 91 | _SDA(); /* data low */ |
prof_al | 0:9b6fbe273511 | 92 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 93 | _SCL(); /* clk low */ |
prof_al | 0:9b6fbe273511 | 94 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 95 | } |
prof_al | 0:9b6fbe273511 | 96 | |
prof_al | 0:9b6fbe273511 | 97 | /***************************************************************************** |
prof_al | 0:9b6fbe273511 | 98 | Name: vStopi2c() |
prof_al | 0:9b6fbe273511 | 99 | Parameters: none |
prof_al | 0:9b6fbe273511 | 100 | Returns: nothing |
prof_al | 0:9b6fbe273511 | 101 | Description: Generiert das Stopsignal für I2C-Bus |
prof_al | 0:9b6fbe273511 | 102 | |
prof_al | 0:9b6fbe273511 | 103 | ******************************************************************************/ |
prof_al | 0:9b6fbe273511 | 104 | void vStopi2c(void){ |
prof_al | 0:9b6fbe273511 | 105 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 106 | _SDA(); /* data low */ |
prof_al | 0:9b6fbe273511 | 107 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 108 | SCL(); /* clk high */ |
prof_al | 0:9b6fbe273511 | 109 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 110 | SDA(); /* data high */ |
prof_al | 0:9b6fbe273511 | 111 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 112 | } |
prof_al | 0:9b6fbe273511 | 113 | |
prof_al | 0:9b6fbe273511 | 114 | |
prof_al | 0:9b6fbe273511 | 115 | /***************************************************************************** |
prof_al | 0:9b6fbe273511 | 116 | Name: vWriteBi2c() |
prof_al | 0:9b6fbe273511 | 117 | Parameters: Byte |
prof_al | 0:9b6fbe273511 | 118 | Returns: nothing |
prof_al | 0:9b6fbe273511 | 119 | Description: Schreibt in Byte auf den I2C-Bus |
prof_al | 0:9b6fbe273511 | 120 | |
prof_al | 0:9b6fbe273511 | 121 | ******************************************************************************/ |
prof_al | 0:9b6fbe273511 | 122 | void vWriteBi2c(byte bData){ |
prof_al | 0:9b6fbe273511 | 123 | byte i = 0; |
prof_al | 0:9b6fbe273511 | 124 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 125 | _SCL(); |
prof_al | 0:9b6fbe273511 | 126 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 127 | for ( i = 0; i < 8; i++){ |
prof_al | 0:9b6fbe273511 | 128 | if ((bData & 0x80) == 0x80){ |
prof_al | 0:9b6fbe273511 | 129 | SDA(); /* Bit := 1 */ |
prof_al | 0:9b6fbe273511 | 130 | } |
prof_al | 0:9b6fbe273511 | 131 | else{ |
prof_al | 0:9b6fbe273511 | 132 | _SDA(); /* Bit := 0 */ |
prof_al | 0:9b6fbe273511 | 133 | } |
prof_al | 0:9b6fbe273511 | 134 | bData <<= 1; |
prof_al | 0:9b6fbe273511 | 135 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 136 | SCL(); |
prof_al | 0:9b6fbe273511 | 137 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 138 | _SCL(); |
prof_al | 0:9b6fbe273511 | 139 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 140 | } |
prof_al | 0:9b6fbe273511 | 141 | _SDA(); |
prof_al | 0:9b6fbe273511 | 142 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 143 | SCL(); |
prof_al | 0:9b6fbe273511 | 144 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 145 | _SCL(); |
prof_al | 0:9b6fbe273511 | 146 | vDelay(nShortDelay); |
prof_al | 0:9b6fbe273511 | 147 | } |
prof_al | 0:9b6fbe273511 | 148 | |
prof_al | 0:9b6fbe273511 | 149 | /***************************************************************************** |
prof_al | 0:9b6fbe273511 | 150 | Name: vDelay() |
prof_al | 0:9b6fbe273511 | 151 | Parameters: Byte |
prof_al | 0:9b6fbe273511 | 152 | Returns: nothing |
prof_al | 0:9b6fbe273511 | 153 | Description: Laufzeitschleife |
prof_al | 0:9b6fbe273511 | 154 | |
prof_al | 0:9b6fbe273511 | 155 | ******************************************************************************/ |
prof_al | 0:9b6fbe273511 | 156 | void vDelay(byte bTime){ |
prof_al | 0:9b6fbe273511 | 157 | while(bTime){ |
prof_al | 0:9b6fbe273511 | 158 | bTime--; |
prof_al | 0:9b6fbe273511 | 159 | } |
prof_al | 0:9b6fbe273511 | 160 | } |
prof_al | 0:9b6fbe273511 | 161 | |
prof_al | 0:9b6fbe273511 | 162 | /***************************************************************************** |
prof_al | 0:9b6fbe273511 | 163 | Name: vSendI2C() |
prof_al | 0:9b6fbe273511 | 164 | Parameters: Zeichenanzahl, String |
prof_al | 0:9b6fbe273511 | 165 | Returns: nothing |
prof_al | 0:9b6fbe273511 | 166 | Description: Schickt den String zum I2C Bus |
prof_al | 0:9b6fbe273511 | 167 | |
prof_al | 0:9b6fbe273511 | 168 | ******************************************************************************/ |
prof_al | 0:9b6fbe273511 | 169 | void vSendI2C(byte bLen, byte *pStr){ |
prof_al | 0:9b6fbe273511 | 170 | word w = 500; |
prof_al | 0:9b6fbe273511 | 171 | while(w--); |
prof_al | 0:9b6fbe273511 | 172 | vStarti2c(); |
prof_al | 0:9b6fbe273511 | 173 | while(bLen){ |
prof_al | 0:9b6fbe273511 | 174 | vWriteBi2c(*pStr); |
prof_al | 0:9b6fbe273511 | 175 | pStr++; |
prof_al | 0:9b6fbe273511 | 176 | bLen--; |
prof_al | 0:9b6fbe273511 | 177 | } |
prof_al | 0:9b6fbe273511 | 178 | vStopi2c(); |
prof_al | 0:9b6fbe273511 | 179 | } |
prof_al | 0:9b6fbe273511 | 180 | |
prof_al | 0:9b6fbe273511 | 181 | /*************************************************************************** |
prof_al | 0:9b6fbe273511 | 182 | * Kurzbeschreibung : OV6620 (OV6630) initialisieren |
prof_al | 0:9b6fbe273511 | 183 | * Uebergabeparameter : -- |
prof_al | 0:9b6fbe273511 | 184 | * Return Value : -- |
prof_al | 0:9b6fbe273511 | 185 | * Author : Jens Altenburg |
prof_al | 0:9b6fbe273511 | 186 | ****************************************************************************/ |
prof_al | 0:9b6fbe273511 | 187 | void I2C_vInit( void ){ |
prof_al | 0:9b6fbe273511 | 188 | vSendI2C(sizeof(abInit), (byte *) &abInit[0]); |
prof_al | 0:9b6fbe273511 | 189 | vSendI2C(sizeof(abCls), (byte *) &abCls[0]); |
prof_al | 0:9b6fbe273511 | 190 | vSendI2C(sizeof(abText1), (byte *) &abText1[0]); |
prof_al | 0:9b6fbe273511 | 191 | vSendI2C(sizeof(abZeile2), (byte *) &abZeile2[0]); |
prof_al | 0:9b6fbe273511 | 192 | vSendI2C(sizeof(abNr0), (byte *) &abNr0[0]); |
prof_al | 0:9b6fbe273511 | 193 | } |
prof_al | 0:9b6fbe273511 | 194 | |
prof_al | 0:9b6fbe273511 | 195 | void I2C_vState(byte bState){ |
prof_al | 0:9b6fbe273511 | 196 | vSendI2C(sizeof(abZeile2), (byte *) &abZeile2[0]); |
prof_al | 0:9b6fbe273511 | 197 | switch(bState){ |
prof_al | 0:9b6fbe273511 | 198 | case 0: |
prof_al | 0:9b6fbe273511 | 199 | vSendI2C(sizeof(abNr0), (byte *) &abNr0[0]); |
prof_al | 0:9b6fbe273511 | 200 | break; |
prof_al | 0:9b6fbe273511 | 201 | case 1: |
prof_al | 0:9b6fbe273511 | 202 | vSendI2C(sizeof(abNr1), (byte *) &abNr1[0]); |
prof_al | 0:9b6fbe273511 | 203 | break; |
prof_al | 0:9b6fbe273511 | 204 | case 2: |
prof_al | 0:9b6fbe273511 | 205 | vSendI2C(sizeof(abNr2), (byte *) &abNr2[0]); |
prof_al | 0:9b6fbe273511 | 206 | break; |
prof_al | 0:9b6fbe273511 | 207 | case 3: |
prof_al | 0:9b6fbe273511 | 208 | vSendI2C(sizeof(abNr3), (byte *) &abNr3[0]); |
prof_al | 0:9b6fbe273511 | 209 | break; |
prof_al | 0:9b6fbe273511 | 210 | case 4: |
prof_al | 0:9b6fbe273511 | 211 | vSendI2C(sizeof(abNr4), (byte *) &abNr4[0]); |
prof_al | 0:9b6fbe273511 | 212 | break; |
prof_al | 0:9b6fbe273511 | 213 | case 5: |
prof_al | 0:9b6fbe273511 | 214 | vSendI2C(sizeof(abNr5), (byte *) &abNr5[0]); |
prof_al | 0:9b6fbe273511 | 215 | break; |
prof_al | 0:9b6fbe273511 | 216 | case 6: |
prof_al | 0:9b6fbe273511 | 217 | vSendI2C(sizeof(abNr6), (byte *) &abNr6[0]); |
prof_al | 0:9b6fbe273511 | 218 | break; |
prof_al | 0:9b6fbe273511 | 219 | case 7: |
prof_al | 0:9b6fbe273511 | 220 | vSendI2C(sizeof(abNr7), (byte *) &abNr7[0]); |
prof_al | 0:9b6fbe273511 | 221 | break; |
prof_al | 0:9b6fbe273511 | 222 | case 8: |
prof_al | 0:9b6fbe273511 | 223 | vSendI2C(sizeof(abNr8), (byte *) &abNr8[0]); |
prof_al | 0:9b6fbe273511 | 224 | break; |
prof_al | 0:9b6fbe273511 | 225 | case 9: |
prof_al | 0:9b6fbe273511 | 226 | vSendI2C(sizeof(abNr9), (byte *) &abNr9[0]); |
prof_al | 0:9b6fbe273511 | 227 | break; |
prof_al | 0:9b6fbe273511 | 228 | } |
prof_al | 0:9b6fbe273511 | 229 | } |
prof_al | 0:9b6fbe273511 | 230 | /*************************************************************************** |
prof_al | 0:9b6fbe273511 | 231 | * EOF: SWMODxC1.C |
prof_al | 0:9b6fbe273511 | 232 | ****************************************************************************/ |
prof_al | 0:9b6fbe273511 | 233 | #endif |