Cantafford
Member
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):
And this is how I connected the encoder: Q1 on RA4(not sure what I have to do with the other 2 pins)
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)