这个是个简单的串口发送程序,往pc上发数据。
#include <mega16.h>
// Standard Input/Output functions
#include <stdio.h>
#include<delay.h>
#define UDRE 5
flash char a[]={'a','b','c','d'};
int i;
// Declare your global variables here
void main(void) //寄存器赋初值
{
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
while (1)
{
for(i=0;i<=3;i++)
{
while(!( UCSRA&(1<<UDRE))); 等待,是否一个发完了。
{UDR=a; 赋值
delay_ms(100);}
if(i>3) i="0";
}
// Place your code here
};
}
这是一个简单的上位机发送,单片机接收的程序,然后在数码管中显示接收到的数据。
#include <mega16.h>
// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#define RXC 7
// Declare your global variables here
int data;
void show(char data) // 数码管显示程序
{
DDRA|=1<<(3);
DDRA|=1<<(4);
DDRB=0XFF;
PORTA|=1<<(3);
PORTB=data;
PORTA&=~(1<<(3));
PORTB=0XFF;
PORTB&=~(1<<(0));
PORTA|=1<<(4);
PORTA&=~1<<(4);
delay_ms(1);
}
void main(void)
{
DDRA|=1<<(2);
PORTA|=1<<(2);
UCSRA=0x00;
UCSRB=0x10;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
while (1)
{ while(!(UCSRA&(1<<RXC)));
data="UDR";
delay_ms(300);
//Place your code here
show(data);
PORTB="0XFF";
};
}
文章评论(0条评论)
登录后参与讨论