EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: Dajgoro on August 10, 2013, 05:17:04 pm

Title: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 10, 2013, 05:17:04 pm
Hi.

I've done tons of projects with all sorts of cpu-s and mcu-s, but I always somehow avoided using AVR stuff.
In my latest project I need the Atmega162@8MHz because it has a parallel bus interface dip package and two uart-s.
I tried using AVR studio 6, but I don't know how to setup the project.
It offers me arduino boards, atmel boards and user boards. The user boards have all sorts of markings, but I don't know what do I need to select.
I just want to make a blinking led program for the atmega162 in a decent environment.

I also have winavr, but that just keeps crashing, so that is hopeless.
Title: Re: AVR Studio 6 and ATmega162
Post by: Kremmen on August 10, 2013, 07:15:10 pm
Hi.

I've done tons of projects with all sorts of cpu-s and mcu-s, but I always somehow avoided using AVR stuff.
In my latest project I need the Atmega162@8MHz because it has a parallel bus interface dip package and two uart-s.
I tried using AVR studio 6, but I don't know how to setup the project.
It offers me arduino boards, atmel boards and user boards. The user boards have all sorts of markings, but I don't know what do I need to select.
I just want to make a blinking led program for the atmega162 in a decent environment.

I also have winavr, but that just keeps crashing, so that is hopeless.
OK, i assume you have started Studio and on the start page you clicked from the left the "New Project..." link. That will pop up the new project dialog where these different boards are shown. Don't pay attention to them, they are all either for specific Atmel demo boards or Arduinos or processors not relevant to you. Instead click on the first item on the list (C/C++) after which the center list will change to show project templates for generic C and C++ executables and libraries. Select either C or C++ executable depending on which language you are going to use. Before clicking do fill the bottom edit fields i.e. Project Name, Location and Solution names as appropriate, paying attention to the check boxes on the right. Once those are correct, click on the language selection and you will proceed to select the specific processor from a list of all Atmel chips supported by Studio. That's it really. Once done, you will get the main source file open in the editor and you can start hacking to your heart's content.
Do familiarize yourself with the AVR-libc http://www.nongnu.org/avr-libc/ (http://www.nongnu.org/avr-libc/) since that is what the embedded winAVR-gcc will use as standard library. All processor specific I/O is handled via the <avr/io.h> header that reads the current chip type from the project environment and includes the correct chip specific I/O header file(s).
More help available if required :)
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 10, 2013, 09:23:04 pm
Thanks!

I got a nice menu with a list with all of the AVR mcu-s.

Unfortunately I think I accidentally killed my atmega162. :-BROKE

Lucky it has the same pinout as the original Intel 8051, and I happen to have one :D, so I'll use it to test the hardware until I somehow obtain a new atmega162.

Btw: What would be the correct fuse settings for a external 8MHz crystal (8 MIPS)?
I found this: http://www.engbedded.com/fusecalc/ (http://www.engbedded.com/fusecalc/)
But I am not sure what to do with all of the options.
Title: Re: AVR Studio 6 and ATmega162
Post by: Kremmen on August 11, 2013, 06:06:02 am
The fuse setting for external crystal is selectable from Studio programming popup. You don't need to mess with the hex values, just select the setting from a list and program it. But make sure you actually have the crystal in place and functional, otherwise you'll brick the chip until there is a working crystal.
Title: Re: AVR Studio 6 and ATmega162
Post by: Psi on August 11, 2013, 06:31:26 am
What makes you think you killed it?
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 12, 2013, 04:16:28 am
What makes you think you killed it?
Because the bus pirate gizmo can't connect to it anymore, and when I plug the chip into the universal programmer I only get errors.

I am using the buspirate 3.5 so I have to set the fuses by hand.
Title: Re: AVR Studio 6 and ATmega162
Post by: Kremmen on August 12, 2013, 04:48:06 am
OK, in that case you need to check the datasheet chapter "memory programming". I assume your chip has such a chapter but i haven't checked, not using that particular MCU myself.

Regarding programming, my recommendation would be to get a JTAG-ICE II or 3 despite its cost, as it will let you program any Atmel chip and enables proper debugging as well. I have always failed to understand how people have planned to quality assure their code without a proper debugger. For trivial programs that may not be necessary. Anything serious - can't be done without.
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 12, 2013, 04:10:16 pm
Thanks!

I usually make stuff with pic mcu-s, but for this project the ATmega162 was the ideal choice.
I do have a pickit 2 clone ;D (it works just like the real thing), but for avr I have the buspirate and a usb serial gizmo that I fail understand how it works.
http://www.ebay.com/itm/1PCS-USBasp-USBISP-3-3V-5V-AVR-Programmer-USB-ATMEGA8-New-/261021924011?pt=LH_DefaultDomain_0&hash=item3cc61e7aab (http://www.ebay.com/itm/1PCS-USBasp-USBISP-3-3V-5V-AVR-Programmer-USB-ATMEGA8-New-/261021924011?pt=LH_DefaultDomain_0&hash=item3cc61e7aab)

It has a ATmega8 and that is it. How can you interface the ATmega8 with usb when it doesn't have usb support(as far as I know)?
Title: Re: AVR Studio 6 and ATmega162
Post by: Kremmen on August 12, 2013, 04:18:08 pm
Probably by similar software USB implementation that used to be in Arduinos. I have an Uno with a BGA version of Mega8 doing the USB bit in software. It is slow but works for that purpose.
Title: Re: AVR Studio 6 and ATmega162
Post by: Psi on August 12, 2013, 11:09:41 pm
Yep, USBasp is a software implementation of USB.

Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 13, 2013, 04:54:48 am
How can you bitbang usb anyway? Don't you need to run it on a 48MHz clock or something like that?
Title: Re: AVR Studio 6 and ATmega162
Post by: Bored@Work on August 13, 2013, 05:42:13 am
How can you bitbang usb anyway? Don't you need to run it on a 48MHz clock or something like that?

