Cantafford
Member
Hello,
I'm trying to control a DC motor with PIC16F877A and use a pot to set it's duty cycle.
I wrote this code:
And header:
When I try to run the program, the motor runs full speed no matter what the position of the pot is. I don't know what I'm doing wrong but I'm guessing it has something to do with my oscillator. Maybe I have not set it up properly. Please help me identify the issue. Here is the schematic from simulator:
I'm trying to control a DC motor with PIC16F877A and use a pot to set it's duty cycle.
I wrote this code:
Code:
/*
* File: main.c
* Author: Paul
*
* Created on June 20, 2016, 7:36 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
void ConfigureADC();
void Getspeed();
void main()
{
TRISA = 0x01;
TRISC = 0;
TRISD = TRISD & 0x03;
PORTCbits.RC2 = 1; // motor oprit(enable in 0)
PR2 = 0b01010010 ;
T2CON = 0b00000111 ;
CCP1M3 = 1;
CCP1M2 = 1;
CCP1M1 = 1;
CCP1M0 = 1;
PORTDbits.RD2=0;
PORTDbits.RD3=0;
ConfigureADC();
while(1)
{
ADON = 1;
if(PORTDbits.RD0 == 0 && PORTDbits.RD1 == 1)
{
PORTDbits.RD4=1; PORTDbits.RD5=0; // sens
PORTDbits.RD2=1; PORTDbits.RD3=0; // leduri
Getspeed();
}
if(PORTDbits.RD0 == 1 && PORTDbits.RD1 == 0)
{
PORTDbits.RD4=0; PORTDbits.RD5=1;
PORTDbits.RD2=0; PORTDbits.RD3=1;
Getspeed();
}
}
}
void ConfigureADC()
{
ADCON0 = 0x00;
ADCON1 = 0b00001110;
}
void Getspeed()
{
GO=1;//Start conversion
while(GO); //wait for the conversion to finish
ADON=0; //switch off adc
CCPR1H = ADRESH;
CCP1X = ADRESL & 0x80;
CCP1Y = ADRESL & 0x40;
}
And header:
Code:
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define __XTAL_FREQ 8000000
When I try to run the program, the motor runs full speed no matter what the position of the pot is. I don't know what I'm doing wrong but I'm guessing it has something to do with my oscillator. Maybe I have not set it up properly. Please help me identify the issue. Here is the schematic from simulator: