实现光电开关125c51-2和M16的IO 口连接,每次来个低电平时通过232给PC发送一个字符,也
//f=12m#include <avr/io.h>
//#include <inttypes.h>
/*串口初始化函数*/
void Uart_Init(void) {
UCSRA = 0x02; /*倍速*/
UCSRB = 0x18; /*允许接收和发送*/
UCSRC = 0x06; /*8位数据*/
UBRRH = 0x00;
UBRRL = 0x4D; //19200
}
/*数据发送,查询方式*/
void Uart_Transmit(unsigned char i) {
while (!(UCSRA & (1<<UDRE))); /* 等待发送缓冲器为空*/
UDR = i; /* 发送数据*/
}
/*数据接收,查询方式*/
unsigned char Uart_Receive( void ) {
while (!(UCSRA & (1<<RXC))); /* 等待接收数据*/
return UDR; /* 获取并返回数据*/
}
void Delay(void)
{
unsigned char a, b;
for (a = 1; a; a++)
for (b = 1; b < 5; b++)
;
}
/*主函数*/
int main(void)
{
DDRA = 0x00; /*方向输入*/
PORTA = 0xFF; /*打开上拉*/
DDRB = 0x00; /*方向输出*/
PORTB = 0xFF; /*电平设置*/
DDRC = 0x00;
PORTC = 0xFF;
DDRD = 0x02;
PORTD = 0xFF;
Uart_Init();
while (1) {
if(!(PIND&0x80))
{
Uart_Transmit('m');//13
Uart_Transmit(0x0d);
Uart_Transmit(0x0a);
while(!(PIND&0x80));/////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////
}
} 我才,这程序貌似跟光电开关无关啊,检测电平{:shocked:}
页:
[1]