Port C.5 as an output pin.

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

Hello Guys. I have a new problem. When i used Mega328p to my experience . my VCCis 5 v.  Used Port C.5 as an output pin. and the output voltage from Port c .5 is in between 2 v. my question is is there anything wrong wit my ics or my seting ? i have a very simple circuit which is a switch to  pd.2 and output to pc.5 .

How can i manage too have a output level above 2 v . It will be nice to have 5 v as output voltage.

many thanks 

Last Edited: Fri. Jun 2, 2017 - 10:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you make the pin an output?  What is connected to the pin?

Is AVcc and Agnd connected and has bypass capacitor?

Show your test program.

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

tommynewman wrote:
I have a new problem.

Then start a new thread.

 

New question ==> New Thread.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When i used Mega328p

Are you STILL USING a Mega328 or something else like a Mega32 etc.?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Maybe you need to disable JTAG.

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

Of course not if he is still using a Mega328 but it's not clear at this stage.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Perhaps this was split from another Thread?

 

Need to see both a schematic of your circuit, and the program you are running.

 

JC

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

please see the code and schematics .  

 

// DigitalInputs.c

// switch on PD2 (pin 4)
// LED on PC5 (pin 28), turns on/off based on switch press

#ifndef F_CPU					// if F_CPU was not defined in Project -> Properties
#define F_CPU 1000000UL			// define it now as 1 MHz unsigned long
#endif

#include <avr/io.h>				// this is always included in AVR programs

#define BIT_IS_SET(byte, bit) (byte & (1 << bit))			// these macros make checking if bits are set or clear easier and more readable
#define BIT_IS_CLEAR(byte, bit) (!(byte & (1 << bit)))

///////////////////////////////////////////////////////////////////////////////////////////////////
int main(void) {
	
	DDRD &= ~(1 << PD2);		// clear DDRD bit 2, sets PD2 (pin 4) for input
	
	PORTD |= (1 << PD2);		// set PORTD bit 2, sets PD2 (pin 4) internal pull-up resistor
	
	DDRC |= (1 << PC5);			// set DDRC bit 5, sets PC5 (pin 28) for output
	
	while (1) {					// begin infinite loop
		if(BIT_IS_CLEAR(PIND, PD2)) {			// check if PIND bit 2 is clear, which indicates switch is pressed (logic low)
			PORTC |= (1 << PC5);					// set PORTC bit 5 to set PC5 (pin 28) high
		} else if(BIT_IS_SET(PIND, PD2)) {		// check if PIND bit 2 is set, which indicates switch is not pressed (logic low)
			PORTC &= ~(1 << PC5);					// clear PORTC, bit 5 to set PC5 (pin 28) low
		} else {
			// ideally should never get here, but may occasionally due to timing
		}
	}
	return(0);					// should never get here, this is to prevent a compiler warning
}

Attachment(s): 

Last Edited: Sat. Jun 3, 2017 - 04:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where are you measuring this voltage?  Right on the AVR pin?  Or later in your circuit?

 

What voltage do you get when you disconnect your LED circuit?

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

right on the avr at 4.9. when put an LED downs to 2 

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

Is there a current limiting resistor between the LED and the AVR?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

470 ohms 

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

Where did you measure the 2V? Across the LED or at the MCU pin? 2V across the LED is quite normal. What do measure at the MCU pin?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. Jun 3, 2017 - 05:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You say that the ISP header is looking from the BACK of the connector, that's not right.

 

The header should look from the same side (TOP) of the connector where the programmer is plugged in.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Hello Guys, I have new trouble of Input from Mega328p. If it is a switch input. The mega328p is acting as a voltage/current source, the switch is connecting one end to GND and the other end to PIND.2 of mega328. 

What about if there is input signal of +5 v goes into PIND.2 ?  

same code below?

DDRD &= ~(1 << PD2);		// clear DDRD bit 2, sets PD2 (pin 4) for input
	
PORTD |= (1 << PD2);		// set PORTD bit 2, sets PD2 (pin 4) internal pull-up resistor

