Интерисут инфа по програмированию АВР на Си....
Если у кого есть куски и или полные коды программ для управления шаговиками, а так же обработки приходящей инфы с RS-232, буду очень благодарен за помощь.
roboforum.ruТехнический форум по робототехнике. |
|
|
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.5 Standard
Automatic Program Generator
c Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mail:office@hpinfotech.com
Project : cpu2
Version :
Date : 09.05.2005
Author : Artem
Company : Art Inc. UnLTD.
Comments:
Chip type : AT90S2313
Clock frequency : 8,000000 MHz
Memory model : Tiny
External SRAM size : 0
Data Stack size : 32
*****************************************************/
#include <90s2313.h>
#define RXB8 1
#define TXB8 0
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// UART Receiver buffer
#define RX_BUFFER_SIZE 10
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on UART Receiver buffer overflow
bit rx_buffer_overflow;
// UART Receiver interrupt service routine
interrupt [UART_RXC] void uart_rx_isr(void)
{
char status,data;
status=USR;
data=UDR;
if ((status & (FRAMING_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the UART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#include <math.h>
signed char cs1=0,cs2=0,ss1=0,ss2=0, cickle=0, cc1,cc2;
int vx, ccounter=0;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=255-7;
// Place your code here
ccounter++;
if (ccounter>1500) {
ccounter=0;
if (cs1<ss1) {cs1=cs1+1;}
if (cs1>ss1) {cs1=cs1-1;}
if (cs2<ss2) {cs2=cs2+1;}
if (cs2>ss2) {cs2=cs2-1;}
}
cc1=cabs(cs1);
cc2=cabs(cs2);
if ((cs1==0)&(ss1<0)) {PORTD.4=1; }
if ((cs1==0)&(ss1>0)) {PORTD.4=0; }
if ((cs2==0)&(ss2<0)) {PORTD.5=1; }
if ((cs2==0)&(ss2>0)) {PORTD.5=0; }
if (cickle==0) {
PORTD.2=1;
PORTD.3=1;
}
if (cc1==cickle) {PORTD.2=0; }
if (cc2==cickle) {PORTD.3=0; }
cickle++;
if (cickle>7) { cickle=0; }
}
// Declare your global variables here
int step1,step2,speed1,speed2,delay;
char bufcounter,crc,res;
char commandbuf[6];
void main(void)
{
// Declare your local variables here
signed char stepa,stepb;
unsigned char ca,cb;
// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=In Func0=In
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=T State0=T
PORTB=0x00;
DDRB=0xFC;
// Port D initialization
// Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0x7F;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125,000 kHz
TCCR0=0x03;
TCNT0=0xE1;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1 output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1H=0x00;
OCR1L=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
GIMSK=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x02;
// UART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// UART Receiver: On
// UART Transmitter: Off
// UART Baud rate: 9600
UCR=0x90;
UBRR=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
// Global enable interrupts
#asm("sei")
ca=1;
cb=1;
stepa=0;
stepb=0;
speed1=1;
speed2=4;
delay=3;
while (1)
{
if (stepa<0) { stepa++; if (ca<4) { ca++; } else { ca=1; } }
if (stepa>0) { stepa--; if (ca>1) { ca--; } else { ca=4; } }
if (stepb<0) { stepb++; if (cb<4) { cb++; } else { cb=1; } }
if (stepb>0) { stepb--; if (cb>1) { cb--; } else { cb=4; } }
delay_ms(5*delay);
if (ca==1) { PORTB.0=1; PORTB.1=1; PORTB.2=0; PORTB.3=0; }
if (ca==2) { PORTB.0=0; PORTB.1=1; PORTB.2=1; PORTB.3=0; }
if (ca==3) { PORTB.0=0; PORTB.1=0; PORTB.2=1; PORTB.3=1; }
if (ca==4) { PORTB.0=1; PORTB.1=0; PORTB.2=0; PORTB.3=1; }
if (cb==1) { PORTB.4=1; PORTB.5=0; PORTB.6=0; PORTB.7=0; }
if (cb==2) { PORTB.4=0; PORTB.5=1; PORTB.6=0; PORTB.7=0; }
if (cb==3) { PORTB.4=0; PORTB.5=0; PORTB.6=1; PORTB.7=0; }
if (cb==4) { PORTB.4=0; PORTB.5=0; PORTB.6=0; PORTB.7=1; }
if (rx_counter>0) {
res=getchar();
if (res==10) {
commandbuf[0]=10;
bufcounter=1;
}
if ((bufcounter!=0)&(res<10)) {
commandbuf[bufcounter]=res;
bufcounter++;
}
if (bufcounter==4) {
if (commandbuf[1]==1) { //ШД
if (commandbuf[2]==0) { stepa=0; stepb=0; }
if (commandbuf[2]==1) { stepa=stepa+commandbuf[3]; }
if (commandbuf[2]==2) { stepb=stepb+commandbuf[3]; }
if (commandbuf[2]==3) { stepa=stepa-commandbuf[3]; }
if (commandbuf[2]==4) { stepb=stepb-commandbuf[3]; }
}
if (commandbuf[1]==2) { //движки
if (commandbuf[2]==0) { ss1=0; ss2=0; }
if (commandbuf[2]==1) { ss1=commandbuf[3]; }
if (commandbuf[2]==2) { ss2=commandbuf[3]; }
if (commandbuf[2]==3) { ss1=0-commandbuf[3]; }
if (commandbuf[2]==4) { ss2=0-commandbuf[3]; }
}
}}
};
}
Вернуться в Новичкам или основы основ роботостроения.
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 12