Lissajous Curve Generator
Reinhard
Posts: 489
;-)
For everyone who has to work hard in an electronics laboratory and for whom the boss constantly looks over his shoulder.
Load the program onto the P2 and your Osci looks very busy.
For everyone who has to work hard in an electronics laboratory and for whom the boss constantly looks over his shoulder.
Load the program onto the P2 and your Osci looks very busy.
/*
* Lissajou Curve on 2 Channel osci
* connect probes with P0 and P2 -- 100mV/Div -- AC
* set osci mode to XY
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <propeller.h>
#define P2_TARGET_MHZ 160
#include "sys/p2es_clock.h"
#define L_PIN 0
#define R_PIN 2
#define SYSTEM_CLOCK (_CLOCKFREQ)
#define SAMPLE_RATE 16000
#define DAC_MODE (3 << 1) // DAC 16-bit dither, PWM
#define DIR_MODE 1 // Output enabled, overrides DIR
#define INIT_8BIT_DAC_VALUE 128
#define ANALOG_OUT_MODE 0x17 // 75 ohm, 2.0V DAC mode
#define SMARTPIN_DAC_MODE (ANALOG_OUT_MODE << 16) | (INIT_8BIT_DAC_VALUE << 8) | (DIR_MODE << 6) | DAC_MODE
#define SAMPLE_PERIOD SYSTEM_CLOCK / SAMPLE_RATE // CPU cycles between sample updates
////////////////////////////////////////////////////////////////////////
void audio_init()
{
__asm {
wrpin ##SMARTPIN_DAC_MODE, #L_PIN // Config smartpin DAC mode on "left" pin
wrpin ##SMARTPIN_DAC_MODE, #R_PIN // Config smartpin DAC mode on "right" pin
wxpin ##SAMPLE_PERIOD, #L_PIN // Set sample period for left audio channel
wxpin ##SAMPLE_PERIOD, #R_PIN // Set sample period for right audio channel
dirh #L_PIN // Enable smartpin DAC mode on left pin
dirh #R_PIN // Enable smartpin DAC mode on right pin
setse1 #(1 << 6) | L_PIN // Event triggered every new sample period (when "left in pin rises")
};
}
////////////////////////////////////////////////////////////////////////
void main ()
{
int phi = 0;
int s1,s2;
unsigned int alpha,tmp;
int frq = 800 * 268435;
clkset(_SETFREQ, _CLOCKFREQ);
audio_init();
while(1)
{
alpha += 0x5000;
if (alpha > 0x7FFFFFFF) tmp = phi - alpha;
else tmp = phi + alpha;
if (getcnt() > 0x7FFF) //curve changed every 10 sec
tmp *= 2;
__asm {
qrotate ##$1000,phi
getqy s1
qrotate ##$1000,tmp
getqy s2
xor s1, ##$8000
xor s2, ##$8000
wypin s1, #L_PIN
wypin s2, #R_PIN
waitse1
add phi,frq
};
}//end while
}//end main

Comments
1.) I find the math behind very interest.
2.) It's a test case for switch/case and getchar statements.
3.) demonstrate the communication between cog and hub in C.
/* * Lissajou Curve on 2 Channel osci * connect probes with P0 and P2 -- 500mV/Div -- AC * set osci mode to XY */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <propeller.h> #define P2_TARGET_MHZ 160 #include "sys/p2es_clock.h" #define L_PIN 0 #define R_PIN 2 #define SYSTEM_CLOCK (_CLOCKFREQ) #define SAMPLE_RATE 16000 #define DAC_MODE (3 << 1) // DAC 16-bit dither, PWM #define DIR_MODE 1 // Output enabled, overrides DIR #define INIT_8BIT_DAC_VALUE 128 #define ANALOG_OUT_MODE 0x17 // 75 ohm, 2.0V DAC mode #define SMARTPIN_DAC_MODE (ANALOG_OUT_MODE << 16) | (INIT_8BIT_DAC_VALUE << 8) | (DIR_MODE << 6) | DAC_MODE #define SAMPLE_PERIOD SYSTEM_CLOCK / SAMPLE_RATE // CPU cycles between sample updates #define BAUD 230400 #define CORDIC_PI 0x80000000 #define CORDIC_PI_4 0x20000000 static long stack[64]; int global_alpha; int global_c; void audio_init(); void lissa (); //////////////////////////////////////////////////////////////////////// void main () { int id; char cin; clkset(_SETFREQ, _CLOCKFREQ); _setbaud(BAUD); sleep(1); id = __builtin_cogstart( lissa(), &stack[0]); printf("cog %d started\n",id); while(1) { puts("special cases of Lissajous Curve\n"); puts("hit 1 .... linear rising\n"); puts("hit 2 .... linear falling\n"); puts("hit 3 .... quadratic down\n"); puts("hit 4 .... quadratic up\n"); cin = getchar(); puts("\n"); switch(cin) { case '1': global_alpha = 0; global_c = 1; break; case '2': global_alpha = CORDIC_PI; global_c = 1; break; case '3': global_alpha = CORDIC_PI_4; global_c = 2; break; case '4': global_alpha = -CORDIC_PI_4; global_c = 2; break; } } } //////////////////////////////////////////////////////////////////////// void audio_init() { __asm { wrpin ##SMARTPIN_DAC_MODE, #L_PIN // Config smartpin DAC mode on "left" pin wrpin ##SMARTPIN_DAC_MODE, #R_PIN // Config smartpin DAC mode on "right" pin wxpin ##SAMPLE_PERIOD, #L_PIN // Set sample period for left audio channel wxpin ##SAMPLE_PERIOD, #R_PIN // Set sample period for right audio channel dirh #L_PIN // Enable smartpin DAC mode on left pin dirh #R_PIN // Enable smartpin DAC mode on right pin setse1 #(1 << 6) | L_PIN // Event triggered every new sample period (when "left in pin rises") }; } //////////////////////////////////////////////////////////////////////// void lissa () { int phi = 0; int s1,s2; unsigned int alpha,tmp,c; int frq = 400 * 268435; audio_init(); while(1) { alpha = global_alpha; c = global_c; tmp = phi + alpha; tmp *= c; __asm { qrotate ##$8000,phi getqy s1 qrotate ##$8000,tmp getqy s2 xor s1, ##$8000 xor s2, ##$8000 wypin s1, #L_PIN wypin s2, #R_PIN waitse1 add phi,frq }; }//end while }//end mainrun with:
Is this second piece of code Propeller GCC or Catalina? I've obviously been napping. Had no idea C was so well along on the P2.
sorry I forget this info.
This is fastspin on the command line. Easy to use ;-)