Advertisement
Guest User

Untitled

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