Newbie question - Trouble reading data from pin on ATtiny817 XPro?

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

Hi all,

 

I apologize if my post violates any rules, as I am brand new to this forum.

 

I'm trying to read digital data from a pin on my ATtiny817 XPro, but everything I've tried doesn't work. I want to essentially turn on an LED when a button is pressed, and I've scoured the internet/datasheet for answers. I found this post which is the closest I've come to an answer, however DDRx and PIN commands do not work when trying to run the script. I've tried using PORTx.DIR, PORTx.IN, and PORTx.OUT commands as detailed in the datasheet, but I still can't get anything to work...

 

Could anyone help me with this? Sample code would be huge, as I've spent so much time trying to figure this out and I'm trying very hard not to just give up at this point. I'm using Atmel Studio 7.0, and I'm writing the program in C. Here's what I have:

 

#define F_CPU 16000000
#include <avr/io.h>

int main(void)
{
	//PB6 = BUTTON INPUT --> THIS IS THE PIN THAT I WANT TO READ
	//PC0 = blue RGB LED lead
	//PC1 = green RGB LED lead
	//PC2 = red RGB LED lead

	PORTB.DIR = 0x00;			// designates all pins in PORT B as input pins
	PORTC.DIR = 0xFF;			// designates all pins in PORT C as output pins

	while(1){
		if(PORTB.IN & 0b01000000 == 0b01000000){    // IS THIS LINE CORRECT?
			PORTC.OUT = 0b00000010;		    // puts voltage on GREEN pin of RGB LED when BUTTON IS PRESSED
		}
		else{
                        PORTC.OUT = 0b00000100;             // puts voltage on RED pin of RGB LED when BUTTON IS *NOT* PRESSED
                }
        }

	return 0;
}

 

This topic has a solution.
Last Edited: Fri. Jun 14, 2019 - 11:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 I'm trying very hard not to just give up at this point.

If you give up can I have your board???

 

Just joking, hang in there.

 

Please state which language and compiler you are using, and post your latest effort.

To post code use the " <> " icon above, and paste your code into the window that pops up.

 

I suspect you are likely using C, which I can't help you with, but which many others can.

 

Welcome to the Forum.

 

JC 

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

Will do, unfortunately I am using C.

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

Relax. So, do you have the board schematic and know which pin the button and the LED are connected to?

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

I just updated my question to include the code I have so far. Button is hooked up to PB6 and since LED is an RGB LED, I have red on PC2, green on PC1, and blue on PC0. The wire connecting the button to PB6 is located between the (-) terminal of the button and the (+) terminal of the subsequent resistor. I suspect my current circuit orientation may be the culprit now that I think about it...

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

PlowDaddyMilk wrote:

	if(PORTB.IN & 0b01000000 == 0b01000000){    // IS THIS LINE CORRECT?

 

You should check operator precedence. I would use:

 

	if((PORTB.IN & 0b01000000) == 0b01000000){

or just 

	if(PORTB.IN & 0b01000000){

 

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

Wow that was literally the problem, you are a godsend. I wish I could buy you a beer, thank you so much!

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

I should stay out of this...

But in Basic one Writes to a Port and reads from a PIN

 

JC

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

PlowDaddyMilk wrote:

Wow that was literally the problem, you are a godsend. I wish I could buy you a beer, thank you so much!

 

Don't tell anyone, but I don't really like beer. Cider would do smiley

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

I agree with this! I worked with the ATmega328P last year to program a stepper motor for a lab, and the C script I wrote used PORT for 'writiing' and PIN for 'reading', as well as DDRx instead of PORTx.DIR for data directions. That seemed way more intuitive, and I'm really curious as to why that syntax doesn't work on my ATtiny817...It makes no sense to me why all Atmel chips can't utilize the same syntax.

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

The 'PORT' on the tiny817 describes a peripheral (that is a named chapter in the datasheet), not a register. The PORT contains registers which can be written to.. So you have to name the register in the PORT peripheral you want to write to. It actually makes more sense the way its done on the newer 0/1 series, and is much easier to use for various reasons.

 

Read the PORT chapter in the datasheet, and read the iotn817.h header to see how they provide access to these peripherals/registers. I do my own thing, so I can end up doing something which is a little nicer to use (imo), but is initially more work-

 

#include "Port.hpp"
int main(){    

    PB6_INLPU_t sw1; //input, low is on, pullup on
    PC1_OUTH_t ledG; //output, high is on
    PC2_OUTH_t ledR; //output, high is on

    for(;;){
        ledG.on( sw1.ison() );

        ledR.on( not sw1.ison() );
    }

}