Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
david.prentice
PostPosted: Mar 14, 2012 - 10:57 AM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16536
Location: Wormshill, England

Quite honestly, most projects only need a few pin-change inputs. So you can use INT0, INT1, INT2 pins. The INTn pins have the advantage of selecting edge or level separately.

OTOH, the INTn pins are not always convenient. The PCINTn_vect interrupts of the modern AVRs can work on any pin.

The first stage of any design is to write down your requirements. Preferably on disk so that you can print on paper.

If you have a clear description of what you want to do, the implementation is easier. Especially because you can ask others to inspect or advise. And when you come back to your project in a year's time, you can understand what you were doing.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
JollyMolly
PostPosted: Mar 14, 2012 - 12:20 PM
Wannabe


Joined: Feb 14, 2012
Posts: 60


Yh i understand as it was OR it shouldnt change the value. But.it.certainly has an affect on the prog
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 14, 2012 - 12:24 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62899
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Yh i understand as it was OR it shouldnt change the value. But.it.certainly has an affect on the prog


No it does not - in fact the optimiser in the compiler will likely recognise that it does nothing and not generate code. If the behaviour changes you are misunderstanding something else.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JollyMolly
PostPosted: Mar 14, 2012 - 12:56 PM
Wannabe


Joined: Feb 14, 2012
Posts: 60


I don't know how I can explain it better,

When initialising the ADC you have to set the control registers
like ADMUX

I had it set as ADMUX |= 1<<REFS0

and the code works fine, displays conversion results as predicted

however

When Later I though to maker it more clear to readers

I set ADMUX |= 0<<REFS1 | 1<<REFS0;

The prog still works but there is a delay on the conversion results, it will display a Zero Value and then update after just over a second.

All that just because I put 0<<REFS1,

I checked all my code every last bit of it and found that removing the 0<<REFS1, fixed my problem.

Bear in mind REFS1 is assigned to Zero by default,
and the initialisation was not in a loop.

Strange, I think it is a compiler Issue.
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Mar 14, 2012 - 01:46 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 26092
Location: Wisconsin USA

Quote:

I set ADMUX |= 0<<REFS1 | 1<<REFS0;

I cannot see the fascination that people seem to have with |= and AVR I/O registers. In particular with ADMUX we see threads over and over with garbage ADMUX.

Is it >>that<< hard to just rebuild it each time? Pseudo code when getting ready for a conversion:
"ADMUX = (reference selection) | (channel selection); "

Now indeed there may be ADLAR or other special-purpose in there. The point is with an AVR app it is rare (at least for my apps) to have more than one reference V for an app.

Code:

// VREF
// ====
//   Note that in ADMUX, bit 7 is REFS1; bit 6 is REFS0; bit 4 is REFS2.

//   REFS2:0      ADMUX Mask      Description
//   -------    ----------      -----------
//   0          0x00         Use Vcc as Aref
//   1         0x40         External AREF on PB0
//   2         0x80         Internal 1.1V bandgap
//   3                     n/a
//   6         0x90         Internal 2.56V w/ capacitor on PB0
//   7         0xd0         Internal 2.56V; no connection to PB0
#define   ADC_VREF_VCC   0x00
#define   ADC_VREF_AREF   0x40
#define   ADC_VREF_BG      0x80
#define   ADC_VREF_INTCAP   0x90
#define   ADC_VREF_INT   0xd0

#define ADC_VREF_TYPE ADC_VREF_VCC
...
// **************************************************************************
// *
// *      R E A D _ A D C
// *
// **************************************************************************
//
// Read the AD conversion result
unsigned int   read_adc         (unsigned char adc_input)
{
   ADMUX=adc_input|ADC_VREF_TYPE;
   // Start the AD conversion
   ADCSRA|=0x40;
   // Wait for the AD conversion to complete
   while ((ADCSRA & 0x10)==0);
   ADCSRA|=0x10;
   return ADCW;
}


No |= or &=~ in sight. Short/fast code sequence and no RMW possible issues. Plain to read.

Quote:

All that just because I put 0<<REFS1,

I checked all my code every last bit of it and found that removing the 0<<REFS1, fixed my problem.
...
Strange, I think it is a compiler Issue.

Let's see the generated code fragments. Then we don't have to "think".
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 14, 2012 - 02:30 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62899
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

I set ADMUX |= 0<<REFS1 | 1<<REFS0;

