/*
CPU: ATMega32
频率: 8.0MHz
编译器:CodeVisionAVR 1.24.8c
串口发送程序
*/
#i nclude <mega32.h>
#i nclude <delay.h>
#i nclude <stdio.h>
#define uchar unsigned char // 0~255
#define uint unsigned int // 0~65535
#define _SPK PORTB.2
#define _LED PORTB.3
#define CLI() #asm("cli")
#define SEI() #asm("sei")
/**//* UCSRA */
#define RXC 7
#define TXC 6
#define UDRE 5
#define FE 4
#define DOR 3
#define PE 2
#define U2X 1
#define MPCM 0
/**//* UCSRB */
#define RXCIE 7
#define TXCIE 6
#define UDRIE 5
#define RXEN 4
#define TXEN 3
#define UCSZ2 2
#define RXB8 1
#define TXB8 0
/**//* UCSRC */
#define URSEL 7
#define UMSEL 6
#define UPM1 5
#define UPM0 4
#define USBS 3
#define UCSZ1 2
#define UCSZ0 1
#define UCPOL 0
//=============================================================================
// 初始化,闪亮LED
//=============================================================================
void initport(void)
{
uchar i;
DDRB = 0xFF;
PORTB = 0xFF;
for(i=0; i<20; i++)
{
_LED = ~_LED;
delay_ms(100);
}
_SPK = 0;
delay_ms(500);
_SPK = 1;
}
#define BAUDRATE 9600
#define F_CPU 8000000
void init_USART(void)//USART 初始化
{
//USART 9600 8, n,1 PC上位机软件(超级终端等)也要设成同样的设置才能通讯
UCSRC = (1<<URSEL) | 0x06; //异步,8位数据,无奇偶校验,一个停止位,无倍速
UBRRL= (F_CPU/BAUDRATE/16-1)%256;
UBRRH= (F_CPU/BAUDRATE/16-1)/256;
UCSRA = 0x00;
// UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN); //使能接收中断,使能接收,使能发送
UCSRB = (1<<TXEN); // 使能发送
}
void put_c(unsigned char c) //发送采用查询方式
{
while( !(UCSRA & (1<<UDRE)) );
UDR="c";
}
void put_s(unsigned char *ptr)
{
while (*ptr)
...{
put_c(*ptr++);
}
put_c(0x0D);
put_c(0x0A); //结尾发送回车换行
}
char ss[10] = "12345";
void main()
{
DDRB = 0xFF;
PORTB = 0xFF;
init_USART();
initport();
while(1)
{
_LED = ~_LED;
delay_ms(500);
put_s(ss);
}
}
文章评论(0条评论)
登录后参与讨论