Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include "mbed.h"
  2.  
  3. #define LED PF_1
  4. #define ADC_1 PA_0
  5. #define ADC_2 PA_1
  6. #define ADC_6 PA_6
  7. #define ADC_7 PA_7
  8. #define ADC_8 PB_0
  9. #define ADC_9 PB_1
  10.  
  11. #define DAC_1 PA_4
  12. #define DAC_2 PA_5
  13.  
  14. DigitalOut led(LED);
  15.  
  16. BufferedSerial serial(PB_10, PB_11);
  17.  
  18. I2C i2c(PB_7, PB_6);
  19.  
  20. AnalogIn adc1(ADC_1);
  21. AnalogIn adc2(ADC_2);
  22. AnalogIn adc6(ADC_6);
  23. AnalogIn adc7(ADC_7);
  24. AnalogIn adc8(ADC_8);
  25. AnalogIn adc9(ADC_9);
  26.  
  27. AnalogOut dac1(DAC_1);
  28. AnalogOut dac2(DAC_2);
  29.  
  30. FileHandle *mbed::mbed_override_console(int fd)
  31. {
  32.     return &serial;
  33. }
  34.  
  35. const int destaddr_write = 0x38;
  36. const int destaddr_read = 0x3A;
  37.  
  38. const int writecmd = 0x00;
  39. const int readcmd = 0xFF;
  40.  
  41. Thread pollingThread;
  42. Mutex buttonMutex;
  43. const unsigned char button[4] = {8,4,2,1};
  44. bool buttonPressed[4] = {false};
  45.  
  46. void keyscan() {
  47.     int rc;
  48.     char cmd[2];
  49.     char data_read;
  50.  
  51.     while(true) {
  52.         cmd[0] = 0x00;
  53.         rc = i2c.write(destaddr_read, &cmd[0], 1);
  54.         rc = i2c.read(destaddr_read, &data_read, 1);
  55.         if (rc != 0) {
  56.             printf("READ FAILED\r\n");
  57.         }
  58.  
  59.         for (int i=0; i<4; i++) {
  60.             if ((~data_read & button[i]) == button[i]) {
  61.                 buttonPressed[i] = true;
  62.                 ThisThread::flags_wait_any(0x01, true);
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. void i2c_init() {
  69.     char cmd[2];
  70.  
  71.     cmd[0] = 0x03;
  72.     cmd[1] = 0xFF;
  73.     i2c.write(destaddr_read, cmd, 2);
  74.  
  75.     cmd[0] = 0x02;
  76.     cmd[1] = 0x00;
  77.     i2c.write(destaddr_read, cmd, 2);
  78.  
  79.     cmd[0] = 0x03;
  80.     cmd[1] = 0x00;
  81.     i2c.write(destaddr_write, cmd, 2);
  82.  
  83.     cmd[0] = 0x02;
  84.     cmd[1] = 0x00;
  85.     i2c.write(destaddr_write, cmd, 2);
  86. }
  87.  
  88. int main() {
  89.     int rc;
  90.     char cmd[2];
  91.     char data_write = 1;
  92.     pollingThread.start(keyscan);
  93.  
  94.     i2c_init();
  95.  
  96.     while(true) {
  97.         // Cycle Interface Board LED
  98.         data_write = (data_write << 1) % 255;
  99.         cmd[0] = 0x01;
  100.         cmd[1] = data_write;
  101.         rc = i2c.write(destaddr_write, cmd, 2);
  102.         if (rc != 0) {
  103.             printf("WRITE FAILED\r\n");
  104.         }
  105.  
  106.         ThisThread::sleep_for(500);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement