Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] DC_motor_with_5_speeds

Status
Not open for further replies.

0leee

Member level 2
Joined
Aug 23, 2011
Messages
42
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,608
i need help;this program works in teory and on simulator,but in real life i do not thinks so;so i need help if enyone wants
to help me ,what i should do to this program work in real life too?




//With one switch speeding with second unspeeding//
#include<htc.h>
#define _XTAL_FREQ 20000000



main()
{


int counter=0;
TRISA=0b11111111;
TRISB=0b11111111;
TRISD=0b00000000;


while(1){
//LED power indicator//
RD5=1;
__delay_ms(100);
RD5=0;
__delay_ms(100);


//speeding;//
if(RA4==0){
while(RA4==0)continue;
counter++;
}

//un speeding//
if(RB0==0){
while(RB0==0)continue;
counter--;
}

//counters that i do not want//
if(counter==-1)counter=0;
if(counter==6)counter=5;

//speed with"n"counter//
if(counter==0){RD6=0;}
if(counter==1){RD6=1;__delay_us(2000);RD6=0;__delay_us(8000);}
if(counter==2){RD6=1;__delay_us(4000);RD6=0;__delay_us(6000);}
if(counter==3){RD6=1;__delay_us(6000);RD6=0;__delay_us(4000);}
if(counter==4){RD6=1;__delay_us(8000);RD6=0;__delay_us(2000);}
if(counter==5){RD6=1;}
}
}
 

What model PIC are you using in the design?

Most PORTs on PICs have multiple peripherals which share the PORT's pins. Usually, you must disable one or more of them before using the pins as digital I/O.

BigDog
 

pic16f877,is this programm OK or must to be written on other way?
 

hardware.PNG this is pic
 

You need to disable several peripherals. You are using the 16F877 and not the 16F877A, correct?

Reference: PIC16F882/883/884/886/887 Data Sheet, pg 39, Section 3.1 PORTA and the TRISA Registers

Note: The ANSEL register must be initialized to
configure an analog channel as a digital
input. Pins configured as analog inputs will
read ‘0’.

PORTA:
PORTA = 0x00;
ADCON1 = 0x06;

TRISA = 0b11111111; or TRISA = 0xFF;

PORTB:
PORTB = 0x00;

TRISB=0b11111111; or TRISB = 0xFF;


PORTD:
PORTD = 0x00;

TRISD=0b00000000; or TRISD = 0x00;

I did not see any indication of the use of ADC AN0 channel in your code so I am going to assume it is not being uses as depicted in the schematic.

If AN0 is being using for ADC, a different initialization of PORTA will be required.

BigDog
 
Last edited:
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
I am only using two switches ,1 LED and dc motor,other things are not used...
Do this programm work like this ,orI need to use inerputs and timers?
Is it possible to work like that?
 

You need to initialize the PORTs A, B and D as I have shown above.

Another problem I see are the following two blocks of code:

//speeding;//
if(RA4==0){
while(RA4==0)continue;
counter++;
}

//un speeding//
if(RB0==0){
while(RB0==0)continue;
counter--;
}

What is your reasoning for using the continue keyword?

I do not believe it produces the results you intend.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
why do i need to initialize them? i think it work without them just fine
continue does nothing,just like ; continue;=; =)

I know its working on simulator,but would be there some problems in real hardware because of cpu ond __delay_us()?
 

Do you have your crystal properly connected with an appropriate value?

Are your configuration bits set correctly? Have you disabled the watchdog timer?

What are your configuration bit settings?

BigDog

---------- Post added at 22:56 ---------- Previous post was at 22:40 ----------

why do i need to initialize them? i think it work without them just fine

Technically, you don't have to, due to the fact you are only using RA4 which is one of the only pin which doesn't have an ADC input on PORTA.

However, why leave the ADC module active if you are not going to use it. If you should add another button switch to a PORTA pin you might have to deal with the frustration of it not working as expected.

continue does nothing,just like ; continue;=; =)

If you think the keyword continue "does nothing" then why included it in your code to begin with?

