Atmega168, i can't use the PB6 abd PB7 pins

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

Hello Everybody..

I working an a project were i need to use almost all the IO pins available in the atmega168.

Ok im using the internal crystal (8mhz), and i want to use the PINs pb6 and Pb7 to read a digital signal, but i can't... I read the data sheet and say that if you are using the internal clock they won't work.. that's true???

But why?, so if you use the external crystal you need to connected it to the pins PB6 and PB7 also, so why they put I/O pins over there???

Many thanks..

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

Quote:
I read the data sheet and say that if you are using the internal clock they won't work..

Sounds weird. Where in the datasheet can we read that? When using the internal clock (btw. it's not a built-in crystal, it's a RC oscillator) those pins should be available for I/O. Unless of course you happen to use timer2 in asynchronous mode, ie. with a watch crystal connected to those pins..?

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

Using an STK500?

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

Quote:
Using an STK500?

Almost certainly in which case PB6 and PB7 actually appear as XT1/XT2 on the PORTE header.

I wonder if there's anyone who owns an STK500 who hasn't been caught by this at some point?

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

Hello, thanks for answer,

"Sounds weird. Where in the datasheet can we read that?"
Yes is in page 79.. With the title: • XTAL2/TOSC2/PCINT7 – Port B, Bit 7.

Im using an AVRISP MKII..

Actually i tried to put the register ASSR to 0x00, but nothing.. The problem is that i have to use the Arduino IDE.. And maybe in some part, arduino is moving something, so i tried to reset the ASSR, but still the same...

If you tell me that is possible to use that pins even if im using the internal RC OSC 8 MHZ, i will continue trying till i get, i have no choice..

The project:
http://diydrones.com/profiles/blog/show?id=705844%3ABlogPost%3A49235

Many thanks,
Jordi

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

Yes, those pins work just fine if the fuses are set for RC Osc.

What does the schematic of the dev board show them running too?

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

Quote:

Quote:
I read the data sheet and say that if you are using the internal clock they won't work..

Sounds weird. Where in the datasheet can we read that?


Quote:

"Sounds weird. Where in the datasheet can we read that?"
Yes is in page 79.. With the title: • XTAL2/TOSC2/PCINT7 – Port B, Bit 7.

OK, which part of the below says that PB6 and PB7 are NOT I/O pins, if the internal clock is selected and ASSR is NOT selected?

Quote:
• XTAL2/TOSC2/PCINT7 – Port B, Bit 7

XTAL2: Chip clock Oscillator pin 2. Used as clock pin for crystal oscillator or Low-frequency crystal Oscillator. When used as a clock pin, the pin can not be used as an I/O pin.

TOSC2: Timer Oscillator pin 2. Used only if internal calibrated RC Oscillator is selected as chip clock source, and the asynchronous timer is enabled by the correct setting in ASSR. When the AS2 bit in ASSR is set (one) and the EXCLK bit is cleared (zero) to enable asynchronous clocking
of Timer/Counter2 using the Crystal Oscillator, pin PB7 is disconnected from the port, and becomes the inverting output of the Oscillator amplifier. In this mode, a crystal Oscillator is connected to this pin, and the pin cannot be used as an I/O pin.

PCINT7: Pin Change Interrupt source 7. The PB7 pin can serve as an external interrupt source.

If PB7 is used as a clock pin, DDB7, PORTB7 and PINB7 will all read 0.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Quote:
I wonder if there's anyone who owns an STK500 who hasn't been caught by this at some point?

Caught me last week, and I fortunately got the answer on the Bascom forum. I read the STK500 manual, several times, and the uC data sheet, looking for open collector, or other possiblities and reasons for it to "not work".

I do not think this is spelled out very clearly in the STK500 manual, and adding just 1 or 2 sentences would have made it clearer to me, and obviously others as well. I guess it is apparent in the schematic, but this is a tough place to go looking when something does not function as one would anticipate it to.

JC

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

theusch wrote:
Quote:

OK, which part of the below says that PB6 and PB7 are NOT I/O pins, if the internal clock is selected and ASSR is NOT selected?

Uy sorry, i got confused about the clocks.. I just want to be sure, if this has something to do with my problem...

Ok back again with my problem, im using the internal RC OSC at 8mhz, so anybody have an idea what register i need to check that may be interfering with the PB6 and PB7 pins??? i tried the ASSR, but still nothing, something else?

Thanks again.

Jordi

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

