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 ADCValue; //declare the adc variable
int main(void)
{
initADC(); //this will run the ADC function
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);
AD1CON1bits.SAMP = 0;
while(!AD1CON1bits.DONE );
AD1CON1bits.DONE = 0;
ADCValue = ADC1BUF0; //stores buffer 0 in ADC variable
ADCValue = ADCValue>>6; // this puts it 6 bits to the right to give it correct resolution
ADCValue = ADCValue/13;
LATB= ADCValue; //displays variable on port B which is adc value
{
LATE=0x04; //this makes motor spin
}
}
}
void initADC(void)
{
AD1CON1 = 0x0204; //this means the signal is 10 bits wide
.
AD1CON2 = 0x0000;
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CHS0 = 0x001F; // 31 in dec. an31. re7 on the pin diagram
AD1CHS123 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CON1bits.ADON = 1; // this enables the ADC module
__delay_us(20); //provides a delay
}
code ends here