MCU

    內容表格
    沒有標頭

    版本為 18:41, 4 Oct 2024

    到這個版本。

    返回到 版本存檔.

    查閱目前版本

    
    串口通訊程式
    
    /*
    版权声明:
    WIFI机器人网·机器人创意工作室版权所有 www.wifi-robots.com
    您可以任意修改本程序,并应用于自行研发的智能小车机器人及其他电子产品上,但是禁止用于向他人牟取暴利。
    By WIFI机器人网·机器人创意工作室
    */
    #include "uart.h"
    #include <stdio.h>
    #include "motor.h"
    #include "steer.h"
    
    
    
    extern uint16 se_timer[5];
    uint8 buffer[3];
    uint8 rec_flag=0;    //等于0等待接受 等于1正在接受
    
    void UART_init(void)
    {
        PCON |= 0x80;        //使能波特率倍速位SMOD
        SCON = 0x50;        //8位数据,可变波特率
        BRT = RELOAD_COUNT;        //设定独立波特率发生器重装值
        AUXR |= 0x04;        //独立波特率发生器时钟为Fosc,即1T
        AUXR |= 0x01;        //串口1选择独立波特率发生器为波特率发生器
        AUXR |= 0x10;        //启动独立波特率发生器
        ES      =   1;    //允许串口中断
        EA      =   1;    //开总中断
        TI      =   1;   
    }
    
    void UART_send_byte(uint8 byte)
    {
        ES     =   0;  //关串口中断
        TI     =   0;  //清零串口发送完成中断请求标志
        SBUF   =   byte;
        while(TI ==0); //等待发送完成
        TI     =   1;  //清零串口发送完成中断请求标志
        ES     =   1;  //允许串口中断
    }
    
    void UART_send(uint8 *Buffer, uint8 Length)
    {
        while(Length != 0)
        {
            UART_send_byte(*Buffer);
            Buffer++;
            Length--;
        }
    
    }
    
    void Communication_Decode(void) 
    {   
        if(buffer[0]==0x00)
        {
            switch(buffer[1])
            {
                case 0x01:MOTOR_GO_FORWARD; return;
                case 0x02:MOTOR_GO_BACK;    return;
                case 0x03:MOTOR_GO_LEFT;    return;
                case 0x04:MOTOR_GO_RIGHT;   return;
                case 0x00:MOTOR_GO_STOP;    return;
                default: return;
            }   
        }
        else if(buffer[0]==0x01)
        {
            if(buffer[2]>180)
                return;
            switch(buffer[1])
            {
                case 0x01:se_timer[0]=buffer[2]; return;
                case 0x02:se_timer[1]=buffer[2]; return;
                case 0x03:se_timer[2]=buffer[2]; return;
                case 0x04:se_timer[3]=buffer[2]; return;
                case 0x05:se_timer[4]=buffer[2]; return;
                default : return;
            }
        }
        else
        {
            return;
        }
    }
    
    void UART_Interrupt_Receive(void) interrupt 4
    {
        static uint8 i;
       
        if(RI==1)
        {
            RI  =   0;
            if(rec_flag==0)
            {
                if(SBUF==0xff)
                {
                    rec_flag=1;   
                    i=0;
                }           
            }
            else
            {
                if(SBUF==0xff)
                {
                    rec_flag=0;   
                    if(i==3)
                    {
                        Communication_Decode();       
                    }
                    i=0;
                }
                else
                {
                    buffer[i]=SBUF;
                    i++;
                }
            }       
        }
        else
        {
            TI  =  0;
        }   
    }
    
    Powered by MindTouch Core