If you're using the internal clock, and don't have the asynchronous timer enabled, PB6 and PB7 should be controllable using DDRB, PORTB and PINB just as any other pins on PORTB. I would suggest checking out the hardware. There should be a schematic somewhere I assume?

Btw. from where (physically) are you trying to access the pins? A pinheader or something? If thats's the case, a very quick and simple test would be to write a small program which sets the pins high, and then simply measure the voltage directly at the IC pins with a multimeter. You should read a voltage very close to the supply voltage. Set the pins low, and you should read very close to zero. This way, you wouldn't be fooled by the PCB layout.

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

It's still not clear which variant of Arduino it is that you are using though the mention of mega168 presumably narrows it down a bit. If it's the Diecimila then this is the schematic:

http://arduino.cc/en/uploads/Mai...

I can't help noticing that pins 9 and 10 there seem to have a 16MHz crystal attached!

If it's not this Arduino which one is it so we can check the schematic?

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

Hello again:

I made the board, and i send it to batch, im absolutely sure there is nothing else connected to PB6 and PB7.

The idea was to read two servos pulses, so i setup the timer2 (CTC mode and prescaler 8)just to increment and execute an interrupt every 200 us with auto counter restart.. So every 200us i increment the counters (sr_in1++; and sr_in2++;).. Then when any of the pins (pb6 and 7) change state, they will execute an external interrupt, then i will check if the pin go to HIGH or to LOW, if the pin go to high, means that the PWM from the receiver start, so i restart the counter sr_inx=0;, then when the pin goes down, i read the counter of the desired servo and stored in a variable that indicates the "servo position" and i plus the current time of the TCNT2 register.. pulse_width=(sr_inX*200)+TCNT2;

And is working perfectly in other pins, now in my project i need to use the PB6 and PB7 because the other ones are reserved...

So of course i made a simple test, to see if everything is working, the simple test was only to read PB6 and also PB7 state, connecting a wire to GND and 5V, and if is HIGH turn on a LED connected to PC0, if not turn it off, i was using arduino IDE, but with the GNU commands.. (PINB, DDRB, etc etc.)..

The LED is working and turning on with using other PINs, in this way i realize that the PB6 and PB7 do not want to work, i also put to zero all the register ASSR,also DDRB, i tried everything, i can't read anything with this pins as normally i do with others...

As soon i got home i will send the schematics, if you want to be sure... and also the code..

Many Thanks.

Jordi

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

Could it be the Arduino IDE that is "interfering" because it 'thinks' PB6/PB7 are already used for the crystal on a real Arduino board?

What does the generated code for reading PB6/PB7 actually look like? (and I mean a disassembly of it showing AVR opcodes)

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

OK, thanks very much, to everyone..

I just tested with avr studio, and the pins are fully working... So is a software problem (arduino IDE).

I will keep trying..

See ya..

Jordi

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

Crikey, that's a real "gotcha" in the Arduino IDE if it's being used to develop for a "derivative design" then! You might want to contact them and point this out.

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

http://svn.berlios.de/viewcvs/ar...

I know little about it (well nothing actually), but the above file indicates they are not 'mapping' PB6/PB7. So I suspect if you use their functions to read pins, you won't get very far. Maybe write your own code for those pins. Or something.

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

OK so I had to find out. I downloaded the Arduino IDE and determined how it works. Rather curiously all Arduino "sketches", their name for programs, do not have a main() and do not apparently have any #includes. Instead there's always (at least) two routines provided by the user called setup() and loop(). You can guess what they do!

Now their usual method for writing pins is to call a library function:

digitalWrite(pin_number, state);

with state=HIGH or LOW. The pin_number goes by that mapping table in Curt's link above:

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
	_BV(0), /* 0, port D */
	_BV(1),
	_BV(2),
	_BV(3),
	_BV(4),
	_BV(5),
	_BV(6),
	_BV(7),
	_BV(0), /* 8, port B */
	_BV(1),
	_BV(2),
	_BV(3),
	_BV(4),
	_BV(5),
	_BV(0), /* 14, port C */
	_BV(1),
	_BV(2),
	_BV(3),
	_BV(4),
	_BV(5),
};

So '13' for pin_number would be PORTB.5. To read pins the library call

state = digitalRead(pin_numer);

is suggested. Thus a typical "sketch" looks like:

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH); // turn LED ON
  }
}

Because PB6 and PB7 don't have a 'pin_number' assigned you apparently cannot access them using DigitalRead() or DigitalWrite()

