UART printing example for dsPIC30F4011 with stepper controlled by potentiometer

This was the example code from today’s class:

//
// dsPIC30F4011 example - simple analog input
// Written by Ted Burke, Last updated 5-10-2015
//
 
#include <xc.h>
#include <stdio.h>
#include <libpic30.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

unsigned int read_analog_channel(int n);
void step_forward();
void step_backward();

int main()
{
    // Declare a variable for the step time
    // so that it can be changed easily
    int v;
    int steps = 0;
     
    // Make RD0-3 digital outputs
    TRISD = 0b0000;

    // Configure analog inputs
    TRISB = 0x01FF;      // Port B all inputs
    ADPCFG = 0xFF00;     // Lowest 8 PORTB pins are analog inputs
    ADCON1 = 0;          // Manually clear SAMP to end sampling, start conversion
    ADCON2 = 0;          // Voltage reference from AVDD and AVSS
    ADCON3 = 0x0005;     // Manual Sample, ADCS=5 -> Tad = 3*Tcy = 0.1us
    ADCON1bits.ADON = 1; // Turn ADC ON

    // Setup UART
    U1BRG = 48;            // 38400 baud @ 30 MIPS
    //_UARTEN = 1; // Enable UART
    U1MODEbits.UARTEN = 1; // Enable UART
    
    // Cycle through the four windings to make
    // the stepper turn forwards
    while(1)
    {
        // Read the analog channel. The result is an
        // integer between 0 and 1023 inclusive.
        v = read_analog_channel(0);
        
        if (steps < v)
        {
            step_forward();
            steps = steps + 1;
        }
        else if (steps > v)
        {
            step_backward();
            steps = steps - 1;
        }
        
        printf("%d %d\n", v, steps);
        __delay32(300000);
    }
    
    return 0;
}

void step_forward()
{
    if      (LATD == 0b0001) LATD = 0b0010;
    else if (LATD == 0b0010) LATD = 0b0100;
    else if (LATD == 0b0100) LATD = 0b1000;
    else if (LATD == 0b1000) LATD = 0b0001;
    else LATD = 0b0001;
}

void step_backward()
{
    if      (LATD == 0b0001) LATD = 0b1000;
    else if (LATD == 0b0010) LATD = 0b0001;
    else if (LATD == 0b0100) LATD = 0b0010;
    else if (LATD == 0b1000) LATD = 0b0100;
    else LATD = 0b0001;
}

// This function reads a single sample from the specified
// analog input. It should take less than 2.5us if the chip
// is running at about 30 MIPS.
unsigned int read_analog_channel(int channel)
{
    ADCHS = channel;          // Select the requested channel
    ADCON1bits.SAMP = 1;      // start sampling
    __delay32(30);            // 1us delay @ 30 MIPS
    ADCON1bits.SAMP = 0;      // start Converting
    while (!ADCON1bits.DONE); // Should take 12 * Tad = 1.2us
    return ADCBUF0;
}
Advertisements
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

One Response to UART printing example for dsPIC30F4011 with stepper controlled by potentiometer

  1. Pingback: Controlling a Servo Motor | fitzgeraldcathal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s