My first shot at the ol LED PWM --what did I do wrong

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

atmega128
stk500

As soon as ocr1a sets..the debugger hangs and wont advance to the next function. The device hangs too. Thing is I'm not sure right. I read the datasheet and websits and figured out each value I wanted.

I feel there is just some order of things to get it to not screw up that I havent quite put them in

anyway..heres my code

Description:
	1. drives LEDs attched to vairious ports with PWM
	

PORTS--------Type--------Description

PORTD:		Output		LED outputs for bottom row of device
PORTB:		Output		LED outputs for middle row of device
PORTE:		Output		LED outputs for top row of device

*/

//Includes & CPU define
#define F_CPU 8000000
#include
#include"delay_x.h"
#include
#include

//Pin defines
#define	r10			PORTD ,0	//LED output pin
#define r11			PORTD ,1	//LED output pin
#define	r12			PORTD ,2	//LED output pin
#define r13			PORTD ,3	//LED output pin
#define	r14			PORTD ,4	//LED output pin
#define r15			PORTD ,5	//LED output pin
#define	r16			PORTD ,6	//LED output pin
#define r17			PORTD ,7	//LED output pin

#define	r20			PORTB ,0	//LED output pin
#define r21			PORTB ,1	//LED output pin
#define	r22			PORTB ,2	//LED output pin
#define r23			PORTB ,3	//LED output pin
#define	r24			PORTB ,4	//LED output pin
#define r25			PORTB ,5	//LED output pin
#define	r26			PORTB ,6	//LED output pin
#define r27			PORTB ,7	//LED output pin

#define	r30			PORTE ,0	//LED output pin
#define r31			PORTE ,1	//LED output pin
#define	r32			PORTE ,2	//LED output pin
#define r33			PORTE ,3	//LED output pin
#define	r34			PORTE ,4	//LED output pin
#define r35			PORTE ,5	//LED output pin
#define	r36			PORTE ,6	//LED output pin
#define r37			PORTE ,7	//LED output pin


// from AVR035: Efficient C Coding for AVR 
//quick pin change macros
#ifndef _AVR035_H_ 
#define _AVR035_H_ 
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT)) 
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) 
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT)) 
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) 
#define SETBITMASK(x,y) (x |= (y)) 
#define CLEARBITMASK(x,y) (x &= (~y)) 
#define FLIPBITMASK(x,y) (x ^= (y)) 
#define CHECKBITMASK(x,y) (x & (y)) 
#define VARFROMCOMB(x, y) x 
#define BITFROMCOMB(x, y) y 
#define C_SETBIT(comb) SETBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 
#define C_CLEARBIT(comb) CLEARBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 
#define C_FLIPBIT(comb) FLIPBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 
#define C_CHECKBIT(comb) CHECKBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 
#endif 

//global varibles
int bottomrow=0;
int middlerow=0;
int toprow=0;

enum { UP, DOWN };

void Initialize_Ports(){
	DDRB=0xFF;
	DDRD=0xFF;
	DDRE=0xFF;
}//Initialize_Ports

void Set_OCR1A(unsigned value)
{
  OCR1AH = value >> 8;
  OCR1AL = value & 0xFF;
}//set OCR1A

void Timer_Init(){
	cli();	// disable all interrupts
	TCCR1B = 0; // disable ticking
	TIMSK = (TIMSK & ~(0x3c)) | 0x04; // enable overflow interrupt
	TCNT1H = 0; // reset counter
	TCNT1L = 0;
	ICR1H = 40000U >> 8; // set overflow value
	ICR1L = 40000U & 0xff;
	TCCR1A = 0xFE; // 11 11 11 10, set channel config
	TCCR1B=0x01A;	//input capture off- mode 14(Wave generation mode)- /8 prescale
	sei(); // reenable interrupts
}//Timer_Init

ISR (TIMER1_COMPA_vect ){
	if (bottomrow==1){
		C_FLIPBIT(r10);
		C_FLIPBIT(r11);
		C_FLIPBIT(r12);
		C_FLIPBIT(r13);
		C_FLIPBIT(r14);
		C_FLIPBIT(r15);
		C_FLIPBIT(r16);
		C_FLIPBIT(r17);
	}//if
	if (middlerow==1){
		C_FLIPBIT(r20);
		C_FLIPBIT(r21);
		C_FLIPBIT(r22);
		C_FLIPBIT(r23);
		C_FLIPBIT(r24);
		C_FLIPBIT(r25);
		C_FLIPBIT(r26);
		C_FLIPBIT(r27);
	}//if
	if (toprow==1){
		C_FLIPBIT(r30);
		C_FLIPBIT(r31);
		C_FLIPBIT(r32);
		C_FLIPBIT(r33);
		C_FLIPBIT(r34);
		C_FLIPBIT(r35);
		C_FLIPBIT(r36);
		C_FLIPBIT(r37);
	}//if	
}//ISR


int main(){
	Initialize_Ports();
		Timer_Init();
	Set_OCR1A(1000);		//at 1Mhz 1000 clocks should be near a millisec


	while(1){

bottomrow=1;//just t get it working
}
}//main

Any help would be appreciated..thanks:-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You enabled the timer overflow interrupt, but didn't provide an ISR for it. The compA ISR will never be entered because you didn't enable it.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ohhhh..ok...I mixed the two up..

btw.theres no reason to call the interrupt at overflow unless I need it right? Like does the uC's state machine resets TCNT1 at overflow for me??

...or do I have to do all the cleanup myself?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Like does the uC's state machine resets TCNT1 at overflow for me??

The timer resets to zero when it hits TOP (which in mode 14 is what you set in ICR1). The overflow will never be hit since it happens at MAX, which is never reached unless TOP is set to MAX. The exception might be that the overflow flag might be set at the start because of the way you set up the timer, but you would have to check to see if that is true.

Regards,
Steve A.

The Board helps those that help themselves.