Program checking PINs level, send it and port name via uart (str[] = {port_name, pin_number})
But when sending a string has length equal 3.
Why and atfter what changing str[2] -> str[3], if string length initialize like str[2] and in code not add values more then 2?
#include "globals.h"
#include "variables.h"
#include "ports.h"
#include "uart.h"
#include "pin_macros.h"
#include "setup.h"
extern volatile uint8_t pinNumber;
extern volatile uint8_t port_value;
extern char str[2];
extern char previouse_str[2];
extern uint8_t trigger_D;
void initialization ()
{
WDT_off();
PORTS_setup();
ACD_setup();
ADC_setup();
interruptsInit();
USART_Init(MYUBRR);
if (!ACTIVE(D10))
{
str[0] = PORT_NAME_B; //0x42
str[1] = (char) (BITNUM(D10) + 7);
USART_send_str(str); // *1
}
}
int main(void)
{
initialization();
while(1){}
}
ISR (PCINT2_vect)
{
_delay_ms(20);
uint8_t count = 0;
str[0] = PORT_NAME_D;
str[1] = (char) 0;
port_value = ~(PIND >> 2);
if ((PIND >> 2) != ALL_PINS_HIGH)
{
pinNumber = 0;
if ((PIND >> 2) == ALL_PINS_HIGH)
{
count = 0;
} else {
count = 1;
} //end IF
port_value &= (~((1<<6) | (1<<7)));
while (port_value)
{
if (port_value != 1)
{
++count;
} //end IF
port_value = port_value >> 1;
} //end while
++count;
pinNumber = count;
} //end IF
str[1] = (char) pinNumber;
if (!((PIND >> 2) == ALL_PINS_HIGH))
{
if (str[1] != previouse_str[1])
{
if (trigger_D == 1)
{
//trigger_D = 0;
USART_send_str(str); //*2
} else {
trigger_D = 1;
} //end IF
} //end IF
} else {
trigger_D = 1;
str[1] = 0;
USART_send_bit(str[0]);
} //end IF
strcpy(previouse_str, str);
}
ISR (PCINT0_vect)
{
uint8_t count = 7;
str[0] = PORT_NAME_B;
str[1] = (char) 0;
_delay_ms(20);
port_value = ~PINB & (~((1<<6) | (1<<7)));
if ((PINB >> 2) != ALL_PINS_HIGH)
{
pinNumber = 0;
while (port_value)
{
if (port_value != 1)
{
++count;
} //end IF
port_value = port_value >> 1;
} //end while
++count;
pinNumber = count;
} //end IF
str[1] = (char) pinNumber;
if (!((PINB) == ALL_PINS_HIGH))
{
if (str[1] != previouse_str[1])
{
trigger_D = 0;
USART_send_str(str);
}
} else {
str[1] = 0;
USART_send_bit(str[0]);
} //end IF
strcpy(previouse_str, str);
}
- send -> 0x42 0x09 0x01, if variable trigger_D change to ‘0’ -> 0x42 0x09 and strlen(str) equal 2.
- in this send string for ping D2 equal “0x44 0x02 0x01”, if change trigger_D to 0 before sending, string will be have length equal 2.