MCP4922_Gen_Sinewave_copy
Dependencies: mbed
Fork of MCP4922_Sinewave by
main.cpp@3:cc5834a48eba, 2015-11-16 (annotated)
- Committer:
- NorNick
- Date:
- Mon Nov 16 17:01:56 2015 +0000
- Revision:
- 3:cc5834a48eba
- Parent:
- 2:2244c8986987
Finish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NorNick | 3:cc5834a48eba | 1 | /* |
NorNick | 3:cc5834a48eba | 2 | #include "mbed.h" |
NorNick | 3:cc5834a48eba | 3 | #include "MCP4922.h" |
soulx | 2:2244c8986987 | 4 | |
NorNick | 3:cc5834a48eba | 5 | MCP4922 MCP(SPI_MOSI, SPI_SCK,SPI_CS); // MOSI, SCLK, CS |
NorNick | 3:cc5834a48eba | 6 | //Serial pc(USBTX,USBRX); |
NorNick | 3:cc5834a48eba | 7 | |
NorNick | 3:cc5834a48eba | 8 | #define PI 3.141593 |
NorNick | 3:cc5834a48eba | 9 | |
NorNick | 3:cc5834a48eba | 10 | */ |
NorNick | 3:cc5834a48eba | 11 | /**** Main Function ***//* |
NorNick | 3:cc5834a48eba | 12 | int main(void) |
NorNick | 3:cc5834a48eba | 13 | { |
NorNick | 3:cc5834a48eba | 14 | MCP.frequency(10000000); |
NorNick | 3:cc5834a48eba | 15 | |
NorNick | 3:cc5834a48eba | 16 | uint16_t i=0; |
NorNick | 3:cc5834a48eba | 17 | uint32_t Freq = 1000; |
NorNick | 3:cc5834a48eba | 18 | uint32_t Fsample=166667;//Sample is 166,667 Hz. |
NorNick | 3:cc5834a48eba | 19 | uint32_t Ampi = 612; |
NorNick | 3:cc5834a48eba | 20 | uint16_t Maxindex; |
NorNick | 3:cc5834a48eba | 21 | |
NorNick | 3:cc5834a48eba | 22 | Maxindex = (int) (Fsample / Freq); |
NorNick | 3:cc5834a48eba | 23 | if(Maxindex > 800) { |
NorNick | 3:cc5834a48eba | 24 | Fsample = Freq * 800; |
NorNick | 3:cc5834a48eba | 25 | Maxindex = 800; |
NorNick | 3:cc5834a48eba | 26 | } |
NorNick | 3:cc5834a48eba | 27 | |
NorNick | 3:cc5834a48eba | 28 | while(1) { |
NorNick | 3:cc5834a48eba | 29 | MCP.writeA((int)(Ampi * (1+ sin((2*PI*Freq*i)/Fsample))/2)); |
NorNick | 3:cc5834a48eba | 30 | i++; |
NorNick | 3:cc5834a48eba | 31 | if(i > Maxindex) |
NorNick | 3:cc5834a48eba | 32 | i=0; |
NorNick | 3:cc5834a48eba | 33 | } |
NorNick | 3:cc5834a48eba | 34 | } |
NorNick | 3:cc5834a48eba | 35 | */ |
NorNick | 3:cc5834a48eba | 36 | /* |
jf1vrr | 0:5737b1972549 | 37 | #include "mbed.h" |
jf1vrr | 0:5737b1972549 | 38 | #include "MCP4922.h" |
jf1vrr | 0:5737b1972549 | 39 | |
soulx | 2:2244c8986987 | 40 | MCP4922 MCP(SPI_MOSI, SPI_SCK,SPI_CS); // MOSI, SCLK, CS |
jf1vrr | 0:5737b1972549 | 41 | |
NorNick | 3:cc5834a48eba | 42 | #define PI 3.141593 |
jf1vrr | 0:5737b1972549 | 43 | |
soulx | 2:2244c8986987 | 44 | int main(void) |
soulx | 2:2244c8986987 | 45 | { |
NorNick | 3:cc5834a48eba | 46 | MCP.frequency(10000000); |
jf1vrr | 0:5737b1972549 | 47 | |
soulx | 2:2244c8986987 | 48 | uint16_t i=0; |
NorNick | 3:cc5834a48eba | 49 | uint16_t Ampi=511; |
NorNick | 3:cc5834a48eba | 50 | uint32_t Freq = 1000; |
NorNick | 3:cc5834a48eba | 51 | |
soulx | 2:2244c8986987 | 52 | while(1) { |
NorNick | 3:cc5834a48eba | 53 | |
NorNick | 3:cc5834a48eba | 54 | MCP.writeA((int)(Ampi * (1+sin((2*PI*Freq*i)/800000))/2)); |
NorNick | 3:cc5834a48eba | 55 | //if(i < 0x0FFF) |
NorNick | 3:cc5834a48eba | 56 | if(i < 800) |
soulx | 2:2244c8986987 | 57 | i++; |
soulx | 2:2244c8986987 | 58 | else |
NorNick | 3:cc5834a48eba | 59 | //i=360; |
NorNick | 3:cc5834a48eba | 60 | //i=90; |
soulx | 2:2244c8986987 | 61 | i=0; |
NorNick | 3:cc5834a48eba | 62 | //MCP.writeA((int)(Ampi * sin(2*PI*Freq*i)/2)); |
NorNick | 3:cc5834a48eba | 63 | //wait_us(100); |
NorNick | 3:cc5834a48eba | 64 | } |
NorNick | 3:cc5834a48eba | 65 | } |
NorNick | 3:cc5834a48eba | 66 | */ |
NorNick | 3:cc5834a48eba | 67 | /* สรุปแปร๊ป |
NorNick | 3:cc5834a48eba | 68 | ถ้าไม่มี 1+... นี่ กราฟจะเป็น ครึ่งภูเขาผสมกับกราฟขีดขึ้นดิ่งตรง |
NorNick | 3:cc5834a48eba | 69 | ถ้าไม่มี /800000 ที่จริงเป็นเลขอะไรก็ได้ในหลักแสนอ่ะ กราฟจะเป็นแค่ภูเขาหนึ่งลูกเท่านั้น (ครึ่งsin) |
NorNick | 3:cc5834a48eba | 70 | ถ้าไม่มี /2 ตรงยอดกราฟจะเป็นเส้นตรงปาดหน้าภูเขา |
NorNick | 3:cc5834a48eba | 71 | ถ้าไม่มี 1+ กับ /2 ล่ะ เหมือนกราฟแบบข้อแรก |
NorNick | 3:cc5834a48eba | 72 | ถ้าไม่มี /800000 กับ /2 กราฟช่วงแรกโค้ง สักพักเป็นแนวตรง ขาลงก็โค้ง |
NorNick | 3:cc5834a48eba | 73 | ถ้าไม่มี 1+ กับ /800000 กราฟเหมือนข้อสอง |
NorNick | 3:cc5834a48eba | 74 | ถ้าไม่มี 1+ , /800000 , /2 ล่ะ ก็เป็นแค่กราฟภูเขาหนึ่งลูกเอง แถมขีดลงให้ด้วย (ครี่งsin)*/ |
NorNick | 3:cc5834a48eba | 75 | |
NorNick | 3:cc5834a48eba | 76 | #include "mbed.h" |
NorNick | 3:cc5834a48eba | 77 | #include "MCP4922.h" |
NorNick | 3:cc5834a48eba | 78 | |
NorNick | 3:cc5834a48eba | 79 | MCP4922 MCP(SPI_MOSI, SPI_SCK,SPI_CS); // MOSI, SCLK, CS |
NorNick | 3:cc5834a48eba | 80 | |
NorNick | 3:cc5834a48eba | 81 | #define PI 3.141593 |
NorNick | 3:cc5834a48eba | 82 | |
NorNick | 3:cc5834a48eba | 83 | int main(void) |
NorNick | 3:cc5834a48eba | 84 | { |
NorNick | 3:cc5834a48eba | 85 | MCP.frequency(100000000);//Frequ 100Mhz |
NorNick | 3:cc5834a48eba | 86 | |
NorNick | 3:cc5834a48eba | 87 | uint16_t i=0; |
NorNick | 3:cc5834a48eba | 88 | uint16_t Ampi=511; |
NorNick | 3:cc5834a48eba | 89 | |
NorNick | 3:cc5834a48eba | 90 | while(1) { |
NorNick | 3:cc5834a48eba | 91 | for(i=0;i<360;i++){ |
NorNick | 3:cc5834a48eba | 92 | MCP.writeA((sin(((i*PI)/180)))*Ampi+2048); |
NorNick | 3:cc5834a48eba | 93 | wait_us(20); |
soulx | 2:2244c8986987 | 94 | } |
soulx | 2:2244c8986987 | 95 | |
soulx | 2:2244c8986987 | 96 | } |
NorNick | 3:cc5834a48eba | 97 | } |