Actually the keyword does more than nothing, it jumps immediately to the beginning of the loop for the next iteration skipping any other statements which follow.

The use of "continue" is usually accompanied by a preceding conditional statement, not in the manner you have used it in your code.


I know its working on simulator,but would be there some problems in real hardware because of cpu ond __delay_us()?

There are numerous programs which function as expected during simulation, however fail with real hardware.

As I mentioned in my last reply, the configuration settings and crystal or oscillator are possibly the issue.

BigDog
 
Thank you very muck on your answers,I know that there could be troubles with xtal,but i was
only wondering about construction of this program,every day i learn more and more thanks to this forum ...
This is something like PWM ....
PWM i can not understand,how to do this programm with pwm,the net is full of pwm examples,but all of them are different and not show on something very simple ...
is there any library for pwm ;where ther will be something like #include<pwm.h>

and then example
x=50%
pwm(x) RA1=1;//RA1 LED lighting 50% ...etc.is there something the easyest?
 

Check your configuration bit settings, an incorrect oscillator setting or the watchdog timer left active are both possible issues.

I do not believe Hi-Tech C for PIC16 family has a PWM library. As you move into higher end PICs, the PIC18 for example, almost all compilers have a PWM library.

PWM is not that difficult to understand. I'll try and find a good tutorial or example project, later today, which explains the concepts in a better fashion.

Once you understand the concepts, you can write your own library. I've done just that for virtual all MCUs that I use in commercial projects.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Please give me some easy example of pwm,i know the idea of pwm ,but for example
when i press the switch ,i want to led flashing from 0% to 100%,there is so manny examples but every is differnt and i just cant in h-tech c to make it working
i need something easy to understand ...
 

  • Like
Reactions: 0leee

    0leee

    Points: 2
    Helpful Answer Positive Rating
i knoe the idea,but could anybody write some programm in hi tech c,wich is simple and which work;from beggining to the end,i thik only this will solve my problem;pls?

---------- Post added at 14:38 ---------- Previous post was at 13:49 ----------

I FOUND IT: :):):)

// Software PWM_pic
// rad programa:

#include <htc.h>//OK
#define _XTAL_FREQ 4000000

unsigned char pulse = 0; // Puls pocinje od nule
unsigned char duty = 15; // Duty cycle
unsigned char freq = 255; // Frequencija


void main()
{
// inicijalizacija
TRISB=0x00; // RB oznaceni kao izlazi
TRISA=0xFF; // RA kao ulazi

while( 1 ) // beskonacna petlja pocetak
{
RB4 = 1; // Upali puls na LED
for(pulse = 0; pulse < duty; pulse++); // Puls postoji dok je manji od duty-a
RB4 = 0; // Gasi puls na LED
for(pulse = 0; pulse < (freq - duty); pulse++); // Continue until frequency
duty++; // povecava duty za 1 i krece ispocetka s vecim duty-em
}
}

sorry on my comments in Crotian =)

greetings from CRO ;)
 

I have only one more question about the program above
This programm does LED flashnih from 0% to 100% in about 1 second,what should I change to LED flashing from 0% to 100% ten times longer? please help =)
 

If you would like a template for bit-banging PWM here is a routine written for the Hi-Tech C compiler to demonstrate servo control without using a CCP module. You'll need to add _XTAL_FREQ for your clock and remove the config.h which is where I usually keep the device's _XTAL_FREQ.

Code:
//==========================================================================
//	Project				: Sample code for control single servo motors using delays
//						  This project is using 16F84A with 4MHz
//						  Has been try succesfully using GWS S03N STD
//						  
//						  Please make sure the hardware is compatible with this code
//
//	Project description	: This source code is used to control a servo motor.
//					  Servo motor will continuosly turn from one position to another position 
//					  Lib func __delay_us() and __delay_ms() is use to generate the pulse width. 
//				        The delay timing is independent of crystal but_XTAL_FREQ must be set correctly.
//						  
//	
//	Author			: BigDogGuru
//	Date				: April 2011
//==========================================================================
// 
//	include
//==========================================================================
#include "config.h"
#include "delays.h"
#include <htc.h>   					// this sample code is using 16F84A !!
								
