Using AVR GPIO Pins to send and receive data

Discussion in 'Embedded Systems and Microcontrollers' started by nudmat, May 27, 2014.

  1. nudmat

    Thread Starter New Member

    May 27, 2014
    Please i need your support on how to put these codes together.I am using Attiny24A to read Temperature from a temperature sensor and i want to send these Temperature value to the Computer through Attiny4313.I have two GPIO Pins from Attiny24A connected to two GPIO Pins of Attiny4313.
    My question is that is it possiple to use These GPIO Pins to send and receive data.The program below was written to Attiny24A
    Code ( (Unknown Language)):
    2.  DDRB|= (1<<PORTB1); //Attiny24A(PB1) as Output Pin(to send DATA)
    3. DDRA|= (1<<PORTA7); //Attiny24A(PA7)as Output Pin(for clocking the DATA)
    4. void ClearClock()
    5. {
    6.  PORTA &= ~(1 << PA7);  // Set PB1 LOW
    7.  }
    8.  void SetClock()
    9. {
    10.  PORTA |= (1 << PA7);   // Set PB1 HIGH
    11.  }
    12.  void ToggleClock()
    13. {
    14.  _delay_us(50);
    15.  SetClock();
    16.  _delay_us(50);
    17.  ClearClock();
    18.  clockcount++;
    19. }
    20.  void SetData()
    21. {
    22.  PORTB |= (1 << PB1);   // Set PB1 HIGH
    23. }
    24.  void ClearData()
    25. {
    26.  PORTB &= ~(1 << PB1);  // Set PB1 LOW
    27. }
    28.  void Send_ObjTemp_8bit(unsigned int data10)
    29.  {
    30.  int i=7;
    31.  do
    32.  {
    33.   if (((data10 & (1<<i))>>i) == 1)
    34.   SetData();
    35.   else
    36.   ClearData();
    37.   ToggleClock();
    38.   i=i-1;
    39.  }
    40.  while (i >= 0);
    41. }
    42. int  main( void )
    43. {
    45.  DDRB|= (1<<PORTB1); //µC Data_Out
    46.  DDRA|= (1<<PORTA7); //µC Clock_In
    48.       sei();
    49.       setup();
    53.   while(1)
    54.             {
    56.   Send_ObjTemp_8bit(Tobj);
    59.             }
    60. }              
    The program below was written to Attiny4313
    Code ( (Unknown Language)):
    2. DDRB &= ~(1 << PB1);//Attiny4313(PB1)as Input Pin(to receive the temperature value from attiny24A)
    3. int  main( void )
    4. {
    5.   unsigned int TempValue;  
    7.   while(1)
    8.             {
    9.              TempValue=PINB1;
    11.       USART_Transmit( TempValue) //this function sends the temperature value to the Computer.
    14.            }
    15. }              
  2. MrChips


    Oct 2, 2009
    Yes, it is possible.

    You do not need two I/O pins. One pin will work.
    You can create a software UART called "bit banging" using NRZI format.
    For improved reliability, you can use phase encoding.

    Did you write the two programs shown or are you copying someone else's code?

    The first program for the ATtiny24A appears to be part of a functioning software SPI.
    As written it is incomplete.

    The second program written for the ATtiny4313 is a half-hearted hack at the problem.

    You need to learn to do some simple programming tasks first and gain more experience in code structure and programming algorithms.

    Edit: Why do you need the ATtiny4313 anyway?
    Last edited: May 27, 2014
  3. nudmat

    Thread Starter New Member

    May 27, 2014
    Thank you very much for the response to my question.i really appreicate your Kind gesture.As you rightly observed the code in the Attiny24A is not complete,I only posted the few lines that are relevant to this question.The program is not entirely mine,part of the codes are taken from other sources.
    Well with respect to using Attiny4313.Like i said earlier, the temperature value from Attiny24A needs to be displayed on a Computer Monitor.So Attiny4313 is the µC i am using to send the temperature value to the PC.Besides this,the two microcontrollers are also on different boards because they have some other tasks they execute .
    Sir,i would be glad if you could help me shed more light on how the data sent by Attiny24A can be stored by Attiny4313.For example,if the value of data sent by Attiny24A is 20,i know that this value(20)will be transmitted as strings of ones and Zeros(e.g00010100 ).My area of confusion is how to use PINB1(i configured this pin as input) to receive these string of ones and zeros and then copy the actual value into another variable for onward transmission to the PC.Something like this:

    TempValue=PINB1;//i am not sure if this line will copy all the 8-bit or just 1-bit into the variable TemValue.
    I will highly apppreciate whatever Support you can render in this regards.
    Once again thank you for the Support.
    Best regards.
  4. MrChips


    Oct 2, 2009
    Your value of 20 in the ATtiny24A can be transmitted in 101 different ways. It is up to you to choose the protocol that best suits your needs.

    Question #1. Why not send the data from the ATtiny24A directly to the PC?

    The program you have shown for the ATtiny24A uses the SPI mechanism.

    Your task on the ATtiny4313 is to reverse the SPI process using two pins on the ATtiny4313.