ADC 사용하기

Embeded : 2009. 1. 7. 15:42
반응형
ADC로 전압측정해서 7-Segment로 전압 출력하는 프로그램.
  1. #include "../../../hw_ints.h"  
  2. #include "../../../hw_adc.h"  
  3. #include "../../../hw_memmap.h"  
  4. #include "../../../hw_types.h"  
  5. #include "../../../src/debug.h"  
  6. #include "../../../src/gpio.h"  
  7. #include "../../../src/adc.h"  
  8. #include "../../../src/interrupt.h"  
  9. #include "../../../src/sysctl.h"  
  10. #include "../../../src/watchdog.h"  
  11. #include "../../../src/timer.h"  
  12. #include "../rit128x96x4.h"  
  13.   
  14. //*****************************************************************************  
  15. //  
  16. //! \addtogroup ek_lm3s8962_list  
  17. //! <h1>Watchdog (watchdog)</h1>  
  18. //!  
  19. //! This example application demonstrates the use of the watchdog as a simple  
  20. //! heartbeat for the system.  If the watchdog is not periodically fed, it will  
  21. //! reset the system.  Each time the watchdog is fed, the LED is inverted so  
  22. //! that it is easy to see that it is being fed, which occurs once every  
  23. //! second.  
  24. //  
  25. //*****************************************************************************  
  26.   
  27. //*****************************************************************************  
  28. //  
  29. // The error routine that is called if the driver library encounters an error.  
  30. //  
  31. //*****************************************************************************  
  32. #ifdef DEBUG  
  33. void  
  34. __error__(char *pcFilename, unsigned long ulLine)  
  35. {  
  36. }  
  37. #endif  
  38.   
  39. //*****************************************************************************  
  40. // The interrupt handler for the watchdog.  This feeds the dog (so that the  
  41. // processor does not get reset) and winks the LED.  
  42. //*****************************************************************************  
  43. void  
  44. WatchdogIntHandler(void)  
  45. {  
  46. /*    
  47.     //  
  48.     // Clear the watchdog interrupt.  
  49.     //  
  50.     WatchdogIntClear(WATCHDOG_BASE);  
  51.   
  52.     //  
  53.     // Invert the GPIO F0 value.  
  54.     //  
  55.     GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,  
  56.                  GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0) ^ GPIO_PIN_0);  
  57. */  
  58. }  
  59. void myTimer(void);  
  60. void printNUM(unsigned int num);  
  61. void printNUM2(unsigned int num);  
  62.   
  63. int main(void)  
  64. {  
  65.     // Set the clocking to run directly from the crystal.  
  66.     SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |  
  67.                    SYSCTL_XTAL_8MHZ);  
  68.   
  69.     ///GPIOA Set, ADC, Timer Set  
  70.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);    //port A enable  
  71.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);    //port C enable  
  72.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);    //port D enable  
  73.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);    //port G enable  
  74.     SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);      //ADC enable  
  75.     SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);   //Timer0 enable  
  76.       
  77.     //ADCProcessorTrigger()에서 ADC 시작. 프로세서 트리거가 일어날때 작동한다.  
  78.     ADCSequenceConfigure(ADC_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);  
  79.     //ADC0의 sequence를 구성한다.sequence number 0인 ADC를 single ended mode로 setting  
  80.     ADCSequenceStepConfigure(ADC_BASE, 0, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH0);  
  81.     //ADC를 사용 가능하게 해준다.  
  82.     ADCSequenceEnable(ADC_BASE, 0);  
  83.   
  84.     //Port A1,3,5,G0 set to output type  
  85.     GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_1); //A1  
  86.     GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3); //A3  
  87.     GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_5); //A5  
  88.     GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4); //C4  
  89.     GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_6); //C6  
  90.     GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_1); //D1  
  91.     GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3); //D3  
  92.     GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_0); //G0  
  93.     GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_1); //G1  
  94.   
  95.   
  96.     TimerConfigure(TIMER0_BASE,TIMER_CFG_32_BIT_PER);   //32-BIT PERIODIC TIMER  
  97.     TimerLoadSet(TIMER0_BASE,TIMER_A,6000000);  
  98.     TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);//Timer0 interrupt enable  
  99.     TimerIntRegister(TIMER0_BASE,TIMER_A, myTimer); //timer interrupt 발생시 myTimer 라는 함수의 이벤트 발생  
  100.     TimerEnable(TIMER0_BASE,TIMER_A);                               //Timer Start  
  101.   
  102.   
  103.     while(1){}  
  104. }  
  105.   
  106. void myTimer(void)  
  107. {  
  108.     unsigned int Voltage;  
  109.     unsigned long ulValue;  
  110.     unsigned long readGPIO;  
  111.     unsigned int i;  
  112.       
  113.     //This function triggers a processor-initiated sample sequence if the sample sequence trigger is  
  114.     //configured to ADC_TRIGGER_PROCESSOR.  
  115.     ADCProcessorTrigger(ADC_BASE, 0);  
  116.       
  117.     //sample sequence 가 완료될때 까지 대기한다.  
  118.     while(!ADCIntStatus(ADC_BASE, 0, false)){}  
  119.   
  120.     // Read the value from the ADC.  
  121.     ADCSequenceDataGet(ADC_BASE, 0, &ulValue);//전압 값을 받아옴  
  122.       
  123.     Voltage = (int)((3.0/1023)*ulValue*10);  
  124.   
  125.   
  126.     //-----------폼으로 껌뻑이게 했음----------------------//  
  127.     if(GPIOPinRead(GPIO_PORTG_BASE,GPIO_PIN_0))  
  128.             GPIOPinWrite(GPIO_PORTG_BASE,GPIO_PIN_0,0);  
  129.     else  
  130.         GPIOPinWrite(GPIO_PORTG_BASE,GPIO_PIN_0,GPIO_PIN_0);  
  131.     //-----------폼으로 껌뻑이게 했음----------------------//  
  132.       
  133.   
  134.     printNUM(Voltage%10);  
  135.     printNUM2(Voltage/10);  
  136.   
  137.     TimerIntClear(TIMER0_BASE,TIMER_TIMA_TIMEOUT);//Timer Interupt clear  
  138. }  
  139.   
  140.   
  141.   
  142.   
  143.   
  144. void printNUM(unsigned int num)  
  145. {  
  146.     if (num&1)  
  147.         GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_5,GPIO_PIN_5);  
  148.     else  
  149.         GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_5,0);  
  150.           
  151.     if (num&2)  
  152.         GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);  
  153.     else  
  154.         GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);  
  155.           
  156.     if (num&4)  
  157.         GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_1,GPIO_PIN_1);  
  158.     else  
  159.         GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_1,0);  
  160.           
  161.     if (num&8)  
  162.         GPIOPinWrite(GPIO_PORTC_BASE,GPIO_PIN_4,GPIO_PIN_4);  
  163.     else  
  164.         GPIOPinWrite(GPIO_PORTC_BASE,GPIO_PIN_4,0);  
  165. }  
  166.   
  167.   
  168. void printNUM2(unsigned int num)  
  169. {  
  170.     if (num&1)  
  171.         GPIOPinWrite(GPIO_PORTC_BASE,GPIO_PIN_6,GPIO_PIN_6);  
  172.     else  
  173.         GPIOPinWrite(GPIO_PORTC_BASE,GPIO_PIN_6,0);  
  174.           
  175.     if (num&2)  
  176.         GPIOPinWrite(GPIO_PORTG_BASE,GPIO_PIN_1,GPIO_PIN_1);  
  177.     else  
  178.         GPIOPinWrite(GPIO_PORTG_BASE,GPIO_PIN_1,0);  
  179.           
  180.     if (num&4)  
  181.         GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_3,GPIO_PIN_3);  
  182.     else  
  183.         GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_3,0);  
  184.           
  185.     if (num&8)  
  186.         GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_1,GPIO_PIN_1);  
  187.     else  
  188.         GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_1,0);  
  189. }  


7Segment 로 입력전압을 측정결과를 출력해야 하는데

7세그먼트가 맛이 간것인지.... 불이 안들어오는 부분이 많아서 테스트를 제대로 못해봤다.

그래서 대충~ 전압값에 따라서 다른핀으로 출력을 하도록 만들었는데 뭔가 좀 잘못된듯 하다. -_-;;

7segment 를 다시 구해서 잘 해봐야 겠다.

약간의 삽질을 해본 결과 몇개의 7 Segment는 맛이간것이 맞았다.

하다가 보니까 LED 들어오는지 확인할 수 있는 핀이 있었다는것을 알아냈다. ㅋ 써있는데도 모르고 있다가 우연히 꼽았는데 알게되었다.  light 테스트 핀에 연결하니 확실하게 테스트가 가능했다.

CPU 특성상 3V 에 해상도가 1K라서 Voltage 계산법이 저렇게 되어있다.

반응형

'Embeded' 카테고리의 다른 글

터치스크린 쓰기.  (0) 2009.01.23
Timer 사용하기  (0) 2009.01.05
LM3S8962 GPIO 연습 코드  (0) 2009.01.02
Posted by Real_G