However this whole Arduino things is just a "smart set of clothes" for GCC. In fact when the above "sketch" is compiled it temporarily creates the .cpp file:

/*
 * Button
 * by DojoDave 
 *
 * Turns on and off a light emitting diode(LED) connected to digital
 * pin 13, when pressing a pushbutton attached to pin 7.
 *
 * http://www.arduino.cc/en/Tutorial/Button
 */

#include "WProgram.h"
void setup();
void loop();
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH); // turn LED ON
  }
}

int main(void)
{
        init();

        setup();

        for (;;)
                loop();

        return 0;
}

then compiles it like a normal C program (because it is now a "normal" C program).

So it should be possible to do any kind of "normal" C in a sketch file. Therefore if I write a sketch such as:

void setup()
{
  DDRB = 0;
  DDRC = 0xFF;
}

void loop()
{
  if (PINB & (1<<PB6)) {
    PORTC = 0xFF;
  }
  else {
    PORTC = 0x00;
  }
}

it temporarily creates:

#include "WProgram.h"
void setup();
void loop();
void setup()
{
  DDRB = 0;
  DDRC = 0xFF;
}

void loop()
{
  if (PINB & (1<<PB6)) {
    PORTC = 0xFF;
  }
  else {
    PORTC = 0x00;
  }
}

int main(void)
{
        init();

        setup();

        for (;;)
                loop();

        return 0;
}

which is compiled without error and creates a .hex and and a .elf which I think will work fine on the board to access bit PB6 as the generated .elf disassembles to be:

int main(void)
  bc:   0e 94 0c 01     call    0x218   ; 0x218 
#include "WProgram.h"
void setup();
void loop();
void setup()
{
  DDRB = 0;
  c0:   14 b8           out     0x04, r1        ; 4
  DDRC = 0xFF;
  c2:   8f ef           ldi     r24, 0xFF       ; 255
  c4:   87 b9           out     0x07, r24       ; 7
}

void loop()
{
  if (PINB & (1<<PB6)) {
  c6:   1e 9b           sbis    0x03, 6 ; 3
  c8:   02 c0           rjmp    .+4             ; 0xce 
    PORTC = 0xFF;
  ca:   88 b9           out     0x08, r24       ; 8
  cc:   fc cf           rjmp    .-8             ; 0xc6 
  }
  else {
    PORTC = 0x00;
  ce:   18 b8           out     0x08, r1        ; 8
  d0:   fa cf           rjmp    .-12            ; 0xc6 

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

So the wrap-up is that the Arduino IDE is tailored for the Arduino boards and should not be used for other boards? Just quirious..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Yes it specifically knows about:

 Directory of C:\arduino-0012\hardware\cores

18/09/2008  12:49              .
18/09/2008  12:49              ..
18/09/2008  12:49              arduino
18/09/2008  12:49              atmega8

though, actually, the "boards.txt" suggests that only "arduino" there is actually used - even for mega8:

##############################################################

atmega8.name=Arduino NG or older w/ ATmega8


atmega8.build.mcu=atmega8
atmega8.build.f_cpu=16000000L
atmega8.build.core=arduino

##############################################################

atmega168.name=Arduino NG or older w/ ATmega168


atmega168.build.mcu=atmega168
atmega168.build.f_cpu=16000000L
atmega168.build.core=arduino

##############################################################

mini.name=Arduino Mini


mini.build.mcu=atmega168
mini.build.f_cpu=16000000L
mini.build.core=arduino

##############################################################

bt.name=Arduino BT


bt.build.mcu=atmega168
bt.build.f_cpu=16000000L
bt.build.core=arduino

##############################################################

diecimila.name=Arduino Diecimila


diecimila.build.mcu=atmega168
diecimila.build.f_cpu=16000000L
diecimila.build.core=arduino

##############################################################

nano.name=Arduino Nano


nano.build.mcu=atmega168
nano.build.f_cpu=16000000L
nano.build.core=arduino

##############################################################

lilypad.name=LilyPad Arduino


lilypad.build.mcu=atmega168
lilypad.build.f_cpu=8000000L
lilypad.build.core=arduino

##############################################################

pro.name=Arduino Pro or Pro Mini (8 MHz)


pro.build.mcu=atmega168
pro.build.f_cpu=8000000L
pro.build.core=arduino

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

Hello Again,

The last time i tried again to use the PB6 and 7 and works normally (in arduino IDE), i don't know what happened, anyway thanks again..

If you want to see the code is here:

http://diydrones.com/profiles/blogs/705844:BlogPost:49751