kramann.info
© Guido Kramann

Login: Passwort:










28 Tonerzeugung über Toggle-Ausgängen im CTC-Mode

28 Sound generation via toggle outputs in CTC mode (EN google-translate)

28 Generowanie dźwięku przez wyjścia przełączające w trybie CTC (PL google-translate)

Ausgangspunkt: Scheduler (s.o.). Ergänzung:

Starting point: Scheduler (s.o.). Complement:

Punkt początkowy: Scheduler (s.o.). uzupełnienie:

  //Toggle auf OC1A:
  TCCR1A &= ~(1<<COM1A1);
  TCCR1A |=  (1<<COM1A0);
  pinMode(9,OUTPUT);  

Code 28-1: Ergänzende Codezeilen, um auf dem digitalen Ausgang 9 (OC1A) die gleiche Frequenz zu haben, wie auf Pin 12 (über Interrupt-Funktion).

Programm insgesamt:

Total program:

Całkowity program:

#include<avr/interrupt.h>


volatile bool AN=false;
volatile bool ZUSTAND=false;


//SIGNAL(SIG_OUTPUT_COMPARE1)
//ISR(SIG_OUTPUT_COMPARE1)
//ISR(TIMER1_COMP_vect)
ISR(TIMER1_COMPA_vect)
{
    digitalWrite(12,ZUSTAND);
    ZUSTAND=!ZUSTAND;        
}

void setup() 
{

//  TCCR1B |= (1<<WGM13); //Mode 12
  TCCR1B &= ~(1<<WGM13); //Mode 4
  TCCR1B |= (1<<WGM12);
  TCCR1A &= ~(1<<WGM11);
  TCCR1A &= ~(1<<WGM10);
    
  //Vorteilung 1: 16000000Hz
  TCCR1B &= ~(1<<CS12);
  TCCR1B &= ~(1<<CS11);
  TCCR1B |= (1<<CS10);
    
  //f = fclk/(2*N*(OCR1A+1))
  //OCR1A = (fclk/(f*2*N))-1
  //OCR1A = 8000000/440 - 1 ==  18181
  OCR1A=18181;  
  
  TIMSK1 |= (1<<OCIE1A); //Mode 4
  sei();
  
  //Toggle auf OC1A:
  TCCR1A &= ~(1<<COM1A1);
  TCCR1A |=  (1<<COM1A0);
  pinMode(9,OUTPUT);  
     
  //durch Interrupt gesteuert:
  pinMode(12,OUTPUT);  
}

void loop() 
{       
}

Code 28-2: CTC mit Timer 1 auf digitalem Ausgang 9.

int zustand = 0;
//A0 = D18
//A1 = D19
//A2 = D20
//A3 = D21
//A4 = D22
//A5 = D23
//A6 = D24
//A7 = D25
//A8 = D26
//A9 = D27
//A10 = D28
//A11 = D29
void setup() 
{
  pinMode(18,OUTPUT);  
  pinMode(19,OUTPUT);  
  //pinMode(3,OUTPUT);  
  //tone(3,710);
  
  
}

void loop() 
{
  digitalWrite(18,1);    
  digitalWrite(19,1);    
  delay(800);
  digitalWrite(18,0);    
  digitalWrite(19,0);    
  delay(800);
}

Code 28-3: Testweise Pin A0 und A1 als digitale Ausgänge verwenden.

Basisprogramm - alles inaktiv, LED D13 blinkt

Basic program - all inactive, LED D13 flashes

Podstawowy program - wszystkie nieaktywne, dioda LED D13 miga

int zustand = 0;
//A0 = D18
//A1 = D19
//A2 = D20
//A3 = D21
//A4 = D22
//A5 = D23
//A6 = D24
//A7 = D25
//A8 = D26
//A9 = D27
//A10 = D28
//A11 = D29
void setup() 
{
  pinMode(18,INPUT);  //A0
  pinMode(19,INPUT);  //A1  
  pinMode(20,INPUT);  //A2  
  pinMode(21,INPUT);  //A3  
  pinMode(22,INPUT);  //A4  
  pinMode(23,INPUT);  //A5

  pinMode(2,INPUT);  //D2
  pinMode(3,INPUT);  //D3
  pinMode(4,INPUT);  //D4
  pinMode(5,INPUT);  //D5
  pinMode(6,INPUT);  //D6
  pinMode(7,INPUT);  //D7
  pinMode(8,INPUT);  //D8
  pinMode(9,INPUT);  //D9
  
  pinMode(13,OUTPUT); //LED  
}

void loop() 
{
  digitalWrite(13,1);    
  delay(500);
  digitalWrite(13,0);    
  delay(500);
}

Code 28-4: Basisprogramm - alles inaktiv, LED D13 blinkt

Analoge Werte lesen und auf die serielle Schnittstelle schreiben

Read analog values ​​and write to the serial interface

