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.

Simulation of PIC C Code...

Status
Not open for further replies.
Ritesh, instead of selecting (for example) PicKit3 as your debugger, select MPLAB SIM.

Then you can add PortB to the watch window and run your program step by step, or add breakpoints at certain places in your code if you wish.


Edit, whoops sorry Ian I missed where you directed Ritesh to MPLAB SIM a couple of posts up.
 
Last edited:
Not so much aware of it..and only arrow is following can't we get more info from it??


Yes you use your 'watch' window.

Then you can add registers to your watch window to see their value as your program runs step by step.
 
Hi,

I want to make PWM using PI16F877A as we have done before in PIC16F676 through software but this this with internal hardware...
 
Ritesh.... Do this..
Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
 
 
#define _XTAL_FREQ 4000000
 
void main (void)
	{
	TRISB=0b00000000;//all pins set as ouput
	PORTB=0;
	while (1)
		{//endless loop
		PORTB = 0x1;
		NOP(); //__delay_ms(1000);
		PORTB = 0x2;
		NOP(); //__delay_ms(1000);
		PORTB = 0x4;
		NOP(); //__delay_ms(1000);
		PORTB = 0x8;
		NOP(); //__delay_ms(1000);
		PORTB = 0xff;
		NOP(); //__delay_ms(1000);
		}
	}
And run it in the MPLAB sim... The nop's will speed things up a bit.

The output from port B is high all the time i have connected LED which was glowing (high), why it is not blinking as per sequence??
 
I use nop() when I simulate, because the simulator runs faster... If you use the code posted above in a real device, the delays will not be long enough for you to see..

Real device
Code:
while (1)
		{//endless loop
		PORTB = 0x1;
		__delay_ms(1000);
		PORTB = 0x2;
		__delay_ms(1000);
		PORTB = 0x4;
		__delay_ms(1000);
		PORTB = 0x8;
		__delay_ms(1000);
		PORTB = 0xff;
		__delay_ms(1000);
		}

Do you see what I mean....

Simulation only
Code:
while (1)
		{//endless loop
		PORTB = 0x1;
		NOP(); 
		PORTB = 0x2;
		NOP(); 
		PORTB = 0x4;
		NOP(); 
		PORTB = 0x8;
		NOP(); 
		PORTB = 0xff;
		NOP(); 
		}
 
Hi Retesh..... My C routines Tutorial 8 uses the pic16f876 ( same code applies ) To drive servo motors.. https://www.electro-tech-online.com/threads/directorial-antenna.467/


Hi,
please explain the code.....
Code:
#include <pic.h>				// pic specific identifiers
#define _XTAL_FREQ 20000000
__CONFIG(0x3F71);				// Config bits

#define MOTOR_PORT 	PORTC		// constants
#define MOTOR_TRIS	TRISC		//
#define RL	 		RC0
#define FL			RC3
#define RR	 		RC4
#define FR			RC5

		// Required prototypes.. each function needs to be declared
		// if called BEFORE definition.

void delayMs(int dly),speed(char motor, int speed);
void reverse(char motor), forward(char motor);

void main(void)						// program entry
	{
	int index = 0;
	unsigned char ch;
	ADCON1 = 0x6;					// Analogue off
	MOTOR_TRIS = 0b00000000;		// Motor port as outputs

	ch = CCP1CON;					// Set up PWM 1
	ch &= 0xF0;						// Dont need capture
	ch |= 0x0C;						// set bit 3 & 4 (PWM mode)
	CCP1CON = ch;

	ch = CCP2CON;					// Set up PWM 2
	ch &= 0xF0;						// Dont need capture
	ch |= 0x0C;						// set bit 3 & 4 (PWM mode)
	CCP2CON = ch;

	PR2 = 126;						// Set to middle 50%

	ch = T2CON;						// Set up pre-scale
	ch &= 0xF8;						// Mask top five bits (I dont know why!)
	ch |= 0x02;						// set bit 1.. 16x
	T2CON = ch;						// Store pre-scale

	ch = T2CON;						// post - scaler
	ch &= 0x07;						// keep lower 3 ( clear five top bits here anyway )
	ch |= 0x00;						// clear post-scaler 0000 = 1:1
	T2CON = ch;						// T2CON should hold 6!

	TMR2ON = 1;						// start timer

	while(1)						// endless Loop
		{
		speed(1,64);				// Left motor forward half speed
		speed(0,64);				// Right     "     "    "    "
		delayMs(5000);				// watch motors

		speed(1,64);				// Left motor forward half speed
		speed(0,192);				// Right motor reverse half speed
		delayMs(5000);

		speed(1,10);				// Left motor forward slow speed
		speed(0,228);				// Right motor reverse full speed
		delayMs(5000);

		speed(1,228);				// Left motor forward full speed
		speed(0,10);				// Right motor forward slow speed
		delayMs(5000);

		}
	}

void speed(char motor, int speed)
	{
	if(speed > 128)
		reverse(motor);				// over 128 reverse motor
	else
		forward(motor);
	speed &= 0x7F;					// Make positive
	if(motor)
		CCPR2L = speed;				// Left speed
	else
		CCPR1L = speed;				// right speed
	}

void forward(char motor)
	{
	if(motor)
		{
		RL = 1;						// left
		FL = 0;
		}
	else
		{
		RR = 1;						// right
		FR = 0;
		}
	}

void reverse(char motor)
	{
	if(motor)
		{
		RL = 0;						// left
		FL = 1;
		}
	else
		{
		RR = 0;						// right
		FR = 1;
		}
	}

void delayMs(int dly)				// __delay_ms() messes up with 20mhz xtal
	{								// when over a certain amount.
	while(dly--)
		__delay_ms(1);
	}
 
I can't explain any more than the remarks in the code. There are two motors.. The pwm out on the PORT C pins controls the motors speed the other the direction.

I noticed that the last post mentioned servo motors.... Servo motors only move around 180 degrees so this code isn't applicable to servo's..However it's much the same!!

If you send the value of 128 in the aforementioned code the servo will sit in the central position.... A value of 10..ish will send the motor to the left whilst 245..ish will send it to the right... You can move the servo quite precisely by altering the value sent to the CCP1L register.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top