Skip to content

Latest commit

 

History

History
421 lines (344 loc) · 16.1 KB

README.lia.md

File metadata and controls

421 lines (344 loc) · 16.1 KB

Lia Include Example

auf Github
mit LiaScript ausprobieren

Beispiel 1



PORTB: DDRB:
// preprocessor definition
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

int main (void) {
   DDRB |= (1 << PB5);
   while(1) {
       // PINB = (1 << PB5);   // Dieses Feature ist im Simulator
       PORTB ^= ( 1 << PB5 );
       _delay_ms(1000);
   }
   return 0;
}

@AVR8jsMem.sketch(example_div_id,100000,1)

Beispiel 2


PORTB: DDRB:
PORTC: DDRC:
PORTD: DDRD:
led 13:

byte leds[] = {13, 12, 11, 10};
void setup() {
  Serial.begin(115200);
  for (byte i = 0; i < sizeof(leds); i++) {
    pinMode(leds[i], OUTPUT);
  }
}

int i = 0;
void loop() {
  Serial.print("LED: ");
  Serial.println(i);
  digitalWrite(leds[i], HIGH);
  delay(250);
  digitalWrite(leds[i], LOW);
  i = (i + 1) % sizeof(leds);
}

@AVR8jsMem.sketch(example2_div_id,100000,1,64000000)

Beispiel 3 (phase correct PWM)



PORTB: DDRB: PINB:
TCCR1:
TCNT1:
OCR1A:

#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif

int main(void){
  DDRB |=  (1 << PORTB1); //Define OCR1A as Output
  TCCR1A |= (1 << COM1A1) | (1 << WGM10);  //Set Timer Register
  TCCR1B |= (1 << CS12) | (1 << CS10);
  OCR1A = 0;
  int timerCompareValue = 0;
  while(1) {
   while(timerCompareValue < 255){ //Fade from low to high
    timerCompareValue++;
    OCR1A = timerCompareValue;
    _delay_ms(4);

  }
   while(timerCompareValue > 0){ //Fade from high to low
    timerCompareValue--;
    OCR1A = timerCompareValue;
    _delay_ms(4);
    }
   }
}

@AVR8jsMem.sketch(example3_div_id,10000,1)

Beispiel 4 (fast PWM)



PORTB: DDRB: PINB:
TCCR1:
TCNT1:
OCR1A:

#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif

int main(void){
  DDRB |=  (1 << PORTB1); //Define OCR1A as Output
  TCCR1A |= (1 << COM1A1) | (1 << WGM10);  //Set Timer Register
  TCCR1B |= (1 << CS12) | (1 << CS10) | (1 << WGM12);
  OCR1A = 0;
  int timerCompareValue = 0;
  while(1) {
   while(timerCompareValue < 255){ //Fade from low to high
    timerCompareValue++;
    OCR1A = timerCompareValue;
    _delay_ms(4);

  }
   while(timerCompareValue > 0){ //Fade from high to low
    timerCompareValue--;
    OCR1A = timerCompareValue;
    _delay_ms(4);
    }
   }
}

@AVR8jsMem.sketch(example4_div_id,3000,1)

Beispiel 5 (Musik macher)

Einfach an Pin B2 (auf Arduino Pin Digital 10) mit einem Widerstand Kopfhörer oder Lautsprecher anschließen und raten was gespielt wird. (NOTE_TICK_DURATION_MILLISECONDS sollte vorher auf 40 gesetzt werden, sonst spielt es zu schnell)



        <!-- memout web komponenten -->
        <!-- Textausgaben -->
<memout-element type="bin" address="PORTB"></memout-element>
<memout-element type="bin" address="DDRB"></memout-element>
<memout-element type="bin" address="PINB"></memout-element><br>
<memout-element type="bin" address="TCCR1" bytes="2" endian="little"></memout-element><br>
<memout-element type="bin" address="TCNT1" bytes="2" endian="little"></memout-element><br>
<memout-element type="bin" address="OCR1A" bytes="2" endian="little"></memout-element><br>
<memout-element type="bin" address="OCR1B" bytes="2" endian="little"></memout-element><br>
        <!-- Diagramm -->
<memout-element
    type="diagram2"
    outputs="[bytesToInt(data[0x84],data[0x85]), bytesToInt(data[0x88],data[0x89]), bytesToInt(data[AVR8_REGISTER.OCR1BL],data[AVR8_REGISTER.OCR1BH]),extractBit(data[0x23],2)*450 + 2500]"
    color="blue"
    min="0"
    max="3000"
    width="800"
    height="600"
    interval="1000000"
    title="TCNT1, OCR1A, OCR1B und PB2"
    colors='["red","blue","yellow","green"]'
    labels='["TCNT1","OCR1A","OCR1B","PB2 (Audio)"]'
