AT90CAN128 and PWM generation

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

Hi ya all. I'm new to this forum and microcontrollers in general. So, the problem is that I want to make my AT90CAN128 generate a PWM wave from OCR1A and OCR1B outputs (they are contained in port B). From what I've read, in the atmel's manual, the counter will reload itself automatically. I just have to configure it correctly. So can anybody tell me why doesn't this piece of code work the way I want?:


int main (void) {
//starting value of the counter
TCNT1 = 0;
//turning on the outputs
DDRB = 0xff;
//counter configuration
TCCR1A = 0xa3; //10100011
TCCR1B = 0x14; //00010100
TCCR1C = 0;
//top value of the counter
OCR1A = 255;
OCR1B = 255;
//setting the timer interrupt mask
TIMSK1 = 0x6;
//setting the global interrupt enable flag
sei();

while (1);
};

What I want to do is to set counter 1 to work in phase correct PWM mode. It's supposed to control the speed of a DC motor via a suitable driver. The only thing I get is the counter counting up and resetting itself just like in fast PWM mode. I know it has to be something really small. Thanks in advance for any help.

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

You dont need to turn on interrupts to enable pwm. If you do turn on ints, you need an int handler. Easiest way to get started is just enable fast pwm 8 bit and just stick an 8 bit number in OCR1A. Does your target board have a serial port or a debug led? Maybe read the a/d and use a pot for the pwm value?

Imagecraft compiler user

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

Thanks a lot. That's just the thing I've found a few moments ago. I changed the configuration of the counter a little and got rid of the interrupts for now, and it started working. Thanks to your help, I can be sure what the problem was :) . I haven't been uploading the program yet, I was just testing it offlne in AVR Studio. As for the hardware, I have a MB-128 AVR minimodule with AT90CAN128 on it and I can debug it easily with JTAG. I'll try to do as you said with the ADC, but first I'll have to learn how to use it from the atmel's manual :) . Thanks again.

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

Hello, I just encountered the same problem today.
I need a Phase Correct PWM on PB6 and PB7.
So I configured Timer1 channels B and C to be PWMs.
But when I enable the Output Compare interrupts for B and C, the microcontroller doesn't do anything.
Instead if I don't enable the interrupts then it works.
Problem is that for the moment I can't measure if the PWM work.
What is the correct sequence and what is the meaning of the interrupts and why doesn't the uC start when I enable the interrupts?
My code is very similar to the one posted above.

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

bbogdanmircea wrote:
My code is very similar to the one posted above.

Show it, as we can't see it, and did you try the answers given above?

bbogdanmircea wrote:
I need a Phase Correct PWM on PB6 and PB7.

What makes you think you need PC PWM?

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

Why would you enable the interrupts? You don't need them for PWM. You do need the correct compare mode (COMnx bits).

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

bbogdanmircea wrote:
Instead if I don't enable the interrupts then it works.

Most likely because you don't have a corresponding interrupt service routine for the enabled interrupt.  Most tool chains have a bad interrupt capture that disables interrupts and loops forever. 

If you need the interrupt for something other then the PWM (as interrupts are not needed for PWM to function) then provide the needed ISR() or don't enable the Output Compare interrupt!!!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

Hello,

You are totally right, if you enabled the interrupt but there is no ISR (even empty) implemented, then the uC will not init.

I defined some empty ISRs for the compare outputs and it worked.

Now forward to more strange issues

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

bbogdanmircea wrote:
I defined some empty ISRs for the compare outputs and it worked.
What is the point of enabling an interrupt just to have an empty handler (apart from waking from sleep?). Surely the actual error here was the unnecessary setting of some IE bits in the first place? Just providing empty ISRs to paper over the cracks is not much of a solution.

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

I agree with you, for me the problem was that I have some libraries that enable the interrupts by default.

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

That in itself is "odd". I understand a library/driver may choose to enable an interrupt in some peripheral but, if so, it's kind of beholden on that library to then also provide the ISR() to catch it.

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

I am new to ATMEL programming but this is my understanding.
One question, does the On Chip Debug work for the AT90CAN128?I have an expensive ATMEL ICE as a JTAG programmer.
If yes, where can I find some guide how to enable it and use it?
I am having some freaky problems with CAN, when I disconnect the CAN plug the uC seems to stop, but I can't find where the problem is and I guess it would be very helpful to debug it especially as I have no way to see what the uC is doing.

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

This is where the money you paid for the Atmel ICE comes into it's own. ISP/JTAG programmers are 10 a penny but the value of the Atmel-ICE is in the debugging it can do.

 

You need to connect the 10 JTAG pins to the right pins onthe CAN128 (four on PORTF I seem to remember) then in AS7 you "start debug and break". If you've not already set "device" to be the Atmel ICE it will prompt you to set it. Otherwise it will program the latest build into the CAN128 and then break at the entry to main() so you can single step, run to breakpoints, watch IO and watch vars etc. etc.

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

Ok, I am using ATMEL ICE just to program the hex file, for the programming I am using a makefile setup.
Is it possible to debug a hex file that is generated externally by using the AVR compiler under Linux, or the code must also be built under ATMEL Studio?

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

There is an extremely tortuous way to debug in Linux using avr-gdb and a "comms proxy" called avarice. You can wrap an IDE such as Eclipse around this to take out some of the pain. But for an easy life get access to Windows and get a free download of AS7.

 

Note that since Microchip took over Atmel there is a move from Windows only AS7 to Windows+Linux MPLABX so you *may* be able to debug the CAN128 with an Atmel ICE from MPLABX but the fact that AT90CAN128 is such an old dinosaur may work against the chances of them supporting that combination.

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

Ok, so basically there is no way to take the .hex generated from the makefile setup in Linux and the debug it with AS7 from Windows?

I should either move all my setup to AS7 or try to find a way to make the avr-gdb work in Linux. Is there some guide on how to do that?

 

And now getting back to the PWM issues, I have 2 PWM that are both generated from the Output Compare Pins from Timer1.

Currently the PWM are configured as 8 bit, but I think the precision of the LSB is not enough for my application.

What is the maximum precision that I can obtain for there 2 outputs, can they go to 10 bit PWM or even 16?

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

16 bit timers can (at the upper limit) give 16 bit resolution.