您的位置 首页 系统

AVR_M8单片机串口操控应用程序

includeiom8vh>includemacrosh>includeIo_definehunsignedcharRX_data[4]={0};串口接收的数据unsignedcharke

#include
#include
#include “Io_define.h”
unsigned char RX_data[4]={0}; //串口接纳的数据
unsigned char keyb_state=0;
unsigned char keyc_state=0;
unsigned char RX_counter=0; //串口接纳到的字节数计数器
void UART_init (void);
void putchar (unsigned char c);
void puts (unsigned char *s);
void UART_rx (void);
void delay_nms (unsigned int n);
void Key_control (unsigned char comm);
void Read_Pin (void);
void putchar(unsigned char c) //字符输出函数
{
while (!(UCSRA&(1 << UDRE))); //判别前次发送有没有完结
UDR = c;
}
#pragma interrupt_handler UART_rx: iv_USART_RX
void UART_rx(void) //串口接纳中止函数
{
RX_data[RX_counter] = UDR;

if (RX_data[RX_counter]==s)
{
RX_data[0]=RX_data[RX_counter];
RX_counter=0;
}

RX_counter++; //接纳的字节数计数
}
void puts(unsigned char *s) //字符串发送函数
{
while (*s)
{
putchar(*s);
s++;
}
}

void UART_init(void)
{
UCSRB = BIT(RXCIE)| BIT(RXEN) |BIT(TXEN); //答应串口发送和接纳,并呼应接纳完结中止
UBRR = 51; //时钟8Mhz,波特率9600
UCSRC = BIT(URSEL)|BIT(UCSZ1)|BIT(UCSZ0); //8位数据+1位stop位
}

void main(void)
{
//unsigned char keyb_state=0;
//unsigned char keyc_state=0;
unsigned char i=0;

OSCCAL=0xa3;

DDRB = 0B11000111; //初始化继电器操控端口
PORTB = 0B00000000;
DDRC = 0B00111111;
PORTC = 0B00000000;
DDRD = 0B10000000;
PORTD = 0B00000000;

DDRB |= 0b00111000; //初始化键盘接口,行为低电平
PORTB&= 0b11000111;

DDRD &= ~0b01111000; //列为高电平
PORTD|= 0b01111000;

DDRD &= ~BIT(2); //设置INT0为输入,高电平状况
PORTD |= BIT(2);
GICR |= BIT(INT0); //答应INT0外部低电平中止

UART_init();

SEI();

while (1)
{
if (RX_counter==4)
{
RX_counter=0;
if (RX_data[3] == t) //接纳到PC机发的状况恳求指令,则发送
{ //操控板的key状况数据给PC
putchar(0); //操控板地址码
delay_nms(10);
putchar(keyb_state);
delay_nms(10);
putchar(keyc_state);
delay_nms(10);
putchar(t); //状况数据标志位
delay_nms(10);
}
else
if ((RX_data[0] == s)&&(RX_data[3] == p)) //查验指令格局是否正确
{
if ((RX_data[1] == u) &&(RX_data[2]==0))//是否是软件晋级指令
{
//puts(“upready”);
asm(“jmp 0xe00”);
}
else
if (RX_data[1]==0) //地址码是否正确
{
Key_control(RX_data[2]);
}
}
}
}
}

void Key_control(unsigned char comm)
{
CLI();
switch (comm)
{
case 0: key0 ^= key0_open; break;
case 1: key1 ^= key1_open; break;
case 2: key2 ^= key2_open; break;
case 3: key3 ^= key3_open; break;
case 4: key4 ^= key4_open; break;
case 5: key5 ^= key5_open; break;
case 6: key6 ^= key6_open; break;
case 7: key7 ^= key7_open; break;
case 8: key8 ^= key8_open; break;
case 9: key9 ^= key9_open; break;
case :: key10 ^= key10_open; break;
case ;: key11 ^= key11_open; break;
case a: {
PORTB |= 0B11000111;
PORTC |= 0B00111111;
PORTD |= 0B10000000;
} break;
case b: {
PORTB &= 0B00111000;
PORTC &= 0B11000000;
PORTD &= 0B01111111;
} break;

}
Read_Pin();
putchar(0); //发送操控板地址码和key状况数据
delay_nms(10);
putchar(keyb_state);
delay_nms(10);
putchar(keyc_state);
delay_nms(10);
putchar(t);
delay_nms(10);
SEI();
}
void Read_Pin(void)
{
asm(“nop”);
keyb_state = ((PINB & 0B11000111)|((PINC & 0B00110000)>>1)|
((PIND & 0B10000000)>>2));
keyc_state = (PINC & 0B00001111);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/259823.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部