//	configuration
//==========================================================================
 __CONFIG ( 0x3FF2 );				// configuration for the  16F84A
											
//	define
//==========================================================================
#define servo	RB5					// servo set RB5





//	function 						(every function must have a function prototype)
//==========================================================================

//  none
//
//	main function					(main fucntion of the program)
//==========================================================================
void main(void)
{
	unsigned int i;
	
	//set IO port for servo pin 
	TRISB = 0b00000000;			//servo motor is RB1
	PORTB = 0b00000000;			//clear port B

								//servo will loop infinity
	while(1)					//from one position to another position
	{
		//*******************************************************
		//
		//Delay determine the servo motors position
		//Try change the value for different position 
		//
		//Value in __delay_xs() function is unsigned long
		//Calculate the timing correctly or servo damage could result!!
		//
		//******************************************************
		
		for(i=0;i<50;i++)		//continues pulse for 50 times
		{
			servo=1;			//set servo pin high
			delay_us(900);	//delay	0.7ms
		
			servo=0;			//set servo pin low
			delay_us(19100);	//delay 19.3ms
		}
		
		//  _	                _                  _
		// | |	               | |                | |
		// | |	               | |                | |              	~~50 times              
		// | |_________________| |________________| |____________________
		// 0.7ms    19ms      0.7ms      19ms     0.7ms   19ms
		// |				   |
		// |<-------20ms------>|
		
		for(i=0;i<50;i++)		//continues pulse for 50 times
		{
			servo=1;			//set servo pin high
			delay_us(1500);	//delay	1.8ms	
								
								
			servo=0;			//set servo pin low
			delay_us(18500);	//delay 18.2ms
								
		}



		for(i=0;i<50;i++)		//continues pulse for 50 times
		{
			servo=1;			//set servo pin high
			delay_us(2000);	//delay	1.8ms	
								
								
			servo=0;			//set servo pin low
			delay_us(18000);	//delay 18.2ms
								
		}	
			
		//  ___	                ___                 ___
		// |   |               |   |               |   |
		// |   |               |   |               |   |              ~~50 times       
		// |   |_______________|   |_______________|   |____________________
		//  2ms       18ms      2ms      18ms       2ms		18ms
		// |				   |
		// |<-------20ms------>|
	}
		
}
	
//subroutines
//============================================================================	
//
// none

BigDog

---------- Post added at 16:28 ---------- Previous post was at 16:14 ----------

This programm does LED flashnih from 0% to 100% in about 1 second,what should I change to LED flashing from 0% to 100% ten times longer?

To modify your program to create a delay between duty cycle changes just and a for loop as shown:

Code:
while( 1 )						// beskonacna petlja pocetak
{
    for(i=0;i<50;i++)
    {
	RB4 = 1;					// Upali puls na LED
	for(pulse = 0; pulse < duty; pulse++);		// Puls postoji dok je manji od duty-a
	RB4 = 0;					// Gasi puls na LED
	for(pulse = 0; pulse < (freq - duty); pulse++);	// Continue until frequency
    }

    duty++;						// povecava duty za 1 i krece ispocetka s vecim duty-em

}

You can modify the iteration value of 50 to suit your needs.

BigDog
 
  • Like
Reactions: 0leee and FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating

    0leee

    Points: 2
    Helpful Answer Positive Rating
nice servo code it will help me a lot,but

this code:
while( 1 ) // beskonacna petlja pocetak
{
for(i=0;i<50;i++)
{
RB4 = 1; // Upali puls na LED
for(pulse = 0; pulse < duty; pulse++); // Puls postoji dok je manji od duty-a
RB4 = 0; // Gasi puls na LED
for(pulse = 0; pulse < (freq - duty); pulse++); // Continue until frequency
}

duty++; // povecava duty za 1 i krece ispocetka s vecim duty-em

}
no matter what I put 50 or200 or 255 always is the sam delay for about one second,do i need to change something else?
 

Here is a functional version of your program:

Code:
#include <htc.h>//OK
#define _XTAL_FREQ 4000000

__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & WRT_OFF);

