![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
can version
Fork of dac_controller2 by
main.cpp@3:29aa7212dda4, 2014-04-29 (annotated)
- Committer:
- dzl5187
- Date:
- Tue Apr 29 03:13:12 2014 +0000
- Revision:
- 3:29aa7212dda4
- Parent:
- 2:c0287d9c2226
final_version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dzl5187 | 1:4bd950ab8756 | 1 | #include "mbed.h" |
dzl5187 | 1:4bd950ab8756 | 2 | #include "log.h" |
dzl5187 | 1:4bd950ab8756 | 3 | #include "DAC.h" |
dzl5187 | 1:4bd950ab8756 | 4 | #include "mcp4725.h" |
dzl5187 | 3:29aa7212dda4 | 5 | #define GROUP_ID 2 |
dzl5187 | 1:4bd950ab8756 | 6 | Timer programTimer; |
dzl5187 | 1:4bd950ab8756 | 7 | //Timer DACC_timer; |
dzl5187 | 1:4bd950ab8756 | 8 | //DigitalOut testLed(LED1); |
erichill44 | 2:c0287d9c2226 | 9 | Ticker read; |
erichill44 | 2:c0287d9c2226 | 10 | CAN can2(p30,p29); |
dzl5187 | 3:29aa7212dda4 | 11 | DigitalOut led2(LED2); |
erichill44 | 2:c0287d9c2226 | 12 | |
erichill44 | 2:c0287d9c2226 | 13 | void canread() { |
erichill44 | 2:c0287d9c2226 | 14 | CANMessage msg; |
erichill44 | 2:c0287d9c2226 | 15 | if(can2.read(msg)) { |
dzl5187 | 3:29aa7212dda4 | 16 | led2 =! led2; |
dzl5187 | 3:29aa7212dda4 | 17 | if( msg.id == GROUP_ID){ |
dzl5187 | 3:29aa7212dda4 | 18 | logMessage( "To our group: Receive command %d %d %d! \n", msg.data[0],msg.data[1],msg.data[2]); |
dzl5187 | 3:29aa7212dda4 | 19 | // data[0]: 1 -> on , 0->off |
dzl5187 | 3:29aa7212dda4 | 20 | // data[1]: 1 -> sine, 0->square |
dzl5187 | 3:29aa7212dda4 | 21 | // data[2]: 1 -> 1HZ, 0->10HZ |
dzl5187 | 3:29aa7212dda4 | 22 | //DAC-on |
dzl5187 | 3:29aa7212dda4 | 23 | if( msg.data[0] == 1){ |
dzl5187 | 3:29aa7212dda4 | 24 | DAC_start(); |
dzl5187 | 3:29aa7212dda4 | 25 | //freq 1HZ |
dzl5187 | 3:29aa7212dda4 | 26 | if( msg.data[1] == 1){ |
dzl5187 | 3:29aa7212dda4 | 27 | //sine |
dzl5187 | 3:29aa7212dda4 | 28 | if(msg.data[2] == 1){ |
dzl5187 | 3:29aa7212dda4 | 29 | DAC_set(3.0, FREQ_1HZ,FORM_SIN); |
dzl5187 | 3:29aa7212dda4 | 30 | logMessage("Set sine wave, 1hz \n"); |
dzl5187 | 3:29aa7212dda4 | 31 | } |
dzl5187 | 3:29aa7212dda4 | 32 | else{ |
dzl5187 | 3:29aa7212dda4 | 33 | DAC_set(3.0, FREQ_1HZ,FORM_SQUARE); |
dzl5187 | 3:29aa7212dda4 | 34 | logMessage("Set square wave, 1hz \n"); |
dzl5187 | 3:29aa7212dda4 | 35 | } |
dzl5187 | 3:29aa7212dda4 | 36 | } |
dzl5187 | 3:29aa7212dda4 | 37 | //freq 10HZ |
dzl5187 | 3:29aa7212dda4 | 38 | else{ |
dzl5187 | 3:29aa7212dda4 | 39 | //sine |
dzl5187 | 3:29aa7212dda4 | 40 | if(msg.data[2] == 1){ |
dzl5187 | 3:29aa7212dda4 | 41 | DAC_set(3.0, FREQ_10HZ,FORM_SIN); |
dzl5187 | 3:29aa7212dda4 | 42 | logMessage("Set sine wave, 10hz \n"); |
dzl5187 | 3:29aa7212dda4 | 43 | } |
dzl5187 | 3:29aa7212dda4 | 44 | else{ |
dzl5187 | 3:29aa7212dda4 | 45 | DAC_set(3.0, FREQ_10HZ,FORM_SQUARE); |
dzl5187 | 3:29aa7212dda4 | 46 | logMessage("Set square wave, 10hz \n"); |
dzl5187 | 3:29aa7212dda4 | 47 | } |
erichill44 | 2:c0287d9c2226 | 48 | } |
erichill44 | 2:c0287d9c2226 | 49 | } |
dzl5187 | 3:29aa7212dda4 | 50 | //DAC-off |
dzl5187 | 3:29aa7212dda4 | 51 | else{ |
dzl5187 | 3:29aa7212dda4 | 52 | DAC_stop(); |
dzl5187 | 3:29aa7212dda4 | 53 | logMessage("Turn off !\n"); |
dzl5187 | 3:29aa7212dda4 | 54 | } |
dzl5187 | 3:29aa7212dda4 | 55 | } |
dzl5187 | 3:29aa7212dda4 | 56 | else{ |
dzl5187 | 3:29aa7212dda4 | 57 | logMessage("Recevie command to group %d \n",msg.id); |
dzl5187 | 3:29aa7212dda4 | 58 | } |
dzl5187 | 3:29aa7212dda4 | 59 | } |
dzl5187 | 3:29aa7212dda4 | 60 | } |
dzl5187 | 1:4bd950ab8756 | 61 | |
dzl5187 | 1:4bd950ab8756 | 62 | int main() |
dzl5187 | 3:29aa7212dda4 | 63 | { |
dzl5187 | 1:4bd950ab8756 | 64 | DAC_set(3.0, FREQ_1HZ, FORM_SQUARE); |
dzl5187 | 3:29aa7212dda4 | 65 | programTimer.start(); |
dzl5187 | 3:29aa7212dda4 | 66 | logMessage( "Finish initilized! \n"); |
dzl5187 | 3:29aa7212dda4 | 67 | read.attach(&canread,1); |
dzl5187 | 1:4bd950ab8756 | 68 | while(1) { |
dzl5187 | 1:4bd950ab8756 | 69 | DAC_generate(); |
dzl5187 | 1:4bd950ab8756 | 70 | } |
dzl5187 | 1:4bd950ab8756 | 71 | |
dzl5187 | 1:4bd950ab8756 | 72 | |
dzl5187 | 1:4bd950ab8756 | 73 | } |