i had tried it.  the mega 328 didn't picked this input signal.  would the Miro controller better to be  current sink rather than current source? What can i do if i want to mega328p picked up input of 5 V fromm other device ? 

thx

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

Silly question but where's the code reading the input? A lot of people make the mistake of reading PORTD rather than PIND for example.

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

Hello Clawson, 

 

DDRD &= ~(1 << PD2);		// clear DDRD bit 2, sets PD2 (pin 4) for input
	
PORTD |= (1 << PD2);		// set PORTD bit 2, sets PD2 (pin 4) internal pull-up resistor

 

if(BIT_IS_CLEAR(PIND, PD2))

{

PORTC |= (1 << PC5);

}

 

here are codes. But when i connected 5v signal to PD.2 . PC.5 does not light on the led.

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

Can anyone help with this ? i want to read a signal from pd.2 of mega328p. circuit is in the attachment. this signal is 5v. infect this is a signal from other device. once i received this 5 volage signal . my led in PC.5 is on.

my old program was not working for my propose. I think it was my DDRX and PORTX program.  i need to have a logic high as input signal.

i have my old code listed below: 

 

// DigitalInputs.c

// 5v signal on PD2 (pin 4)
// LED on PC5 (pin 28), turns on/off based on 5v signal received or not 

#ifndef F_CPU					// if F_CPU was not defined in Project -> Properties
#define F_CPU 1000000UL			// define it now as 1 MHz unsigned long
#endif

#include <avr/io.h>				// this is always included in AVR programs

#define BIT_IS_SET(byte, bit) (byte & (1 << bit))			// these macros make checking if bits are set or clear easier and more readable
#define BIT_IS_CLEAR(byte, bit) (!(byte & (1 << bit)))

///////////////////////////////////////////////////////////////////////////////////////////////////
int main(void) {
	
	DDRD &= ~(1 << PD2);		// clear DDRD bit 2, sets PD2 (pin 4) for input
	
	PORTD |= (1 << PD2);		// set PORTD bit 2, sets PD2 (pin 4) internal pull-up resistor
	
	DDRC |= (1 << PC5);			// set DDRC bit 5, sets PC5 (pin 28) for output
	
	while (1) {					// begin infinite loop
		if(BIT_IS_CLEAR(PIND, PD2)) {			
			PORTC |= (1 << PC5);					// set PORTC bit 5 to set PC5 (pin 28) high
		} else if(BIT_IS_SET(PIND, PD2)) {		
			PORTC &= ~(1 << PC5);					// clear PORTC, bit 5 to set PC5 (pin 28) low
		} else {
			// ideally should never get here, but may occasionally due to timing
		}
	}
	return(0);					// should never get here, this is to prevent a compiler warning
}

Attachment(s): 

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

I'm still waiting to find out why the board could not be programmed in the original thread.

 

And why is PD2 wired up to +5V now??

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

tommynewman wrote:

 

if(BIT_IS_CLEAR(PIND, PD2))

{

PORTC |= (1 << PC5);

}

 

here are codes. But when i connected 5v signal to PD.2 . PC.5 does not light on the led.

 

What do you expect?

Your code say to turn 5V on pin PC5 when pin PD2 is clear... not set... you have it backwards.

Wagner Lipnharski
Orlando Florida USA

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

js wrote:

You say that the ISP header is looking from the BACK of the connector, that's not right.

 

The header should look from the same side (TOP) of the connector where the programmer is plugged in.

 

John, I think he said "Back" of the cable connector as not being the "Face" of the cable connector coming from the programmer, to be plugged into the board.

The side of the connector where the wires coming out, could be called "back", isn't it ?

 

Back of the cable connector is the same as the Face of the receiving male on the board.

It is a strange connotation, yes, not to be used since it creates confusion.

The biggest confusion here, is that he saying "back" to the connector wired to the AVR... that's wrong, that should be "top" or "face".

 

I rather use to represent the connector as seen from the top, plugged on the PCI... what is the same as saying "back" of the cable connector.

Wagner Lipnharski
Orlando Florida USA

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

tommynewman wrote:

 

// DigitalInputs.c


// 5v signal on PD2 (pin 4)
// LED on PC5 (pin 28), turns on/off based on 5v signal received or not 

