I've been trying hard on this. I have two avr AT-Tiny45 ,these lack hardware Uart so I'm designing my own using Timers and their interrupt to switch a GPIO pin to H/L state as required by the transmission format and protocol.
So far I have made these two test programs, Using async mode with 1 stop bit and no parity
Transmitter code
Receiver code
But for some reason it doesn't work , the if(receivedByte==0x0A) section never executes!
Can you help correct it?
So far I have made these two test programs, Using async mode with 1 stop bit and no parity
Transmitter code
Rich (BB code):
/*
* tiny45Tx.c
*
*/
#define F_CPU 1000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
unsigned short byteToSend,bit;
unsigned int timerCount=0;
short state=0,idle=0x00,transmitting=0xff,bitCount=0;
void setTimer(int on){
if(on){
TCNT0=0;
TIMSK|=(1<<OCIE0A);
TCCR0A|=(1<<WGM01);
OCR0A=25;
TCCR0B|=(1<<CS01)|(1<<CS00);
}
else{
TIMSK&=~(1<<OCIE0A);
TCCR0B&=~((1<<CS01)|(1<<CS00));
}
}
void setupIO(){
DDRB=(1<<PB4)|(1<<PB0);
PORTB|=(1<<PB3);
state=idle;
PORTB|=(1<<PB4)|(1<<PB0);
_delay_ms(500);
PORTB&=~((1<<PB4)|(1<<PB0));
_delay_ms(500);
}
void sendByte(unsigned short dataByte)
{
if(state!=idle)
return;
else
{
bitCount=-1;
state=transmitting;
byteToSend=dataByte;
}
}
int main(void)
{
sei();
setupIO();
setTimer(1);
while(1)
{
sendByte(0x0A);
}
}
ISR(TIMER0_COMPA_vect){
if(state==idle)
bit=1;
else{
if(bitCount<8)
{if(bitCount==-1)
bit=0;
else
bit=(byteToSend<<bitCount)&0x80;
}
else{
bit=1;
state=idle;
}
bitCount++;
}
if(bit){
PORTB|=(1<<PB4); //PB4 being used as Uart pin
}
else{
PORTB&=~(1<<PB4);
}
}
Rich (BB code):
/*
* tiny45Rx.c
*
*/
#define F_CPU 1000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
unsigned short byteToSend,bit;
unsigned int timerCount=0;
short state=0,idle=0x00,transmitting=0xff,bitCount=0;
unsigned short receivedByte;
unsigned int byte;
void setTimer(int on){
if(on){
TCNT0=0;
TIMSK|=(1<<OCIE0A);
TCCR0A|=(1<<WGM01);
OCR0A=25;
TCCR0B|=(1<<CS01)|(1<<CS00);
}
else{
TIMSK&=~(1<<OCIE0A);
TCCR0B&=~((1<<CS01)|(1<<CS00));
}
}
void setupIO(){
DDRB=(1<<PB4);
PORTB|=(1<<PB4);
_delay_ms(500);
PORTB&=~(1<<PB4);
_delay_ms(500);
}
int main(void)
{
sei();
setupIO();
setTimer(1);
while(1)
{
if(receivedByte==0x0A)
PORTB|=(1<<PB4);
else
PORTB&=~(1<<PB4);
}
}
ISR(TIMER0_COMPA_vect){
byte=byte<<1;
if(~PINB&(1<<PB2))
byte|=1;
if((byte&(0b10<<10))&&(byte&1))
receivedByte=0x0A;
else
receivedByte=0x00;
}
Can you help correct it?