Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / CUDA

Xilinx FPGA with AVRILOS

Rate me:
Please Sign up or sign in to vote.
4.87/5 (12 votes)
9 Nov 2011CDDL21 min read 49.9K   689   14  
How-To Embed Xilinx FPGA Configuration Data to AVRILOS
/***************************************************************************
 Project:		AVRILOS
 Title:			Timer1 Functions, PWM, Servo control
 Author:		Ilias Alexopoulos
 Version:		1.00
 Last updated:	10-May-2011
 Target:		ATMEGA164p
 File:			Motor.c

* Support E-mail:
* avrilos@ilialex.gr
* 
* license: See license.txt on root directory (CDDL)
*

* DESCRIPTION
* Control of Motor through Dual H bridge with PWM control
***************************************************************************/

#include <avr/io.h>

#include "../includes/types.h"
#include "../includes/settings.h"
#include "../includes/ifc_time.h"
#include "Motor.h"

#ifndef MOD_MOTORTIMER_ON

void f_InitMOTOR(void) {}
void f_MOTOREnable(INT8U v_channel) {}
void f_MOTORDisable(INT8U v_channel) {}
void f_MOTORSet(INT8U v_channel, INT16U v_setpoint)  {}
void f_MOTORDIR(INT8U v_channel, type_motordir v_dir) {}

#else




extern INT8U v_SysStat;


void f_InitMOTOR(void)
{
	/* Stop Timer 1 for initialization*/
    
    (TCCR1A) = 0xA3; // 10-bit PWM
	//(TCCR1B) = 0x0D; // CLK/1024 = 8000Khz (Servo RC)
    (TCCR1B) = 0x0B;  // CLK/64, DC Motor
    
    f_MOTORDisable(0);
    f_MOTORDisable(1);
    (c_MOTORDDR) |= (1 << c_MOTORA_1) | (1 << c_MOTORA_2) | (1 << c_MOTORB_1) | (1 << c_MOTORB_2);
}

void f_MOTOREnable(INT8U v_channel)
{
    
    if (v_channel == 0)
    {
        (c_PWMDDR) |= (1 << b_PWM1);
    }
    else if (v_channel == 1)
    {
        (c_PWMDDR) |= (1 << b_PWM2);
    }
    else //unknown channel, do nothing
    {
    
    }
}

void f_MOTORDisable(INT8U v_channel)
{
    
    if (v_channel == 0)
    {
        (c_PWMDDR) &= ~(1 << b_PWM1);
    }
    else if (v_channel == 1)
    {
        (c_PWMDDR) &= ~(1 << b_PWM2);
    }
    else //unknown channel, do nothing
    {
    
    }
}


void f_MOTORSet(INT8U v_channel, INT16U v_setpoint) 
{
	INT8U v_hi, v_lo;
	
	v_hi = (INT8U) ((v_setpoint >> 8) & 0x03);
	v_lo = (INT8U) (v_setpoint & 0xFF);
	
    if (v_channel == 0)
    {
        (OCR1AH) = v_hi;
        (OCR1AL) = v_lo;
    }
    else if (v_channel == 1)
    {
        (OCR1BH) = v_hi;
        (OCR1BL) = v_lo;
    }
    else //unknown channel, do nothing
    {
    
    }

}

void f_MOTORFwd(INT8U v_channel)
{
	INT8U v_temp;
	
	v_temp = (c_MOTORPORT);
	
    if (v_channel == 0)
    {
		v_temp &= ~( (1 << c_MOTORA_1) | (1 << c_MOTORA_2) );
		v_temp |= (1 << c_MOTORA_1);
    }
    else if (v_channel == 1)
    {
		v_temp &= ~( (1 << c_MOTORB_1) | (1 << c_MOTORB_2) );
		v_temp |= (1 << c_MOTORB_1);
    }
    else //unknown channel, do nothing
    {
    
    }
	
	(c_MOTORPORT) = v_temp;
}

void f_MOTORBwd(INT8U v_channel)
{
	INT8U v_temp;
	
	v_temp = (c_MOTORPORT);
	
    if (v_channel == 0)
    {
		v_temp &= ~( (1 << c_MOTORA_1) | (1 << c_MOTORA_2) );
		v_temp |= (1 << c_MOTORA_2);
    }
    else if (v_channel == 1)
    {
		v_temp &= ~( (1 << c_MOTORB_1) | (1 << c_MOTORB_2) );
		v_temp |= (1 << c_MOTORB_2);
    }
    else //unknown channel, do nothing
    {
    
    }
	
	(c_MOTORPORT) = v_temp;
}

void f_MOTORBrake(INT8U v_channel)
{
	INT8U v_temp;
	
	v_temp = (c_MOTORPORT);
	
    if (v_channel == 0)
    {
		v_temp &= ~( (1 << c_MOTORA_1) | (1 << c_MOTORA_2) );
		v_temp |= ( (1 << c_MOTORA_1) | (1 << c_MOTORA_2) );
    }
    else if (v_channel == 1)
    {
		v_temp &= ~( (1 << c_MOTORB_1) | (1 << c_MOTORB_2) );
		v_temp |= ( (1 << c_MOTORB_1) | (1 << c_MOTORB_2) );
    }
    else //unknown channel, do nothing
    {
    
    }
	
	(c_MOTORPORT) = v_temp;
}


void f_MOTORStop(INT8U v_channel)
{
	INT8U v_temp;
	
	v_temp = (c_MOTORPORT);
	
    if (v_channel == 0)
    {
		v_temp &= ~( (1 << c_MOTORA_1) | (1 << c_MOTORA_2) );
    }
    else if (v_channel == 1)
    {
		v_temp &= ~( (1 << c_MOTORB_1) | (1 << c_MOTORB_2) );
    }
    else //unknown channel, do nothing
    {
    
    }
	
	(c_MOTORPORT) = v_temp;
}


void f_MOTORDIR(INT8U v_channel, type_motordir v_dir)
{

	switch(v_dir)
	{
		case fwd:
			f_MOTORFwd(v_channel);
			break;
		case bwd:
			f_MOTORBwd(v_channel);
			break;
		case brake:
			f_MOTORBrake(v_channel);
			break;
		case stop:
			f_MOTORStop(v_channel);
			break;
		default:;
	}

}
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Systems Engineer AI ZeroCaliber Ltd
Cyprus Cyprus
More than 15 year of Embedded Systems development designing both hardware & software.
Experience with Product Development,lab prototypes and Automated Testers, Sensors, motors and System Engineering. Have used numerous micro-controllers/processors, DSP & FPGAs.

Please check AI ZeroCaliber if you need any help.
You may find also my personal site: Ilialex and my blog site: Ilialex Blog

Comments and Discussions