USB low-speed runs at 1.5 MBit/s.
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 20, 2013, 04:12:08 am
Wee, new chip arrived, and it works like a charm.
It didn't work at first, but then I figured out that I forgot to set the DDR port, oops. ::)
Also since I am still getting used to win 7 I hit the don't copy button when trying to overwrite the new hex file in the programmers folder.

Anyway, how does the compiler know for which chip is compiling when there is no chip define statement in the code. When using PICs I had to define the pic.

Next step, trying to figure out how to use the external bus, like with the 8051.
Title: Re: AVR Studio 6 and ATmega162
Post by: Psi on August 20, 2013, 04:30:29 am
It uses the compilers command-line switch "-mmcu=" (supplied by IDE for your project) to set the correct register addresses for your selected MCU.

So, including io.h gets all your io addresses for your mcu automatically.

I imagine interrupt.h, wdt.h etc. do the same thing
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 20, 2013, 05:13:18 am
It uses the compilers command-line switch "-mmcu=" (supplied by IDE for your project) to set the correct register addresses for your selected MCU.

So, including io.h gets all your io addresses for your mcu automatically.

I imagine interrupt.h, wdt.h etc. do the same thing
Thanks.

Btw, for accessing the external bus I wrote this code:

Register init:
Code: [Select]
MCUCR=0b11000000;
DDRC = 0xFF;
PORTC = 0x00;
EMCUCR=0b00001111; /*long cycle for slow peripherals*/

Read and write routines:
Code: [Select]
unsigned char *p;

unsigned char b_rd(unsigned short A)
{
PORTC=A>>8;
return p[0x8000+(A&255)];
}

void b_wr(unsigned short A, unsigned char B)
{
PORTC=A>>8;
p[0x8000+(A&255)]=B;
return;
}

It is working for me, I used a 8k ram to test it, but if you spot any issues please let me know.
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 27, 2013, 01:04:23 am
I am trying to get a 8 bit PWM working on portb.1, fast mode.
I did read the datasheet, but I must have missed something, since it is not working.
All of the tuts are made for OC0, which I can't use because the pin is set as a clock output already.
So what registers should be set to get the OC2 working?
Title: Re: AVR Studio 6 and ATmega162
Post by: Psi on August 27, 2013, 02:33:38 am
DDRB   (set PB1 as output pin)

TCCR2
  -WGM21 1 (set waveform mode to fast pwm)
  -WGM20 1 (set waveform mode to fast pwm)

  -CS22 0  (set timer clock freq)
  -CS21 1  (set timer clock freq)
  -CS20 1  (set timer clock freq)

  -COM21  1  (clear compare output on match and set on overflow)
  -COM20  0  (clear compare output on match and set on overflow)


OCR2 = 128  (half of 8 bit counter, so 50% duty)


Should be around 50% pwm @ 1khz from a 8mhz xtal if i did my math right.
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 27, 2013, 03:30:35 am
Won't work.

Code:
Code: [Select]
DDRB=0b11111011;

/*FOC2 WGM20 COM21 COM20 WGM21 CS22 CS21 CS20*/
TCCR2=0b01101011;

OCR2=192;/*non 50% signal*/
Title: Re: AVR Studio 6 and ATmega162
Post by: Psi on August 27, 2013, 05:43:19 am
hm.. i cant see anything obvious wrong.

Check you dont have the mcu in ATMega161 compatibility mode  (M161C fuse bit set (programmed to 0) )
It's off by default so shouldn't be the issue unless you've been changing fuses.

If your trying the code together with other code maybe quickly try it in a new project, just to be sure something else in your code isn't interfering with the timer settings

You should also avoid setting things like DDRB=0b11111011;  It's easy to overwrite a DDRB setting your using somewhere else.

DDRB |= (1<<PB1);   //will set the PB1 bit without changing anything else.
DDRB &= ~(1<<PB1);   //will clear the PB1 bit without changing anything else.
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 27, 2013, 11:48:39 pm
I didn't set that fuse.
I use the DDRB=0b11111011; notation because I like to set all of the pins at once.
Later I tried enabling the uart1, and that worked, but I still can't get the pwm to work for some reason.
Title: Re: AVR Studio 6 and ATmega162
Post by: Psi on August 28, 2013, 12:16:32 am
It could be to do with setting bits in the right order. Maybe you can't set them all at once like that.

I dunno if it applies to the timers but sometimes enabling something will clear other settings.
Trying to enable something and configure it in the same operation may not work.

Also, try writing 1 to the TCNT2 (the reg that holds the count)  Maybe it need somewhere to start from.

Also, i noticed the datasheet says to switch DDRB after configuring the timer.
So try move your DDRB line to after the setup.
(i doubt this is the issue but its worth a try, i think they only say that so you don't get garbage out the pin while configuring bits)

Can you post your entire .c file. Maybe i can spot an issue somewhere else that's causing this.

If you really get stuck i can pull the ATmega162 out of my car pc, put it on a breadboard and get OC2 working.
I'd rather not if possible though, as it will take ages to get out.
Title: Re: AVR Studio 6 and ATmega162
Post by: Dajgoro on August 28, 2013, 06:04:12 am
Didn't help.

Here is the code, it is a bit messy, since it is a work in progress.
Btw, I am also trying to get both of the uarts working.
https://mega.co.nz/#!olFTVarL!TNvhFyNsOfg7_rcl2h0OZELBO_5jRKmaP_seN9OANm8 (https://mega.co.nz/#!olFTVarL!TNvhFyNsOfg7_rcl2h0OZELBO_5jRKmaP_seN9OANm8)