Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

DC Motor Driver(ADC doesn't work)

Status
Not open for further replies.
Ian Rogers

I have done that and the speed is displayed as zero on the LCD no matter what the actuall speed is.

This is the code(the last function is the one that is supposed to display speed, the one in which I followed your instructions):

Code:
/*
* File:   main.c
* Author: Paul
*
* Created on November 1, 2015, 11:24 AM
*/

#include <stdio.h>
#include <stdlib.h>
#include "header.h"
#include <plib/adc.h>
#include <plib/xlcd.h>
#include <plib/delays.h>

void Initialize_LCD(void); // Initialize the LCD
void DelayFor18TCY(void); //  Delay for 18 cycles
void DelayPORXLCD(void);  //  Delay for 15ms
void DelayXLCD(void);     //  Delay for 5ms
void DisplaySpeed(void);



unsigned int ADCResult = 0;
float speed;
unsigned char ResultString[20];

void main()
{
   
    OSCCON = 0x76; // set internal oscillator to 8Mhz
    TRISC = 0b11100111; // RC0-2: buttons, RC3,4-outputs to drive the H bridge
    LATCbits.LATC3 = 0; LATCbits.LATC4 = 0; // motor is stopped
    Initialize_LCD();
    while(1)
    {
     if(PORTCbits.RC0 == 0 && PORTCbits.RC1 == 1 && PORTCbits.RC2 == 1) // clockwise
     {
         LATCbits.LATC3 = 1;
         LATCbits.LATC4 = 0;
         DisplaySpeed();
     }

     if(PORTCbits.RC0 == 1 && PORTCbits.RC1 == 0 && PORTCbits.RC2 == 1) // clockwise
     {
         LATCbits.LATC3 = 0;
         LATCbits.LATC4 = 1;
         DisplaySpeed();
     }

     if(PORTCbits.RC2 == 0) // motor stops
     {
         LATCbits.LATC3 = 1;
         LATCbits.LATC4 = 1;
         DisplaySpeed();
     }
    }
}


void Initialize_LCD(void)
{
    OpenXLCD(FOUR_BIT&LINES_5X7);
    while(BusyXLCD());
    WriteCmdXLCD(0x06); // move cursor right and don't shift display
    WriteCmdXLCD(0x0C); // turn on the display without blinking cursor
    putrsXLCD("DC motor driver"); // Display
    SetDDRamAddr(0x40); // Shift cursor to beginning of second line
    for(int x = 0; x<=20; x++) __delay_ms(50); // 1 second delay
    for(int x = 0; x<=20; x++) __delay_ms(50); // 1 second delay
    for(int x = 0; x<=20; x++) __delay_ms(50); // 1 second delay
    WriteCmdXLCD(0x01); // Clear screen
}

void DelayFor18TCY(void)
{
    Delay10TCYx(20);
}

void DelayPORXLCD(void)
{
    Delay1KTCYx(30);
}

void DelayXLCD(void)
{
    Delay1KTCYx(10);
}

void DisplaySpeed(void)
{
        
        TMR0ON = 1; // set timer0 on
        PSA = 0; // to set preescaler to Timer0 module
        T0CS = 1; // to select counter mode
        T0SE = 0; // to select rising edge as incrementing edge
        
        T0CON = 0x10;
        TMR0 = 0;
        __delay_ms(50);
        __delay_ms(50);
        __delay_ms(50);
        __delay_ms(50);
        __delay_ms(50);
        speed = TMR0 / 6;

        putrsXLCD("Speed is: "); // Display
        sprintf(ResultString, "%.3g", speed); // Convert ADCResult to a string
        putsXLCD(ResultString); // Display voltage on the screen
        putrsXLCD("  "); // Clear extra digits
        WriteCmdXLCD(0x02); // Home position on LCD
}

And this is how I connected the encoder: Q1 on RA4(not sure what I have to do with the other 2 pins)
2w4xx08.png
 
Two things stick out... You set T0CS then turn it off with the T0CON statement!!
If you turn PSA on you don't need to worry about setting the prescaler

The other is the use of floats...

This line..
speed = TMR0 / 6; It you are going to use floats then you have to cast..
speed = (float)TMR0 / 6; This way the TMRO is cast to float BEFORE it is divided...

C:
void DisplaySpeed(void)
{
   
  TMR0ON = 1; // set timer0 on
  PSA = 1; // to set preescaler to Timer0 module
  T0CS = 1; // to select counter mode
  T0SE = 0; // to select rising edge as incrementing edge
       T08BIT = 1;
  TMR0 = 0;
  __delay_ms(50);
  __delay_ms(50);
  __delay_ms(50);
  __delay_ms(50);
  __delay_ms(50);
  speed = (float)TMR0 / 6;

  putrsXLCD("Speed is: "); // Display
  sprintf(ResultString, "%.3g", speed); // Convert ADCResult to a string
  putsXLCD(ResultString); // Display voltage on the screen
  putrsXLCD("  "); // Clear extra digits
  WriteCmdXLCD(0x02); // Home position on LCD
}

Lastly... If you are going to use floats in ISIS place this line in the properties box

DT_FLOAT = HITECH,24 Then you can debug the values easier!!

upload_2015-11-6_19-29-39.jpeg
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top