unsigned char pulse = 0; // Puls pocinje od nule
unsigned char duty = 0; // Duty cycle
unsigned char freq = 255; // Frequencija
unsigned char i;


void main()
{
	// inicijalizacija
	TRISB=0x00; // RB oznaceni kao izlazi
	TRISA=0xFF; // RA kao ulazi

	while( 1 ) // beskonacna petlja pocetak
	{
		for(i=0; i<25; i++)
		{
			RB4 = 1; // Upali puls na LED
			for(pulse = 0; pulse < duty; pulse++); // Puls postoji dok je manji od duty-a
			RB4 = 0; // Gasi puls na LED
			for(pulse = 0; pulse < (freq - duty); pulse++); // Continue until frequency
        }

		duty++; // povecava duty za 1 i krece ispocetka s vecim duty-em
	}
}

Notice the __CONFIG(); statement which is used to configure your configuration bits. Make sure you enable the "configuration bits set in code" option under the Configure menu.

BigDog
 
  • Like
Reactions: 0leee

    0leee

    Points: 2
    Helpful Answer Positive Rating
OK,works just fine;
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF );--- can you explayn me this line ??
& BOREN_OFF & LVP_OFF & CPD_OFF & WRT_OFF --- I do not have theese
 

Certainly,

Example the header file for your device, which is located in:

C:\Program Files\HI-TECH Software\PICC\9.81\include

Reference pic16f877.h:

//
// Configuration mask definitions
//


// Config Register: CONFIG
#define CONFIG 0x2007
// Oscillator Selection bits
// RC oscillator
#define FOSC_EXTRC 0xFFFF
// HS oscillator
#define FOSC_HS 0xFFFE
// XT oscillator
#define FOSC_XT 0xFFFD
// LP oscillator
#define FOSC_LP 0xFFFC
// Watchdog Timer Enable bit
// WDT enabled
#define WDTE_ON 0xFFFF
// WDT disabled
#define WDTE_OFF 0xFFFB
// Power-up Timer Enable bit
// PWRT disabled
#define PWRTE_OFF 0xFFFF
// PWRT enabled
#define PWRTE_ON 0xFFF7
// FLASH Program Memory Code Protection bits
// Code protection off
#define CP_OFF 0xFFFF
// 1F00h to 1FFFh code protected
#define CP_UPPER_256 0xEFEF
// 1000h to 1FFFh code protected
#define CP_HALF 0xDFDF
// 0000h to 1FFFh code protected
#define CP_All 0xCFCF
// Brown-out Reset Enable bit
// BOR enabled
#define BOREN_ON 0xFFFF
// BOR disabled
#define BOREN_OFF 0xFFBF
// Low Voltage In-Circuit Serial Programming Enable bit
// RB3/PGM pin has PGM function; low-voltage programming enabled
#define LVP_ON 0xFFFF
// RB3 is digital I/O, HV on MCLR must be used for programming
#define LVP_OFF 0xFF7F
// Data EE Memory Code Protection
// Code Protection off
#define CPD_OFF 0xFFFF
// Data EEPROM memory code-protected
#define CPD_ON 0xFEFF
// FLASH Program Memory Write Enable
// Unprotected program memory may be written to by EECON control
#define WRT_ON 0xFFFF
// Unprotected program memory may not be written to by EECON control
#define WRT_OFF 0xFDFF
// In-Circuit Debugger Mode bit
// In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
#define DEBUG_OFF 0xFFFF
// In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
#define DEBUG_ON 0xF7FF

Inside the header file you will find the Configuration mask definitions, which the compiler directive __CONFIG(); used to configure the PIC's configuration bits. When using the compiler directive to set your configuration bits, use the masks defined in the devices header file above. You'll notice each as a brief description as well.

I usually add this file to my project window by right click and selecting "Add Files...", a shortcut to the header is the created. You can then open the header and use it to reference various features of the device by using the recognized mnemonics by the compiler.

You will also notice that each mask in the __CONFIG() directive is bitwise ANDed (&) together.

BigDog
 
  • Like
Reactions: 0leee

    0leee

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top