Re: [AVR-Chat] SERIAL PORT IN ATTINY
2004-11-18 by David Jones
>>> josecarlosfuentes@yahoo.com.ar 19/11/2004 12:49:38 am >>>
I want to use a Attiny11 (price under 0.54) and
implement a serial port. Since it doesn't have a
USART I have to write the software to allow the
microcontroller to send and receive data. If somebody
has done something like this in assembly or C please
let me know. Attiny11 only has 1KB FLASH and 32
general purpose registers.
Thanks.
Jose
Here is some C code for a basic hard coded RS232 port.
A Delay() routine is required for the appropriate baud rate.
If this is for a production design then don't muck around with
calibrated RC timers and this software uarts, spend a few extra cents
and do it properly with a crystal controlled UART.
//***********************************************************
// Serial Port TX routine
// A "bit banged" 8N1 serial port without using interrupts or timers
void Serial_TX(unsigned char b){
unsigned char c;
TXpinHIGH;
TXpinLOW; //start bit,
beginning of the transmission
delay(bit_delay);
for (c=0;c<8;c++){ //send each bit
if ((b>>c)& 1) TXpinHIGH; else TXpinLOW;
delay(bit_delay); //delay for one bit
}
TXpinHIGH; //stop bit
delay(bit_delay);
return;
}
//***********************************************************
// Serial Port RX routine
// A "bit banged" 8N1 serial port without using interrupts or timers
unsigned char Serial_RX(void){
unsigned char t;
unsigned char c;
unsigned char b;
while(RXpin); //wait for start bit,
beginning of the transmission
LEDon; //Turn RED LED
on to indicate the RS232 transmission
delay(bit_delay+(bit_delay/2)); //delay for 1.5 bits to put
sample in middle of bit
t=0; //assume
character is 0x00
b=1; //reset binary
bit counter
for (c=0;c<8;c++){
if (RXpin) t=t+b;
delay(bit_delay);
b=b*2; //update binary
bit count
}
LEDoff;
return(t); //return the
received character
}