2024年3月26日火曜日

fastPWM and ADC/INT0,PCINT (attiny13a)

// https://www.adnbr.co.uk/articles/adc-and-pwm-basics

// code:   https://gist.github.com/adnbr/9289235

#define F_CPU 9600000

#define LED PB1

#include <avr/io.h>


void pwm_setup(){

  TCCR0B |= (1<<CS01); // prescale 8

  TCCR0A |= (1<<WGM01) | (1<<WGM00);// fast PWM

  // Clear OC0B output on compare match, upwards counting.

  TCCR0A |= (1<<COM0B1);

}


void pwm_write(int val)

{

OCR0B = val;

}


void adc_setup(void)

{

// set the ADC input to PB2/ADC1

  ADMUX |= (1<<MUX0); // mux1/0=01 select adc1

  ADMUX |= (1<<ADLAR);

  // set the prescaler 128 , adps2 add by me

  ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADEN);

}


int adc_read(void)

{

ADCSRA |= (1<<ADSC); // start conversion

  while(ADCSRA&(1<<ADSC));

  return ADCH;

}

int main (void)

{

   int adc_in;

    // LED is an output.

    DDRB |= (1 << LED);

    adc_setup();

    pwm_setup();

    while (1) {

        // Get the ADC value

        adc_in = adc_read();

        // Now write it to the PWM counter

        pwm_write(adc_in);

    }

}

ーーーーーーーーーINT0ーーーーーーーーーーーーー

// https://kurobekoblog.com/attiny_gaibuwarikomi 

#include <avr/interrupt.h>

volatile byte state = 0;

int16_t cnt;

ISR(INT0_vect){

  state = 1;

}

void sw_up(){

  switch(state){

   case 0:

    break;

   case 1:

    cnt = 1000;

    state = 2;

    break;

  case 2:

    cnt --; // avoid chattering

    if(cnt == 0){

    PORTB ^= 0b00000001; // toggle led on off

    state = 0;

   }

 }

}

void setup(){

  DDRB  |=  0b0000001 ; // pb0 output

  PORTB |=  0b0000010 ; // pb1 input pullup

  MCUCR &= ~0b00000011; // if low level, int0 occur

  GIMSK |= (1<<INT0) ; // int0 enable

  sei();

}

void loop(){

  sw_up();

}

ーーーPCINTーーーーーーーーーーーーーーーーーーー

注:pb3をinput pullupでスイッチにつなぎ、スイッチはグランドともつなぐ

 #include <avr/interrupt.h>

#include <avr/sleep.h>

#define F_CPU 960000UL 

#define LED PB0

//#define SWITCH PINB3 どっちでもうごくが。。。。

#define SWITCH PB3

ISR(PCINT0_vect){ // 0番台の割り込みがpcint0~5ピンに割当られているようだ

  if(PINB & _BV(SWITCH)){

    PORTB ^= _BV(LED);

  }

}

int main(void)

{

   DDRB |= _BV(LED); // SET PORT B0 AS OUTPUT(ALL OTHERS ARE INPUT)

 PORTB = 0b0001000 ; // pb3(switch) input pullup

 PCMSK |= _BV(SWITCH); // SET PCINT MASK TO LISTEN TO PORT B3

 GIMSK |= _BV(PCIE);

 sei();

 set_sleep_mode(SLEEP_MODE_PWR_DOWN);

 for (;;){

  sleep_mode();

}

}

ーーーーーーPCINTもう一つーーーーーーーーー  

// https://karlsnautr.blogspot.com/2013/04/attiny13apcint.html
#include <avr/interrupt.h>
#include <avr/sleep.h>
#define F_CPU 960000UL

//#define SWITCH PINB3
#define SWITCH PB3

ISR(PCINT0_vect){
  if(bit_is_set(PINB,PORTB4)){ 
    PORTB ^= (1<<PORTB2); // PB2を反転
  }
}

void initIOInterrupt(){
  GIMSK |= (1<<PCIE); // PCINT enable
  PCMSK = 0b00010000; // PB4// switch押すで5Vかかる
  sei();
}
int main(void)
{
    DDRB |= (0<<DDB4)|(1<<DDB2); // PB4が入力,PB2が出力
    PORTB = 0b0010100 ; // PORTBの出力値
    initIOInterrupt();
    while(true){
    }
}
  

0 件のコメント:

コメントを投稿