Odczytaj wartości analogowe i napisz do interfejsu szeregowego

int sensorwert=0;
//A0 = D18
//A1 = D19
//A2 = D20
//A3 = D21
//A4 = D22
//A5 = D23
//A6 = D24
//A7 = D25
//A8 = D26
//A9 = D27
//A10 = D28
//A11 = D29
void setup() 
{
  pinMode(18,INPUT);  //A0
  pinMode(19,INPUT);  //A1  
  pinMode(20,INPUT);  //A2  
  pinMode(21,INPUT);  //A3  
  pinMode(22,INPUT);  //A4  
  pinMode(23,INPUT);  //A5

  pinMode(2,INPUT);  //D2
  pinMode(3,INPUT);  //D3
  pinMode(4,INPUT);  //D4
  pinMode(5,INPUT);  //D5
  pinMode(6,INPUT);  //D6
  pinMode(7,INPUT);  //D7
  pinMode(8,INPUT);  //D8
  pinMode(9,INPUT);  //D9
  
  pinMode(13,OUTPUT); //LED 
 
  Serial.begin(9600);   

  //LINKER MOTOR:
  
}

void loop() 
{
  sensorwert = analogRead(11);
 
  Serial.write(48+(sensorwert/1000)%10);
  Serial.write(48+(sensorwert/100)%10);
  Serial.write(48+(sensorwert/10)%10);
  Serial.write(48+(sensorwert/1)%10);
  Serial.write('
');
  Serial.write('
');
  
  digitalWrite(13,1);    
  delay(250);
  digitalWrite(13,0);    
  delay(250);
}

Code 28-5: Analoge Werte lesen und auf die serielle Schnittstelle schreiben

Vorwärtsfahrt-Test

Forward driving test

Test jazdy do przodu

int sensorwert=0;
//A0 = D18
//A1 = D19
//A2 = D20
//A3 = D21
//A4 = D22
//A5 = D23
//A6 = D24
//A7 = D25
//A8 = D26
//A9 = D27
//A10 = D28
//A11 = D29
void setup() 
{
  pinMode(18,INPUT);  //A0
  pinMode(19,INPUT);  //A1  
  pinMode(20,INPUT);  //A2  
  pinMode(21,INPUT);  //A3  
  pinMode(22,INPUT);  //A4  
  pinMode(23,INPUT);  //A5

  pinMode(2,INPUT);  //D2
  pinMode(3,INPUT);  //D3
  pinMode(4,INPUT);  //D4
  pinMode(5,INPUT);  //D5
  pinMode(6,INPUT);  //D6
  pinMode(7,INPUT);  //D7
  pinMode(8,INPUT);  //D8
  pinMode(9,INPUT);  //D9
  
  pinMode(13,OUTPUT); //LED 
 
  Serial.begin(9600);   

  //Rechte Motorseite:
  pinMode(10,OUTPUT);  //Enable 1, L293_PIN1
  pinMode(8,OUTPUT);   //Input 2, L293_PIN7
  pinMode(3,OUTPUT);   //Input 1, L293_PIN2
  
  digitalWrite(3,1);    
  digitalWrite(8,0);    
  analogWrite(10,0);
  
  //Linke Motorseite:
  pinMode(13,OUTPUT);  //Enable 2, L293_PIN9
  pinMode(19,OUTPUT);   //A1 Input 3, L293_PIN15
  pinMode(11,OUTPUT);   //Input 4, L293_PIN10
  
  digitalWrite(19,1);    
  digitalWrite(11,0);    
  analogWrite(13,0);
  
}

void loop() 
{
  sensorwert = analogRead(11);
 
  Serial.write(48+(sensorwert/1000)%10);
  Serial.write(48+(sensorwert/100)%10);
  Serial.write(48+(sensorwert/10)%10);
  Serial.write(48+(sensorwert/1)%10);
  Serial.write('
');
  Serial.write('
');
  
  digitalWrite(13,1);    
  analogWrite(10,0);
  analogWrite(13,255);
  delay(250);
  digitalWrite(13,0);    
  analogWrite(10,255);
  analogWrite(13,0);  
  delay(250);
}

Code 28-6: Vorwärtsfahrt-Test

An einer Wand entlang fahren

Drive along a wall

Jedź wzdłuż ściany

int sensorwert=0;
int regler=0;
//A0 = D18
//A1 = D19
//A2 = D20
//A3 = D21
//A4 = D22
//A5 = D23
//A6 = D24
//A7 = D25
//A8 = D26
//A9 = D27
//A10 = D28
//A11 = D29
void setup() 
{
  pinMode(18,INPUT);  //A0
  pinMode(19,INPUT);  //A1  
  pinMode(20,INPUT);  //A2  
  pinMode(21,INPUT);  //A3  
  pinMode(22,INPUT);  //A4  
  pinMode(23,INPUT);  //A5

  pinMode(2,INPUT);  //D2
  pinMode(3,INPUT);  //D3
  pinMode(4,INPUT);  //D4
  pinMode(5,INPUT);  //D5
  pinMode(6,INPUT);  //D6
  pinMode(7,INPUT);  //D7
  pinMode(8,INPUT);  //D8
  pinMode(9,INPUT);  //D9
  
  pinMode(13,OUTPUT); //LED 
 
//  Serial.begin(9600);   

  //Rechte Motorseite:
  pinMode(10,OUTPUT);  //Enable 1, L293_PIN1
  pinMode(8,OUTPUT);   //Input 2, L293_PIN7
  pinMode(3,OUTPUT);   //Input 1, L293_PIN2
  
  digitalWrite(3,1);    
  digitalWrite(8,0);    
  analogWrite(10,0);
  
  //Linke Motorseite:
  pinMode(13,OUTPUT);  //Enable 2, L293_PIN9
  pinMode(19,OUTPUT);   //A1 Input 3, L293_PIN15
  pinMode(11,OUTPUT);   //Input 4, L293_PIN10
  
  digitalWrite(19,1);    
  digitalWrite(11,0);    
  analogWrite(13,0);
  
}

void loop() 
{
  sensorwert = analogRead(11);
 
  regler = 250-sensorwert;

  if(regler>100)
      regler=100;
  else if(regler<-100)    
      regler=-100;
  regler/=32;  
  /*
  Serial.write(48+(sensorwert/1000)%10);
  Serial.write(48+(sensorwert/100)%10);
  Serial.write(48+(sensorwert/10)%10);
  Serial.write(48+(sensorwert/1)%10);
  Serial.write('
');
  Serial.write('
');
  */
  //digitalWrite(13,1);    
  analogWrite(13,255-regler);
  
  //digitalWrite(13,0);    
  analogWrite(10,255+regler);
  
  delay(10);
  
}

Code 28-7: An einer Wand entlang fahren

void setup() 
{
  TCCR1B &= ~(1<<WGM13); //Mode 4
  TCCR1B |= (1<<WGM12);
  TCCR1A &= ~(1<<WGM11);
  TCCR1A &= ~(1<<WGM10);
    
  //Vorteilung 1: 16000000Hz
  TCCR1B &= ~(1<<CS12);
  TCCR1B &= ~(1<<CS11);
  TCCR1B |= (1<<CS10);
    
  //f = fclk/(2*N*(OCR1A+1))
  //OCR1A = (fclk/(f*2*N))-1
  //OCR1A = 8000000/440 - 1 ==  18181
  OCR1A=18181;  //0,5Hz == 4 Schläge!
  
  //Toggle auf OC1A:
  TCCR1A &= ~(1<<COM1A1);
  TCCR1A |=  (1<<COM1A0);
  pinMode(9,OUTPUT);    
}

void loop() 
{       
}

Code 28-8: Nur CTC-Toggle ohne Interrupt.

void setup() 
{

//  TCCR1B |= (1<<WGM13); //Mode 12
  TCCR3B &= ~(1<<WGM33); //Mode 4
  TCCR3B |= (1<<WGM32);
  TCCR3A &= ~(1<<WGM31);
  TCCR3A &= ~(1<<WGM30);
    
  //Vorteilung 1: 16000000Hz
  TCCR3B &= ~(1<<CS32);
  TCCR3B &= ~(1<<CS31);
  TCCR3B |= (1<<CS30);
    
  //f = fclk/(2*N*(OCR1A+1))
  //OCR1A = (fclk/(f*2*N))-1
  //OCR1A = 8000000/440 - 1 ==  18181
  OCR3A=18181;  //0,5Hz == 4 Schläge!
  
  //Toggle auf OC1A:
  TCCR3A &= ~(1<<COM3A1);
  TCCR3A |=  (1<<COM3A0);
  pinMode(5,OUTPUT);    //OC3A
}

void loop() 
{       
}

Code 28-9: Das Gleiche für Timer 3 (ebenfalls 16Bit).

void setup() 
{
  //Vorteilung 8: 2000000Hz
  TCCR4B &= ~(1<<CS43);
  TCCR4B |=  (1<<CS42);
  TCCR4B &= ~(1<<CS41);
  TCCR4B &= ~(1<<CS40);
    
  //f = fclk/(2*N*(OCR1A+1))
  //OCR1A = (fclk/(f*2*N))-1
  //OCR1A = 1000000/1760 - 1 ==  568
  OCR4A=568; 
  
  //Toggle auf OC1A:
  TCCR4A &= ~(1<<COM4A1);
  TCCR4A |=  (1<<COM4A0);
  pinMode(13,OUTPUT);    //OC4A
}

void loop() 
{       
}

Code 28-10: Das Gleiche für Timer 4 (allerdings 10 Bit, darum höhere Frequenz ausgewählt, da unempfindlicher bzgl. kleinerer Abweichungen.)