#ifndef F_CPU					// if F_CPU was not defined in Project -> Properties
#define F_CPU 1000000UL			// define it now as 1 MHz unsigned long
#endif

#include <avr/io.h>				// this is always included in AVR programs

#define BIT_IS_SET(byte, bit) (byte & (1 << bit))			// these macros make checking if bits are set or clear easier and more readable
#define BIT_IS_CLEAR(byte, bit) (!(byte & (1 << bit)))

///////////////////////////////////////////////////////////////////////////////////////////////////
int main(void) {
	
	DDRD &= ~(1 << PD2);		// clear DDRD bit 2, sets PD2 (pin 4) for input
	
	PORTD |= (1 << PD2);		// set PORTD bit 2, sets PD2 (pin 4) internal pull-up resistor
	
	DDRC |= (1 << PC5);			// set DDRC bit 5, sets PC5 (pin 28) for output
	
	while (1) {					// begin infinite loop
		if(BIT_IS_CLEAR(PIND, PD2)) {			
			PORTC |= (1 << PC5);					// set PORTC bit 5 to set PC5 (pin 28) high
		} else if(BIT_IS_SET(PIND, PD2)) {		
			PORTC &= ~(1 << PC5);					// clear PORTC, bit 5 to set PC5 (pin 28) low
		} else {
			// ideally should never get here, but may occasionally due to timing
		}
	}
	return(0);					// should never get here, this is to prevent a compiler warning
}

 

Nothing wrong with that, but for the brain's sake, you could also learn how to program in Assembly... would be only ;

 

    CBI  DDRD,2        ; PORTD2 = input

    SBI  PORTD,2       ; PORTD2 = pullup active

    SBI  DDRC,5        ; PORTC5 = output

 

    SBIS PIND,2        ; Skip next if PIND2 is Set

    CBI  PORTC,5       ; Here if PIND2 is clear, drop PORTC5

    SBIC PIND,2        ; Skip next if PIND2 is Clear

    SBI  PORTC,5       ; Here if PIND2 is set, raise PORTC5

 

    RJMP PC-4          ; Make a loop

Wagner Lipnharski
Orlando Florida USA

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

i mean i want the PD.2 as an input. i can handle s switch as a input. that was for switch which one end of switch goes to GND and other end connect to PD.2 . in this case the micro controller as a current source.

i wan to learn how to handle sing which is 5 v or similar from PD.2. in this case i think the Mcu is curent sink.

 

May i please ask how am i able to d that ?

 

 

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

tommynewman wrote:

i mean i want the PD.2 as an input. i can handle s switch as a input. that was for switch which one end of switch goes to GND and other end connect to PD.2 . in this case the micro controller as a current source.

i wan to learn how to handle sing which is 5 v or similar from PD.2. in this case i think the Mcu is curent sink.

 

May i please ask how am i able to d that ?

 

 

 

When the port pin (PORTD,2) is setup as input (DDRD,2=0), and you have a switch connecting PD2 to ground or not, you need something to make the port pin to go up when the switch is open, this is why you use the PD2 pullup = 1 ( DDRD,2=0 + PORTD,2=1).  So, when the PD2 is floating, it has the pullup internal resistor feeding 5V to the pin.

 

You could do the othe way around, connected the other end of the switch to +5V, but then you would need an external resistor to ground, because AVR has no input pulldown internal resistor.

Wagner Lipnharski
Orlando Florida USA

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

i wan to learn how to handle sing which is 5 v or similar from PD.2. in this case i think the Mcu is curent sink.

Why on earth do you mix those sink and source with PD2?

PD2 is an input.

Input does neither sink nor source any current.

It only indicates which level (high or low) is connected to it. 

Imagine input as an isolated MOSFET gate.

 

Last Edited: Mon. Jun 5, 2017 - 06:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you wagnerlip your ideal works perfect with switch connect to 5v and pulldown resistor in20k.

What about this input signal is not from a swith ? it's from a pulse signal of yop at 5v button at 0v (motor rotary encoder) ? is the pulldown works ?

 

