Pantid Chantangphol
/
MCP4922_Sinewave1
labSPI
Fork of MCP4922_Sinewave by
Revision 2:2244c8986987, committed 2015-08-24
- Comitter:
- soulx
- Date:
- Mon Aug 24 12:36:59 2015 +0000
- Parent:
- 1:09b69f38fc73
- Commit message:
- Triangle wave
Changed in this revision
diff -r 09b69f38fc73 -r 2244c8986987 MCP4922.h --- a/MCP4922.h Thu May 12 11:12:47 2011 +0000 +++ b/MCP4922.h Mon Aug 24 12:36:59 2015 +0000 @@ -30,8 +30,8 @@ /* Reference: Microchip Technology (2005), MCP4821/MCP4822 DAC Data Sheet. */ // MCP4922 reference voltage. -#define MCP4922_VREF 2048 // Reference voltage (mV) -//#define MCP4922_VREF 3300 // Reference voltage (mV) +//#define MCP4922_VREF 2048 // Reference voltage (mV) +#define MCP4922_VREF 3300 // Reference voltage (mV) /* Define possible combinations of 16-bit command register bits */ #define MCP4922_REG_A1 0x3000 // Channel A gain 1
diff -r 09b69f38fc73 -r 2244c8986987 TextLCD.lib --- a/TextLCD.lib Thu May 12 11:12:47 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 09b69f38fc73 -r 2244c8986987 main.cpp --- a/main.cpp Thu May 12 11:12:47 2011 +0000 +++ b/main.cpp Mon Aug 24 12:36:59 2015 +0000 @@ -1,56 +1,36 @@ -/*This is a program to output a sinwave in 12 bits -Digital to Analog converter MCP4922. Sampling frequency is -166,667Hz and produce an interrupt of Ticker every 6uS. -The Program calculates the data of the sinewave beforehand. -*/ + #include "mbed.h" -#include "TextLCD.h" #include "MCP4922.h" -MCP4922 MCP(p5, p7,p8); // MOSI, SCLK, CS -TextLCD lcd(p24, p26, p27, p28, p29, p30); -Ticker sampling; - -#define PI 3.141593 -#define MaxRes 800 -/// Global Variables -unsigned int OutData[MaxRes]; -unsigned int Maxindex; -unsigned long Fsample; -unsigned long Freq; -unsigned int index = 0; - -void GenWave(void) { - unsigned int i; +MCP4922 MCP(SPI_MOSI, SPI_SCK,SPI_CS); // MOSI, SCLK, CS - Maxindex = (int) (Fsample / Freq); - if(Maxindex > MaxRes) { - Fsample = Freq * MaxRes; - Maxindex = MaxRes; - } - for(i=0; i<Maxindex; i++) { - OutData[i] = (int)(0x1FF * (1+ sin((2*PI*Freq*i)/Fsample))/2); - } -} -void timer_int(){ - /// Output to D/A Converter - MCP.writeA(OutData[index]); - index++; - if(index >= Maxindex) index = 0; -} /**** Main Function ***/ -int main(void) { - MCP.frequency(20000000); - - lcd.cls(); - lcd.printf("MCP4922 Sinewave"); +int main(void) +{ + MCP.frequency(20000000); - Fsample = 166667; - Freq = 1000; - GenWave(); - - sampling.attach_us(&timer_int, 6); - while(1){ + uint16_t i=0; + uint8_t state=0; + while(1) { + if(i < 0x0FFF) + { + i++; + } + else + { + i=0; + state = ~state; + } + + if(state ==0) + { + MCP.writeA(i); + } + else{ + MCP.writeA( ((~i)+1)&0x0FFF); + } + wait_us(20); + } } \ No newline at end of file
diff -r 09b69f38fc73 -r 2244c8986987 mbed.bld --- a/mbed.bld Thu May 12 11:12:47 2011 +0000 +++ b/mbed.bld Mon Aug 24 12:36:59 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912 +http://mbed.org/users/mbed_official/code/mbed/builds/8ed44a420e5c \ No newline at end of file