Code:
Chip type : ATmega16
Program type : Application
Clock frequency : 8,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
volatile unsigned char uc_channel;
volatile unsigned int ui_pwm[4];
volatile unsigned int ui_cycle;
// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
PORTC=0; // Port C abschalten = alle Ausgänge aus
}
// Timer 1 output compare B interrupt service routine
interrupt [TIM1_COMPB] void timer1_compb_isr(void)
{
switch(uc_channel)
{
case 0:
PORTC.0=1; // Kanal 1 ein
OCR1A=ui_pwm[0];
break;
case 1:
PORTC.1=1; // Kanal 2 ein
OCR1A=ui_pwm[1];
break;
case 2:
PORTC.2=1; // Kanal 3 ein
OCR1A=ui_pwm[2];
break;
case 3:
PORTC.3=1; // Kanal 4 ein
OCR1A=ui_pwm[3];
break;
};
TCNT1=0;
uc_channel++;
if ( uc_channel>3){uc_channel=0;};
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0x0F;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000,000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: On
TCCR1A=0x00;
TCCR1B=0x02;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x18;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
uc_channel=0;
ui_pwm[0]=1300; // Pulsbreite Kanal 1
ui_pwm[1]=1400; // Pulsbreite Kanal 2
ui_pwm[2]=1500; // Pulsbreite Kanal 3
ui_pwm[3]=1600; // Pulsbreite Kanal 4
ui_cycle=5000; // 4 Kanale, jeder 4te Zyklus bediendt wieder den selben Kanal
OCR1B=ui_cycle;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
};
}
Probier den Code mal aus, sollte die Funktion so erfüllen wie ich es beschrieben hab. Also 8MHz Quarz mit 8/1 Taktteilung auf Timer 1.
Der Code ist für die Bedienung von 4 Kanälen auf PortC 0 ... 3 ausgelegt.
Die gewünschten PWM Werte sind in ui_pwm[0...3] abzulegen.
Der Überlaufwert und der Zyklenzähler ist auf die Anzahl der Kanäle einzustellen.
Ich hab den Code mal so auf die schnelle zusammengefügt - man möge mir bitte deshalb eventuelle Fehler verzeihen.
Wenn man es noch schneller haben will, kann man die Interrupt Routinen auch in Assembler schreiben.
Die Impulserzeugung läuft in Software, deshalb ist jeder beliebige Port als Ausgang verwendbar.
@oberallgeier
Mit dem L293D hab ich noch nichts gebastelt - ich kann dir also nicht sagen ob der Code auch damit geht.
Lesezeichen