What is the order of precendence of those operators? (personally I don'tknow and would have to look it up - using parentheses avoids a lot of manual reading!)

OK, I couldn't help myself so I had to check:
Code:
   ADMUX |= 1<<REFS0;
  3c:   3e 9a          sbi   0x07, 6   ; 7
   ADMUX |= 0<<REFS1 | 1<<REFS0;
  3e:   3e 9a          sbi   0x07, 6   ; 7

Those lines are logically identical so cannot possibly have changed the program behaviour.

Oh and just to check this is the dog's breakfast you get fro -O0:
Code:
   ADMUX |= 1<<REFS0;
  74:   a7 e2          ldi   r26, 0x27   ; 39
  76:   b0 e0          ldi   r27, 0x00   ; 0
  78:   e7 e2          ldi   r30, 0x27   ; 39
  7a:   f0 e0          ldi   r31, 0x00   ; 0
  7c:   80 81          ld   r24, Z
  7e:   80 64          ori   r24, 0x40   ; 64
  80:   8c 93          st   X, r24
   ADMUX |= 0<<REFS1 | 1<<REFS0;
  82:   a7 e2          ldi   r26, 0x27   ; 39
  84:   b0 e0          ldi   r27, 0x00   ; 0
  86:   e7 e2          ldi   r30, 0x27   ; 39
  88:   f0 e0          ldi   r31, 0x00   ; 0
  8a:   80 81          ld   r24, Z
  8c:   80 64          ori   r24, 0x40   ; 64
  8e:   8c 93          st   X, r24

But still identical.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JollyMolly
PostPosted: Mar 14, 2012 - 08:12 PM
Wannabe


Joined: Feb 14, 2012
Posts: 60


Sorry they had parentheses around them like (0<<REFS1)|(1<<REFS0)

If you saying they are identical then are really don't know.

Try 01 optimization, with avr studio 5.1
 
 View user's profile Send private message  
Reply with quote Back to top
JollyMolly
PostPosted: Mar 18, 2012 - 04:15 AM
Wannabe


Joined: Feb 14, 2012
Posts: 60


Hello,

I need help with serial interface USART0, can anyone show me how to initiliase with 1284p using internal 8mhz oscilator?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 18, 2012 - 01:05 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62899
Location: (using avr-gcc in) Finchingfield, Essex, England

There are articles about this in the tutorial forum. It can be as simply as setting UBRR and the TXEN/RXEn bits and then for transmit waiting for UDRE and flling UDR and for reception waiting for RXC and reading UDR.

Using the internal oscillator is not a great idea - I'd pay $0.25 for a crystal if I were you to rule out one of the likely problems you'll face.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JollyMolly
PostPosted: Mar 18, 2012 - 01:33 PM
Wannabe


Joined: Feb 14, 2012
Posts: 60


I tried to simulate the serial port using VSPE and Proteus but it does not work.

I just want to test it using a simple program that transmits a string of characters to the pc.

Can't find a code for the 1284p. I even tried the code from the usart tutorial and it didn't work.
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Mar 18, 2012 - 01:56 PM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16536
Location: Wormshill, England

We are very kind and helpful to those students that show some effort.

The USART of a mega1284P is the same as the mega88/168/328 used in the Arduino or the 164/324/644/1284 family or most modern AVRs. You can see many examples.

It is slightly different to the old mega8/16/32 chips. Compare the differences by looking at URSEL in the mega32 data sheet.

But you can use <util/setbaud.h>.
Look it up in the Studio Help->avr-libc.a docs

Show what effort you have made.
Describe your symptoms / problems.
Ask specific questions.

There is no problem in students posting their code and asking for help. If it means that you understand how and why anything works, not only does your project succeed but you can do the next one yourself

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
bobgardner
PostPosted: Mar 18, 2012 - 05:12 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21390
Location: Orlando Florida

If you ask several AVR programmers what steps will get the serial port talking, I bet most would say: 1)write a function to init the uart. Name it void inituart(void); 2)Write a function to send a char parameter to the uart. Name it void putchar(char c); 3) Write a function to return a char from the uart. Name it char getchar(void); 4)Write a main program that calls inituart() then calls putchar('U'); in a loop. Plug the AVR RS232 transmit pin into your PC RS232 receive pin, and see if you can read the UUUUUUUU from the AVR. If you can't then the 8MHz RC oscillator in the AVR is not within the required 2% accuracy for serial comms. It is usually within 10%, but if it is greter than 2%, I'd expect to see garbage on the serial port. Perform these steps and report your results. Hurry. This is urgent. You have a dozen engineers with 30 years of experience on 5 continents on planet earth waiting for your next message.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
indianajones11
PostPosted: Mar 18, 2012 - 05:28 PM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3627
Location: San Diego, Ca

OP's saying the simulation's not working, it seems to me since the sentence before it not working was about Proteus . Try it on the hardware, the tutorial UART code WORKS ( after adjusting UART registers for a different MCU than in the tut. ) .

_________________
1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 18, 2012 - 06:31 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21390
Location: Orlando Florida

Yo Jolly... your problem is either hardware or software. Post a picture of the mega1284p board you are using. We will look to see if it has an RS232 voltage level translator chip on it like a MAX232. Your multimeter should see -7V sitting on the pin2 from the AVR DB9. The PC DB9 should have -7V on pin3. These are the tx lines passing each other on their way to and from the AVR and PC. It won't work if you try to make PC tx talk to AVR tx.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits