Writing a AVR Software UART

Hello Guys,

Myself and a friend have been engaged in a personal project where we are utilising a ATmega32a to eventually control 12 hobby servo motors in four legs, each with three joints. The ATmega32a is installed on a board a friend of mine designed called a Penguino, which quite nicely fits on a prototyping bread board. To this end the hardware UART is occupied with the computer interface and so we have to write our own software based UART to communicate through the DIO pins.

We thought it would be fun to write our own UART rather than use a supplied one but allas we have gotten quite stuck. I was wondering if you could help us. Bellow is a simplified version of our code which (also) does not work. The version bellow just uses the ISR to output a changing signal. When we measure the signal sent through the pin with an oscilloscope all we get is a signal high.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <avr/io.h>
#include <avr/interrupt.h>

#define OUTPUT_PIN (1<<7)

void initGeneral(void)
{
    // Set Port A pin as output
    DDRA |= OUTPUT_PIN;
    
    // Set Port A pin output to 0
    PORTA &= ~OUTPUT_PIN;
}

void initInterruptTimer0Comp(void)
{
    // Set the value to count to for the timer
    OCR0 = 25;

    // Set prescalar to 64, and clear timer on compare
    TCCR0 = ((1<<CS00)|(1<<CS01)|(0<<CS02)) | (0<<WGM00)|(1<<WGM01);

    // Clear pending interrupts
    TIFR |= 1<<OCF0;

    // Set enable Output Compare Interrupt for Timer 0
    TIMSK |= 1<<OCIE0;

    // Enable all interrupts (AVR gcc specific)
    sei();
}

ISR(TIMER0_COMP_vect)
{
    static int sendBit = 7;
	char sendByte = 0xAA;

    if (sendBit < 0);
	{
		sendBit = 7;
	}

	char signal = sendByte & (1 << sendBit);

	if(signal == 0)
    {
        PORTA &= ~OUTPUT_PIN;
    }
    else
    {
        PORTA |= OUTPUT_PIN;
    }

	sendBit--;
	

}

int main(void)
{
    initGeneral();
    initInterruptTimer0Comp();

    while(1);

    return 0;
}


However we know that there is nothing wrong with the interrupt calling the ISR because if we replace the ISR with the code bellow we get an alternating signal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ISR(TIMER0_COMP_vect)
{
static int x=0;

    if (x == 0)
    {
        x=1;
        PORTA &= ~OUTPUT_PIN;
    }
    else
    {
        x=0;
        PORTA |= OUTPUT_PIN;
    }
}


Do you guys have any thoughts?

Thanks a heap for your time. My name is David and my collaborators name is Davids. So, thanks form Dave and Dave
Topic archived. No new replies allowed.