Skip to content

Instantly share code, notes, and snippets.

@wattnotions
Created May 25, 2014 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wattnotions/7744420bf5e354283e23 to your computer and use it in GitHub Desktop.
Save wattnotions/7744420bf5e354283e23 to your computer and use it in GitHub Desktop.
This program controls an x-y plotter. Can be used to draw basic lines and shapes
#include <xc.h>
#include <libpic30.h>
#include <stdio.h>
#include <math.h>
#include<uart.h>
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF); // Watchdog timer off
_FBORPOR(MCLR_DIS); // Disable reset pin
//Functions
void setup(void);
void x_step_pos(int n);
void x_step_neg(int n);
void y_step_pos(int n);
void y_step_neg(int n);
void sine_draw(void);
void x_y_plot(int x, int y);
void square_example(void);
void pen_up(void);
void pen_down(void);
void draw_circle(int r);
//Variables
int count = 0;
int t = 0;
int xpoint;
int ypoint;
int n;
int x_state = 1;
int y_state = 1;
int _EEDATA(2) table[] = {2,3};
int x,y;
int main(void)
{
setup();
int temp = 0;
while(1)
{
void draw_circle(100);
}
while(1);
}
void setup(void)
{
//configure pins
TRISD = 0b00000000;
TRISC = 0b00000000;
TRISF = 0b00000000;
//pwm setup
PWMCON1 = 0x00FF; // Enable all PWM pairs in complementary mode
PTCONbits.PTCKPS = 3; // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
PTPER = 9375; // 20ms PWM period (15-bit period value)
PDC1 = 1406; // 1.5ms pulse width on PWM channel 1
PTCONbits.PTEN = 1; // Enable PWM time base
U1BRG = 48; // 38400 baud @ 30 MIPS
U1MODEbits.UARTEN = 1; // Enable UART
}
void x_step_pos(int n)
{
count = 0;
while( count < n )
{
if (count == n) break;
if (x_state == 4)
{
_LATD1 = 1;
__delay32(300000);
_LATD1 = 0;
count++;
x_state =3;
}
if (count == n) break;
if (x_state == 3)
{
_LATD3 = 1;
__delay32(300000);
_LATD3 = 0;
count++;
x_state = 2;
}
if (count == n) break;
if (x_state == 2)
{
_LATC13 = 1;
__delay32(300000);
_LATC13 = 0;
count++;
x_state = 1;
}
if (count == n) break;
if (x_state == 1)
{
_LATC14 = 1;
__delay32(300000);
count++;
_LATC14 = 0;
x_state = 4;
}
}
xpoint = xpoint + n;
}
void x_step_neg(int n)
{
count = 0;
while( count < n )
{
if (count == n) break;
if(x_state ==1)
{
_LATC14 = 1;
__delay32(300000);
_LATC14 = 0;
count++;
x_state = 2;
}
if (count == n) break;
if (x_state == 2)
{
x_state = 3;
_LATC13 = 1;
__delay32(300000);
_LATC13 = 0;
count++;
x_state = 3;
}
if (count == n) break;
if (x_state ==3)
{
_LATD3 = 1;
__delay32(300000);
_LATD3 = 0;
count++;
x_state = 4;
}
if (count == n) break;
if (x_state == 4)
{
_LATD1 = 1;
__delay32(300000);
count++;
_LATD1 = 0;
x_state = 1;
}
}
xpoint = xpoint - n;
}
void y_step_pos(int n)
{
count = 0;
while(count < n)
{
if (count == n) break;
if( y_state == 1)
{
_LATD0 = 1;
__delay32(300000);
_LATD0 = 0;
count ++;
y_state = 2;
}
if (count == n) break;
if (y_state ==2)
{
_LATF5 = 1;
__delay32(300000);
_LATF5 = 0;
count++;
y_state = 3;
}
if (count == n) break;
if (y_state == 3)
{
_LATD2 = 1;
__delay32(300000);
_LATD2 = 0;
count++;
y_state = 4;
}
if (count == n) break;
if (y_state == 4)
{
_LATF4 = 1;
__delay32(300000);
count++;
_LATF4 = 0;
y_state = 1;
}
}
ypoint = ypoint + n;
}
void y_step_neg(int n)
{
count = 0;
while(count < n)
{
if (count == n) break;
if (y_state == 4)
{
_LATF4 = 1;
__delay32(300000);
_LATF4 = 0;
count++;
y_state = 3;
}
if (count == n) break;
if (y_state == 3)
{
_LATD2 = 1;
__delay32(300000);
_LATD2 = 0;
count++;
y_state = 2;
}
if (count == n) break;
if (y_state == 2 )
{
_LATF5 = 1;
__delay32(300000);
_LATF5 = 0;
count++;
y_state = 1;
}
if (count == n) break;
if (y_state == 1)
{
_LATD0 = 1;
__delay32(300000);
count++;
_LATD0 = 0;
y_state = 4;
}
}
ypoint = ypoint - n;
}
void sine_draw(void)
{
while(1)
{
int v = 0 ;
int f = 1;
int n = 0;
t = 0;
while( t < 200 )
{
v = 200*sin(2*3.14*f*t);
v = abs(v);
// printf("%d\n",v);
x_y_plot(t,v);
t++;
}
x_y_plot(0,0);
t = 0;
}
}
void x_y_plot(int x, int y)
{
int x_steps = 0;
int y_steps = 0;
x_steps = x - xpoint ;
y_steps = y - ypoint;
//printf("x_step = %d y_step = %d x_point = %d y_point = %d\n",x_steps, y_steps, xpoint, ypoint);
if (x_steps > 0)
{
x_steps = abs(x_steps);
x_step_pos(x_steps);
}
if (x_steps < 0)
{
x_steps = abs(x_steps);
x_step_neg(x_steps);
}
if (y_steps > 0)
{
y_steps = abs(y_steps);
y_step_pos(y_steps);
}
if (y_steps < 0)
{
y_steps = abs(y_steps);
y_step_neg(y_steps);
}
}
void square_example(void)
{
while(1)
{
if((x<200) && (y<200))
{
x = x+20;
y = y+20;
x_y_plot(x,y);
x_y_plot(0,0);
}
else if ( x>200 || y>200)
{
x_y_plot(0,0);
break;
}
}
}
void pen_up(void)
{
PDC1 = 700;
__delay32(15000000);
}
void pen_down(void)
{
PDC1 = 1500;
__delay32(15000000);
}
void draw_circle(int r)
{
pen_up();
x_y_plot(100,100);
int theta = 0;
while(theta <= 360)
{
y = 100 +(r*sin(theta *(3.14/180))) ;
x = 100+(r*cos(theta *(3.14/180))) ;
printf(" x = %d, y = %d, theta = %d\n",x,y, theta);
x_y_plot(x,y);
if (theta == 0) pen_down();
theta++;
}
pen_up();
x_y_plot(0,0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment