Vollständiges C Projekt mit
Frequenzsynthese für Schrittmotor Betrieb
Communikation und Befehle über serielle Schnittstelle
Im Format z.B: Mo,100<CR>
= Motor ON, 100Hz Nur
als Prinzip. also anpassen an Hardware usw....
16MHz Quarz Software
als ZIP
für WINAVR Stepmot.zip
main.c
//------------------------------------------------------------------------------
// Programmed by Christof Ermer
//------------------------------------------------------------------------------
//gu8SineVal[iNN] = round( 127.5 * sin(2*M_PI* iNN/256.0) + 1 );
// f(x)= Amplitude * sin ( 2* M_PI * NN/X_AUFLOESUNG ) + OFFSET
/*
//f(x) = a * sin (2*Pi*x - b) + o
a ist die Amplitude
b ist die Phasenverschiebung
o ist der "Offset"-Wert
für x setzt du halt n/d ein. (n € N)
d ist die Schrittanzahl pro Periode
*/
/*
ODER
iNN)= 0..255
F(x) = 128 + 127* sin( iNN/2562*M_PI* )
*/
#ifdef UART_USE_ENABLED
static char* gpcRxPars;
static char gcaRxStr[RX_STRMAX + 1]; //+1 for the 0
static char gcaNumStr[NUMMAX + 1]; //+1 for the 0
static char gcaStr[STRMAX + 1]; //+1 for the 0
static COMMAND_TYPE gsCmd;
#endif
//static uint8_t gcaSegmentStr[SEGMENTSTRMAX + 1]; //+1 for the 0
uint32_t gu32_Ticks; // T2 timeCounter, only NN_Counter for
Keypressings
#ifdef USE_ERROR_FLAG
uint8_t gbErrorFlag;
#endif
#ifdef TIMER_TICK_USED
// ****************************************************************
//volatile SIGNAL(TIMER2_OVF_vect) //oder
SIGNAL--INTERRUPT(SIG_OVERFLOW2) = mit SEI am Anfang
SIGNAL( TIMER2_OVF_vect ) //oder SIGNAL--INTERRUPT(SIG_OVERFLOW2) = mit
SEI am Anfang
// *******************************************************************
{
gu32_Ticks++;
UART_TXRX_Check(); //IMMER VORNAHANDEN !!
IN ALLEN LOOPS DIE ZEIT BRAUCHEN
};
#endif
#ifdef UART_USE_ENABLED
// *******************************************************************
void UART_TXRX_Check(void)
/*
* Get received character from ringbuffer
* uart_getc() returns in the lower byte the received character and
* in the higher byte (bit_MSK) the last receive error
* UART_NO_DATA is returned when no data is available.
*/
// *******************************************************************
{
uint16_t uiRxUart;
uiRxUart = uart_getc();//!FETCH ALLWAYS BYTE FROM RX RINGBUFF INTERRUPT!
if( !(uiRxUart & UART_NO_DATA) ) //0x0100
{
// * new data available from UART
// * check for Frame or Overrun error
#ifdef UART_ERRORMSG
if( uiRxUart & (UART_FRAME_ERROR
| UART_OVERRUN_ERROR | UART_BUFFER_OVERFLOW) )
{
ResetRxBuff();
uart_init( UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) );// !! Init UART
interface
// Framing Error detected, i.e no stop bit detected
Print_PStrCR( PSTR("UartErr") );
};
#endif
//NOW SELCT ACTION FROM ORDER
//Check CR
if(gpcRxPars < (gcaRxStr +
RX_STRMAX)) //ENDANSCHLAG ERREICHT ?
{
switch(
(unsigned char)uiRxUart )
{
case 32: // SPACE weg
{
break;
};
case 10: // LF weg
{
break;
};
case 13: // order!! from Terminal
{
CheckOrder(gcaRxStr);//gcaRxStr = accumulated str
ResetRxBuff();
break;
};
default: //Accumulate
{
// Accumulate String
only to gcaRxStr with uiAAA Parser
*gpcRxPars++ =
(unsigned char)(uiRxUart & 0x0FF);//Append rxByte
*gpcRxPars = 0; //ZERO carry
};
};//switch
}// if
(pcRxPars < (gcaRxStr + RXSTRMAX))
else // To much data Rx, than
clear simple all
{
//overflow of RX clears all
ResetRxBuff(); // Clear Rx Parspointer in
Buff an terminate RxStr with 0
};//if(gpcRxPars < (gcaRxStr + RXSTRMAX))
// ENDE DER RX EMPFANGSAUSWERTUNG
};
};
#endif
// *******************************************************************
int main(void)
// *******************************************************************
{
//uint8_t u8NN;
//INITS
cli();//The global interrupt flag is maintained in the I bit of the
status register (SREG).
//Wichtige Schritte.. so bald wie möglich
//SFIOR = (1<<PUD); //Pullup Disable
StartTickTimer(); //8 Bit Timer 2 = MS
Tick Counter
//Pointer and Strings init
wdt_enable(WDTO_2S); //Set 1.9Sec WatchDog ENABLE
wdt_reset();
//WATCHDOG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Initialize UART library, pass baudrate and avr cpu clock
// with the macro UART_BAUD_SELECT()
ResetRxBuff();
uart_init( UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) );
gu32_Ticks = 0;
// ************ MAIN LOOP *********************
for (;;) /* loop forever */ //Aber schnell > 200.000
mal/Sekunde
// ************ MAIN LOOP *********************
{
sei();
wdt_reset(); //WATCHDOG!
//SekundenTick
if( !(gu32_Ticks % 244 ) ) //Hier kann
was gemacht werden jede 1/10 Sekunde
{
PrintLongCR( gu32_Ticks);
};
}; //main loop Ende
#ifdef UART_USE_ENABLED
UART_TXRX_Check(); //IMMER VORNAHANDEN !!
IN ALLEN LOOPS DIE ZEIT BRAUCHEN
#endif
};
// ******************* MAIN END
********************************************
main.h
#ifndef MAIN_HEADER //Prevents multiple includes
#define MAIN_HEADER
#include "stepmot.h"
typedef struct
{
float
fSM_Soll_FRQ;
float fSM_Ist_Frq;
//Wird
incrementiert bis == gsStMPar.fSM_Soll_FRQ für
Rampe
uint8_t u8SM_RampenSteigung;
uint8_t
u8SM_Mode; //Full=1 Half = 2
uint32_t u32MovedSteps;//compare with MovedSteps //Wieviel hat sich der
Motor gedreht..
uint32_t
u32SM_ToRunCnt; //PULSE die der Motor
laufen soll
uint16_t u16SM_StepsRotationType;
//=PREFDEF_STEPS_PER_ROTATION
uint16_t u16StepsPerRotation;
//u8SM_Mode * u16SM_StepsRotationType=PREFDEF_STEPS_PER_ROTATION=
uint8_t u8Stop_PWM_Rate; //u0 ..255
}STEPMPARTYPE;
//gsStMPar.ucStepMConditionFlag