maygapixel

Untitled

Dec 16th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include "LPC214x.H" // LPC2148 MPU Register
  2. #include <stdio.h>
  3. #include "Timer.h"
  4. #include "uart0.h" // For Used Function printf
  5.  
  6. /* pototype section */
  7. void init_serial0 (void); // Initil UART-0
  8. int putchar (int ch); // Put Char to UART-0
  9. int getchar (void); // Get Char From Uart-0
  10. void delay(unsigned long int); // Delay Time Function
  11.  
  12.  
  13. unsigned int val; // ADC Result (HEX)
  14. int i = 0;
  15. int volt=0;
  16. const static unsigned short table_sine[64] = // Sine Function Table(12Bit)
  17. {
  18. 0x07FF, 0x08C8, 0x098E, 0x0A51,
  19. 0x0B0F, 0x0BC4, 0x0C71, 0x0D12,
  20. 0x0DA7, 0x0E2E, 0x0EA5, 0x0F0D,
  21. 0x0F63, 0x0FA6, 0x0FD7, 0x0FF5,
  22. 0x0FFF, 0x0FF5, 0x0FD7, 0x0FA6,
  23. 0x0F63, 0x0F0D, 0x0EA5, 0x0E2E,
  24. 0x0DA7, 0x0D12, 0x0C71, 0x0BC4,
  25. 0x0B0F, 0x0A51, 0x098E, 0x08C8,
  26. 0x07FF, 0x0736, 0x0670, 0x05AD,
  27. 0x04EF, 0x043A, 0x038D, 0x02EC,
  28. 0x0257, 0x01D0, 0x0159, 0x00F1,
  29. 0x009B, 0x0058, 0x0027, 0x0009,
  30. 0x0000, 0x0009, 0x0027, 0x0058,
  31. 0x009B, 0x00F1, 0x0159, 0x01D0,
  32. 0x0257, 0x02EC, 0x038D, 0x043A,
  33. 0x04EF, 0x05AD, 0x0670, 0x0736
  34. };
  35.  
  36. int main(void)
  37. {
  38. uart_init(); // Initial UART0 = 9600,N,8,1
  39. printf("\nCP-JR ARM7 USB-LPC2148...TEST ADC7(P0.5)\n"); // Call prinff Function
  40. printf("Input Voltage 0-3.3V to P0.5 For Test\n"); // Call prinff Function
  41.  
  42. // Initial ADC7 (GPIO-0.5) By Set PINSEL0[11:10=11]
  43. // xxxx xxxx xxxx xxxx xxxx 11xx xxxx xxxx
  44. PINSEL0 &= 0xFFFFF3FF; // Select ADC7 Pin Connect P0.5
  45. PINSEL0 |= 0x00000C00;
  46.  
  47. // Initial ADC0 (ADCR=0x01210680)
  48. AD0INTEN = (0x100)|(7<<1); // Disable ADC Interrupt
  49. AD0CR &= 0x00000000; // Clear All Bit Control
  50. AD0CR |= 0x00000080; // Select ADC = AIN7
  51. AD0CR |= 0x00000600; // ADC Clock = VBP(PCLK) / 7
  52. AD0CR |= 0x00010000; // Busrt = 1 = Conversion Continue
  53. AD0CR &= 0xFFF1FFFF; // CLKS = 000 = 10Bit : 11 Cycle Clock Conversion
  54. AD0CR |= 0x00200000; // PDN = 1 = Active ADC Module
  55. AD0CR &= 0xFF3FFFFF; // TEST[1:0] = 00 = Normal Mode
  56. AD0CR &= 0xF7FFFFFF; // EDGE = 0 = Conversion on Falling Edge
  57. AD0CR |= 0x01000000; // START = 001 = Start Conversion Now
  58.  
  59.  
  60. // Start Test Read ADC0 and Display on UART0 //
  61. while(1) // Loop Continue
  62. {
  63. do // Loop Read ADC1
  64. {
  65. val = AD0DR7; // Read A/D Data Register
  66. }
  67. while ((val & 0x80000000) == 0); // Wait ADC Conversion Complete
  68. val = (val >> 6) & 0x03E8; // Shift ADC Result to Integer
  69.  
  70. //volt = val * 3.3 / 1023.0;
  71. val = (val+50)/50; // Volt = ADC Result x [3.3V / 1024]
  72. printf("ADC7 Result = %d Volt.\n",val); // Display 3-Digit Result(0-3.3V)
  73. val = val/64;
  74. TimerInit();
  75. delay(10000);
  76. }
  77. }
  78.  
  79. /***********************/
  80. /* Delay Time Function */
  81. void delay(unsigned long int count1)
  82. {
  83. while(count1 > 0) {count1--;} // Loop Decrease Counter
  84. }
  85.  
  86. void TimerInit(void)
  87. {
  88. VPBDIV = 1; //Configure the VPB divi
  89. T0PR = 60; //Load prescaler
  90. T0TCR = 2; //Reset counter and prescaler
  91. T0MR0 = ((val)-1);
  92. T0MCR = 3; //On match reset the counter and generate an interrupt
  93. T0TCR = 1;
  94.  
  95. VICVectAddr4 = (unsigned)T0isr; //Set the timer ISR vector address
  96. VICVectCntl4 = (0x20 | 4); //Set channel
  97. VICIntEnable |= (1 << 4); //Enable the interrupt
  98. PINSEL1 &= 0xFFF3FFFF; // Select DAC Pin Connect P0.25
  99. PINSEL1 |= 0x00080000;
  100. }
  101.  
  102. void T0isr (void) __irq
  103. {
  104. DACR = ((table_sine[i]/4) << 6);
  105. i++;
  106. i &= 0x3F;
  107. T0IR = 1;
  108. VICVectAddr = 0;
  109. //printf("val = %d\n",val);
  110. }
  111.  
  112. void TimerOn(void){}
  113. void TimerOff(void){}
Add Comment
Please, Sign In to add comment