Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 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. #define CTRL_REG1 0x2A
  20. #define CTRL_REG1_ACTIVE 0x01
  21. #define UINT14_MAX 16383
  22. #define MMA8452_I2C_ADDRESS (0x1d<<1)
  23.  
  24. unsigned int* pinsel0 = reinterpret_cast<unsigned int*>(0x4002C000);
  25. unsigned int* pinsel4 = reinterpret_cast<unsigned int*>(0x4002C010);
  26. unsigned int* pinmode0 = reinterpret_cast<unsigned int*>(0x4002C040);
  27. unsigned int* pinmode4 = reinterpret_cast<unsigned int*>(0x4002C050);
  28.  
  29. unsigned int* fio2dir0 = reinterpret_cast<unsigned int*>(0x2009C040);
  30.  
  31. unsigned int* io2intenr = reinterpret_cast<unsigned int*>(0x400280B0);
  32. unsigned int* io2intstatr = reinterpret_cast<unsigned int*>(0x400280A4);
  33. unsigned int* io2intclr = reinterpret_cast<unsigned int*>(0x400280AC);
  34.  
  35. unsigned int* uart1FCR = reinterpret_cast<unsigned int*>(0x40010008);
  36. unsigned int* uart1LCR = reinterpret_cast<unsigned int*>(0x4001000C);
  37. unsigned int* uart1DLL = reinterpret_cast<unsigned int*>(0x40010000);
  38. unsigned int* uart1DLM = reinterpret_cast<unsigned int*>(0x40010004);
  39. unsigned int* uart1THR = reinterpret_cast<unsigned int*>(0x40010000);
  40.  
  41. int cijfer[12] = {63,6,91,79,102,109,124,7,127,103,64,0};
  42.  
  43. I2C i2c(p28, p27);
  44.  
  45. int last_display;
  46. int last_brightness;
  47. int _count;
  48. int angles[3];
  49. DigitalIn in(p22);
  50.  
  51. class Data {
  52. public:
  53. Data(int pin) : _input(pin) { }
  54. ~Data() { }
  55.  
  56. void push() {
  57. char tmp = in.read();
  58. _trame = _trame << 1;
  59. _trame = _trame | tmp;
  60. }
  61. char get_trame() {
  62. char tmp = _trame;
  63. _trame= 0x00;
  64. return tmp;
  65. }
  66.  
  67. bool validate() {
  68. int parity = _trame & 1;
  69. int ones =0;
  70. for (int i =0;i<5;i++){
  71. if (_trame & 1<< i)
  72. ones++;
  73. }
  74. if ( ( (parity == 1) && (ones % 2 == 0) ) || ((parity == 0 ) && (ones % 2 != 0)) ){
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80. private:
  81. volatile char _trame;
  82. int _input;
  83. };
  84.  
  85. void waitDataCleared() {
  86. while((*uart1LCR & (~(1<<SBIT_THRE))) == 0) { wait(0.000001); }
  87. }
  88.  
  89. void sendCharUart1(char data) {
  90. waitDataCleared();
  91. *uart1THR = data;
  92. }
  93.  
  94. void sendIntUart1(int data) {
  95. waitDataCleared();
  96. *uart1THR = (data & 0xFF);
  97. }
  98.  
  99. void writeInt(int value) {
  100.  
  101. int x1,x2,x3,x4;
  102.  
  103. if (value < 0) {
  104. value = value * -1;
  105.  
  106. x1 = value%10;
  107. x2 = value%100/10;
  108. x3 = 10;
  109. x4 = 11;
  110. }
  111. else {
  112. x1 = value%10;
  113. x2 = value%100/10;
  114. x3 = 11;
  115. x4 = 11;
  116. }
  117.  
  118. sendCharUart1(0x7B);
  119. wait(0.01);
  120. sendCharUart1(cijfer[x4]);
  121. wait(0.01);
  122. sendCharUart1(0x7C);
  123. wait(0.01);
  124. sendCharUart1(cijfer[x3]);
  125. wait(0.01);
  126. sendCharUart1(0x7D);
  127. wait(0.01);
  128. sendCharUart1(cijfer[x2]);
  129. wait(0.01);
  130. sendCharUart1(0x7E);
  131. wait(0.01);
  132. sendCharUart1(cijfer[x1]);
  133. wait(0.01);
  134. }
  135.  
  136. Data _packet(pin22);
  137.  
  138. void runCmd(int trame) {
  139.  
  140. int cmdBit = trame & 0b10000;
  141. cmdBit = cmdBit >>4;
  142. int cmd = trame & 0b01110;
  143. cmd = cmd >>1;
  144. int mask = 0b111;
  145. int dec = 0b000000; //afficheur de decimaux
  146.  
  147. if (cmd==5){
  148. sendCharUart1(0x7A);
  149. wait(0.01);
  150. sendCharUart1(last_brightness+10);
  151. wait(0.01);
  152. return;
  153. }
  154. if (cmdBit) {
  155. dec = 1<<(cmd);
  156. dec = last_display | dec;
  157. last_display = dec;
  158. } else {
  159. dec = 1<<(cmd);
  160. dec = last_display & ~dec;
  161. last_display = dec;
  162. }
  163.  
  164. writeInt(dec);
  165. sendCharUart1(0x77);
  166. wait(0.01);
  167. sendIntUart1(dec);
  168. wait(0.01);
  169. }
  170.  
  171. void EINT3_init (void) {
  172. *pinsel4 &= ~((1 << 7) | (1 << 6));
  173. *pinmode4 |= (1 << 7) | (1 << 6);
  174. *pinsel4 &= ~((1 << 9) | (1 << 8));
  175. *pinmode4 |= (1 << 9) | (1 << 8);
  176. *pinsel4 &= ~((1 << 11) | (1 << 10));
  177. *pinmode4 |= (1 << 11) | (1 << 10);
  178.  
  179. *fio2dir0 &= ~pin21;
  180. *fio2dir0 &= ~pin22;
  181. *fio2dir0 &= ~pin23;
  182.  
  183. *io2intenr |= (pin21);
  184. //*io2intenr |= (pin22);
  185. *io2intenr |= (pin23);
  186.  
  187. NVIC_EnableIRQ(EINT3_IRQn);
  188. }
  189.  
  190. void updateAngleRef() {
  191.  
  192. }
  193.  
  194. //EINT3 Handler
  195. extern "C" void EINT3_IRQHandler(void) __irq {
  196. if ((*io2intstatr & pin21) == pin21) {
  197. *io2intclr = (pin21);
  198.  
  199. _count++;
  200. if (_count >1 && _count <7) {
  201. _packet.push();
  202. } else if (_count == 7) {
  203. _count = 0;
  204. runCmd(_packet.get_trame());
  205. }
  206.  
  207. return;
  208. }
  209.  
  210. if ((*io2intstatr & pin23) == pin23) {
  211. *io2intclr = pin23;
  212.  
  213. LPC_GPIO1->FIOSET |= LED3;
  214.  
  215. updateAngleRef();
  216. wait(0.01);
  217. LPC_GPIO1->FIOCLR |= LED3;
  218.  
  219. return;
  220. }
  221. }
  222.  
  223. void initUart1(unsigned int baudrate) {
  224.  
  225. *pinsel0 &= ~0xF0000000;
  226. *pinsel0 |= 0x40000000;
  227. *pinmode0 |= 0xC0000000;
  228.  
  229. *uart1FCR = (1<<SBIT_FIFO) | (1<<SBIT_RxFIFO) | (1<<SBIT_TxFIFO);
  230. *uart1LCR = (0x03<<SBIT_WordLength) | (1<<SBIT_DLAB);
  231.  
  232. int _baudrate = ((96000000/4) / (16 * baudrate));
  233.  
  234. *uart1DLL = _baudrate & 0xFF;
  235. *uart1DLM = (_baudrate << 0x08) & 0xFF;
  236.  
  237. *uart1LCR &= ~(1<<SBIT_DLAB);
  238. }
  239.  
  240. void readRegs(int reg_addr, uint8_t * data, int len)
  241. {
  242. char t[1] = {reg_addr};
  243. i2c.write(MMA8452_I2C_ADDRESS, t, 1, true);
  244. i2c.read(MMA8452_I2C_ADDRESS, (char *)data, len);
  245. }
  246.  
  247. void writeRegs(uint8_t * data, int len)
  248. {
  249. i2c.write(MMA8452_I2C_ADDRESS, (char *)data, len);
  250. }
  251.  
  252. float get_acc_deg(uint8_t * data)
  253. {
  254. int16_t acc = (data[0] << 6) | (data[1] >> 2);
  255. if (acc > UINT14_MAX/2)
  256. acc -= UINT14_MAX;
  257. float tmp = (float(acc/4096.0));
  258. float deg = tmp*90;
  259. return deg;
  260. }
  261.  
  262. void readAngle(int* angles){
  263. int reg_addr = 0x01;//read cmd register adress
  264. uint8_t data[6];
  265.  
  266. //Read x,y,z output values
  267. readRegs(reg_addr, data, 6);
  268.  
  269. angles[0] = get_acc_deg(&data[0]);
  270. angles[1] = get_acc_deg(&data[2]);
  271. angles[2] = get_acc_deg(&data[4]);
  272. }
  273.  
  274. int main() {
  275.  
  276. LPC_GPIO1->FIODIR |= LED1 | LED2 | LED3 | LED4;
  277. LPC_GPIO1->FIOSET |= LED4;
  278.  
  279. EINT3_init();
  280.  
  281. initUart1(9600);
  282. wait(1);
  283.  
  284. sendCharUart1(0x76);
  285. wait(0.01);
  286. void readRegs(int reg_addr, uint8_t * data, int len);
  287. void writeRegs(uint8_t * data, int len);
  288. float get_acc_deg(uint8_t * data);
  289. //set full scale selection ACTIVE (reads LSBs)
  290. uint8_t init[2] = {CTRL_REG1,CTRL_REG1_ACTIVE};
  291. writeRegs(init,2);
  292. while(1) {
  293. readAngle(angles);
  294. wait(1);
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement