Advertisement
Guest User

Untitled

a guest
May 27th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include "bmptk.h"
  2. #include "pins.h"
  3. #include "timer.h"
  4. #include "sw-uart.h"
  5.  
  6. void set_ad_converter(int port, int pin){
  7. // Enable A/D clock
  8. LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13);
  9.  
  10. // Enable IO config
  11. LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 16);
  12.  
  13. // Configure A/D pin
  14. LPC_IOCON->R_PIO1_0 &= ~(1 << 7);
  15. LPC_IOCON->R_PIO1_0 |= 0x02;
  16.  
  17. // Enable A/D
  18. LPC_SYSCON->PDRUNCFG &= ~(1 << 4);
  19.  
  20. LPC_ADC->CR |= (1 << 0);
  21. LPC_ADC->CR |= (2 << 8);
  22. LPC_ADC->CR |= (0 << 16);
  23. }
  24.  
  25. int read_ad_converter() {
  26. LPC_ADC->CR |= (1 << 24);
  27. int value = 0;
  28.  
  29. delay(100);
  30. // Check if the 31 bit of LPC_ADC->GDR is equal to 0
  31. while((value & (1 << 31)) == 0){
  32. value = LPC_ADC->GDR;
  33. }
  34.  
  35. uart_put_int_hexadecimal(value, 8);
  36.  
  37. value = (value & 0xFFC0) >> 6;
  38. LPC_ADC->CR &= ~(1 << 24);
  39.  
  40. return value;
  41. }
  42.  
  43. int main( void ){
  44. set_ad_converter(1, 0);
  45. LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 12);
  46. LPC_SYSCON->UARTCLKDIV = 0x01;
  47. timer_init();
  48. uart_init();
  49. uart_put_string("A/D conversion results\n");
  50. while(1) {
  51. uart_put_string("\n");
  52. uart_put_int_decimal(read_ad_converter());
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement