📄 serial.c
字号:
#include "defines.h"
void SerialInit(void)
{
#ifdef _ATMEGA32
UBRRL = 11; // 19200bps @ 3.69 MHz
UCSRB = (1<<RXEN) | (1<<TXEN); // Enable recieve and transmit UART1
#endif
#ifdef _ATMEGA161
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00; //disable while setting baud rate
UBRR0 = 0x0B; //set baud rate
UBRRHI = 0x00;
UCSR0A = 0x00; //enable
UCSR0B = 0x18; //enable#endif
#endif
#ifdef _ATMEGA163
UBRRL = 11; // 19200bps @ 3.69 MHz
UCSRB = (1<<RXEN) | (1<<TXEN); // Enable recieve and transmit
#endif
#ifdef _ATMEGA8
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = 0x86;
UBRRL = 0x0B; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0xF8;
#endif
}
void sendchar(char c)
{
#ifdef _ATMEGA32
UDR=c;
while (!(UCSRA & (1<<TXC))); //wait until byte sendt
UCSRA |= (1<<TXC); //delete TXCflag
#endif
#ifdef _ATMEGA161
UDR0=c;
while (!(UCSR0A & (1<<TXC0))); //wait until byte sendt
UCSR0A |= (1<<TXC0); //delete TXCflag
#endif
#ifdef _ATMEGA163
UDR=c;
while (!(UCSRA & (1<<TXC))); //wait until byte sendt
UCSRA |= (1<<TXC); //delete TXCflag
#endif
#ifdef _ATMEGA8
UDR=c;
while (!(UCSRA & (1<<TXC))); //wait until byte sendt
UCSRA |= (1<<TXC); //delete TXCflag
#endif
}
void sendstr(char *s)
{while(*s)
{
sendchar(*s);
s++;
}
sendchar(0x0a);
//sendchar(0x0d);
}
char recchar(void)
{
unsigned char temp;
#ifdef _ATMEGA32
while(!(UCSRA & (1<<RXC))); // Wait for data to come
return UDR;
#endif
#ifdef _ATMEGA161
while(!(UCSR0A & (1<<RXC0))); // Wait for data to come
temp=UDR0;
return temp;
#endif
#ifdef _ATMEGA163
while(!(UCSRA & (1<<RXC))); // Wait for data to come
return UDR;
#endif
#ifdef _ATMEGA8
while(!(UCSRA & (1<<RXC))) ;
temp=UDR;
return temp;
#endif
}
char reccharcommand(void)
{unsigned int i;
unsigned char temp;
i=0;
#ifdef _ATMEGA32
while(!(UCSRA & (1<<RXC))); // Wait for data to come
return UDR;
#endif
#ifdef _ATMEGA161
while(!(UCSR0A & (1<<RXC0))); // Wait for data to come
temp=UDR0;
return temp;
#endif
#ifdef _ATMEGA163
while(!(UCSRA & (1<<RXC))); // Wait for data to come
return UDR;
#endif
#ifdef _ATMEGA8
while(!(UCSRA & (1<<RXC)))
{i++;
if(i>6000)
return 0x00;}; // Wait for data to come
temp=UDR;
return temp;
#endif
}
char reccharcommand2(void)
{unsigned int i;
unsigned char temp;
i=0;
#ifdef _ATMEGA32
while(!(UCSRA & (1<<RXC))); // Wait for data to come
return UDR;
#endif
#ifdef _ATMEGA161
while(!(UCSR0A & (1<<RXC0))); // Wait for data to come
temp=UDR0;
return temp;
#endif
#ifdef _ATMEGA163
while(!(UCSRA & (1<<RXC))); // Wait for data to come
return UDR;
#endif
#ifdef _ATMEGA8
while(!(UCSRA & (1<<RXC)))
{i++;
if(i>50000)
return 0x00;}; // Wait for data to come
temp=UDR;
return temp;
#endif
}
/*
void sendint(unsigned int intdata)
{
unsigned char i,tempdata,tempdata1,tempdata2,tempdata3,tempdata4,tempdata5;
unsigned char firstzero=0;
for(i=0;i<5;i++)
{
tempdata=intdata%10;
intdata=intdata/10;
switch(i)
{
case 0:
tempdata1=tempdata;
break;
case 1:
tempdata2=tempdata;
break;
case 2:
tempdata3=tempdata;
break;
case 3:
tempdata4=tempdata;
break;
case 4:
tempdata5=tempdata;
break;
default:
break;
}
}
if(tempdata5!=0)
{
sendchar(tempdata5+48);
firstzero=1;
}
if((tempdata4!=0)||firstzero)
{
sendchar(tempdata4+48);
firstzero=1;
}
if((tempdata3!=0)||firstzero)
{
sendchar(tempdata3+48);
firstzero=1;
}
if((tempdata2!=0)||firstzero)
{
sendchar(tempdata2+48);
}
sendchar(tempdata1+48);
sendchar(0x0a);
sendchar(0x0d);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -