by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:c0fbef1ae93c, 2013-06-16 (annotated)
- Committer:
- robt
- Date:
- Sun Jun 16 15:42:27 2013 +0000
- Revision:
- 0:c0fbef1ae93c
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:c0fbef1ae93c | 1 | /* Program Example 14.6: Explore ADC conversion times, programming control registers directly. ADC value is transferred to DAC, while an output pin is strobed to indicate conversion duration. Observe on oscilloscope |
robt | 0:c0fbef1ae93c | 2 | */ |
robt | 0:c0fbef1ae93c | 3 | //…. |
robt | 0:c0fbef1ae93c | 4 | //…. |
robt | 0:c0fbef1ae93c | 5 | |
robt | 0:c0fbef1ae93c | 6 | int main() { |
robt | 0:c0fbef1ae93c | 7 | FIO2DIR0=0xFF; // set lower bits port 2 to output |
robt | 0:c0fbef1ae93c | 8 | PINSEL1=0x00210000; //set bits 21-20 to 10 for analog output (mbed p18) |
robt | 0:c0fbef1ae93c | 9 | //and bits 17-16 to 01 to enable ADC channel 1 (AD0.1, mbed pin 16) |
robt | 0:c0fbef1ae93c | 10 | |
robt | 0:c0fbef1ae93c | 11 | //initialise the ADC. |
robt | 0:c0fbef1ae93c | 12 | //…. |
robt | 0:c0fbef1ae93c | 13 | //…. |
robt | 0:c0fbef1ae93c | 14 | while(1){ // infinite loop |
robt | 0:c0fbef1ae93c | 15 | // start A/D conversion by modifying bits in the AD0CR register |
robt | 0:c0fbef1ae93c | 16 | AD0CR &= (AD0CR & 0xFFFFFF00); |
robt | 0:c0fbef1ae93c | 17 | FIO2PIN0 |= 0x01; // OR bit 0 with 1 to set pin high |
robt | 0:c0fbef1ae93c | 18 | AD0CR |= (1 << ADC_channel) | (1 << 24); |
robt | 0:c0fbef1ae93c | 19 | // wait for it to finish by polling the ADC DONE bit |
robt | 0:c0fbef1ae93c | 20 | while((AD0GDR & 0x80000000) == 0) { |
robt | 0:c0fbef1ae93c | 21 | } |
robt | 0:c0fbef1ae93c | 22 | FIO2PIN0 &= ~0x01; // AND bit 0 with 0 to set pin low |
robt | 0:c0fbef1ae93c | 23 | |
robt | 0:c0fbef1ae93c | 24 | ADCdata = AD0GDR; // get the data from AD0GDR |
robt | 0:c0fbef1ae93c | 25 | AD0CR &= 0xF8FFFFFF; //stop ADC by setting START bits to zero |
robt | 0:c0fbef1ae93c | 26 | |
robt | 0:c0fbef1ae93c | 27 | // shift data 4 bits to right justify, and 2 more to give 10-bit ADC value |
robt | 0:c0fbef1ae93c | 28 | ADCdata=(ADCdata>>6)&0x03FF; //and mask |
robt | 0:c0fbef1ae93c | 29 | DACR=(ADCdata<<6); //could be merged with previous line, |
robt | 0:c0fbef1ae93c | 30 | // but separated for clarity |
robt | 0:c0fbef1ae93c | 31 | //delay(); //insert delay if wished |
robt | 0:c0fbef1ae93c | 32 | } |
robt | 0:c0fbef1ae93c | 33 | } |
robt | 0:c0fbef1ae93c | 34 |