⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 用ADE7169F16单片机实现了单向多费4率电能表
💻 C
字号:
/*************************************
;Ade7169 demo program     
;*************************************
;AUTHOR:        Su RuTong
;DATE:          03 09 2006
;*************************************/


#include "hal.h"
#include "public.h"
#include "utilities.h"
#include "UART.h"
#include "RTC.h"
#include "Measure.h"
#include "sys_event.h"
#include "storage.h"


//Fcore=4.09MHz Mode 1, 9600
void InitUART(void)
{
    SCON=0x50;
    SBAUDT=0x04;//0x84;// 0x82;
    SBAUDF=0xAB;//0x2B;// 43d;
    ES=1;//IE_bit.ES=1;
}

// UART
void Re_Init_Uart_Rec(void)
{
    bIn_rec=0;
    bBuf_InUse=0;
    Uart_buf_RecCnt=0x00;
}

void Re_Init_Uart_Snd(void)
{
    bIn_Send=0;
    bBuf_InUse=0;
    Uart_buf_SndCnt=0x00;
}

void _uart_start_tx(void)
{
    Uart_buf_SndCnt=Uart_buf[1]+3-1;Uart_buf[2]|=0x80;
    Uart_buf[Uart_buf[1]+2]=CalCS(&Uart_buf[2],Uart_buf[1]);
    Uart_Snd_Pointer=&Uart_buf[1];
    SBUF=Uart_buf[0];
}

void _uart_start_rx(void)
{
}
/*****************************************************************************
// 发送 接收 使用同一缓冲区 Uart_buf[]  注意保护BUF不被随便修改
******************************************************************************/
// UART interrupt service
#ifdef __IAR_SYSTEMS_ICC__
#pragma register_bank=1
#pragma vector=RI_int
__interrupt void _uart_isr(void)
#else
void _uart_isr(void) interrupt 4 using 3
#endif
{
    unsigned char buffer;

    if(RI)
    {
        RI=0;
        buffer=SBUF;
        Uart_Rec_Tout_Cnt=255;                  // Recive time out per byte 100*10ms
#ifdef _UART_TEST_
        SBUF=buffer;
#endif
        if(buffer==0x5A)//(buffer==0x7e)                        // Frame start
        {
            P0 &= ~BIT3;
            bIn_rec=1;
            bBuf_InUse=1;
            Uart_buf_RecCnt=0;
            // *Uart_Rec_Pointer=buffer;
            // Uart_Rec_Pointer++;
            // Uart_buf[Uart_buf_RecCnt++]=buffer;
        }
		if(bIn_rec)
		{
		    #if 0
			if((Uart_buf_RecCnt==(Uart_buf[1]+3+1))||(Uart_buf_RecCnt>UART_BUF_MAX))
			{
				goto Rec_End;
			}
			else
			{
				Uart_buf[Uart_buf_RecCnt++]=buffer;
				return;
			}
            #endif
            Uart_buf[Uart_buf_RecCnt++]=buffer;
            if((Uart_buf_RecCnt==(Uart_buf[1]+3))||(Uart_buf_RecCnt>UART_BUF_MAX))
			{
                Uart_Rec_Tout_Cnt=0x00;
			    ADD_EVT(EVT_OF_COMM);
			    // Receive End Re-initialize uart Rec
			    Re_Init_Uart_Rec();
			}
		}
	}
    else if(TI)
    {
        TI=0;
        if(Uart_buf_SndCnt)
        {
            SBUF=*Uart_Snd_Pointer;
            Uart_Snd_Pointer++;
            // If pointer point to then end of buffer, reload pointer
            if(Uart_Snd_Pointer>(&Uart_buf[0]+UART_BUF_MAX-1/*&Sys_buf[0]+SYS_BUF_MAX-1*/))
            {
                Uart_Snd_Pointer=&Uart_buf[0];//Sys_buf[0];
            }
            Uart_Snd_Tout_Cnt=255;                  // timout 255*10ms
            // bBuf_InUse=1;
            bIn_Send=1;

            if(--Uart_buf_SndCnt==0x00)
            {
                // bBuf_InUse=0;
                // bIn_Send=0;
                Uart_Snd_Tout_Cnt=0x00;
                // Send end re-initialize uart Send
                Re_Init_Uart_Snd();
            }
        }
    }
}

// 08: Read ADE SFR           AH AL        // 5a 01 08 [2bytes address H,L]
void _Read_ADE_SFR(void)
{
    Read_ADE_SFR((unsigned char __idata *)&Uart_buf[3], Uart_buf[3]);
    Uart_buf[1]=0x04;
    Uart_buf[2]=0x08;
    _uart_start_tx();
}
// 09: Write ADE SFR          AH AL DH DM DL      // 5a 01 09 [2bytes address H,L; D]
void _Write_ADE_SFR(void)
{
    Write_ADE_SFR(Uart_buf[4], Uart_buf[5], Uart_buf[3]);
    //Uart_buf[1]=0x01;
    //Uart_buf[2]=0x01;
    //_uart_start_tx();
}
// 10: Read eeprom  [3]:AddrH,AddrL,LEN
void _Read_EEPROM(void)
{
    ee_read_bytes((unsigned short)(Uart_buf[3]<<8|Uart_buf[4]),&Uart_buf[3],Uart_buf[5]);
    Uart_buf[1]=Uart_buf[1] - 3;
    Uart_buf[2]=0x0A;
    _uart_start_tx();
}

// 11: Write eeprom [3]:AddrH,AddrL,[data]
void _Write_EEPROM(void)
{
    ee_write_bytes((unsigned short)(Uart_buf[3]<<8|Uart_buf[4]),&Uart_buf[5],(Uart_buf[1]-3));
    //Uart_buf[1]=Uart_buf[1] - 3;
    //Uart_buf[2]=0x0A;
    //_uart_start_tx();
}

#define UART_EVENT_MAX (sizeof(_UartPROC) / sizeof(COMM_PROC_PTR))

code const COMM_PROC_PTR _UartPROC[]=
{
    _SetRTC,
    _ReadRTC,
    _ReadVRMS,
    _ReadIRMS,
    _ReadActive,
    _ReadRActive,
    _ReadRA,
    _ReadRR,
    _Read_ADE_SFR,
    _Write_ADE_SFR,
    _Read_EEPROM,
    _Write_EEPROM,
};


void UartTest(void)
{
    unsigned char tmp=0x55;
    while(1)
    {
        //if(SCON_bit.RI)
        {
            RI=0;
            TI=0;
            //do
            //{
            //    tmp=getchar();// tmp=SBUF;
            //}while(tmp==NULL);
            //putchar(tmp);
            SBUF=tmp;
            while(!TI);
            TI=0;
            CLR_WDT();
            delay(3500);
        }
    }
}

/*
5A LEN C DATA CS
LEN: 1 byte, Length of C to Data,include data feild.
CS:  1 byte, Sum from C to Data
*/
void ParseFrame(void)
{
    //if(Uart_buf[0]==0x5a)
    //{ 
        P0 |= BIT3;
        if(Uart_buf[Uart_buf[1]+2] == CalCS(&Uart_buf[2],Uart_buf[1]))
        {
            if(Uart_buf[2]<UART_EVENT_MAX){_UartPROC[Uart_buf[2]]();}
        }
    //}
    //else
    //{
    //}
}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -