Guest User

Untitled

a guest
Jan 3rd, 2021
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.47 KB | None | 0 0
  1. #include <at89c51ed2.h>
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7.  
  8. #include "i2c.h"
  9. #include "delay.h"
  10. //#include "stddef.h"
  11. //#include "lcd_i2c.h"
  12. //#include "stdutils.h"
  13.  
  14. #include "eeprom.h"
  15. #include "timers.h"
  16. #include "uart.h"
  17. #include "stdutils.h"
  18. //#include <reg52.h>
  19. //#include <8052.h>
  20.  
  21.  
  22. //#define __STC12C5A60S2__
  23. #ifdef __STC12C5A60S2__
  24. __sfr __at(0x97) CLK_DIV;
  25. #endif // __STC12C5A60S2__
  26.  
  27. //#define __W78E516D__
  28. #ifdef __W78E516D__
  29. __sfr __at(0xF6) CHPENR;
  30. __sfr __at(0xBF) CHPCON;
  31. #define CLR_POR() PCON &= ~0b00010000 // clear power on reset flag in W78E516D
  32.  
  33. __sfr __at(0x86) P0UPR;
  34. #define CLR_P0UP() P0UPR &= ~0b00000001 // port 0 pins are open drain in W78E516D
  35. #define SET_P0UP() P0UPR |= 0b00000001 // port 0 pins are internally pull up, same as port 2 in W78E516D
  36.  
  37. #endif // __W78E516D__
  38.  
  39.  
  40.  
  41.  
  42.  
  43. // eeprom via SPI on the other board (e.g. AT24C02)
  44. #define _AT89C51ED2_SPI_EEPROM
  45. #ifdef _AT89C51ED2_SPI_EEPROM
  46. /*#define _AT89C51ED2_SPI*/
  47.  
  48. // select only one from 2 following
  49. //#define _SPI_AS_MASTER
  50. #define _SPI_AS_SLAVE
  51.  
  52. // uncomment to use chip select /SS
  53. #define _SPI_USE_SS
  54.  
  55. #ifdef _SPI_AS_MASTER
  56. #ifdef _SPI_USE_SS
  57. #define SPI_MASTER_SLAVE_SS P1_1
  58. #endif
  59. #endif // _SPI_AS_MASTER
  60.  
  61. #define SPI_EEPROM_READ 'R'
  62. #define SPI_EEPROM_WRITE 'W'
  63.  
  64. #define MAX_EEPROM_ADDR 255
  65.  
  66.  
  67. volatile unsigned char SPI_INT_Serial_Data_IN;
  68. #ifdef _SPI_AS_SLAVE
  69. //volatile unsigned char SPI_INT_Serial_Data_OUT;
  70. #endif
  71. volatile unsigned char SPI_INT_Transmit_Completed = 0;
  72.  
  73. void ED2_SPI_EEPROM_Init(void);
  74. void ED2_SPI_EEPROM_WriteByte(uint16_t var_eepromAddress_u16, uint8_t var_eepromData_u8);
  75. uint8_t ED2_SPI_EEPROM_ReadByte(uint16_t var_eepromAddress_u16);
  76. void it_SPI(void) __interrupt(9); /* interrupt address is 0x004B */
  77.  
  78. void ED2_SPI_EEPROM_Init(void)
  79. {
  80. // SPCON = 0x14; // default state after reset
  81. // SPCON |= SPEN | MSTR | SPR2 | SPR1; // set enable_spi, master, baud rate divisor is 128 (0xd2)
  82.  
  83. /*
  84. // P1 port configuration
  85. #ifdef _SPI_AS_MASTER
  86. P1 |= 0b00100000;
  87. #elif defined(_SPI_AS_SLAVE)
  88. P1 |= 0b01000010;
  89. #endif
  90. */
  91.  
  92. #ifdef _SPI_AS_MASTER
  93. SPCON |= 0x10; // set master mode
  94. SPCON |= 0x82; /* Fclk Periph/128 */
  95. #elif defined(_SPI_AS_SLAVE)
  96. SPCON &= ~0x10; // set slave mode
  97. #endif
  98.  
  99. //SPCON |= 0x10; /* Master mode */
  100. //SPCON |= 0x82; /* Fclk Periph/128 */
  101. #ifdef _SPI_USE_SS
  102. SPCON &= ~0x20; /* P1.1 is available as SPI /SS pin */
  103. #ifdef _SPI_AS_MASTER
  104. SPI_MASTER_SLAVE_SS = 0;
  105. #endif
  106. #else
  107. SPCON |= 0x20; /* P1.1 is available as standard I/O pin */
  108. #endif
  109.  
  110. SPCON &= ~0x08; /* CPOL=0; transmit mode example */
  111. // SPCON |= 0x04; /* CPHA=1; transmit mode example */
  112. SPCON &= ~0x04; /* CPHA=0; transmit mode example */
  113. IEN1 |= 0x04; /* enable spi interrupt */
  114. SPCON |= 0x40; /* run spi */
  115. }
  116.  
  117. #ifdef _SPI_AS_MASTER // this functions can be in master mode
  118. void ED2_SPI_EEPROM_WriteByte(uint16_t var_eepromAddress_u16, uint8_t var_eepromData_u8)
  119. {
  120. if(var_eepromAddress_u16 > MAX_EEPROM_ADDR) return;
  121.  
  122. #ifdef _SPI_USE_SS
  123. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  124. #endif
  125. SPDAT = SPI_EEPROM_WRITE;
  126. while (!SPI_INT_Transmit_Completed);
  127. SPI_INT_Transmit_Completed = 0;
  128. #ifdef _SPI_USE_SS
  129. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  130. #endif
  131.  
  132.  
  133. #ifdef _SPI_USE_SS
  134. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  135. #endif
  136. SPDAT = var_eepromAddress_u16 >> 8;
  137. while (!SPI_INT_Transmit_Completed);
  138. SPI_INT_Transmit_Completed = 0;
  139. #ifdef _SPI_USE_SS
  140. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  141. #endif
  142.  
  143.  
  144. #ifdef _SPI_USE_SS
  145. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  146. #endif
  147. SPDAT = var_eepromAddress_u16 & 0xFF;
  148. while (!SPI_INT_Transmit_Completed);
  149. SPI_INT_Transmit_Completed = 0;
  150. #ifdef _SPI_USE_SS
  151. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  152. #endif
  153.  
  154.  
  155. #ifdef _SPI_USE_SS
  156. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  157. #endif
  158. SPDAT = var_eepromData_u8;
  159. while (!SPI_INT_Transmit_Completed);
  160. SPI_INT_Transmit_Completed = 0;
  161. #ifdef _SPI_USE_SS
  162. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  163. #endif
  164.  
  165. }
  166.  
  167. #define ui16_SPI_EEP_RESP_TIME 250
  168. uint8_t ED2_SPI_EEPROM_ReadByte(uint16_t var_eepromAddress_u16)
  169. {
  170. uint8_t var_eepromData_u8;
  171.  
  172. if(var_eepromAddress_u16 > MAX_EEPROM_ADDR) return 0;
  173.  
  174. #ifdef _SPI_USE_SS
  175. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  176. #endif
  177. SPDAT = SPI_EEPROM_READ;
  178. while (!SPI_INT_Transmit_Completed);
  179. SPI_INT_Transmit_Completed = 0;
  180. #ifdef _SPI_USE_SS
  181. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  182. #endif
  183.  
  184. #ifdef _SPI_USE_SS
  185. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  186. #endif
  187. SPDAT = var_eepromAddress_u16 >> 8;
  188. while (!SPI_INT_Transmit_Completed);
  189. SPI_INT_Transmit_Completed = 0;
  190. #ifdef _SPI_USE_SS
  191. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  192. #endif
  193.  
  194. #ifdef _SPI_USE_SS
  195. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  196. #endif
  197. SPDAT = var_eepromAddress_u16 & 0xFF;
  198. while (!SPI_INT_Transmit_Completed);
  199. SPI_INT_Transmit_Completed = 0;
  200. #ifdef _SPI_USE_SS
  201. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  202. #endif
  203.  
  204. DELAY_ms(ui16_SPI_EEP_RESP_TIME);
  205.  
  206. #ifdef _SPI_USE_SS
  207. SPI_MASTER_SLAVE_SS = 1; // inform slave to start operation
  208. #endif
  209. SPDAT = 0x00;
  210. while (!SPI_INT_Transmit_Completed);
  211. SPI_INT_Transmit_Completed = 0;
  212.  
  213. var_eepromData_u8 = SPI_INT_Serial_Data_IN;
  214. #ifdef _SPI_USE_SS
  215. SPI_MASTER_SLAVE_SS = 0; // no operation with slave
  216. #endif
  217.  
  218. return var_eepromData_u8;
  219. }
  220. #endif // _SPI_AS_MASTER
  221.  
  222. void it_SPI(void) __interrupt(9) /* interrupt address is 0x004B */
  223. {
  224. switch( SPSTA ) /* read and clear spi status register */
  225. {
  226. case 0x80:
  227. SPI_INT_Serial_Data_IN = SPDAT; /* read receive data */
  228. SPI_INT_Transmit_Completed = 1;/* set software flag */
  229. break;
  230. case 0x10:
  231. /* put here for mode fault tasking */
  232. break;
  233. case 0x40:
  234. /* put here for overrun tasking */
  235. break;
  236. }
  237. #ifdef _SPI_AS_SLAVE
  238. SPDAT = SPI_INT_Serial_Data_IN; /* needed to complete clearing sequence */
  239. #endif
  240. }
  241. #endif // _AT89C51ED2_SPI_EEPROM
  242.  
  243. /**** includes at89c51ed2.h ****
  244. //#define _AT89C51ED2_SPI
  245. #ifdef _AT89C51ED2_SPI
  246. __sfr __at (0xC3) SPCON; //SPI Control Register
  247. #define SPR2 0x80 //SPI Clork Rate select bit 2.
  248. #define SPEN 0x40 //SPI enable bit. When set enables SPI.
  249. #define SSDIS 0x20 //Cleared to enable SS in both Master and Slave modes.
  250. #define MSTR 0x10 //1=master mode. 0=slave mode.
  251. #define CPOL 0x08 //1=SCK is high when idle (active low), 0=SCK is low when idle (active high).
  252. #define CPHA 0x04 //1=shift triggered on the trailing edge of SCK. 0=shift trig. on leading edge.
  253. #define SPR1 0x02 //SPI Clork Rate select bit 1.
  254. #define SPR0 0x01 //SPI Clork Rate select bit 0.
  255. //SPR2 SPR1 SPR0 Baud Rate Divisor
  256. // 0 0 0 2
  257. // 0 0 1 4
  258. // 0 1 0 8
  259. // 0 1 1 16
  260. // 1 0 0 32
  261. // 1 0 1 64
  262. // 1 1 0 128
  263. // 1 1 1 Invalid: Don't Use
  264.  
  265. __sfr __at (0xC4) SPSTA; //Serial Peripheral Status register
  266. #define SPIF 0x80 //Serial Peripheral Data Transfer Flag
  267. #define WCOL 0x40 //Write collision Flag.
  268. #define SSERR 0x20 //Synchronous Serial Slave Error Flag
  269. #define MODF 0x10 //Mode Fault Flag
  270.  
  271. __sfr __at (0xC5) SPDAT; //SPI Data
  272. #endif // _AT89C51ED2_SPI
  273. **** includes at89c51ed2.h ****/
  274.  
  275. #define _WATCHDOG_TIMER
  276. #ifdef _WATCHDOG_TIMER
  277. void rst_Watchdog( void );
  278. #define _AT89C51ED2_WDT
  279. #ifdef _AT89C51ED2_WDT
  280. __sfr __at(0xA6) WDTRST;
  281. __sfr __at(0xA7) WDTPRG;
  282. #endif
  283. //#define _AT89C51ED2_PCA_WDT
  284. #ifdef _AT89C51ED2_PCA_WDT
  285. __sfr __at(0xD8) ED2_PCA_CCON;
  286. __sfr __at(0xD9) ED2_PCA_CMOD;
  287. __sfr __at(0xDE) ED2_PCA_CCAPM4;
  288. __sfr __at(0xE9) ED2_PCA_CL;
  289. __sfr __at(0xF9) ED2_PCA_CH;
  290. __sfr __at(0xEE) ED2_PCA_CCAP4L;
  291. __sfr __at(0xFE) ED2_PCA_CCAP4H;
  292. #endif
  293.  
  294. //#define _W78E516D_WDT
  295. #ifdef _W78E516D_WDT
  296. __sfr __at(0x8F) WDTC;
  297. #endif
  298.  
  299. //#define __AT89S52__
  300. #ifdef __AT89S52__
  301. __sfr __at(0xA6) WDTRST;
  302. //__sfr __at(0xA7) WDTPRG;
  303. #endif
  304. #endif
  305.  
  306.  
  307. // eeprom which uses I2C (e.g. AT24C02)
  308. #define _I2C_EEPROM // can be used by SPI interface
  309.  
  310. // eeprom which contains AT89C51ED2
  311. //#define _AT89C51ED2_EEPROM
  312. #ifdef _AT89C51ED2_EEPROM
  313. __sfr __at(0x0D2) ED2_EEP_EECON;
  314. #define ED2_EEP_EEE 0x02
  315. #define ED2_EEP_EEBUSY 0x01
  316. void ED2_EEPROM_WriteByte(uint16_t var_eepromAddress_u16, uint8_t var_eepromData_u8);
  317. uint8_t ED2_EEPROM_ReadByte(uint16_t var_eepromAddress_u16);
  318.  
  319. void ED2_EEPROM_WriteByte(uint16_t var_eepromAddress_u16, uint8_t var_eepromData_u8)
  320. {
  321. __xdata uint8_t *p;
  322. p = var_eepromAddress_u16;
  323. if(var_eepromAddress_u16 > 2047) return;
  324.  
  325. while(ED2_EEP_EECON & ED2_EEP_EEBUSY);
  326. EA = 0;
  327. ED2_EEP_EECON |= ED2_EEP_EEE;
  328. /* __asm
  329. MOV DPTR, var_eepromAddress_u16;
  330. MOV ACC, var_eepromData_u8
  331. MOVX @DPTR, A
  332. __endasm;
  333. */
  334. *p = var_eepromData_u8;
  335. ED2_EEP_EECON &= ~ED2_EEP_EEE;
  336. EA = 1;
  337. }
  338. uint8_t ED2_EEPROM_ReadByte(uint16_t var_eepromAddress_u16)
  339. {
  340. __xdata uint8_t *p;
  341. p = var_eepromAddress_u16;
  342. uint8_t var_eepromData_u8;
  343.  
  344. if(var_eepromAddress_u16 > 2047) return 0;
  345.  
  346. while(ED2_EEP_EECON & ED2_EEP_EEBUSY);
  347. EA = 0;
  348. ED2_EEP_EECON |= ED2_EEP_EEE;
  349. /* __asm
  350. MOV DPTR, var_eepromAddress_u16;
  351. MOV ACC, var_eepromData_u8
  352. MOVX @DPTR, A
  353. __endasm;
  354. */
  355. var_eepromData_u8 = *p;
  356. ED2_EEP_EECON &= ~ED2_EEP_EEE;
  357. EA = 1;
  358. return var_eepromData_u8;
  359. }
  360. #endif // _AT89C51ED2_EEPROM
  361.  
  362. //#define BUF_SIZE 8
  363. #define BUF_SIZE 16
  364.  
  365. #define S2_KEY P3_2
  366. #define S3_KEY P3_3
  367. #define S4_KEY P3_4
  368. #define S5_KEY P3_5
  369.  
  370.  
  371. // ZS5110 dev board
  372. //#define BRD_ZS5110
  373.  
  374. // JZ-K3 dev board
  375. #define BRD_JZK3
  376.  
  377.  
  378. #ifdef BRD_ZS5110
  379. #define LED_PORT P2
  380. #endif // BRD_ZS5110
  381.  
  382. #ifdef BRD_JZK3
  383. #define LED_PORT P1
  384. #define SEG3 P2_7
  385. #define SEG2 P2_6
  386. #define SEG1 P2_5
  387. #define SEG0 P2_4
  388. #endif // BRD_JZK3
  389.  
  390. #define KEY_PORT P3
  391.  
  392. void software_Reset(void);
  393. void prn_b_delay(void);
  394.  
  395. #ifdef BRD_JZK3
  396. void disp_b_delay(void);
  397. #endif // BRD_JZK3
  398.  
  399. void led_on(void);
  400. void led_off(void);
  401. int get_input(int end_char, char *buf, int max_length, int *recv_length);
  402.  
  403. void timer_intr(void);
  404. void timer_events(void);
  405. void S2_KEY_events(void);
  406. void S3_KEY_events(void);
  407.  
  408.  
  409.  
  410. /*------------------------------------------------------------------------------
  411. rst_Watchdog() : Resets the watchdog timer to avoid microcontroller reset
  412. For __AT89C51ED2__
  413. This is done by writing to the WDTRST sfr with 0x1E and
  414. then 0xE1. This will reset the timer to 0x0.
  415. ------------------------------------------------------------------------------*/
  416.  
  417. #ifdef _WATCHDOG_TIMER
  418. void rst_Watchdog( void )
  419. {
  420. #ifdef _AT89C51ED2_WDT
  421. WDTPRG |= 0x00; // minimum delay
  422. // WDTPRG |= 0x07; // maximum delay
  423. WDTRST = 0x1E;
  424. WDTRST = 0xE1;
  425. #endif
  426. #ifdef _AT89C51ED2_PCA_WDT
  427. // ED2_PCA_CL = 0xFF;
  428. // ED2_PCA_CH = 0xFF;
  429. ED2_PCA_CCAP4L = 0xFF;
  430. ED2_PCA_CCAP4H = 0xFF;
  431. ED2_PCA_CCAPM4 = 0x48; // ECOM & MAT
  432. ED2_PCA_CMOD |= 0x40; // WDTE
  433. ED2_PCA_CCON |= 0x40; // turn on counter
  434. #endif
  435.  
  436. #ifdef _W78E516D_WDT
  437. WDTC |= 0x80; // minimum delay
  438. // WDTC |= 0x87; // maximum delay
  439. #endif
  440.  
  441. #ifdef __AT89S52__
  442. WDTRST = 0x1E;
  443. WDTRST = 0xE1;
  444. #endif
  445. }
  446. #endif
  447.  
  448. void software_Reset(void) {
  449. // wdt_enable(WDTO_15MS);
  450. //while(1) { }
  451. //wdt_reset();
  452. /* EA = 0;
  453. WDTRST = 0x1E; // Enable the watchdog
  454. WDTRST = 0xE1;
  455. for (;;); // Infinite loop*/
  456.  
  457. #ifdef _WATCHDOG_TIMER
  458. rst_Watchdog();
  459. while(1);
  460. #else
  461. void (*reset)(void);
  462. reset = 0x0000;
  463.  
  464. reset();
  465. #endif
  466. }
  467. /*
  468. void _delay_ms(uint16_t delayTimeMS)
  469. {
  470. delay_ms(delayTimeMS);
  471. }
  472. */
  473. volatile unsigned char b_delay;
  474.  
  475. void prn_b_delay(void) {
  476. // uint16_t tmp;
  477. // int8_t i;
  478.  
  479. UART_Printf("b_delay: %4d\n\r", (sint16_t) b_delay*50 );
  480.  
  481. /* lcd_i2c_setCursor(13,1);
  482. lcd_i2c_setCursor(12,1);
  483. lcd_i2c_setCursor(11,1);
  484. lcd_i2c_setCursor(10,1);
  485. */
  486. /* tmp = b_delay * 50;
  487. i = 3;
  488.  
  489. while(i >= 0) {
  490. lcd_i2c_setCursor(10 + i, 1);
  491. lcd_i2c_write(0x30 + (tmp % 10));
  492. i--;
  493. tmp /= 10;
  494. }
  495. lcd_i2c_setCursor(14,1);
  496. lcd_i2c_write('m');
  497. lcd_i2c_write('s');*/
  498. }
  499.  
  500. #ifdef BRD_JZK3
  501. unsigned char LEDDis[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF};
  502.  
  503. void disp_b_delay(void) {
  504.  
  505. unsigned int ds = b_delay * 5;
  506.  
  507. // switch(cur_seg) {
  508. // case 2:
  509. P0 = LEDDis[ds/100];
  510. P0 &= 0x7f;
  511. SEG2=0;
  512. DELAY_us(10);
  513. SEG2=1;
  514. // cur_seg = 1;
  515. // break;
  516.  
  517. // case 1:
  518. P0=LEDDis[(ds-(ds/100)*100)/10];
  519. SEG1=0;
  520. DELAY_us(10);
  521. SEG1=1;
  522.  
  523. // case 0:
  524. P0=LEDDis[(ds-(ds/100)*100)-((ds-(ds/100)*100)/10)*10];
  525. SEG0=0;
  526. DELAY_us(10);
  527. SEG0=1;
  528. // }
  529. }
  530. #endif // BRD_JZK3
  531.  
  532. void led_on(void)
  533. {
  534. //PORTD |= _BV(PD4); //Pin 2 of MCU as output
  535. LED_PORT &= 0x00;
  536. }
  537.  
  538. void led_off(void)
  539. {
  540. //PORTD &= ~_BV(PD4);
  541. LED_PORT |= 0xff;
  542. }
  543.  
  544.  
  545. int get_input(int end_char, char *buf, int max_length, int *recv_length)
  546. {
  547. int lastRead;
  548. static int idx = 0;
  549. /*
  550. if (RI) {
  551. lastRead = UART_RxChar();
  552. UART_Printf(" - %c\n\r", (char) lastRead);
  553. }
  554. */
  555.  
  556. // when there are no characters to read, or the character isn't a newline
  557. if (RI) {
  558. // something to read
  559. if (end_char != (lastRead = UART_RxChar())) {
  560. buf[idx] = lastRead;
  561. idx++;
  562. if (idx == max_length) {
  563. *recv_length = idx;
  564. idx = 0;
  565. return 1;
  566. }
  567. } else {
  568. *recv_length = idx;
  569. idx = 0;
  570. return 1; // when we get a newline, break out of loop
  571. }
  572. }
  573. return 0;
  574. }
  575.  
  576. volatile unsigned char led_flag = 1;
  577.  
  578. volatile unsigned int timer0_run = 1;
  579. volatile unsigned int TimerCount0 = 0;
  580.  
  581. void timer_intr(void)
  582. {
  583. if(timer0_run) TimerCount0 ++;
  584. }
  585.  
  586. void timer_events(void)
  587. {
  588. if(TimerCount0 >= b_delay) {
  589. timer0_run = 0;
  590. TimerCount0 = 0;
  591. if(led_flag) {
  592. led_flag = 0;
  593. led_on();
  594. } else {
  595. led_flag = 1;
  596. led_off();
  597. }
  598. timer0_run = 1;
  599. }
  600. }
  601.  
  602. volatile unsigned char S2_processed = 1;
  603. volatile unsigned char S3_processed = 1;
  604. //unsigned char S4_processed = 1;
  605. //unsigned char S5_processed = 1;
  606.  
  607.  
  608. void S2_KEY_events(void)
  609. {
  610. //if( KEY_PORT & S2_KEY ) { // not pressed
  611. if( S2_KEY ) { // not pressed
  612. S2_processed = 1;
  613. } else { // pressed
  614. if( S2_processed == 1 ) {
  615. S2_processed = 0;
  616.  
  617. //_delay_ms(20);
  618.  
  619. //TIMER_Stop(0);
  620. b_delay += 1;
  621. if( b_delay > 100 ) b_delay = 100;
  622.  
  623. #ifdef _AT89C51ED2_EEPROM
  624. ED2_EEPROM_WriteByte(0, b_delay);
  625. #endif
  626. #ifdef _I2C_EEPROM
  627. EEPROM_WriteByte(0, b_delay);
  628. #endif
  629. #if defined(_AT89C51ED2_SPI_EEPROM) && defined(_SPI_AS_MASTER)
  630. //#ifdef _AT89C51ED2_SPI_EEPROM
  631. ED2_SPI_EEPROM_WriteByte(0, b_delay);
  632. #endif
  633.  
  634. if( b_delay == 1 ) {
  635. timer0_run = 1;
  636. }
  637. prn_b_delay();
  638. }
  639. }
  640. }
  641.  
  642. void S3_KEY_events(void)
  643. {
  644. //if( KEY_PORT & S3_KEY ) { // not pressed
  645. if( S3_KEY ) { // not pressed
  646. S3_processed = 1;
  647. } else { // pressed
  648. if( S3_processed == 1 ) {
  649. S3_processed = 0;
  650.  
  651. //_delay_ms(20);
  652.  
  653. if( b_delay > 0 ) {
  654. b_delay -= 1;
  655.  
  656. #ifdef _AT89C51ED2_EEPROM
  657. ED2_EEPROM_WriteByte(0, b_delay);
  658. #endif
  659. #ifdef _I2C_EEPROM
  660. EEPROM_WriteByte(0, b_delay);
  661. #endif
  662. #if defined(_AT89C51ED2_SPI_EEPROM) && defined(_SPI_AS_MASTER)
  663. //#ifdef _AT89C51ED2_SPI_EEPROM
  664. ED2_SPI_EEPROM_WriteByte(0, b_delay);
  665. #endif
  666.  
  667. if( b_delay == 0 ) {
  668. timer0_run = 0;
  669. }
  670. }
  671. prn_b_delay();
  672. }
  673. }
  674. }
  675.  
  676. volatile char last_command[BUF_SIZE];
  677. volatile char sbuf[BUF_SIZE];
  678.  
  679. void main(void)
  680. {
  681. // int temp;
  682. // int readbytes;
  683.  
  684. uint8_t eep_cmd = 0;
  685. uint16_t eep_address = 0;
  686.  
  687. // if(EA == 0) {
  688. // EnableGlobalInterrupts();
  689. // software_Reset();
  690. // }
  691. //
  692. // DisableGlobalInterrupts();
  693.  
  694. #ifdef __STC12C5A60S2__
  695. // CLK_DIV = 0x03; // divide crystal frequency to 2^3 (8)
  696. CLK_DIV = 0x00; // divide crystal frequency to 2^0 (1)
  697. #endif // __STC12C5A60S2__
  698.  
  699. #ifdef __W78E516D__
  700. if(PCON & 0x00010000) { // power on reset flag is set?
  701. CLR_POR(); // clear power on reset flag
  702. } else {
  703. CHPENR = 0x87;
  704. CHPENR = 0x59; // enter CHPCON configuration
  705. //CHPCON = 0b00010000; // enable swreset, enable AUX-RAM, start from APROM,
  706. CHPCON = 0x80; // software reset
  707. // CHPENR = 0x00; // exit CHPCON configuration
  708. }
  709. #endif
  710. #ifdef __W78E516D__
  711. // SET_P0UP();
  712. // CLR_P0UP();
  713. #endif
  714.  
  715. DELAY_ms(500);
  716.  
  717. P3 |= 0b00111100; // set as input
  718.  
  719. i2c_init();
  720.  
  721. DELAY_ms(50);
  722. //lcd_i2c_init(0x3f, 20, 4);
  723. // lcd_i2c_init(0x27, 20, 4);
  724.  
  725. DELAY_ms(20);
  726.  
  727. // lcd_i2c_setCursor(0,0);
  728. // lcd_i2c_printstr("Engine");
  729. /*
  730. lcd_i2c_setCursor(0,1);
  731. lcd_i2c_printstr("12345678901234567890");
  732. // lcd_i2c_printstr("1234567890 ");
  733.  
  734. lcd_i2c_setCursor(0,2);
  735. lcd_i2c_printstr("AAAAAAAAAAAAAAAAAAAA");
  736.  
  737. lcd_i2c_setCursor(0,3);
  738. lcd_i2c_printstr("It's Work.!!!");
  739.  
  740. // lcd_backlight(ON);
  741. // transceiver(LCD_BACKLIGHT);
  742. while(1) {
  743. lcd_i2c_setCursor(13,3);
  744. // lcd_i2c_setCursor(12,1);
  745. sprintf(str, "%03u", i++);
  746. lcd_i2c_printstr(str);
  747. //transceiver(LCD_BACKLIGHT);
  748. DELAY_ms(1);
  749. }
  750. */
  751. /*
  752. led_on();
  753. DELAY_ms(500);
  754. led_off();
  755. DELAY_ms(500);
  756. */
  757. #ifdef _AT89C51ED2_SPI_EEPROM
  758. ED2_SPI_EEPROM_Init();
  759. #endif
  760.  
  761. b_delay = 1;
  762. #ifdef _AT89C51ED2_EEPROM
  763. b_delay = ED2_EEPROM_ReadByte(0);
  764. #endif
  765. #ifdef _I2C_EEPROM
  766. b_delay = EEPROM_ReadByte(0);
  767. #endif
  768. #if defined(_AT89C51ED2_SPI_EEPROM) && defined(_SPI_AS_MASTER)
  769. //#if _AT89C51ED2_SPI_EEPROM
  770. b_delay = ED2_SPI_EEPROM_ReadByte(0);
  771. #endif
  772.  
  773. if(b_delay > 100) {
  774. b_delay = 100;
  775. #ifdef _I2C_EEPROM
  776. EEPROM_WriteByte(0, b_delay);
  777. #endif
  778. }
  779.  
  780.  
  781. /*
  782. strcpy(last_command, "help");
  783.  
  784.  
  785.  
  786. TIMER_Init(0, 50000);
  787. TIMER_AttachInterrupt(0, timer_intr);
  788.  
  789. TIMER_Start(0); // Timer Number, Interrupts (Enabled,Disabled);
  790.  
  791. // Open serial communications and wait for port to open:
  792. //UART_Init(9600);
  793. UART_Init(12000000, 9600);
  794. // UART_Init(12000000, 19200);
  795. // UART_Init(12000000, 38400);
  796. // UART_Init(11059200, 115200);
  797.  
  798. EnableGlobalInterrupts();
  799.  
  800. UART_TxString("\033[2J\033[;HStart\n\r");
  801. prn_b_delay();
  802. UART_TxString("cmd: \n\r");
  803.  
  804. //RI = 0; // clear receive flag
  805. //TI = 0; // clear transmit flag
  806.  
  807. //rst_Watchdog();
  808. */
  809. SPI_INT_Transmit_Completed = 0;
  810. EnableGlobalInterrupts();
  811.  
  812. while (1) { // run over and over
  813. if(SPI_INT_Transmit_Completed) {
  814. eep_cmd = SPI_INT_Serial_Data_IN;
  815. // SPDAT = SPI_INT_Serial_Data;
  816. SPI_INT_Transmit_Completed = 0;
  817. }
  818. if(eep_cmd == 'W') {
  819. eep_address = 0;
  820. while(!SPI_INT_Transmit_Completed);
  821. eep_address = SPI_INT_Serial_Data_IN << 8;
  822. // SPDAT = SPI_INT_Serial_Data;
  823. SPI_INT_Transmit_Completed = 0;
  824. while(!SPI_INT_Transmit_Completed);
  825. eep_address |= SPI_INT_Serial_Data_IN;
  826. // SPDAT = SPI_INT_Serial_Data;
  827. SPI_INT_Transmit_Completed = 0;
  828. while(!SPI_INT_Transmit_Completed);
  829. b_delay = SPI_INT_Serial_Data_IN;
  830. // SPDAT = SPI_INT_Serial_Data;
  831. SPI_INT_Transmit_Completed = 0;
  832. #ifdef _I2C_EEPROM
  833. EEPROM_WriteByte(0, b_delay);
  834. #endif
  835. eep_cmd = 0;
  836. }
  837. if(eep_cmd == 'R') {
  838. eep_address = 0;
  839. while(!SPI_INT_Transmit_Completed);
  840. eep_address = SPI_INT_Serial_Data_IN << 8;
  841. // SPDAT = SPI_INT_Serial_Data;
  842. SPI_INT_Transmit_Completed = 0;
  843. while(!SPI_INT_Transmit_Completed);
  844. eep_address |= SPI_INT_Serial_Data_IN;
  845. // SPDAT = SPI_INT_Serial_Data;
  846. SPI_INT_Transmit_Completed = 0;
  847. #ifdef _I2C_EEPROM
  848. b_delay = EEPROM_ReadByte(0);
  849. #endif
  850. // SPI_INT_Serial_Data = b_delay;
  851. while(!SPI_INT_Transmit_Completed);
  852. // SPDAT = SPI_INT_Serial_Data;
  853. SPDAT = b_delay;
  854. SPI_INT_Transmit_Completed = 0;
  855. eep_cmd = 0;
  856. }
  857.  
  858. /*
  859. timer_events();
  860.  
  861. S2_KEY_events();
  862. S3_KEY_events();
  863.  
  864.  
  865. if( 0 < (temp = get_input('\r', sbuf, BUF_SIZE - 1, &readbytes)) ) {
  866. sbuf[readbytes] = '\0';
  867. UART_TxString("input: \n\r");
  868. UART_Printf("%s\n\r", sbuf);
  869.  
  870.  
  871. if( strcmp(sbuf, "a") != 0 ) strcpy(last_command, sbuf); // save last meaningful command
  872.  
  873. repeat_last_command:
  874. if( strcmp(sbuf, "ledon") == 0 ) {
  875.  
  876. //TIMER_Stop(0);
  877. b_delay += 1;
  878. if( b_delay > 100 ) b_delay = 100;
  879.  
  880. #ifdef _AT89C51ED2_EEPROM
  881. ED2_EEPROM_WriteByte(0, b_delay);
  882. #endif
  883. #ifdef _I2C_EEPROM
  884. EEPROM_WriteByte(0, b_delay);
  885. #endif
  886. #ifdef _AT89C51ED2_SPI_EEPROM
  887. ED2_SPI_EEPROM_WriteByte(0, b_delay);
  888. #endif
  889.  
  890. if( b_delay == 1 ) TIMER_Start(0);
  891.  
  892. prn_b_delay();
  893.  
  894. } else if( strcmp(sbuf, "ledoff") == 0 ) {
  895.  
  896. if( b_delay > 0 ) {
  897. b_delay -= 1;
  898.  
  899. #ifdef _AT89C51ED2_EEPROM
  900. ED2_EEPROM_WriteByte(0, b_delay);
  901. #endif
  902. #ifdef _I2C_EEPROM
  903. EEPROM_WriteByte(0, b_delay);
  904. #endif
  905. #ifdef _AT89C51ED2_SPI_EEPROM
  906. ED2_SPI_EEPROM_WriteByte(0, b_delay);
  907. #endif
  908.  
  909. if( b_delay == 0 ) TIMER_Stop(0);
  910. }
  911. prn_b_delay();
  912.  
  913. } else if( strcmp(sbuf, "r") == 0 ) {
  914.  
  915. #ifdef _AT89C51ED2_EEPROM
  916. b_delay = ED2_EEPROM_ReadByte(0);
  917. #endif
  918. #ifdef _I2C_EEPROM
  919. b_delay = EEPROM_ReadByte(0);
  920. #endif
  921. #ifdef _AT89C51ED2_SPI_EEPROM
  922. b_delay = ED2_SPI_EEPROM_ReadByte(0);
  923. #endif
  924. prn_b_delay();
  925.  
  926. } else if( strcmp(sbuf, "a") == 0 ) {
  927. UART_Printf("rep cmd: %s\n\r", last_command);
  928. strcpy(sbuf, last_command); // restore last meaningful command
  929. goto repeat_last_command;
  930.  
  931. } else if( strcmp(sbuf, "rst") == 0 ) {
  932. software_Reset(); // reset arduino
  933. }
  934.  
  935. UART_TxString("cmd: \n\r");
  936. }
  937. #ifdef BRD_JZK3
  938. disp_b_delay();
  939. #endif // BRD_JZK3
  940. */
  941. }
  942. }
  943.  
Advertisement
Add Comment
Please, Sign In to add comment