Спойлер
Код: Выделить всё
/*******************************************************
This program was created by the
CodeWizardAVR V3.14 Advanced
Automatic Program Generator
© Copyright 1998-2014 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project : test_5
Version :
Date : 24.09.2021
Author :
Company :
Comments:
Chip type : ATmega8A
Program type : Application
AVR Core Clock frequency: 1,000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*******************************************************/
#include <mega8.h>
#include <inttypes.h>
//#include <avr/io.h>
#include <interrupt.h>
#include <delay.h>
volatile uint8_t period = 3;
volatile uint8_t pulsewidth = 125;
volatile uint16_t icr1_temp;
// ADC interrupt service routine
ISR(TIM1_OVF){
icr1_temp = (uint16_t) ((uint32_t) ((uint32_t)period * 0x10000)/256);
/*if(icr1_temp < 850){ //Без этого куска кода МК виснет при значении ICR1 < 850
icr1_temp = 850;
}*/
ICR1 = icr1_temp;
OCR1A = (uint16_t) ((uint32_t) ((uint32_t)pulsewidth * ICR1)/256);
}
ISR(ADC_INT){
if(ADMUX & (1<<MUX0)){
pulsewidth = ADCH; //get ch 1
ADMUX = (0<<REFS1)|(1<<REFS0)|(1<<ADLAR) | (1<<MUX1) | (0<<MUX0); //set ch 0
}else{
period = ADCH;
PORTD = period; //get ch 0
ADMUX = (0<<REFS1)|(1<<REFS0)|(1<<ADLAR) | (1<<MUX1) | (1<<MUX0); //set ch 1
}
//ICR1 = (uint16_t) ((uint32_t) ((uint32_t)period * 0x10000)/256);
//OCR1A = (uint16_t) ((uint32_t) ((uint32_t)pulsewidth * ICR1)/256);
//delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
}
void main(void)
{
// Write your code here
// Input/Output Ports initialization
// Port B initialization
// Function: Bit1=Out
DDRB = (1<<DDB1);
// State: Bit1=0
PORTB = (0<<PORTB1);
// Port D initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: ...... kHz
// Mode: Ph. & fr. cor. PWM top=ICR1
// OC1A output: Non-Inverted PWM
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 0 us
// Output Pulse(s):
// OC1A Period: 0 us
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (1<<CS10);
TCNT1=0x00;
ICR1=0x0300;
OCR1A=0x00;
OCR1B=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0);
// Analog Comparator: Off
ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
// ADC initialization
// ADC Clock frequency: ..... kHz
// ADC Voltage Reference: AVCC pin
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX = (0<<REFS1) | (1<<REFS0) | (1<<ADLAR);
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADFR) | (0<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
SFIOR=(0<<ACME);
ADCSRA |= (1<<ADSC); // Start the AD conversion
sei();
while (1){
}
}


