Using AVR GPIO Pins to send and receive data

Thread Starter

nudmat

Joined May 27, 2014
4
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
Rich (BB code):
 DDRB|= (1<<PORTB1); //Attiny24A(PB1) as Output Pin(to send DATA)
DDRA|= (1<<PORTA7); //Attiny24A(PA7)as Output Pin(for clocking the DATA)
void ClearClock()
{
 PORTA &= ~(1 << PA7);  // Set PB1 LOW
 }
 void SetClock()
{
 PORTA |= (1 << PA7);   // Set PB1 HIGH
 }
 void ToggleClock()
{
 _delay_us(50);
 SetClock();
 _delay_us(50);
 ClearClock();
 clockcount++;
}
 void SetData()
{
 PORTB |= (1 << PB1);   // Set PB1 HIGH
}
 void ClearData()
{
 PORTB &= ~(1 << PB1);  // Set PB1 LOW
}
 void Send_ObjTemp_8bit(unsigned int data10)
 {
 int i=7;
 do
 {
  if (((data10 & (1<<i))>>i) == 1)
  SetData();
  else
  ClearData();
  ToggleClock();
  i=i-1;
 }
 while (i >= 0);
}
int  main( void )
{
 
 DDRB|= (1<<PORTB1); //µC Data_Out
 DDRA|= (1<<PORTA7); //µC Clock_In
   
      sei();
      setup();
    
  
  
  while(1)
            {
    
  Send_ObjTemp_8bit(Tobj);
      
   
            } 
}
---------------------------------------------------
The program below was written to Attiny4313
Rich (BB code):
DDRB &= ~(1 << PB1);//Attiny4313(PB1)as Input Pin(to receive the temperature value from attiny24A)
int  main( void )
{
  unsigned int TempValue;   
  
  while(1)
            {
             TempValue=PINB1;
    
      USART_Transmit( TempValue) //this function sends the temperature value to the Computer. 
         
   
           } 
}
 

MrChips

Joined Oct 2, 2009
30,720
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:

Thread Starter

nudmat

Joined May 27, 2014
4
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.
 

MrChips

Joined Oct 2, 2009
30,720
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.
 
Top