I had tried it. It didn't works with 20k pulldown resistors. I have an anypin  interrupt function in PD.2  input quadrature is in the attachment  

 

code below:

 

// Any_Pin_Interrupt.c

// switch on PD2/PCINT18 (pin 4)
// LED on PC4 (pin 27), cycles via delay in main()
// LED on PC5 (pin 28), responds to button press

#ifndef F_CPU					// if F_CPU was not defined in Project -> Properties
#define F_CPU 1000000UL			// define it now as 1 MHz unsigned long
#endif

#include <avr/io.h>				// this is always included in AVR programs
#include <util/delay.h>			// add this to use the delay function
#include <avr/interrupt.h>

#define BIT_IS_SET(byte, bit) (byte & (1 << bit))
#define BIT_IS_CLEAR(byte, bit) (!(byte & (1 << bit)))

///////////////////////////////////////////////////////////////////////////////////////////////////
int main(void) {

	DDRD &= ~(1 << PD2);		// clear DDRD bit 2, sets PD2 (pin 4) for input

	DDRC |= (1 << PC5) | (1 << PC4);			// set PC5 (pin 28) and PC4 (pin 27) for output

	/*
	PCICR - Pin Change Interrupt Control Register

	bit         7       6       5       4       3         2          1        0
	name        -       -       -       -       -       PCIE2      PCIE1    PCIE0
	set to      0       0       0       0       0         1          0        0

	bit 7 = 0
	bit 6 = 0
	bit 5 = 0
	bit 4 = 0
	bit 3 = 0
	PCIE2 = 1     enable pin change interrupt on pins PCINT23 through PCINT16
	PCIE1 = 0     don't enable pin change interrupt on pins PCINT14 through PCINT8
	PCIE0 = 0     don't enable pin change interrupt on pins PCINT7 through PCINT0
	*/
	PCICR = 0b00000100;

	/*
	PCMSK2 - Pin Change Mask Register 2

	bit           7           6           5           4          3            2            1           0
	name       PCINT23     PCINT22     PCINT21     PCINT20     PCINT19     PCINT18      PCINT17     PCINT16
	set to        0           0           0           0           0           1            0           0

	PCINT23 = 0   don't enable PCINT23 pin change interrupt
	PCINT22 = 0   don't enable PCINT22 pin change interrupt
	PCINT21 = 0   don't enable PCINT21 pin change interrupt
	PCINT20 = 0   don't enable PCINT20 pin change interrupt
	PCINT19 = 0   don't enable PCINT19 pin change interrupt
	PCINT18 = 1   enable PCINT18 pin change interrupt
	PCINT17 = 0   don't enable PCINT17 pin change interrupt
	PCINT16 = 0   don't enable PCINT16 pin change interrupt
	*/
	PCMSK2 = 0b00000100;

	sei();				// enable interrupts

	while (1) {					// begin infinite loop
		PORTC ^= (1 << PC4);	// toggle LED on PC4
		_delay_ms(1500);		// delay 1.5 sec
	}
	return(0);					// should never get here, this is to prevent a compiler warning
}

///////////////////////////////////////////////////////////////////////////////////////////////////
ISR(PCINT2_vect) {
	if(BIT_IS_CLEAR(PIND, PD2)) {
		PORTC |= (1 << PC5);				// turn on PC5 LED
	} else if(BIT_IS_SET(PIND, PD2)) {
		PORTC &= ~(1 << PC5);				// turn off PC5 LED
	} else {
		// ideally should never get here, but may occasionally due to timing
	}
}

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
		// ideally should never get here, but may occasionally due to timing

Then don't read PIND twice! The bit is either 0 or it is 1 if you just read once:

ISR(PCINT2_vect) {
	if(PIND & (1 << PD2)) {
		PORTC |= (1 << PC5);				// turn on PC5 LED
	} else {
		PORTC &= ~(1 << PC5);				// turn off PC5 LED
	}
}

 

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

It's Schrödinger's bit.  It's both 0 and 1 until you look at it.

 

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

steve17 wrote:
It's Schrödinger's bit.
ROFL!yes

David (aka frog_jr)