labSPI

Dependencies:   mbed

Fork of MCP4922_Sinewave by FRA221_2015

Committer:
jf1vrr
Date:
Thu May 12 11:12:47 2011 +0000
Revision:
1:09b69f38fc73
Parent:
0:5737b1972549
Child:
2:2244c8986987
Rev, 0.01A New 2011/05/11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jf1vrr 0:5737b1972549 1 /*This is a program to output a sinwave in 12 bits
jf1vrr 0:5737b1972549 2 Digital to Analog converter MCP4922. Sampling frequency is
jf1vrr 0:5737b1972549 3 166,667Hz and produce an interrupt of Ticker every 6uS.
jf1vrr 1:09b69f38fc73 4 The Program calculates the data of the sinewave beforehand.
jf1vrr 0:5737b1972549 5 */
jf1vrr 0:5737b1972549 6 #include "mbed.h"
jf1vrr 0:5737b1972549 7 #include "TextLCD.h"
jf1vrr 0:5737b1972549 8 #include "MCP4922.h"
jf1vrr 0:5737b1972549 9
jf1vrr 0:5737b1972549 10 MCP4922 MCP(p5, p7,p8); // MOSI, SCLK, CS
jf1vrr 0:5737b1972549 11 TextLCD lcd(p24, p26, p27, p28, p29, p30);
jf1vrr 0:5737b1972549 12 Ticker sampling;
jf1vrr 0:5737b1972549 13
jf1vrr 0:5737b1972549 14 #define PI 3.141593
jf1vrr 0:5737b1972549 15 #define MaxRes 800
jf1vrr 0:5737b1972549 16 /// Global Variables
jf1vrr 0:5737b1972549 17 unsigned int OutData[MaxRes];
jf1vrr 0:5737b1972549 18 unsigned int Maxindex;
jf1vrr 0:5737b1972549 19 unsigned long Fsample;
jf1vrr 0:5737b1972549 20 unsigned long Freq;
jf1vrr 0:5737b1972549 21 unsigned int index = 0;
jf1vrr 0:5737b1972549 22
jf1vrr 0:5737b1972549 23 void GenWave(void) {
jf1vrr 0:5737b1972549 24 unsigned int i;
jf1vrr 0:5737b1972549 25
jf1vrr 0:5737b1972549 26 Maxindex = (int) (Fsample / Freq);
jf1vrr 0:5737b1972549 27 if(Maxindex > MaxRes) {
jf1vrr 0:5737b1972549 28 Fsample = Freq * MaxRes;
jf1vrr 0:5737b1972549 29 Maxindex = MaxRes;
jf1vrr 0:5737b1972549 30 }
jf1vrr 0:5737b1972549 31 for(i=0; i<Maxindex; i++) {
jf1vrr 0:5737b1972549 32 OutData[i] = (int)(0x1FF * (1+ sin((2*PI*Freq*i)/Fsample))/2);
jf1vrr 0:5737b1972549 33 }
jf1vrr 0:5737b1972549 34 }
jf1vrr 0:5737b1972549 35 void timer_int(){
jf1vrr 0:5737b1972549 36 /// Output to D/A Converter
jf1vrr 0:5737b1972549 37 MCP.writeA(OutData[index]);
jf1vrr 0:5737b1972549 38 index++;
jf1vrr 0:5737b1972549 39 if(index >= Maxindex) index = 0;
jf1vrr 0:5737b1972549 40 }
jf1vrr 0:5737b1972549 41
jf1vrr 0:5737b1972549 42 /**** Main Function ***/
jf1vrr 0:5737b1972549 43 int main(void) {
jf1vrr 0:5737b1972549 44 MCP.frequency(20000000);
jf1vrr 0:5737b1972549 45
jf1vrr 0:5737b1972549 46 lcd.cls();
jf1vrr 0:5737b1972549 47 lcd.printf("MCP4922 Sinewave");
jf1vrr 0:5737b1972549 48
jf1vrr 0:5737b1972549 49 Fsample = 166667;
jf1vrr 0:5737b1972549 50 Freq = 1000;
jf1vrr 0:5737b1972549 51 GenWave();
jf1vrr 0:5737b1972549 52
jf1vrr 0:5737b1972549 53 sampling.attach_us(&timer_int, 6);
jf1vrr 0:5737b1972549 54 while(1){
jf1vrr 0:5737b1972549 55 }
jf1vrr 0:5737b1972549 56 }