by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:ee400a44f6f3, 2013-06-16 (annotated)
- Committer:
- robt
- Date:
- Sun Jun 16 15:41:50 2013 +0000
- Revision:
- 0:ee400a44f6f3
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:ee400a44f6f3 | 1 | /* Program Example 14.4: Sawtooth waveform on DAC output. View on oscilloscope. Port 0.26 is used for DAC output, i.e. mbed Pin 18 |
robt | 0:ee400a44f6f3 | 2 | */ |
robt | 0:ee400a44f6f3 | 3 | // function prototype |
robt | 0:ee400a44f6f3 | 4 | void delay(void); |
robt | 0:ee400a44f6f3 | 5 | // variable declarations |
robt | 0:ee400a44f6f3 | 6 | int dac_value; //the value to be output |
robt | 0:ee400a44f6f3 | 7 | //define addresses of control registers, as pointers to volatile data |
robt | 0:ee400a44f6f3 | 8 | #define DACR (*(volatile unsigned long *)(0x4008C000)) |
robt | 0:ee400a44f6f3 | 9 | #define PINSEL1 (*(volatile unsigned long *)(0x4002C004)) |
robt | 0:ee400a44f6f3 | 10 | |
robt | 0:ee400a44f6f3 | 11 | int main(){ |
robt | 0:ee400a44f6f3 | 12 | PINSEL1=0x00200000; //set bits 21-20 to 10 to enable analog out on P0.26 |
robt | 0:ee400a44f6f3 | 13 | while(1){ |
robt | 0:ee400a44f6f3 | 14 | for (dac_value=0;dac_value<1023;dac_value=dac_value+1){ |
robt | 0:ee400a44f6f3 | 15 | DACR=(dac_value<<6); |
robt | 0:ee400a44f6f3 | 16 | delay(); |
robt | 0:ee400a44f6f3 | 17 | } |
robt | 0:ee400a44f6f3 | 18 | } |
robt | 0:ee400a44f6f3 | 19 | } |
robt | 0:ee400a44f6f3 | 20 | |
robt | 0:ee400a44f6f3 | 21 | void delay(void){ //delay function. |
robt | 0:ee400a44f6f3 | 22 | int j; //loop variable j |
robt | 0:ee400a44f6f3 | 23 | for (j=0; j<1000000; j++) { |
robt | 0:ee400a44f6f3 | 24 | j++; |
robt | 0:ee400a44f6f3 | 25 | j--; //waste time |
robt | 0:ee400a44f6f3 | 26 | } |
robt | 0:ee400a44f6f3 | 27 | } |
robt | 0:ee400a44f6f3 | 28 |