problem with reading pin state using usart

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

i have written a program that toggles PB0 and PB1 by sending '1' and '2' and reads PIN states by sending '3'.

my problem is that PB1 state is not read correctly.

what is the reason?

here is the code:

#include <avr/io.h>
#include <avr/interrupt.h>





void usart_init();
volatile uint8_t state0,state1;


int main(void)
{
    usart_init();
	DDRB=(1<<DDB0)|(1<<DDB1);
	sei();
    while (1);
}

ISR (USART_RXC_vect)
{

	uint8_t data=UDR;
	
	if (data=='1') 
		PORTB^=(1<<PB0);
		if(bit_is_clear(PINB,PB0))
		state0=0;
		else state0=1;			
					
	if (data=='2') {
		PORTB^=(1<<PB1);
		if(bit_is_clear(PINB,PB1))
		state1=0;
		else state1=1;
	}
	if (data=='3') {
		while( !( UCSRA & (1<<UDRE)) );
		UDR=state0;
		while( !( UCSRA & (1<<UDRE)) );
		UDR=state1;
			}
		
}

void usart_init(){
	UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);//enable recieve & interrupt
	UCSRC=(1<<UCSZ0)|(1<<UCSZ1);//8N1
	UBRRH=0;
	UBRRL=12;//BAUD 38400
}

 

This topic has a solution.
Last Edited: Fri. Nov 3, 2017 - 07:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't have brackets in your first if?
.
MG

I don't know why I'm still doing this hobby

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

Braces, spaces and comments are your friends, use them:

    if (data == '1') {
        PORTB ^=  (1 << PB0);           // Toggle PB0
        if(bit_is_clear(PINB, PB0)) {
            state0 = 0;    // Do you want 0x00 or '0'??
	} else {
            state0 = 1;    // Do you want 0x01 or '1'?? 
        }
    }

Are you wanting to send a human readable (ASCII) code below?

    if (data == '3') {
        while( !( UCSRA & (1 << UDRE)) );
        UDR = state0;  // Send 0x00 or 0x01 to UART???
        while( !( UCSRA & (1 << UDRE)) );
        UDR = state1;  // Send 0x00 or 0x01 to UART???
    }

Edit: typo

     0 does not equal '0'

David (aka frog_jr)

Last Edited: Fri. Nov 3, 2017 - 03:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I edited the peogram

now both PB0 and PB1 are read incorrectly.

when PINS are high UART says they are Low and vice versa

#include <avr/io.h>
#include <avr/interrupt.h>





void usart_init();

volatile uint8_t state0,state1;


int main(void)
{
    usart_init();
	DDRB=(1<<DDB0)|(1<<DDB1);	
	sei();
    while (1);
}

ISR (USART_RXC_vect)
{

	uint8_t data=UDR;
	
	if (data=='1') {
		PORTB^=(1<<PB0);
		if(bit_is_clear(PINB,PB0))
		state0=0x00;
		else state0=0x01;			
	}
	if (data=='2') {
		PORTB^=(1<<PB1);
		if(bit_is_clear(PINB,PB1))
		state1=0x00;
		else state1=0x01;
	}
	if (data=='3') {
		while( !( UCSRA & (1<<UDRE)) );
		UDR=state0;
		while( !( UCSRA & (1<<UDRE)) );
		UDR=state1;
			}
		
}

void usart_init(){
	UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);//enable recieve & interrupt
	UCSRC=(1<<UCSZ0)|(1<<UCSZ1);//8N1
	UBRRH=0;
	UBRRL=12;//BAUD 38400
}

 

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

frog_jr wrote:

 

Are you wanting to send a human readable (ASCII) code below?

    if (data == '3') {
        while( !( UCSRA & (1 << UDRE)) );
        UDR = state0;  // Send 0x00 or 0x01 to UART???
        while( !( UCSRA & (1 << UDRE)) );
        UDR = state1;  // Send 0x00 or 0x01 to UART???
    }

Edit: typo

     0 does not equal '0'

 

yes

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

Are you sure "the pins are high"? Or is this actually about switches being pressed or not?

If this is a starter kit board or similar then it is not unlikely that switches are wired so that when pressed the pin is low (pulled down) and when not pressed ithe pin is high (pulled up).

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

my circuit is on breadboard and two LED's are connected to indicate PIN state

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

It takes a clock time for the port state to be updated in the pin register. You need a nop(); after the port toggle

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

thank you so much

problem solved!