/*****************************
File: l298_control.c
Author: Chris @ http://www.pyroelectro.com
Date: 3/15/2008

Description:
This program varies a PWM duty cycle every 1/2 second
on portb which is used to control a motor with the l298hn
motor driver. The Input PWM runs at 1 KHz frequency.
A simple for loop creates the PWM.
****************************/
#include <p18f452.h>
#include <delays.h>
void main(void)
{
			//Used In Main Control
	long int i=0;
	int on_off =1;

			//L298 Control Parameters
	int speed_pos=5;
	int speed_neg=45;
	int direction = 0;
	
			//Digital Port Setups
TRISA = 0x00;
TRISB = 0x00;

PORTA = 0x00;
PORTB = 0x00;

			//Enable The Motor
PORTBbits.RB0 = 1;

			//Main Infinite While Loop
	while(1){
					//Change LED State Each Loop
			PORTAbits.RA0 = on_off;
			
					//Generates PWM To Drive The Motor
							//For 0.5 Seconds
		for(i=0;i<500;i++){
			Delay100TCYx(speed_pos);
			PORTBbits.RB1 = 1;
			PORTBbits.RB2 = 0;
			
			Delay100TCYx(speed_neg);
			PORTBbits.RB1 = 0;
			PORTBbits.RB2 = 1;
		}
		
			//Control Method For Continous
				//Speed Up/Slow Down & Reversing Direction
		if(speed_pos == 5 && speed_neg == 48)
			direction = 0;
		else if(speed_pos == 45 && speed_neg == 5)
			direction = 1;
			
			//Speed Variation Through Changing The PWM
		if(direction == 0){
			speed_pos++;
			speed_neg--;
		}
		if(direction == 1){
			speed_pos--;
			speed_neg++;
		}
	}

}