Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. #include "mbed.h"
  2.  
  3. #define LED1 (1 << 18)
  4. #define LED2 (1 << 20)
  5. #define LED3 (1 << 21)
  6. #define LED4 (1 << 23)
  7.  
  8. #define pin21 (1 << 5)
  9. #define pin22 (1 << 4)
  10. #define pin23 (1 << 3)
  11.  
  12. #define SBIT_WordLength 0x00u
  13. #define SBIT_DLAB 0x07u
  14. #define SBIT_FIFO 0x00u
  15. #define SBIT_RxFIFO 0x01u
  16. #define SBIT_TxFIFO 0x02u
  17. #define SBIT_THRE 0x05u
  18.  
  19. unsigned int* pinsel0 = reinterpret_cast<unsigned int*>(0x4002C000);
  20. unsigned int* pinsel4 = reinterpret_cast<unsigned int*>(0x4002C010);
  21. unsigned int* pinmode0 = reinterpret_cast<unsigned int*>(0x4002C040);
  22. unsigned int* pinmode4 = reinterpret_cast<unsigned int*>(0x4002C050);
  23.  
  24. unsigned int* fio2dir0 = reinterpret_cast<unsigned int*>(0x2009C040);
  25.  
  26. unsigned int* io2intenr = reinterpret_cast<unsigned int*>(0x400280B0);
  27. unsigned int* io2intstatr = reinterpret_cast<unsigned int*>(0x400280A4);
  28. unsigned int* io2intclr = reinterpret_cast<unsigned int*>(0x400280AC);
  29.  
  30. unsigned int* uart1FCR = reinterpret_cast<unsigned int*>(0x40010008);
  31. unsigned int* uart1LCR = reinterpret_cast<unsigned int*>(0x4001000C);
  32. unsigned int* uart1DLL = reinterpret_cast<unsigned int*>(0x40010000);
  33. unsigned int* uart1DLM = reinterpret_cast<unsigned int*>(0x40010004);
  34. unsigned int* uart1THR = reinterpret_cast<unsigned int*>(0x40010000);
  35.  
  36.  
  37. void EINT3_init (void) {
  38. *pinsel4 &= ~((1 << 7) | (1 << 6));
  39. *pinmode4 |= (1 << 7) | (1 << 6);
  40. *pinsel4 &= ~((1 << 9) | (1 << 8));
  41. *pinmode4 |= (1 << 9) | (1 << 8);
  42. *pinsel4 &= ~((1 << 11) | (1 << 10));
  43. *pinmode4 |= (1 << 11) | (1 << 10);
  44.  
  45. *fio2dir0 &= ~pin21;
  46. *fio2dir0 &= ~pin22;
  47. *fio2dir0 &= ~pin23;
  48.  
  49. *io2intenr |= (pin21);
  50. *io2intenr |= (pin22);
  51. *io2intenr |= (pin23);
  52.  
  53. NVIC_EnableIRQ(EINT3_IRQn);
  54. }
  55.  
  56. //EINT3 Handler
  57. extern "C" void EINT3_IRQHandler(void) __irq {
  58. if ((*io2intstatr & pin21) == pin21) {
  59. *io2intclr = (pin21);
  60.  
  61. LPC_GPIO1->FIOSET |= LED1;
  62. wait(0.01);
  63. LPC_GPIO1->FIOCLR |= LED1;
  64.  
  65. return;
  66. }
  67. if ((*io2intstatr & pin22) == pin22) {
  68. *io2intclr = pin22;
  69.  
  70. LPC_GPIO1->FIOSET |= LED2;
  71. wait(0.01);
  72. LPC_GPIO1->FIOCLR |= LED2;
  73.  
  74. return;
  75. }
  76. if ((*io2intstatr & pin23) == pin23) {
  77. *io2intclr = pin23;
  78.  
  79. LPC_GPIO1->FIOSET |= LED3;
  80. wait(0.01);
  81. LPC_GPIO1->FIOCLR |= LED3;
  82.  
  83. return;
  84. }
  85. }
  86.  
  87.  
  88. void initUart1(unsigned int baudrate) {
  89.  
  90. *pinsel0 &= ~0xF0000000;
  91. *pinsel0 |= 0x40000000;
  92. *pinmode0 |= 0xC0000000;
  93.  
  94. *uart1FCR = (1<<SBIT_FIFO) | (1<<SBIT_RxFIFO) | (1<<SBIT_TxFIFO);
  95. *uart1LCR = (0x03<<SBIT_WordLength) | (1<<SBIT_DLAB);
  96.  
  97. int _baudrate = ((96000000/4) / (16 * baudrate));
  98.  
  99. *uart1DLL = _baudrate & 0xFF;
  100. *uart1DLM = (_baudrate << 0x08) & 0xFF;
  101.  
  102. *uart1LCR &= ~(1<<SBIT_DLAB);
  103. }
  104.  
  105. void waitDataCleared() {
  106. while((*uart1LCR & (~(1<<SBIT_THRE))) == 0) { wait(0.000001); }
  107. }
  108.  
  109. void sendCharUart1(char data) {
  110. waitDataCleared();
  111. *uart1THR = data;
  112. }
  113.  
  114. void sendIntUart1(int data) {
  115. waitDataCleared();
  116. *uart1THR = (data & 0xFF);
  117. }
  118. /*
  119. SPI spi(p5, p6, p7);
  120. DigitalOut cs(p8);
  121. */
  122.  
  123. int main() {
  124. //set led 1 and 2 pins as output
  125.  
  126.  
  127. LPC_GPIO1->FIODIR |= LED1 | LED2 | LED3 | LED4;
  128. LPC_GPIO1->FIOSET |= LED4;
  129.  
  130. EINT3_init();
  131.  
  132. initUart1(9600);
  133. wait(2);
  134.  
  135. sendCharUart1(0x76);
  136. wait(0.01);
  137.  
  138. /*
  139. cs = 1;
  140. spi.format(8,3);
  141. spi.frequency(250000);
  142.  
  143. cs = 0;
  144.  
  145. spi.write(0x76);
  146. wait(0.02);
  147.  
  148. cs = 1;
  149. */
  150. while(1) {
  151.  
  152. sendCharUart1(0x7B);
  153. wait(0.01);
  154. sendIntUart1(63);
  155. wait(0.01);
  156. sendCharUart1(0x7C);
  157. wait(0.01);
  158. sendIntUart1(91);
  159. wait(0.01);
  160. sendCharUart1(0x7D);
  161. wait(0.01);
  162. sendIntUart1(63);
  163. wait(0.01);
  164. sendCharUart1(0x7E);
  165. wait(0.01);
  166. sendIntUart1(91);
  167. wait(0.01);
  168.  
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement