Haz una pregunta
  Foros de Electrónica » Diseño digital » Interfaces y Programación
Foros Registrarse ¿Olvidaste tu contraseña?

Temas similares

01/11/2014 #1

Avatar de locodelafonola

problemas con TCCR1A , TCCR1B , TIMSK y OCR1A en compilacion C avr studio
hola gente .. bueno trate de pasar de un micro attiny2313 a atmega8515 ... y al cambiar el micro en la compilación original .. creo que cometo un error y no se cual es ... porque me larga la siguiente advertencia """ AVR Simulator: Excessive stack overflow, stop sim
"""" la librería original es esta
Código:
 /*------------------------------------------------------------------------------
 AVR studio 4.13 atiny2313
 Version:        18.7.2014
 Description:    DMX_3Kanal_Dimmer_1kanal_strobo_1kanal_master 

------------------------------------------------------------------------------*/

#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdio.h>

#define F_CPU 12000000

#define LED_R_PIN_OUT DDRD |= (1<<PD5);
#define LED_G_PIN_OUT DDRD |= (1<<PD4);
#define LED_B_PIN_OUT DDRD |= (1<<PD3);

#define LED_R_ON		PORTD |= (1<<PD4);
#define LED_R_OFF		PORTD &= ~(1<<PD4);
#define LED_B_ON		PORTD |= (1<<PD5);
#define LED_B_OFF		PORTD &= ~(1<<PD5);
#define LED_G_ON		PORTD |= (1<<PD3);
#define LED_G_OFF		PORTD &= ~(1<<PD3);


#define DMX_BAUD 250000
#define DMX_LOST_TIMEOUT 8000

volatile unsigned int dmx_lost = DMX_LOST_TIMEOUT;

volatile unsigned int dmx_adresse = 0;
volatile unsigned char dmx_buffer[6];

volatile unsigned char led_kanal[3];

//############################################################################
//DMX Senderoutine
ISR (USART_RX_vect)
//############################################################################
{
	static unsigned int dmx_channel_rx_count = 0;
	static unsigned char dmx_valid = 0;
	unsigned char tmp = 0;
	
	tmp =  UDR;
	
	if(UCSRA&(1<<FE))
	{
		if(dmx_channel_rx_count > 1)
		{
			dmx_lost = 0;
		}
		dmx_channel_rx_count = 0;	
		dmx_buffer[0] = tmp;
		if(dmx_buffer[0] == 0)
		{
			dmx_valid = 1;
			dmx_channel_rx_count++;
		}
		else
		{
			dmx_valid = 0;
		}
		return;
	}
	
	if(dmx_valid)
	{
		if(dmx_channel_rx_count == dmx_adresse) dmx_buffer[1] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+1) dmx_buffer[2] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+2) dmx_buffer[3] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+3) dmx_buffer[4] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+4) dmx_buffer[5] = tmp;
		
		if(dmx_channel_rx_count < 514)
		{
			dmx_channel_rx_count++;
		}
		return;
	}
}

//############################################################################
//Hier wird die Zeit gezählt (Tick 1ms)
ISR (TIMER0_COMPA_vect)
//############################################################################
{
	static unsigned char pwm_counter = 0;

	pwm_counter++;
	if(pwm_counter == 255)
	{
		pwm_counter = 0;
	}
	
	if(pwm_counter >= led_kanal[0]) 
	{
		LED_R_OFF;
	}
	else
	{
		LED_R_ON;
	}
	if(pwm_counter >= led_kanal[1])
	{
		LED_G_OFF;
	}
	else
	{
		LED_G_ON;
	}
	if(pwm_counter >= led_kanal[2]) 
	{
		LED_B_OFF;
	}
	else
	{
		LED_B_ON;
	}
	
	if(dmx_lost<DMX_LOST_TIMEOUT)
	{
		dmx_lost++;
	}
}

//############################################################################
//Hauptprogramm
int main (void) 
//############################################################################
{  
	unsigned int dmx_adresse_tmp;
	unsigned long strobe_counter = 0;
	unsigned char tmp1,tmp2,tmp3;

	//Init usart DMX-BUS
	UBRRH   = (unsigned char)(F_CPU / (DMX_BAUD * 16L) - 1)>>8;
	UBRRL   = (unsigned char)(F_CPU / (DMX_BAUD * 16L) - 1);
	UCSRB|=(1 << RXEN | 1<< RXCIE);
	UCSRC|=(1<<USBS); //USBS0 2 Stop bits	
	
	//Timer0 Timecounter für DMX Ausfall und PWM
	TCCR0A |= (1<<WGM01);
	TCCR0B |= (1<<CS00);
	TIMSK |= (1<<OCIE0A);
	OCR0A = F_CPU/1024/100 - 1; //Tick 1ms
	
	PORTB |= 0xFF;
	PORTD |= (1<<PD6);
	
	DDRD |=(1<<PD2);
	PORTD &=~(1<<PD2);
	
	LED_R_PIN_OUT;
	LED_G_PIN_OUT;
	LED_B_PIN_OUT;
	
	LED_R_ON;
	for(unsigned long tmp = 0;tmp<500000;tmp++)asm("nop");
	LED_R_OFF;
	LED_G_ON;
	for(unsigned long tmp = 0;tmp<500000;tmp++)asm("nop");
	LED_G_OFF;
	LED_B_ON;
	for(unsigned long tmp = 0;tmp<500000;tmp++)asm("nop");
	LED_B_OFF;
	
	sei();//Globale Interrupts Enable
	
	//Endlosschleife
	while(1)
	{
		dmx_adresse_tmp = 0;
		
		if(!(PIND&(1<<PD6))) dmx_adresse_tmp |= 0x01;
				
		if(!(PINB&(1<<PB0))) dmx_adresse_tmp |= 0x02;
 		if(!(PINB&(1<<PB1))) dmx_adresse_tmp |= 0x04;
		if(!(PINB&(1<<PB2))) dmx_adresse_tmp |= 0x08;
		if(!(PINB&(1<<PB3))) dmx_adresse_tmp |= 0x10;
		if(!(PINB&(1<<PB4))) dmx_adresse_tmp |= 0x20;
		if(!(PINB&(1<<PB5))) dmx_adresse_tmp |= 0x40;
		if(!(PINB&(1<<PB6))) dmx_adresse_tmp |= 0x80;
		if(!(PINB&(1<<PB7))) dmx_adresse_tmp |= 0x0100;
		
		if(dmx_adresse != dmx_adresse_tmp) dmx_adresse =  dmx_adresse_tmp;
		
		if(dmx_lost==DMX_LOST_TIMEOUT)
		{
			dmx_buffer[1] = 0;
			dmx_buffer[2] = 0;
			dmx_buffer[3] = 0;
			dmx_buffer[4] = 0;
		}
		
		tmp1 = dmx_buffer[1]*dmx_buffer[4]/255;
		tmp2 = dmx_buffer[2]*dmx_buffer[4]/255;
		tmp3 = dmx_buffer[3]*dmx_buffer[4]/255;
		
		
		if(dmx_buffer[5]<10)
		{
			led_kanal[0] = tmp1;
			led_kanal[1] = tmp2;
			led_kanal[2] = tmp3;
		}
		else
		{
			strobe_counter++;
			if(strobe_counter > (256 - dmx_buffer[5]))
			{
				led_kanal[0] = tmp1;
				led_kanal[1] = tmp2;
				led_kanal[2] = tmp3;
			}
			else
			{
				led_kanal[0] = 0;
				led_kanal[1] = 0;
				led_kanal[2] = 0;
			}
			
			if(strobe_counter > (2 * (256 - dmx_buffer[5])))
			{
				strobe_counter = 0;
				led_kanal[0] = 0;
				led_kanal[1] = 0;
				led_kanal[2] = 0;
			}
		}
	}
}
la cual yo cambie los PORT y el cristal para el atmega8515 ..tal vez cometi algun error mi librería es esta """
Código:
/*------------------------------------------------------------------------------
 
 AVR studio 4.13
 Version:        22.5.2014
 Description:    DMX_3Kanal_Dimmer


 . 
------------------------------------------------------------------------------*/

#include <avr/interrupt.h>
#include <avr/io.h>


#define F_OSC   8000000

#define LED_R_PIN_OUT DDRA |= (1<<PA0);
#define LED_G_PIN_OUT DDRA |= (1<<PA1);
#define LED_B_PIN_OUT DDRA |= (1<<PA2);

#define LED_R_ON		PORTA |=  (1<<PA0);
#define LED_R_OFF		PORTA &= ~(1<<PA0);
#define LED_G_ON		PORTA |=  (1<<PA1);
#define LED_G_OFF		PORTA &= ~(1<<PA1);
#define LED_B_ON		PORTA |=  (1<<PA2);
#define LED_B_OFF		PORTA &= ~(1<<PA2);


#define DMX_BAUD 250000
#define DMX_LOST_TIMEOUT 8000

volatile unsigned int dmx_lost = DMX_LOST_TIMEOUT;

volatile unsigned int dmx_adresse = 0;
volatile unsigned char dmx_buffer[6];

volatile unsigned char led_kanal[3];

//############################################################################
// Rutina de transmisión DMX
ISR (USART_RX_vect)
//############################################################################
{
	static unsigned int dmx_channel_rx_count = 0;
	static unsigned char dmx_valid = 0;
	unsigned char tmp = 0;
	
	tmp =  UDR;
	
	if(UCSRA&(1<<FE))
	{
		if(dmx_channel_rx_count > 1)
		{
			dmx_lost = 0;
		}
		dmx_channel_rx_count = 0;	
		dmx_buffer[0] = tmp;
		if(dmx_buffer[0] == 0)
		{
			dmx_valid = 1;
			dmx_channel_rx_count++;
		}
		else
		{
			dmx_valid = 0;
		}
		return;
	}
	
	if(dmx_valid)
	{
		if(dmx_channel_rx_count == dmx_adresse) dmx_buffer[1] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+1) dmx_buffer[2] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+2) dmx_buffer[3] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+3) dmx_buffer[4] = tmp;
		if(dmx_channel_rx_count == dmx_adresse+4) dmx_buffer[5] = tmp;
		
		if(dmx_channel_rx_count < 514)
		{
			dmx_channel_rx_count++;
		}
		return;
	}
}

//############################################################################
//  Aquí, el tiempo contado (garrapata es de 1 ms)
ISR (TIMER0_COMPA_vect)
//############################################################################
{
	static unsigned char pwm_counter = 0;

	pwm_counter++;
	if(pwm_counter == 255)
	{
		pwm_counter = 0;
	}
	
	if(pwm_counter >= led_kanal[0]) 
	{
		LED_R_OFF;
	}
	else
	{
		LED_R_ON;
	}
	if(pwm_counter >= led_kanal[1])
	{
		LED_G_OFF;
	}
	else
	{
		LED_G_ON;
	}
	if(pwm_counter >= led_kanal[2]) 
	{
		LED_B_OFF;
	}
	else
	{
		LED_B_ON;
	}
	
	if(dmx_lost<DMX_LOST_TIMEOUT)
	{
		dmx_lost++;
	}
}

//############################################################################
// Programa principal
int main (void) 
//############################################################################
																																																																																																																																																																																																																																																																																																																			{  
	unsigned int dmx_adresse_tmp;
	unsigned long strobe_counter = 0;
	unsigned char tmp1,tmp2,tmp3;
   
    											//enable watchdog (Timeout= 0.5s)
    
	// Init USART DMX-BUS
	UBRRH   = (unsigned char)(F_OSC / (DMX_BAUD * 16L) - 1)>>8;
	UBRRL   = (unsigned char)(F_OSC / (DMX_BAUD * 16L) - 1);
	UCSRB|=(1 << RXEN | 1<< RXCIE);
	UCSRC|=(1<<USBS); // USBS0 2 bits de parada	
	
	// Temporizador 0 Contador de tiempo de interrupción DMX y PWM
	TCCR1A |= (1<<WGM01);
	TCCR1B |= (1<<CS00);
	TIMSK |= (1<<OCIE1A);
	OCR1A = F_OSC/1024/100 - 1; // Marque 1 ms
	
	PORTC |= 0xFF;
	PORTE |= (1<<PE2);
	
	DDRE |=(1<<PE1);
	PORTE &=~(1<<PE1);
	
	LED_R_PIN_OUT;
	LED_G_PIN_OUT;
	LED_B_PIN_OUT;
	
	LED_R_ON;
	for(unsigned long tmp = 0;tmp<500000;tmp++)asm("nop");
	LED_R_OFF;
	LED_G_ON;
	for(unsigned long tmp = 0;tmp<500000;tmp++)asm("nop");
	LED_G_OFF;
	LED_B_ON;
	for(unsigned long tmp = 0;tmp<500000;tmp++)asm("nop");
	LED_B_OFF;
	
	sei();  // Habilitar Global Interrupciones
	
	// Bucle infinito
	while(1)
	{
		dmx_adresse_tmp = 0;
		
		if(!(PINC&(1<<PC0))) dmx_adresse_tmp |= 0x01;
				
		if(!(PINC&(1<<PC1))) dmx_adresse_tmp |= 0x02;
 		if(!(PINC&(1<<PC2))) dmx_adresse_tmp |= 0x04;
		if(!(PINC&(1<<PC3))) dmx_adresse_tmp |= 0x08;
		if(!(PINC&(1<<PC4))) dmx_adresse_tmp |= 0x10;
		if(!(PINC&(1<<PC5))) dmx_adresse_tmp |= 0x20;
		if(!(PINC&(1<<PC6))) dmx_adresse_tmp |= 0x40;
		if(!(PINC&(1<<PC7))) dmx_adresse_tmp |= 0x80;
		if(!(PINE&(1<<PE2))) dmx_adresse_tmp |= 0x0100;
		
		if(dmx_adresse != dmx_adresse_tmp) dmx_adresse =  dmx_adresse_tmp;
		
		if(dmx_lost==DMX_LOST_TIMEOUT)
		{
			dmx_buffer[1] = 0;
			dmx_buffer[2] = 0;
			dmx_buffer[3] = 0;
			dmx_buffer[4] = 0;
		}
		
		tmp1 = dmx_buffer[1]*dmx_buffer[4]/255;
		tmp2 = dmx_buffer[2]*dmx_buffer[4]/255;
		tmp3 = dmx_buffer[3]*dmx_buffer[4]/255;
		
		
		if(dmx_buffer[5]<10)
		{
			led_kanal[0] = tmp1;
			led_kanal[1] = tmp2;
			led_kanal[2] = tmp3;
		}
		else
		{
			strobe_counter++;
			if(strobe_counter > (256 - dmx_buffer[5]))
			{
				led_kanal[0] = tmp1;
				led_kanal[1] = tmp2;
				led_kanal[2] = tmp3;
			}
			else
			{
				led_kanal[0] = 0;
				led_kanal[1] = 0;
				led_kanal[2] = 0;
			}
			
			if(strobe_counter > (2 * (256 - dmx_buffer[5])))
			{
				strobe_counter = 0;
				led_kanal[0] = 0;
				led_kanal[1] = 0;
				led_kanal[2] = 0;
			}
		}
	}
}
tambien subo el proyecto completo del atmel estudio 4.13 dentro de el esta la libreria original ...gracias
Respuesta
¿Tienes una mejor respuesta a este tema? ¿Quieres hacerle una pregunta a nuestra comunidad y sus expertos? Registrate

Buscar más temas sobre:
Lupa Interfaces y Programación

Lenguajes de programación, gestión y manejo de puertos

Cerrar
Foros de Electrónica » Diseño digital » Interfaces y Programación

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2011, Crawlability, Inc.