新手入门
- 积分
- 13
- 金钱
- 13
- 注册时间
- 2017-7-9
- 在线时间
- 4 小时
|
4金钱
#include <iom128v.h>
#include <macros.h>
#define uchar unsigned char //定义无符号字符为 uchar
#define uint unsigned int //定义无符号整型
#define Crystal 8000000 //晶振8MHZ?
#define Baud 9600 //波特率
uchar usart0_data_receive[22];
volatile static uchar usart0_data_counter=0;//串口接收中断计数
void uart0_init(void);
void usart0_char_send(uchar i);
void usart0_str_send(char *s);
void usart0_str_sendArray(uchar *p,uchar len);
/*****************延时tm秒子函数*******************/
void _delay_us(unsigned int us)
{
unsigned int i;
i = us/2;
i--;
asm("nop"); asm("nop");
for(;i!=0;i--)
{
asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop"); asm("nop");
}
}
void _delay_ms(unsigned int t)
{
for(;t>0;t--)
_delay_us(1100);
}
//****************发送一个字符******************************
void usart0_char_send(uchar i)
{
while(!(UCSR0A&(1<<UDRE0)));
UDR0=i;
}
void debug(uchar temp)
{
usart0_char_send(temp);
//_delay_ms(200);
}
//*********************发送一个字符串**********************
void usart0_str_send(uchar *p)
{
uchar i;
for(i=0;*(p+i)==0;i++) //控制字符个数
{
usart0_char_send(*p);
p++;
}
}
//接受一个字符
uchar usart0_char_receive()
{
while(!(UCSR0A&(1<<RXC0)));
return UDR0;
}
/*****************串口初始化*********************************/
void usart0_init(void)
{
UCSR0B = 0x00; //禁止发送和接收
UCSR0A = 0x00; //倍速异步模式USX0=1
UCSR0C = 0x06; //0000 0110,UCSZ01=1,UCSZ00=1;8位字符,1位停止位?
UBRR0L=0x67; //若为正常异步模式USX0=0则位(Crystal/16/(Baud+1))%256
UBRR0H=0; //参见ATMEGA128使用手册
UCSR0B=(1<<RXEN0)|(1<<TXEN0) ; //允许发送和接收,接收结束中断使能
}
//*********************中断发送服务处理子程序**************************
#pragma interrupt_handler usart0_TX_interrupt:iv_USART0_TX // 21
void usart0_TX_interrupt(void)
{
_NOP();
UCSR0A |= (1<<6); //发送结束清零
}
/*******************主程序****************************/
/****************中断接收服务处理子程序*******************************/
#pragma interrupt_handler usart0_RX_interrupt:iv_USART0_RX // 19
void usart0_RX_interrupt(void)
{
//UCSR0B=0x00; //禁止发送和接收
uchar data_temp;
data_temp = UDR0; //从数据缓冲器中接收数据
usart0_data_receive[usart0_data_counter]=data_temp;
usart0_data_counter++;
//UCSR0B=0xD8 ; //允许发送和接收,接收和发送结束中断使能
if(usart0_data_counter==22) //如果已接收到的字符个数为22
{
usart0_str_send("UART0 works well!");
usart0_data_counter = 0; //计数复位
_delay_ms(200);
}
}
/*******************主程序****************************/
void main(void)
{
uchar i,j=150;
// PORTE=0x00;
// DDRE|=(1<<PE1);
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0xFF;
DDRC = 0x00;
PORTE = 0xFF; //设置RXD0和TXD0
DDRE = 0x02;
PORTD = 0x00; //输出控制LED灯亮灭、
DDRD = 0xFF;
PORTF = 0xFF;
DDRF = 0x00;
PORTG = 0xFF;
DDRG = 0xFF;
CLI(); //总中断禁止
usart0_init();
SEI();
usart0_str_send("UART0 works well!"); //总中断允许
for(;j>0;j--)
_delay_ms(200);//传感器30秒后正常采集数据
while(1) //循环等待中断处理
{
switch(usart0_data_counter/2)
{
case 0:
i=usart0_char_receive();
usart0_char_send(i);
break;
case 1:
usart0_str_send("pm1.0;");
i=usart0_char_receive();
usart0_char_send(i);
break;
case 2:
usart0_char_send('\n');
usart0_str_send("pm2.5;");
i=usart0_char_receive();
usart0_char_send(i);
break;
case 3:
usart0_char_send('\n');
usart0_str_send("pm10;");
i=usart0_char_receive();
usart0_char_send(i);
break;
case 4:
usart0_char_send('\n');
usart0_str_send("airpm1.0;");
i=usart0_char_receive();
usart0_char_send(i);
break;
case 5:
usart0_char_send('\n');
usart0_str_send("airpm2.5;");
i=usart0_char_receive();
usart0_char_send(i);
break;
case 6:
usart0_char_send('\n');
usart0_str_send("airpm10;");
i=usart0_char_receive();
usart0_char_send(i);
break;
case 7:
case 8:
case 9:
usart0_char_send('\n');
usart0_char_send(00);//0传感器(为传感器编译标号)
break;
default:
i=usart0_char_receive();
usart0_char_send(i);
break;
}
}
}
|
|