# how to display an output variable from a speed sensor to a 7 segment display in C. DSPIC33E

I have just started an electrical engineering degree and been set an assignment to create a closed loop motor speed control system in C programming language. The PIC is a DSPIC33E (BLO032) and the boards that are connected to this are eblocks2 by matrix. There is a combo board (BLO114). This has buttons LED's , 4x 7 segment displays, a pot and light sensor. There is also an actuator board (BLO127) and this has a DC motor controlled by a H bridge and a feedback speed sensor.

This subject is pretty alien to me so excuse me if I come across vague here. So far I have managed to get the motor to spin ( i will put the code at the end of here) and from this the speed sensor then generates an analog signal for feedback. I have converted the analog signal to a digital signal. This is where I am not sure what to do.

I know i need to get this digital number,do some calcs, split it into 2 parts and display each part on one of the 7 segments. I am using 2 seven segments. The compiler I am using is MPlab. THe 7 segment displays are common anode. The number will be a 2 digit number so only 2 x 7 segments will be needed. I know this because at max speed I have divided the number i have has the resolution of 0-1024. I then divided this by 13 to give me a number of 78 when the motor is going its max speed.

*** code starts here***

#define Ffrc 7372800ULL
#define Fosc 80000000ULL
#define FCY Fosc/2
#include "xc.h"
#include <libpic30.h>
#include <p33Exxxx.h>
#include "xc.h"

void initADC(void);       //declare the analog to digital function

int main(void)
{

TRISE=0x0002;        //this sets port D to 0000 00010. The actuator board is connected to port E.

TRISB = 0x0000;     //this sets port B to all outputs. The combo board is connected to port B

TRISE = 0x0080;     //sets port e to 1000 0000 which makes the speed sensor
input and motor output

ANSELB = ANSELC = ANSELD = ANSELE = ANSELG =0x0000; //this sets all to digital

ANSELEbits.ANSE7 = 1;   //this sets pin 7 to analog on portE which is the speed
sensor.

while (1)
{
__delay_us(10000);
ADCValue = ADCValue>>6; // this puts it 6 bits to the right to give it correct resolution

{
LATE=0x04;   //this makes motor spin
}

}
}

{

AD1CON1 = 0x0204;  //this means the signal is 10 bits wide
.

AD1CHS0 = 0x001F;   // 31 in dec. an31. re7 on the pin diagram

__delay_us(20);            //provides a delay
}


code ends here

• If you want good answers, be sure to ask good questions. Provide as many details as possible. We don't know your existing code, so what do you want us to say? We don't know what your signals look like, if you're getting errors, or what components you are using. We simply don't have much to work with. – Bort Apr 2 '19 at 18:21
• In your words: "I'm pretty new to code...". Could the same be said about your skills in Control Systems? The depth (and quality) of the answers will depend on this. – Dirceu Rodrigues Jr Apr 2 '19 at 21:04
• Your question title is about closed loop speed control. As near as I can tell, the specific question is about displaying speed on a 2-digit 7-segment readout. Either edit the title, and we'll help you make progress on the speed readout, or edit the question and we'll help you make progress on the speed control. You can always ask both questions at the same time, if need be. – TimWescott Apr 2 '19 at 21:16
• "A code" is the thing you enter into a keypad to unlock a door with an electronic lock. It is also the thing you use to encrypt secret messages. Program code is always singular, and never spoken of as though there could be more than one. "Program code" or "the code." Never "program codes" or "a code." You have been given the task of writing a program to display values on a 7 segment display. You have no idea what the code to do that might look like. – JRE Apr 3 '19 at 20:47
• The information you added in the comments belong in the question. Edit it, and put that information in the question. While you are at it, you might mention how many digits your 7 segment display has and how it should be connected. – JRE Apr 3 '19 at 20:50

This is a high-level question, so I'll give you a high level answer.

You need to break up your functionality into sub-components and get those working individually.

Lets look at the components you'll need:

• You need to be able to set a motor throttle, and have that modulate the power to the motor. You'll probably want to use a PWM output for that, or you can bit-bang it.
• You need to be able to time your speed sensor reading and convert that into rpms.

• You need an algorithm to convert your RPM error signal into a change in the motor throttle. For example, PID control.

• You need a function that can display an arbitrary number between 0 99 on your 7 segment display.

Get all these things working individually first. At that point you'll be most of the way there to a full solution. These "components" don't need to be functions, you just need to logically separate them in your head. Give each component clear inputs and outputs, and then use those to make the components interface with one another.