Test_outputs.ino 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //test ports pour platine HVSP
  2. //test des entrées/sortie et I2C_Scanner
  3. //Oled Screen 0x3C
  4. //Ram 0x50
  5. #include <avr/io.h> // for GPIO
  6. #include <avr/pgmspace.h> // to store data in programm memory
  7. #include <util/delay.h> // for delays
  8. #include <Wire.h>
  9. /* Rappel connections :
  10. #define RST_PIN PB5 //13 // 12V !RESET Pin 1 of target device
  11. #define SCI_PIN PB4 //12 // Serial Clock Input (SCI) Pin 2 of target device
  12. #define SDO_PIN PB3 //11 // Serial Data Output (SDO) Pin 7 of target device
  13. #define SII_PIN PB2 //10 // Serial Instruction Input (SII) Pin 6 of target device
  14. #define SDI_PIN PB1 //9 // Serial Data Input (SDI) Pin 5 of target device
  15. #define VCC_PIN PB0 //8 // Target VCC Pin 8 of target device
  16. #define I2C_SCL PC5 // I2C Serial Clock (SCK)
  17. #define I2C_SDA PC4 // I2C Serial Data (SDA)
  18. #define BUTTON PD7 // OK-Button
  19. */
  20. int boucle;
  21. #define PIN PB0
  22. #define I2C_SCL PC5 // I2C Serial Clock (SCK)
  23. #define I2C_SDA PC4 // I2C Serial Data (SDA)
  24. // ===================================================================================
  25. // I2C Implementation
  26. // ===================================================================================
  27. // I2C macros
  28. #define I2C_SDA_HIGH() DDRC &= ~(1<<I2C_SDA) // release SDA -> pulled HIGH by resistor
  29. #define I2C_SDA_LOW() DDRC |= (1<<I2C_SDA) // SDA as output -> pulled LOW by MCU
  30. #define I2C_SCL_HIGH() DDRC &= ~(1<<I2C_SCL) // release SCL -> pulled HIGH by resistor
  31. #define I2C_SCL_LOW() DDRC |= (1<<I2C_SCL) // SCL as output -> pulled LOW by MCU
  32. #define I2C_DELAY() asm("lpm") // delay 3 clock cycles
  33. #define I2C_CLOCKOUT() I2C_SCL_HIGH();I2C_DELAY();I2C_SCL_LOW() // clock out
  34. // I2C init function
  35. void I2C_init(void) {
  36. DDRC &= ~((1<<I2C_SDA)|(1<<I2C_SCL)); // pins as input (HIGH-Z) -> lines released
  37. PORTC &= ~((1<<I2C_SDA)|(1<<I2C_SCL)); // should be LOW when as ouput
  38. }
  39. // I2C transmit one data byte to the slave, ignore ACK bit, no clock stretching allowed
  40. void I2C_write(uint8_t data) {
  41. for(uint8_t i = 8; i; i--, data<<=1) { // transmit 8 bits, MSB first
  42. (data & 0x80) ? (I2C_SDA_HIGH()) : (I2C_SDA_LOW()); // SDA HIGH if bit is 1
  43. I2C_CLOCKOUT(); // clock out -> slave reads the bit
  44. }
  45. I2C_DELAY(); // delay 3 clock cycles
  46. I2C_SDA_HIGH(); // release SDA for ACK bit of slave
  47. I2C_CLOCKOUT(); // 9th clock pulse is for the ignored ACK bit
  48. }
  49. // I2C start transmission
  50. void I2C_start(uint8_t addr) {
  51. I2C_SDA_LOW(); // start condition: SDA goes LOW first
  52. I2C_SCL_LOW(); // start condition: SCL goes LOW second
  53. I2C_write(addr); // send slave address
  54. }
  55. // I2C stop transmission
  56. void I2C_stop(void) {
  57. I2C_SDA_LOW(); // prepare SDA for LOW to HIGH transition
  58. I2C_SCL_HIGH(); // stop condition: SCL goes HIGH first
  59. I2C_SDA_HIGH(); // stop condition: SDA goes HIGH second
  60. }
  61. // ===================================================================================
  62. // OLED Implementation
  63. // ===================================================================================
  64. // OLED definitions
  65. #define OLED_ADDR 0x78 // OLED write address
  66. #define OLED_CMD_MODE 0x00 // set command mode
  67. #define OLED_DAT_MODE 0x40 // set data mode
  68. #define OLED_INIT_LEN 9 // length of init command array
  69. // OLED 5x8 pixels character set
  70. const uint8_t OLED_FONT[] PROGMEM = {
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x07, 0x00, 0x07, 0x00,
  72. 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x23, 0x13, 0x08, 0x64, 0x62,
  73. 0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x1C, 0x22, 0x41, 0x00,
  74. 0x00, 0x41, 0x22, 0x1C, 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14, 0x08, 0x08, 0x3E, 0x08, 0x08,
  75. 0x00, 0x00, 0xA0, 0x60, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x60, 0x60, 0x00, 0x00,
  76. 0x20, 0x10, 0x08, 0x04, 0x02, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00, 0x42, 0x7F, 0x40, 0x00,
  77. 0x42, 0x61, 0x51, 0x49, 0x46, 0x21, 0x41, 0x45, 0x4B, 0x31, 0x18, 0x14, 0x12, 0x7F, 0x10,
  78. 0x27, 0x45, 0x45, 0x45, 0x39, 0x3C, 0x4A, 0x49, 0x49, 0x30, 0x01, 0x71, 0x09, 0x05, 0x03,
  79. 0x36, 0x49, 0x49, 0x49, 0x36, 0x06, 0x49, 0x49, 0x29, 0x1E, 0x00, 0x36, 0x36, 0x00, 0x00,
  80. 0x00, 0x56, 0x36, 0x00, 0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x14, 0x14, 0x14, 0x14, 0x14,
  81. 0x00, 0x41, 0x22, 0x14, 0x08, 0x02, 0x01, 0x51, 0x09, 0x06, 0x32, 0x49, 0x59, 0x51, 0x3E,
  82. 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x3E, 0x41, 0x41, 0x41, 0x22,
  83. 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x7F, 0x09, 0x09, 0x09, 0x01,
  84. 0x3E, 0x41, 0x49, 0x49, 0x7A, 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00, 0x41, 0x7F, 0x41, 0x00,
  85. 0x20, 0x40, 0x41, 0x3F, 0x01, 0x7F, 0x08, 0x14, 0x22, 0x41, 0x7F, 0x40, 0x40, 0x40, 0x40,
  86. 0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x3E, 0x41, 0x41, 0x41, 0x3E,
  87. 0x7F, 0x09, 0x09, 0x09, 0x06, 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x7F, 0x09, 0x19, 0x29, 0x46,
  88. 0x46, 0x49, 0x49, 0x49, 0x31, 0x01, 0x01, 0x7F, 0x01, 0x01, 0x3F, 0x40, 0x40, 0x40, 0x3F,
  89. 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x63, 0x14, 0x08, 0x14, 0x63,
  90. 0x07, 0x08, 0x70, 0x08, 0x07, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00, 0x7F, 0x41, 0x41, 0x00,
  91. 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x41, 0x41, 0x7F, 0x00, 0x04, 0x02, 0x01, 0x02, 0x04,
  92. 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x01, 0x02, 0x04, 0x00, 0x20, 0x54, 0x54, 0x54, 0x78,
  93. 0x7F, 0x48, 0x44, 0x44, 0x38, 0x38, 0x44, 0x44, 0x44, 0x20, 0x38, 0x44, 0x44, 0x48, 0x7F,
  94. 0x38, 0x54, 0x54, 0x54, 0x18, 0x08, 0x7E, 0x09, 0x01, 0x02, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,
  95. 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00, 0x44, 0x7D, 0x40, 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,
  96. 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,
  97. 0x7C, 0x08, 0x04, 0x04, 0x78, 0x38, 0x44, 0x44, 0x44, 0x38, 0xFC, 0x24, 0x24, 0x24, 0x18,
  98. 0x18, 0x24, 0x24, 0x18, 0xFC, 0x7C, 0x08, 0x04, 0x04, 0x08, 0x48, 0x54, 0x54, 0x54, 0x20,
  99. 0x04, 0x3F, 0x44, 0x40, 0x20, 0x3C, 0x40, 0x40, 0x20, 0x7C, 0x1C, 0x20, 0x40, 0x20, 0x1C,
  100. 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x44, 0x28, 0x10, 0x28, 0x44, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,
  101. 0x44, 0x64, 0x54, 0x4C, 0x44, 0x08, 0x36, 0x41, 0x41, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00,
  102. 0x00, 0x41, 0x41, 0x36, 0x08, 0x08, 0x04, 0x08, 0x10, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  103. };
  104. // OLED init settings
  105. const uint8_t OLED_INIT_CMD[] PROGMEM = {
  106. 0xC8, 0xA1, // flip screen
  107. 0xA8, 0x1F, // set multiplex ratio
  108. 0xDA, 0x02, // set com pins hardware configuration
  109. 0x8D, 0x14, // set DC-DC enable
  110. 0xAF // display on
  111. };
  112. // OLED variables
  113. uint8_t OLED_x, OLED_y; // current cursor position
  114. // OLED init function
  115. void OLED_init(void) {
  116. I2C_init(); // initialize I2C first
  117. I2C_start(OLED_ADDR); // start transmission to OLED
  118. I2C_write(OLED_CMD_MODE); // set command mode
  119. for(uint8_t i = 0; i < OLED_INIT_LEN; i++)
  120. I2C_write(pgm_read_byte(&OLED_INIT_CMD[i])); // send the command bytes
  121. I2C_stop(); // stop transmission
  122. }
  123. // OLED set the cursor
  124. void OLED_setCursor(uint8_t xpos, uint8_t ypos) {
  125. I2C_start(OLED_ADDR); // start transmission to OLED
  126. I2C_write(OLED_CMD_MODE); // set command mode
  127. I2C_write(xpos & 0x0F); // set low nibble of start column
  128. I2C_write(0x10 | (xpos >> 4)); // set high nibble of start column
  129. I2C_write(0xB0 | (ypos & 0x07)); // set start page
  130. I2C_stop(); // stop transmission
  131. OLED_x = xpos; OLED_y = ypos; // set the cursor variables
  132. }
  133. // OLED clear line
  134. void OLED_clearLine(uint8_t line) {
  135. OLED_setCursor(0, line); // set cursor to line start
  136. I2C_start(OLED_ADDR); // start transmission to OLED
  137. I2C_write(OLED_DAT_MODE); // set data mode
  138. for(uint8_t i=128; i; i--) I2C_write(0x00); // clear the line
  139. I2C_stop(); // stop transmission
  140. }
  141. // OLED clear screen
  142. void OLED_clearScreen(void) {
  143. for(uint8_t i=0; i<4; i++) // 4 lines
  144. OLED_clearLine(i); // clear line
  145. }
  146. // OLED print a single character
  147. void OLED_printChar(char c) {
  148. uint16_t ptr = c - 32; // character pointer
  149. ptr += ptr << 2; // -> ptr = (ch - 32) * 5;
  150. I2C_write(0x00); // write space between characters
  151. for(uint8_t i=5 ; i; i--) I2C_write(pgm_read_byte(&OLED_FONT[ptr++]));
  152. OLED_x += 6; // update cursor
  153. if(OLED_x > 122) { // line end ?
  154. I2C_stop(); // stop data transmission
  155. OLED_setCursor(0,++OLED_y); // set next line start
  156. I2C_start(OLED_ADDR); // start transmission to OLED
  157. I2C_write(OLED_DAT_MODE); // set data mode
  158. }
  159. }
  160. // OLED print a string from program memory
  161. void OLED_printPrg(const char* p) {
  162. I2C_start(OLED_ADDR); // start transmission to OLED
  163. I2C_write(OLED_DAT_MODE); // set data mode
  164. char ch = pgm_read_byte(p); // read first character from program memory
  165. while(ch) { // repeat until string terminator
  166. OLED_printChar(ch); // print character on OLED
  167. ch = pgm_read_byte(++p); // read next character
  168. }
  169. I2C_stop(); // stop transmission
  170. }
  171. // OLED convert byte nibble into hex character and prints it
  172. void OLED_printNibble(uint8_t nibble) {
  173. char c;
  174. if(nibble <= 9) c = '0' + nibble;
  175. else c = 'A' + nibble - 10;
  176. OLED_printChar(c);
  177. }
  178. // OLED print byte as hex
  179. void OLED_printHex(uint8_t value) {
  180. I2C_start(OLED_ADDR); // start transmission to OLED
  181. I2C_write(OLED_DAT_MODE); // set data mode
  182. OLED_printNibble(value >> 4); // print high nibble
  183. OLED_printNibble(value & 0x0F); // print low nibble
  184. I2C_stop(); // stop transmission
  185. }
  186. const char TitleScreen[] PROGMEM = "Virtual PIN : ";
  187. const char TitlePb[] PROGMEM = "PB";
  188. const char TitlePd[] PROGMEM = "PD";
  189. const char TitleScreen2[] PROGMEM = "Physical PIN : ";
  190. const char BaudScreen[] PROGMEM = "I2Cscanner 115200 bds";
  191. void setup() {
  192. OLED_init(); // setup I2C OLED
  193. OLED_clearScreen();
  194. OLED_setCursor(0,0);
  195. OLED_printPrg(TitleScreen);
  196. Serial.begin(9600);
  197. Serial.println("\nI2C Scanner :");
  198. switch (PIN) {
  199. case PB0:
  200. DDRB |= (1<<PIN);
  201. OLED_printPrg(TitlePb);
  202. OLED_printHex(0x00);
  203. OLED_setCursor(0,1);
  204. OLED_printPrg(TitleScreen2);
  205. OLED_printHex(0x08);
  206. OLED_setCursor(0,3);
  207. OLED_printPrg(BaudScreen);
  208. break;
  209. case PB1:
  210. DDRB |= (1<<PIN);
  211. OLED_printPrg(TitlePb);
  212. OLED_printHex(0x01);
  213. OLED_setCursor(0,2);
  214. OLED_printPrg(TitleScreen2);
  215. OLED_printHex(0x09);
  216. break;
  217. case PB2:
  218. DDRB |= (1<<PIN);
  219. OLED_printPrg(TitlePb);
  220. OLED_printHex(0x02);
  221. OLED_setCursor(0,2);
  222. OLED_printPrg(TitleScreen2);
  223. OLED_printHex(0x10);
  224. break;
  225. case PB3:
  226. DDRB |= (1<<PIN);
  227. OLED_printPrg(TitlePb);
  228. OLED_printHex(0x03);
  229. OLED_setCursor(0,2);
  230. OLED_printPrg(TitleScreen2);
  231. OLED_printHex(0x11);
  232. break;
  233. case PB4:
  234. DDRB |= (1<<PIN);
  235. OLED_printPrg(TitlePb);
  236. OLED_printHex(0x04);
  237. OLED_setCursor(0,2);
  238. OLED_printPrg(TitleScreen2);
  239. OLED_printHex(0x12);
  240. break;
  241. case PB5:
  242. DDRB |= (1<<PIN);
  243. OLED_printPrg(TitlePb);
  244. OLED_printHex(0x05);
  245. OLED_setCursor(0,2);
  246. OLED_printPrg(TitleScreen2);
  247. OLED_printHex(0x13);
  248. break;
  249. case PD7:
  250. DDRD |= (1<<PIN);
  251. OLED_printPrg(TitlePd);
  252. OLED_printHex(0x07);
  253. OLED_setCursor(0,2);
  254. OLED_printPrg(TitleScreen2);
  255. OLED_printHex(0x07);
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. void loop() {
  262. byte error, address;
  263. int nDevices;
  264. switch (boucle){
  265. case 10:
  266. Serial.println("Scanning...");
  267. nDevices = 0;
  268. for(address = 1; address < 127; address++ )
  269. {
  270. Wire.beginTransmission(address);
  271. error = Wire.endTransmission();
  272. if (error == 0)
  273. {
  274. Serial.print("I2C device found at address 0x");
  275. if (address<16)
  276. Serial.print("0");
  277. Serial.print(address,HEX);
  278. Serial.println(" !");
  279. nDevices++;
  280. }
  281. else if (error==4)
  282. {
  283. Serial.print("Unknow error at address 0x");
  284. if (address<16)
  285. Serial.print("0");
  286. Serial.println(address,HEX);
  287. }
  288. }
  289. if (nDevices == 0)
  290. Serial.println("No I2C devices found\n");
  291. else
  292. Serial.println("done\n");
  293. boucle=0;
  294. break;
  295. default:
  296. boucle++;
  297. }
  298. switch (PIN) {
  299. case PD7 :
  300. PORTD &= ~(1<<PIN);
  301. delay(100);
  302. PORTD |= (1<<PIN);
  303. delay(100);
  304. break;
  305. default:
  306. PORTB &= ~(1<<PIN);
  307. delay(100);
  308. PORTB |= (1<<PIN);
  309. delay(100);
  310. break;
  311. }
  312. }