Обсуждаем контроллеры компании Atmel.
Ответить

Atmega8 ШИМ. Нюансы.

Сб сен 18, 2021 11:31:09

Добрый день. Есть вот такой код. Который в Протеусе работает отлично - а на железе не хочет работать ШИМ.
Суть кода - замеряем частоту на ноге PD4 и повышаем или понижаем напряжение аппаратным ШИМ. При нажатии PC1 или PCD - отключаем ШИМ на определенном выводе. Что не так, подскажите


#include <mega8.h>
#include <delay.h>
// Declare your global variables here
unsigned long int freqtaho;

// Timer2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
// Place your code here
freqtaho = TCNT0;
if( freqtaho>36) {
if(OCR1A>0x00){
OCR1A=OCR1A-1;

}
if(OCR1B>0x00){
OCR1B=OCR1B-1;

}
}



if(freqtaho<30) {
if(OCR1A<0xff){
OCR1A=OCR1A+1;

}
if(OCR1B<0xff){
OCR1B=OCR1B+1;

}
}
TCNT0=0;
}

void main(void)
{
// Declare your local variables here
DDRC.0=0;
PORTC.0=1;
DDRC.1=0;
PORTC.1=1;
PORTB.1=0;
DDRB.1=1;
PORTB.2=0;
DDRB.2=1;
DDRC.2=1;
PORTC.2=1;
DDRC.3=1;
PORTC.3=1;
// Input/Output Ports initialization

// Port C initialization




// Timer/Counter 0 initialization
// Clock source: T0 pin Falling Edge
TCCR0=(1<<CS02) | (1<<CS01) | (0<<CS00);
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 125,000 kHz
// Mode: Fast PWM top=0x00FF
// OC1A output: Inverted PWM
// OC1B output: Inverted PWM
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 2,048 ms
// Output Pulse(s):
// OC1A Period: 2,048 ms Width: 2,048 ms
// OC1B Period: 2,048 ms Width: 2,048 ms
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (1<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (1<<WGM12) | (0<<CS12) | (1<<CS11) | (1<<CS10);
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: TOSC1 pin
// Clock value: PCK2/64
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=1<<AS2;
TCCR2=(0<<PWM2) | (0<<COM21) | (0<<COM20) | (0<<CTC2) | (1<<CS22) | (0<<CS21) | (0<<CS20);
TCNT2=0x00;
OCR2=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (1<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<TOIE0);

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);

// USART initialization
// USART disabled
UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (0<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);


// SPI initialization
// SPI disabled
SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);

// TWI initialization
// TWI disabled
TWCR=(0<<TWEA) | (0<<TWSTA) | (0<<TWSTO) | (0<<TWEN) | (0<<TWIE);

// Global enable interrupts
#asm("sei")

while (1)
{
// Place your code here

if((PINC.0==0)&&(PINC.1==1)){

TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (1<<WGM10);
PORTC.2=1;
PORTC.3=0;
}
if((PINC.0==1)&&(PINC.1==0)) {

TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (1<<WGM10);

PORTC.2=0;
PORTC.3=1;
}



}
}
Ответить