></memout-element><br>
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
const int NOTE_TICK_DURATION_MILLISECONDS = 10;//schneller für Simulation
//const int NOTE_TICK_DURATION_MILLISECONDS = 40;//Mit echten Arduino bitte dies verwenden!

const float BASE_FREQ = 440.0;
const float FRQ_MULT[] = {1.0,2.3,0.6,4.1};


typedef struct {
  float tone;
  float pulseWidth;
  int duration; // duration in note-ticks
} Note;

Note notes[] = {
 {.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=2,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=12,.pulseWidth=0.5,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=12,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=10,.pulseWidth=0.5,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=10,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=12,.pulseWidth=0.5,.duration=4},{.tone=-16,.pulseWidth=0.25,.duration=4},{.tone=10,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-16,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=-21,.pulseWidth=0.125,.duration=4},{.tone=-9,.pulseWidth=0.125,.duration=4},{.tone=-21,.pulseWidth=0.08333333333333333,.duration=4},{.tone=-9,.pulseWidth=0.08333333333333333,.duration=4},{.tone=-21,.pulseWidth=0.0625,.duration=4},{.tone=-9,.pulseWidth=0.0625,.duration=4},{.tone=-21,.pulseWidth=0.05,.duration=4},{.tone=-9,.pulseWidth=0.041666666666666664,.duration=4},{.tone=-21,.pulseWidth=0.03571428571428571,.duration=4},{.tone=-9,.pulseWidth=0.03125,.duration=4},{.tone=-21,.pulseWidth=0.027777777777777776,.duration=4},{.tone=-9,.pulseWidth=0.025,.duration=4},{.tone=-21,.pulseWidth=0.020833333333333332,.duration=4},{.tone=0,.pulseWidth=1,.duration=16}
};
int notesLength = sizeof(notes)/sizeof(Note);

int calcOcrFq(int prescaler,float freq) {
  return (int)(F_CPU/(prescaler*freq) + 0.5) & 0xffff;
}
//toneId: ... ,A (440Hz) :0,  A# : 1, ... , C : 3, A (880Hz) : 12, ...
int calcFreq(float toneId) {
  return pow(2.0,toneId/12.0)*BASE_FREQ;
}
// pulseWidth between 0.0 and 1.0
void playFq(int prescaler,float fq, float pulseWidth) {
  int timerTop = calcOcrFq(prescaler,fq);
  int timerCompare = timerTop*pulseWidth + 0.5;
  OCR1A = timerTop;
  OCR1B = timerCompare;
  TCNT1 = 0;
}

int main(void){
  DDRB |=  (1 << PORTB2); //Define OCR1B as Output (Pin Name auf arduino: 10)
  TCCR1A |= (1 << COM1A0) | (1 << COM1B1) | (1 << WGM10); // PB2: output, phase correct PWM Modus 9
  TCCR1B |= (1 << CS10) | (1 << CS11) | (1 << WGM13);// prescaler=64, Modus 9
  OCR1A = 0;
  OCR1B = 500;
  int n = 0;
  while(1) {
    for (int i = 0; i < notesLength; i++) {
      playFq(64, calcFreq(notes[i].tone)*FRQ_MULT[n%4], notes[i].pulseWidth);
      for (int t = 0; t < notes[i].duration; t++) _delay_ms(NOTE_TICK_DURATION_MILLISECONDS);
    }
    n++;
  }
}

@AVR8jsMem.sketch(example5_div_id,3000,0)

Beispiel 6 Button





// preprocessor definition
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

int main (void) {
   DDRB |= (1 << PB5);
   int state = 0;
   // aktives überwachen des Pins
   while(1) {
   	   if (state ^ ((PINB >> PB4) & 1)) {
      		state ^= 1;
      		if (state) {
      			PORTB ^= ( 1 << PB5 );
      		}
   	   }
       _delay_ms(1);
   }
   return 0;
}

@AVR8jsMem.sketch(example_div6_id,100000,1)

Beispiel 7 ADC mit JoyStick

Button an Pin 10, x an A0 und y an A1 angeschlossen.



void setup() {
   pinMode(A0,INPUT);
   pinMode(A1,INPUT);
   pinMode(10,INPUT);
   Serial.begin(9600);
}
void loop() {
  Serial.print("x=");
  Serial.print(analogRead(A0));
  Serial.print(" y=");
  Serial.print(analogRead(A1));
  Serial.print(" button=");
  Serial.println(digitalRead(10));
  delay(25);
}

@AVR8jsMem.sketch(example_div7_id,100000,1)

Beispiel 8 ADC

Button an Pin 10, x an A0 und y an A1 angeschlossen.



void setup() {
   pinMode(A0,INPUT);
   Serial.begin(9600);
}
void loop() {
  Serial.println(analogRead(A0));
  delay(25);
}

@AVR8jsMem.sketch(example_div8_id,100000,1)