- avr16_V120.zip
- avr16
- build.dep
- adc.P
- applic.P
- dbgext.P
- debugger.P
- delay.P
- eeprom.P
- fpga_cfg.P
- fpgassi.P
- kernel.P
- Motor.P
- robolayer.P
- serapp.P
- Timer0.P
- typeconv.P
- Uart.P
- xcs_cfg.P
- build.err
- build.lst
- adc.lst
- applic.lst
- dbgext.lst
- debugger.lst
- delay.lst
- eeprom.lst
- fpga_cfg.lst
- fpgassi.lst
- kernel.lst
- kernel.map
- Motor.lst
- robolayer.lst
- serapp.lst
- Timer0.lst
- typeconv.lst
- Uart.lst
- xcs_cfg.lst
- build.obj
- adc.o
- applic.o
- dbgext.o
- debugger.o
- delay.o
- eeprom.o
- fpga_cfg.o
- fpgassi.o
- kernel.elf
- kernel.o
- kernel.obj
- Motor.o
- robolayer.o
- serapp.o
- Timer0.o
- typeconv.o
- Uart.o
- xcs_cfg.o
- build.rom
- kernel.eep
- kernel.rom
- cfg
- compile.in
- compileflags.in
- env.in
- hw.in
- srcobj.in
- swdef.in
- kernel_elf.aps
- kernel_elf.aws
- license.txt
- makefile
- src
- applic
- debug
- includes
- periphext
- periphint
- Utils
|
/***************************************************************************
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.
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